I'm new to Elixir and trying to get a random letter from a function.
I'm tryng to define a function that return a random letter between a and z.
For for some reason this sometimes returns a blank character.
Why?
defp random_letter do
"abcdefghijklmnopqrstuvwxyz"
|> String.split("")
|> Enum.random
end
def process do
Enum.each(1..12, fn(number) ->
IO.puts random_letter
end)
end
Output:
g
m
s
v
r
o
m
x
e
j
w
String.split("abcdefghijklmnopqrstuvwxyz", "")
returns
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""]
Look at the last element in the list and you get your answer :)
To avoid that, you can use trim option like this:
String.split("abcdefghijklmnopqrstuvwxyz", "", trim: true)
When you want to split the string, here are two alternatives. The second one is used when you have Unicode strings.
iex(1)> String.codepoints("abcdefghijklmnopqrstuvwxyz")
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
iex(2)> String.graphemes("abcdefghijklmnopqrstuvwxyz")
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
Or you can use
iex(1)> <<Enum.random(?a..?z)>>
"m"
Related
I am training a few-shot learning algorithm and I need to split my dictionary into training and test set based on types key
Here is how the data looks:
word is a text data/sentence and their corresponding label is in labels. types is different entities present in data.
{ "word":
[
["it", "was", "produced", "under", "the", "banner", "of", "pritish", "nandy", "communications", "and", "was", "written", "and", "directed", "by", "first", "time", "director", "saket", "chaudhary", "."],
["``", "the", "new", "york", "times", "``", "described", "the", "book", "as", "a", "``", "user", "'s", "manual", "for", "how", "to", "attract", "targeted", "traffic", "by", "a", "deeper", "understanding", "of", "google", "adsense", "code", "''", "."],
["the", "commodore", "1571", "is", "commodore", "'s", "high-end", "5\u00bc", "''", "floppy", "disk", "drive", "."],
["her", "interest", "in", "environmental", "and", "social", "issues", "led", "her", "to", "obtain", "a", "photography", "degree", "from", "edinburgh", "college", "of", "art", "and", "a", "m.a", "in", "photojournalism", "and", "documentary", "photography", "from", "the", "london", "college", "of", "communication", "in", "2006", "."],
["in", "1852", ",", "he", "sold", "a", "landscape", "painting", "``", "aus", "dem", "bondhusthal", "``", "(", "``", "from", "the", "bondhusdalen", "``", ")", ",", "to", "bridgewater", "gallery", "in", "london", "."]
],
"label":
[
["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "person-director", "person-director", "O"],
["O", "art-other", "art-other", "art-other", "art-other", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],
["O", "product-other", "product-other", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],
["O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "other-educationaldegree", "other-educationaldegree", "O", "O", "O", "O", "O", "O", "O", "other-educationaldegree", "other-educationaldegree", "other-educationaldegree", "other-educationaldegree", "other-educationaldegree", "other-educationaldegree", "O", "O", "O", "O", "O", "O", "O", "O", "O"],
["O", "O", "O", "O", "O", "O", "O", "O", "O", "art-painting", "art-painting", "art-painting", "O", "O", "O", "art-painting", "art-painting", "art-painting", "O", "O", "O", "O", "O", "O", "O", "O", "O"]
]
"types": ["art-other", "product-other", "other-educationaldegree"]
}
I want to split the data into train and test and keep the same structure intact.
I followed these links but it did not wok out in my case:
Split a dictionary where values of keys are multiple lists into train and test set Python
I have a function which iterates for loop over some data and output the list of items.
eg: Output of function is :-
["A", "B", "C", "D"]
["E", :F", "G", :H"]
["I", "J", "K", "L"]
I want to have a single list of this above lists as:-
rows = [["A", "B", "C", "D"], ["E", :F", "G", :H"], ["I", "J", "K", "L"]]
so that i can use writer.csv(rows) to write file to csv with one single header of the csv file.
Any suggestions ?
Here's a way, I used it to fetch details from list and append it csv by creating another list of given values`
row1 = ["A", "B", "C", "D"]
row2 = ["E", :F", "G", :H"]
row3 = ["I", "J", "K", "L"]
rows = row1,row2,row3
print(rows)
How would you code a program ( preferably in Java or Python ) to break a random ciphertext where key can't be determined by shifts i.e the key substitution is random.
This website (https://www.guballa.de/substitution-solver) has done it.
I have to do it by frequency analysis (https://en.wikipedia.org/wiki/Frequency_analysis)
The main problem I am facing is to check that if the words are looking like English words or not when I substitute.
Please guide me on how to approach this problem
Thanks
hakid
This is maybe a late answer but this code can used as a start for you.
from operator import itemgetter
letterFrequency = [
[12.00, 'E'], [9.10, 'T'],
[8.12, 'A'], [7.68, 'O'],
[7.31, 'I'], [6.95, 'N'],
[6.28, 'S'], [6.02, 'R'],
[5.92, 'H'], [4.32, 'D'],
[3.98, 'L'], [2.88, 'U'],
[2.71, 'C'], [2.61, 'M'],
[2.30, 'F'], [2.11, 'Y'],
[2.09, 'W'], [2.03, 'G'],
[1.82, 'P'], [1.49, 'B'],
[1.11, 'V'], [0.69, 'K'],
[0.17, 'X'], [0.11, 'Q'],
[0.10, 'J'], [0.07, 'Z']]
plain_to_cipher = {
"a": "l", "b": "f",
"c": "w", "d": "o",
"e": "a", "f": "y",
"g": "u", "h": "i",
"i": "s", "j": "v",
"k": "z", "l": "m",
"m": "n", "n": "x",
"o": "p", "p": "b",
"q": "d", "r": "c",
"s": "r", "t": "j",
"u": "t", "v": "q",
"w": "e", "x": "g",
"y": "h", "z": "k",
}
cipher_to_plain = {v: k for k, v in plain_to_cipher.items()}
alphabet = "qwertyuioplkjhgfdsazxcvbnm"
message = input("Enter message to encrypt: ")
message = message.lower()
ciphertext = ""
for c in message:
if c not in alphabet:
ciphertext += c
else:
ciphertext += plain_to_cipher[c]
print("\nRandom substitution Encryption is: \n\t{}".format(ciphertext))
# .......................................................................
# calculate letter frequency of ciphertext
letter_list = []
cipher_len = 0
for c in ciphertext:
if c in alphabet:
cipher_len += 1
if c not in letter_list:
letter_list.append(c)
letter_freq = []
for c in letter_list:
letter_freq.append([round(ciphertext.count(c) / cipher_len * 100, 2), c])
# ....................................................................................
# Now sort list and decrypt each instance of ciphertext according to letter frequency
letter_freq = sorted(letter_freq, key=itemgetter(0), reverse=True)
decrypted_plaintext = ciphertext
index = 0
for f, c in letter_freq:
print("Replacing {} of freq {} with {}.".format(c, f, letterFrequency[index][1]))
decrypted_plaintext = decrypted_plaintext.replace(c, letterFrequency[index][1])
index += 1
print("\nThe Plaintext after decryption using frequency analysis: \n\t{}".format(decrypted_plaintext))
SIDE NOTES: this program can most of the time successfully decrypt most used letters like e, t, a, o but would fail to successfully map less used letters(since frequency differences start to decrease making results less predictable). This problem can be overcomed a little bit by also analyzing English most used bigrams(like th) and using the results to make more accurate predictions. Another note you can take advantage of is that the letter a is easy to break making breaking the letter i less painfull since any sentence having one ciphertext character in between is likely to correspond to a(ex: a book) or i(ex: i went)(and we already deduced a so any other single ciphertext character is likely to be i)
I want to sort a list of strings alphabetically and by length so it gets sorted like this:
["a", "b", "ba", "ab", "cccc", "cccef", "c"]
to
["a", "ab", "b", "ba", "c", "cccc", "cccef"]
It's probably a duplicate question and the solution is probably quite simple but i can't seem to figure it out.
The first argument is just x which sorting by alphabetically(default) and the second is len(x) which sort by the length.
s = ["a", "b", "ba", "ab", "cccc", "cccef", "c"]
s = sorted(s, key = lambda x: (x,len(x)))
print (s)
>>>['a', 'ab', 'b', 'ba', 'c', 'cccc', 'cccef']
When I run this equation in Excel it tells me that there is only 1 argument for the IF statement. I am not sure why it is saying that when I have 3 arguments. Within the OR statement I have 2 different AND statements. It works just fine if I get rid of the second AND statement. Did I mess up a parentheses somewhere? I am not sure what is wrong. Any help would be greatly appreciated. Thanks!
=IF(OR(ARRAYFORMULA(SUM(COUNTIF(B19:O19,{"I","Ip","Ia","It","Ih","A","Aa","Ap","At","Ah","X","R","Rt","Rx","Rp","Rh","K","Kt","E","Et","AL","HL","TV*","FFSL","ADM*"})))=10, AND(ARRAYFORMULA(SUM(COUNTIF(B19:O19,{"R-10","Rx-10*","Rp-10","Rt-10*","Rh-10","I-10","Ia-10","Ip-10","It-10","Ih-10","X-10*","A-10*","At-10"})))=4, ARRAYFORMULA(SUM(COUNTIF(B19:O19,{"I","Ip","Ia","It","Ih","A","Aa","Ap","At","Ah","X","R","Rt","Rx","Rp","Rh","K","Kt","E","Et","AL","HL","TV*","FFSL","ADM*"})))=5),AND(ARRAYFORMULA(SUM(COUNTIF(B19:O19,{"HL-9","X-9","N-9","E-9","J-9","Jh-9","Nh-9","Eh-9"})))=8,ARRAYFORMULA(SUM(COUNTIF(B19:O19,{"I","Ip","Ia","It","Ih","A","Aa","Ap","At","Ah","X","R","Rt","Rx","Rp","Rh","K","Kt","E","Et","AL","HL","TV*","FFSL","ADM*"})))=1) ,"80 Hours","Error"))
This question makes me think "If only there was an online Excel Formula Beautifier".
Oh look, there is.
If you copy-and-paste it into the beautifier you get the code below.
You can now see that your parameters "80 Hours", "Error" are parameters of the first ARRAYFORMULA function, not the IF function.
=IF(
OR(
ARRAYFORMULA(
SUM(
COUNTIF(
B19:O19,
{ "I",
"Ip",
"Ia",
"It",
"Ih",
"A",
"Aa",
"Ap",
"At",
"Ah",
"X",
"R",
"Rt",
"Rx",
"Rp",
"Rh",
"K",
"Kt",
"E",
"Et",
"AL",
"HL",
"TV*",
"FFSL",
"ADM*"
ARRAYROWSTOP)
ARRAYSTOP)
)
)
) = 10,
AND(
ARRAYFORMULA(
SUM(
COUNTIF(
B19:O19,
{ "R-10",
"Rx-10*",
"Rp-10",
"Rt-10*",
"Rh-10",
"I-10",
"Ia-10",
"Ip-10",
"It-10",
"Ih-10",
"X-10*",
"A-10*",
"At-10"
ARRAYROWSTOP)
ARRAYSTOP)
)
)
) = 4,
ARRAYFORMULA(
SUM(
COUNTIF(
B19:O19,
{ "I",
"Ip",
"Ia",
"It",
"Ih",
"A",
"Aa",
"Ap",
"At",
"Ah",
"X",
"R",
"Rt",
"Rx",
"Rp",
"Rh",
"K",
"Kt",
"E",
"Et",
"AL",
"HL",
"TV*",
"FFSL",
"ADM*"
ARRAYROWSTOP)
ARRAYSTOP)
)
)
) = 5
),
AND(
ARRAYFORMULA(
SUM(
COUNTIF(
B19:O19,
{ "HL-9",
"X-9",
"N-9",
"E-9",
"J-9",
"Jh-9",
"Nh-9",
"Eh-9"
ARRAYROWSTOP)
ARRAYSTOP)
)
)
) = 8,
ARRAYFORMULA(
SUM(
COUNTIF(
B19:O19,
{ "I",
"Ip",
"Ia",
"It",
"Ih",
"A",
"Aa",
"Ap",
"At",
"Ah",
"X",
"R",
"Rt",
"Rx",
"Rp",
"Rh",
"K",
"Kt",
"E",
"Et",
"AL",
"HL",
"TV*",
"FFSL",
"ADM*"
ARRAYROWSTOP)
ARRAYSTOP)
)
)
) = 1
),
"80 Hours",
"Error"
)
)