[PYTHON] From a book that programmers can learn: Verification of rune checksum (fixed length)

Getting started with programming in Python Continued from Last time

Also, this time, Problem presentation before that ・ Know that you have read the numbers to the end I will challenge the problem to solve this.

Rune checksum verification (fixed length) > Write a program that takes a 6-digit identification number (including a check digit) and checks if the value is valid in a rune expression. In this program, after reading one digit, it is necessary to process that digit before proceeding to the next digit.

However, as always, it's difficult to keep going.

○ Simple checksum verification (fixed length) > Write a program that takes a 6-digit identification number (including a check digit) and checks if the value is valid in a rune expression. If the result of adding the numerical values of each digit is divisible by 10, the value is considered to be valid. In this program, after reading one digit, the processing of that digit must be completed before proceeding to the next digit.
char digit;
int checksum = 0;
cout << "Enter a six-digit number:";
for (int position = 1; position <= 6; position ++) {
    cin >> digit;
    checksum += digit - '0';
}
cout << "Checksum is " << checksum << ". \n";
if (checksum % 10 == 0) {
    cout << "Checksum is divisible by 10. Valid. \n";
} else {
    cout << "Checksum is not divisible by 10. Invalid. \n";
}

My answer (Python)

ConsoleOut.py


#!/usr/bin/env python
#coding:utf-8

#Using cout in python taught by shiracamus
from ConsoleOut import cout


def number():
    checksum = 0
    for i in range(6):
        cout << "Enter a six-digit number:"
        digit = input()
        checksum += int(digit)
    cout << "Checksum is " + str(checksum) + ". \n";
    if checksum % 10 == 0:
        cout << "Checksum is divisible by 10. Valid .\n"
    else:
        cout << "Checksum is not divisible by 10. Invalid. \n"

test01.py(Add 6-digit input numbers)


###I wrote it though it is not so much that I can say that I made it myself.
#!/usr/bin/env python
#coding:utf-8

from ConsoleOut import cout


def number():
    checksum = 0
    for i in range(6):
        cout << "Enter a six-digit number:"
        digit = input()
        checksum += int(digit)
    cout << "Checksum is " + str(checksum) + ". \n";
    if checksum % 10 == 0:
        cout << "Checksum is divisible by 10. Valid .\n"
    else:
        cout << "Checksum is not divisible by 10. Invalid. \n"

Most of the parts are written by diversion so far, The understanding of int type, str type, internal variables, etc. is still insufficient, and the first checksum + = int (digit) For some reason, I was worried that they wouldn't add up. Declared with checksum = 0? If you do, they will add up. I wonder if I could do it for the time being (sweat)

(Addition) At the same time, double the odd-numbered input The original problem is that the odd-numbered numbers are doubled, and if the number is two digits, each digit is added. Since it is necessary to add it, I made the function under the guidance before, so I will divert it Click here for functions

test.py(Function to add each digit if the number of inputs is 10 or more)


#!/usr/bin/env python
#coding:utf-8

def doubleDigitValue(digit):
    doubledDigit = digit * 2
    if (doubledDigit >= 10):
        sum = 1 + doubledDigit % 10 
    else:
        sum = doubledDigit
    return sum

And here is the previous one

test.py(Added doubleDigitValue function)


#!/usr/bin/env python
#coding:utf-8

from ConsoleOut import cout
from test import doubleDigitValue


def number():
    checksum = 0
    for i in range(6):
        cout << "Enter a six-digit number:"
        digit = input()
        if i % 2 == 0:
            checksum += int(digit)
        else:  #If it is an odd number, double it and if it is 10 or more, add each digit.
            checksum += doubleDigitValue(int(digit)) 
    cout << "Checksum is " + str(checksum) + ". \n";
    if checksum % 10 == 0:
        cout << "Checksum is divisible by 10. Valid .\n"
    else:
        cout << "Checksum is not divisible by 10. Invalid. \n"

Execution result


>>> from test01 import number
>>> number()
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Enter a six-digit number:1
Checksum is 9. 
Checksum is not divisible by 10. Invalid. 
>>> number()
Enter a six-digit number:2  #2
Enter a six-digit number:1  #1*2
Enter a six-digit number:1  #1 
Enter a six-digit number:1  #1*2
Enter a six-digit number:1  #1
Enter a six-digit number:1  #1*2
Checksum is 10. 
Checksum is divisible by 10. Valid .
>>> 
#The problem has been solved. Thank you!

As a first result (1 * 2) + 1 + (1 * 2) + 1 + (1 * 2) +1 = 9 // 1st time When the result is displayed, it seems to be correct at first I think it's different to enter it the second time. (2 * 2) + 1 + (1 * 2) + 1 + (1 * 2) +1 = 10 ??? // 2nd time It's an inadequate situation, but let's think a little more.

Recommended Posts

From a book that programmers can learn: Verification of rune checksum (fixed length)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
From a book that programmers can learn ... (Python): Review of arrays
From a book that programmers can learn ... (Python): Pointer
From a book that programmers can learn ... (Python): About sorting
From a book that programmers can learn (Python): Decoding messages
From a book that programmers can learn (Python): Find the mode
From a book that programmers can learn ... Collect small problem parts
From a book that programmers can learn (Python): Statistical processing-deviation value
From a book that programmers can learn (Python): Conditional search (maximum value)
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn: Converting characters that represent numbers to integer types
From a book that makes the programmer's way of thinking interesting (Python)
Programmer's way of thinking is from XX book (Python)
"A book that understands Flask from scratch" Reading memo
Programmer's way of thinking is from XX book (Python)
Is your dll a dll that can take advantage of multi-core?
Iterator that can scan forward from the middle of the sequence