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 |
[00. Reverse order of strings](https://nlp100.github.io/ja/ch01.html#00-%E6%96%87%E5%AD%97%E5%88%97%E3%81% The problem of AE% E9% 80% 86% E9% A0% 86) is as follows.
Get a string in which the characters of the string "stressed" are arranged in reverse (from the end to the beginning).
If you read the question, the answer is desserts
because it's a question of getting a string of stressed
in reverse.
All of the answers below will return desserts
when the last expression is evaluated.
There is a method to add it to the beginning of the character string obediently.
In Python, you could combine strings with +
to generate a new string.
#simple
s = "stressed"
result = ""
for c in s:
result = c + result
result #=> 'desserts'
It's too simple to be functional, but it can be expressed by a combination of functions.
The reversed
function returns an iterator to retrieve in reverse order given an iterator, so concatenate with an empty string to get the desired result.
#Functional
s = "stressed"
"".join(reversed(s)) #=> 'desserts'
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.
Specifying -1
for step
reverses the order. It goes by -1
.
# Pythonic
s = "stressed"
s[::-1] #=> 'desserts'
There is also the following method that combines the above.
If you add something to the end or beginning of the string, a copy will occur, and if you add an element other than the end of the list, a copy will occur.
To avoid that, I add an element to the end of the list and then string it with " ". Join
.
#A combination of simple and functional
s = "stressed"
l = []
for c in reversed(s):
l.append(c)
"".join(l) #=> 'desserts'
There is also a method of turning by index obediently.
for n in range (len (s)):
is one of the syntaxes for turning for for an index.
len (s)
returns the length of the string s
, andrange (n)
returns an iterator to scan 0, ..., n-1
.
#Perform the process equivalent to reversed by yourself
s = "stressed"
l = []
for n in range(len(s)):
l.append(s[len(s)-n-1])
"".join(l) #=> 'desserts'
There is another way to index.
ʻEnumerate (s)is an iterator for scanning
(0, s [0]), ..., (n-1, s [n-1])with
n = len (s)Returns. I also use
_` to mean variables that I don't use. I just put it in.
#Perform the process equivalent to reversed by yourself
s = "stressed"
l = []
for n,_ in enumerate(s):
l.append(s[len(s)-n-1])
"".join(l) #=> 'desserts'
You can also write in list comprehension.
s = "stressed"
l = [c for c in reversed(s)]
"".join(l) #=> 'desserts'
However, if you think about it carefully, you may think that the generator comprehension may be used.
s = "stressed"
g = (c for c in reversed(s))
"".join(g) #=> 'desserts'
Then, it will return to the following functional type.
#Functional
s = "stressed"
"".join(reversed(s)) #=> 'desserts'
I think each person has their own taste, but my favorite answer is below.
#A combination of simple and functional
s = "stressed"
l = []
for c in reversed(s):
l.append(c)
"".join(l) #=> 'desserts'
It is easy to understand that the for statement conveys the feeling that you want to turn it in the opposite direction, and that you are obediently adding elements to the list.
Also, when solving an actual problem, I often write the process of processing each element and putting it in another list, so it is the basic form.
" ". Join (reversed (s))
and s [:: -1]
are great for solving this problem, but they make it harder to add additional processing.
Recommended Posts