I would like to summarize what I learned a little from studying atcoder.
ASCII Do you know about character codes? According to Wikipedia
A character code is provided for each character (may not be a single character) when a sentence is handled in the form of text rather than as graphic data such as an image in an electronic medium such as a computer. It's a code. (wikipedia)
There is. Simply put, it's like a rule (code) for expressing characters on a computer. As a type
Character code | Use | Amount of information |
---|---|---|
ASCII | Codes representing alphabets and numbers | 7-bit code |
SJIS | Code that can express Japanese | 16-bit code |
EUC-JIS | Code for expressing Japanese on UNIX | 16-bit code |
Unicode | Codes that represent characters around the world | 16-21 code |
UTF-8 | Compatible with the most popular code, ASCII | 3 bytes |
That's about it. I think there are many people who have seen UTF-8.
Atcoder146 B - ROT N
Problem statement There is a string S consisting only of uppercase letters. It is also given the integer N. Output a character string in which each character of S is replaced with the character after N in alphabetical order. However, the letter after Z in alphabetical order is regarded as A.
I used the ASCII idea above in solving this problem.
test.py
N = int(input())
S = input()
def change():
s=[]
for i in range(len(S)):
m = ((ord(S[i])-ord("A")+N) % 26)
s.append(chr(m+ord("A")))
return "".join(s)
print(change())
Please forgive me for the dung code (laughs) The most important thing here is
ʻOrd ()and
chr ()` functions. This function is a function that converts letters and numbers written in ASCII into pre-assigned numbers and letters. [ASCII code]: http://www12.plala.or.jp/mz80k2/electronics/ascii/ascii.html
For example, the letter "A" is ** 65 ** according to the ASCII character code. Also, if you use a function
chr("a")
I write it like this. On the contrary, if you want to assign a number to asciiord(65)
It will be like that.
There are various character codes, so it's interesting to study! !!
Recommended Posts