I was wondering if there was a site where I could practice python (3.X) to kill time, but I found a site called 100 Language Processing Knock 2015, so I tried only the first five problems. http://www.cl.ecei.tohoku.ac.jp/nlp100/
Please point out if there is something wrong with this.
Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).
00.py
s = "stressed"
print(s[::-1])
desserts
In s [i: j: k], it means that k pieces of s [j-1] are skipped from s [i], so s [:: -1] means from the end to the beginning one by one. Right.
Take out the 1st, 3rd, 5th, and 7th characters of the character string "Patatokukashi" and get the concatenated character string.
01.py
s = "Patatoku Kashii"
print(s[::2])
Police car
It is an application of 00. (I can't think of anything in particular)
Obtain the character string "Patatokukashi" by alternately connecting the characters "Police car" + "Taxi" from the beginning.
02.py
s = ""
for i,j in zip("Police car", "taxi"):
s += i+j
print(s)
Patatoku Kashii
There was a convenient zip function that allows you to loop through multiple sequences at once, so you can use this to bring characters one by one and stick them together.
Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
03.py
s = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
count_list = []
for i in s.split():
count_list.append(len(i.strip(",.")))
print(count_list)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
When I first saw this problem, where was the factor of pi? I thought, If you look closely, it's like that ... ~~ It's too aggressive ~~
The way to do this is to separate the strings with split (), remove the',' and'.' With strip, and get the number of characters with len (). It feels like processing by looping as much as this is divided.
Break down the sentence "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can." Into words 1, 5, 6, 7, 8, 9, 15, 16, The 19th word is the first character, and the other words are the first two characters, and the associative array (dictionary type or map type) from the extracted character string to the word position (what number of words from the beginning) Create.
04.py
s = "Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."
dict = {}
for (i, w) in enumerate(s.replace(".", "").split(), 1):
if i in (1,5,6,7,8,9,15,16,19):
dict.update({w[0]:i})
else:
dict.update({w[:2]:i})
print(dict)
{'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10, 'Na': 11, 'Mi': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, 'Ca': 20}
I used the enumerate function to get the element with an index when looping. This will get the string and index with the'.' Removed, and only the first character for the 1,5,6,7,8,9,15,16,19th, otherwise up to the second character , And so on, write an if statement and you're done.
I saw the element symbol after a long time. ~~ By the way, what is Mi? ~~
I would like to continue when I feel like it.
Recommended Posts