Now, as usual, I was instructed to break down this into a small problem as to how to make it into Python code, so I will show you the details.
--Read one character at a time until you reach the end of the line (this is solved with Python's input ()) --Convert a string representing a number to an integer. (This is also solved by Python input ()) --Convert integer values from 1 to 26 to uppercase alphabet. --Convert integer values from 1 to 26 to lowercase alphabets. --Convert integer values from 1 to 8 to symbols according to Table 2-3. --Remember the decryption mode.
I would like to proceed with the above contents. So, immediately
If it is the contents of a book, it can be converted to characters from A to Z by matching it with the ASCII character code and adding some character code to the integer values from input 1 to 26. I am proceeding with.
Like last time, I will try a different method in Python without using ASCII character code, but in Python it is rather rudimentary or it is okay to put it in a list and extract it with an index. If this --Convert integer values from 1 to 26 to uppercase alphabet. --Convert integer values from 1 to 26 to lowercase alphabets. --Convert integer values from 1 to 8 to symbols according to Table 2-3. I think we can handle it.
test12.py
#!/usr/bin/env python
#coding:utf-8
from ConsoleOut import cout #cout in python<<Prepare a class to use(Taught me)
import enum
import math
UPPERCASE = [' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
LOWERCASE = [' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
PUNCTUATION = [' ','!','?',',','.',' ',';','"','\n']
modeType = UPPERCASE
while modeType in [UPPERCASE,LOWERCASE,PUNCTUATION]:
cout << "Enter some numbers ending with -1: "
digit = input()
if modeType == UPPERCASE:
cout << "Number read: " + digit + "\n"
digit = int(digit) % 27
cout << ". Modulo 27: " + UPPERCASE[digit] + "\n"
if digit == 0:
modeType = LOWERCASE
else:
continue
elif modeType == LOWERCASE:
cout << "Number read: " + digit + "\n"
digit = int(digit) % 27
cout << ". Modulo 27: " + LOWERCASE[digit] + "\n"
if digit == 0:
modeType = PUNCTUATION
else:
continue
elif modeType == PUNCTUATION:
cout << "Number read: " + digit + "\n"
digit = int(digit) % 9
cout << ". Modulo 27: " + PUNCTUATION[digit] + "\n"
if digit == 0:
modeType = UPPERCASE
else:
continue
・ ・ ・ ・(Terminal execution)
>>> import test12
Enter some numbers ending with -1: 18
Number read: 18
. Modulo 27: R
Enter some numbers ending with -1: 12312
Number read: 12312
. Modulo 27:
Enter some numbers ending with -1: 171
Number read: 171
. Modulo 27: i
Enter some numbers ending with -1: 763
Number read: 763
. Modulo 27: g
Enter some numbers ending with -1: 98423
Number read: 98423
. Modulo 27: h
Enter some numbers ending with -1: 1208
Number read: 1208
. Modulo 27: t
Enter some numbers ending with -1: 216
Number read: 216
. Modulo 27:
Enter some numbers ending with -1: 11
Number read: 11
. Modulo 27: ?
Enter some numbers ending with -1: 500
Number read: 500
. Modulo 27:
Enter some numbers ending with -1: 18
Number read: 18
. Modulo 27:
Enter some numbers ending with -1: 241
Number read: 241
. Modulo 27: Y
Enter some numbers ending with -1: 0
Number read: 0
. Modulo 27:
Enter some numbers ending with -1: 32
Number read: 32
. Modulo 27: e
Enter some numbers ending with -1: 20620
Number read: 20620
. Modulo 27: s
Enter some numbers ending with -1: 27
Number read: 27
. Modulo 27:
Enter some numbers ending with -1: 10
Number read: 10
. Modulo 27: !
Enter some numbers ending with -1:
For the time being, it is now possible to output from integer values to alphabets and symbols by switching modes with an infinite loop, but there are many subtle points such as the part where extra spaces appear on the way and how to exit the infinite loop. However, it worked for the time being.
By the way, here is the answer for C ++
char outputCharacter;
enum modeType {UPPERCASE,LOWERCASE,PUNCTUATION};
modeType mode = UPPERCASE;
char digitChar;
do {
digitChar = cin.get();
int number = (digitChar - '0');
digitChar = cin.get();
while ((digitChar != 10)&&(digitChar != ',')){
number = number * 10 + (digitChar - '0');
digitChar = cin.get();
}
switch (mode){
case UPPERCASE:
number = number % 27;
outputCharacter = number + 'A' - 1;
if(number == 0){
mode = LOWERCASE;
continue;
}
break;
case LOWERCASE:
number = number % 27;
outputCharacter = number + 'a' - 1;
if (number == 0){
mode = PUNCTUATION;
continue;
}
break;
case PUNCTUATION:
number = number % 9;
switch(number){
case 1: outputCharacter = '!';break;
case 2: outputCharacter = '?';break;
case 3: outputCharacter = ',';break;
case 4: outputCharacter = '.';break;
case 5: outputCharacter = ' ';break;
case 6: outputCharacter = ';';break;
case 7: outputCharacter = '"';break;
case 8: outputCharacter = '\';break;
}
if(number == 0){
mode = UPPERCASE;
continue;
}
break;
}
cout << outputCharacter;
}while (digitChar != 10);
cout << "\n";
This time I couldn't divide it into small pieces, so I wrote my own python code while looking at the answer code.
Recommended Posts