[PYTHON] From a book that programmers can learn ... Collect small problem parts

Getting started with programming in Python Continued from Last time

At the end of this question, each part will be put together to become the final answer. (It seems that it was already completed last time (sweat)

Problem: (First task) Verification of rune checksum > (Please note that the problem statement is duplicated) The rune method is a method often used to verify the identification number. For each digit of the original number, double that number every other digit. After that, add each digit (if there is a number that doubles to 2 digits, add the tens digit and the ones digit individually). If the sum is divisible by 10, the identification number is considered valid. Let's write a program that receives an identification number of any length and checks whether the value is valid in a rune expression. In this program, after reading one digit, the digit must be processed before proceeding to the next digit.

Know which digit to double > [Answer](http://qiita.com/teratera/items/503be633b4148824b368) As you can see in this comment, Python's input () function is used to input everything once, calculate the string length, and derive even and odd numbers.

When the result of doubling becomes 2 digits, each digit is treated individually > [Answer](http://qiita.com/teratera/items/6dfa72d6b5d1e987c2df) (0-9) The calculation after doubling was messed up, but I was able to get the number I wanted by 1 + (double number% 10).

Know that you have read the numbers to the end > [Answer](http://qiita.com/teratera/items/a76667d8e6cc23078292) This is also the answer to the problem. In C ++, it seems that the ASCII character code is calculated and judged by the line feed code, but in Python, the input () function seems to judge the line feed, so it was not particularly difficult.

Read each digit individually [Answer](http://qiita.com/teratera/items/387be64bd9b40971cd38) Repeat with the range () function and input individually with the input () function (However, the input () function could be input as a character string)

From the above, the answer is as follows.

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

from ConsoleOut import cout
from test import doubleDigitValue


def number():
    checksum = 0
    position = 0
    cout << "Enter a number:"
    digits = input()
    while position < len(digits):
        digit = digits[position]
        if position % 2 == 0:
            checksum += int(digit)
        else:
            checksum += doubleDigitValue(int(digit))
        position += 1

    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. Invalud. \n"

・ ・ ・(Terminal results)
Enabled or disabled when problem 1762483 is calculated by checksum
>>> from test03 import number
>>> number()
Enter a number:1762483
Checksum is 30. 
Checksum is divisible by 10. Valid .(Effectiveness)

If it is divisible by 10, the result is that you want to ask for "effective". Up to this point, I have learned a myriad of things, but I will take advantage of these to tackle the next task.

Recommended Posts

From a book that programmers can learn ... Collect small problem parts
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 ... (Python): Review of arrays
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: Verification of rune checksum (fixed length)
From a book that programmers can learn (Python): Class declaration (public / private, etc.)
From a book that programmers can learn ... Verification of rune checksum (variable length) Part 2
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)
"A book that understands Flask from scratch" Reading memo
Make a Kindle book that images mathematical formulas from TeX files
8 services that even beginners can learn Python (from beginners to advanced users)
I made a simple timer that can be started from the terminal
TensorFlow To learn from a large number of images ... (Unsolved problem) → 12/18 Solved