It is a challenge record of Language processing 100 knock 2015. The environment is Ubuntu 16.04 LTS + Python 3.5.2 : : Anaconda 4.1.1 (64-bit). Click here for a list of past knocks (http://qiita.com/segavvy/items/fb50ba8097d59475f760).
Break down the sentence "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics." Into words, and create a list of the number of characters (in the alphabet) of each word in order of appearance.
The finished code:
main.py
# coding: utf-8
target = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
result = []
words = target.split(' ')
for word in words:
result.append(len(word) - word.count(',') - word.count('.'))
print(result)
Execution result:
Terminal
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]
I feel that the handling of ,
and .
is not very good. It would be nice if str.split ()
can specify multiple delimiter strings. However.
main2.py
# coding: utf-8
target = 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'
result = []
words = target.split(' ')
for word in words:
count = 0
for char in word:
if char.isalpha():
count += 1
result.append(count)
print(result)
Looking at the code of the seniors, it seems that you can write more efficiently by using the inclusion notation. However, it is unfamiliar and difficult to understand at first glance, so I think I will keep it redundant until I get used to it ^^; That's all for the fourth knock. If you have any mistakes, I would appreciate it if you could point them out.
Recommended Posts