Let's try knocking 100 natural languages that are popular in the streets with Python. I think there are various ways to solve it, so I would like to show you various ways to solve it. Of course, it is not possible to cover all the solutions. If you have any questions such as "There is such a solution" or "I'm wrong here", please let me know in the comments.
Languistic processing 100 knock 2020 is Inui Suzuki Laboratory of Tohoku University. /) This is the teaching material used in the Programming Basic Research Study Group. For details, refer to About 100 language processing knocks.
1st question | 2nd question | 3rd question | 4th question | 5th question | 6th question | 7th question | 8th question | 9th question | 10th question | |
---|---|---|---|---|---|---|---|---|---|---|
Chapter 1 | 00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 |
Chapter 2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
Chapter 3 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
Chapter 4 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
Chapter 5 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
Chapter 6 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
Chapter 7 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 |
Chapter 8 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
Chapter 9 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 |
Chapter 10 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 |
[01. "Patatokukashi"](https://nlp100.github.io/ja/ch01.html#01-%E3%83%91%E3%82%BF%E3%83%88%E3%82%AF The problem of% E3% 82% AB% E3% 82% B7% E3% 83% BC% E3% 83% BC) is as follows.
1 of the character string "Patatokukashi",3,5,Take out the 7th character and get the concatenated string.
If you read the question, the answer is police car
because it is the 1,3,5,7th character of Patatokukasi
.
All of the answers below will return police car
when you evaluate the last formula.
There is a method to add it to the end of the character string obediently.
ʻEnumerate (s)is an iterator for scanning
(0, s [0]), ..., (n-1, s [n-1])with
n = len (s)` Returns.
#simple
s = "Patatoku Kashii"
result = ""
for n,c in enumerate(s):
if n%2==0:
result += c
result #=> 'Police car'
You can also write using map
and filter
derived from functional types.
You can't really feel the benefits in this example ...
#Functional
s = "Patatoku Kashii"
"".join(
map(
lambda x:x[1],
filter(
lambda x:x[0]%2==0,
enumerate(s)
)
)
) #=> 'Police car'
Is it slightly better to name the function defined in the lambda expression?
#Functional
s = "Patatoku Kashii"
second_elem = lambda x:x[1]
is_first_elem_even = lambda x:x[0]%2==0
"".join(map(
second_elem,
filter(
is_first_elem_even,
enumerate(s)
)
)
) #=> 'Police car'
It feels overkill if you give it too many names.
#Functional
s = "Patatoku Kashii"
first_elem = lambda x:x[0]
second_elem = lambda x:x[1]
is_first_elem_even = lambda x:first_elem(x)%2==0
"".join(map(
second_elem,
filter(
is_first_elem_even,
enumerate(s)
)
)
) #=> 'Police car'
I hope that such a function is prepared. The function definition by the lambda expression is the same as the normal function definition below.
def first_elem(x):
return x[0]
def second_elem(x):
return x[1]
def is_first_elem_even(x):
return first_elem(x)%2==0
You may want to put it in a variable quietly.
Since filter
and map
return an iterator (generator), the list is not generated every time you put it in a variable.
Like JavaScript, I don't feel like the callback should be on the back side.
#Functional
s = "Patatoku Kashii"
first_elem = lambda x:x[0]
second_elem = lambda x:x[1]
is_first_elem_even = lambda x:first_elem(x)%2==0
filtered = filter(is_first_elem_even, enumerate(s))
mapped = map(second_elem, filtered)
"".join(mapped) #=> 'Police car'
It is easier to write in comprehension notation without sticking to map
or filter
.
Writing [x for x in l]
is a list comprehension, and writing (x for x in l)
is a generator comprehension.
f ((x for x in l))
can be written with f (x for x in l)
and one less parenthesis.
Assigning values to multiple variables at the same time is easier to understand than receiving them with x
and retrieving them withx [0]
orx [1]
.
#Comprehension notation
s = "Patatoku Kashii"
"".join(c for n,c in enumerate(s) if n%2==0) #=> 'Police car'
Pythonic
Being Python-like is called ** Pythonic **, but is this code the most Pythonic?
The method of accessing a list or string with s [start: end: step]
is called slicing.
If you specify 2
for step
, it will be skipped by one.
# Pythonic
s = "Patatoku Kashii"
s[::2] #=> 'Police car'
There is also a way to prepare your own index and rotate it without using ʻenumerate`.
#Turn by yourself
s = "Patatoku Kashii"
result = ""
n = 0
for c in s:
if n%2==0:
result += c
n += 1
result #=> 'Police car'
Solve 100 language processing knock 2020 (01. Reverse order of character strings) You can add it to the list and combine it later. I can do it.
#simple
s = "Patatoku Kashii"
l = []
for n, c in enumerate(s):
if n%2==0:
l.append(c)
"".join(l) #=> 'Police car'
You may be wondering which is better, "combine strings" or "add to list". If you handle a list of general objects instead of characters, you will inevitably add it to the list, but if the goal is to generate a string like this time, it doesn't matter. If it gets bigger and slower, stop and think about it.
#simple
s = "Patatoku Kashii"
result = ""
for n,c in enumerate(s):
if n%2==0:
result += c
result #=> 'Police car'
#simple
s = "Patatoku Kashii"
l = []
for n, c in enumerate(s):
if n%2==0:
l.append(c)
"".join(l) #=> 'Police car'
Recommended Posts