AtCoder's ABC146 B Problem gave me the opportunity to become familiar with Unicode.
Suppose that "shifting an alphabetic character string by N characters" means "converting to the one after N characters in alphabetical order", and after'Z', it returns to'A'. The same definition applies to problem B above.
ord () `` `and
chr () `` `Python3 implements the built-in functions ord () `` `and
chr () `` `, which convert characters and their Unicode code points back to each other.
Reference: [Python3.8 official document #ord]
(https://docs.python.org/ja/3/library/functions.html#ord)
sample
>>> ord('A')
65
>>> ord('D')
68
>>> chr(65)
'A'
>>> chr(70)
'F'
As you can see, it is called "Unicode code point", but it breaks through with the recognition of "a world where various character symbols are represented by a one-to-one correspondence with numbers". Japanese is a world with a lot of expressions, so [books that look good](https://www.amazon.co.jp/%E6%94%B9%E8%A8%82%E6 % 96% B0% E7% 89% 88-% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E% E3% 81% AE% E3% 81% 9F% E3% 82% 81% E3% 81% AE% E6% 96% 87% E5% AD% 97% E3% 82% B3% E3% 83% BC% E3% 83% 89% E6% 8A% 80% E8% A1% 93% E5% 85% A5% E9% 96% 80-WEB-PRESS-plus% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 4297102919 / ref = sr_1_1? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82% AB% E3% 83% 8A & keywords =% E6% 96% 87% E5% AD% 97% E3% 82% B3% E3% 83% BC% E3% 83% 89 & qid = 1585548181 & sr = 8-1).
Apparently, for the uppercase alphabet,'A' is code 65, and from there it is a serial number in decimal. By the way, between uppercase and lowercase
sample
>>> ord('Z')
90
>>> chr(91)
'['
>>> chr(92)
'\\'
>>> chr(93)
']'
>>> chr(94)
'^'
>>> chr(95)
'_'
>>> chr(96)
'`'
>>> chr(97)
'a'
And some symbols were sandwiched. Hmm.
As you may have noticed in the arguments, both ord () `` `and
chr () `assume single character conversion. When converting the character string S, convert it to the empty string
''
one character at a time with the
`for``` statement, then combine and repeat.
And in order to consider when returning from'Z'to'A', add the remainder `` `, which is the integer obtained by adding N to the original character code point by 26, to the code point of'A'. Calculate the character code point after conversion with.
rot_n.py
def rot_n(s, n):
answer = ''
for letter in s:
answer += chr(ord('A') + (ord(letter)-ord('A')+n) % 26)
return answer
I will check it.
test_rot
>>> rot_n('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 13)
'NOPQRSTUVWXYZABCDEFGHIJKLM'
Click here for submission of AtCoder B problem ↓
b.py
def rot_n():
n = int(input())
s = input()
answer = ''
for letter in s:
answer += chr(ord('A') + (ord(letter)-ord('A')+n) % 26)
print(answer)
if __name__ == '__main__':
rot_n()
By the way, the function that restores the converted character string S (shifts it before N characters) is
derot_n.py
def derot_n(s, n):
answer = ''
for letter in s:
answer += chr(ord('Z') - (ord('Z')-ord(letter)+n) % 26)
return answer
It will be.
test_derot
>>> derot_n('NOPQRSTUVWXYZABCDEFGHIJKLM', 13)
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
I learned from the AtCoder problem I wrote at the beginning. You've generalized ROT13, which is a simple cipher that shifts 13 characters back.
Recommended Posts