Getting Started with Python Exercises (23) - CCF CSP certification exam Zhenti: ISBN number

Problem Description

Questions Number : 201312-2
questions Name : ISBN Number
Time limit : 1.0s
Memory Limit : 256.0MB

Problem description
  of each of the present official publication of the book has a corresponding ISBN number, ISBN code comprising a 9-digit number, an identification code and a separator 3, which is a predetermined format such as "x-xxx-xxxxx-x ", wherein the symbol "-" is a delimiter (minus sign on the keyboard), the last one is the identification code, e.g. ISBN 0-670-82162-4 is a standard code. The first digit represents the ISBN code of books published in five languages, such as 0 for English; first separator "-" represents the three digits after the press, such as 670 representatives of Viking Press; the second five digits after separation the book represents the number of the publishing house; the last one is the identification code.
  Identification code calculated as follows:
  the first number multiplied by 1 plus the second digit is multiplied by 2 ...... and so on, with the results obtained mod 11, resulting remainder is the identification code, if the remainder is 10, the identification code is capital letter X. E.g. ISBN 0-670-82162-4 number of the identification code is thus obtained 4: 067,082,162 of nine digits, from left to right, are multiplied by 1, 2, ..., 9, and then summed, i.e., 0 × 1 + 6 × 2 + ...... + 2 × 9 = 158, 158 mod 11 and then take the results of 4 as an identification code.
  Analyzing programming ISBN number inputted identification code is correct, if correct, then the output only "Right"; if an error, the output is correct ISBN number.
Input format
  Enter only one line, it is a sequence of characters representing ISBN number of a book (to ensure input the required format ISBN numbers).
Output format
  output line, ISBN number if the identification code entered correctly, then the output "Right", otherwise, according to a predetermined format, output the correct ISBN number (including separators "-").
Sample input
0-670-82162-4
Sample Output
Right
Sample Input
0-670-82162-0
sample output
0-670-82162-4

Problem-solving ideas

  1. Removing the input string delimiter '-', to give a 10-digit string. This facilitates calculation.
  2. Calculated by the calculation method for the identification code of the identification code. The remainder is converted to a 10 character 'X'.
  3. Check the identification code is correct.

Answers

isbn_str = input()
#1. 去掉输入串中的分隔符“-”,得到有10位数字的字符串。
isbn_str_no_sp = ''
for s in isbn_str:
    if s != '-':
        isbn_str_no_sp += s
#2. 按识别码的计算方法计算出识别码。
total = 0
for i in range(9):
    total += int(isbn_str_no_sp[i]) * (i + 1)
check_code = total % 11

if check_code == 10:
    check_code = 'X'
else:
    check_code = str(check_code)
#3. 检查识别码是否正确
if isbn_str[-1] == check_code:
    print('Right')
else:
    print(isbn_str[:12] + check_code)

Test Case

Title description given in the two sets of test cases has covered the identification code and the identification code error correct two cases. These two sets of test cases, prove the correctness of the program basically. Test given below to further verify the identification code calculation is correct.
  It is worthy of discussion:

  1. Is not the first 9 digits of each must be tested separately? My view is, do not, after all, the calculation rules are simple enough. For this reason I verified first before 3 segment numbers, ie 1,2,5 digital.
  2. How to quickly construct a test case? Strategy is to transform the basis of the test case in the description given on the subject.

Increase test case as follows.

  1. Review No. 1 on the basis of a sample. Finally, an identification code is 'X'. PIN correctly.
    Sample input
    6-670-82162-X
    Sample Output
    Right
  2. Review No. 1 on the basis of a sample. Finally, an identification code is 'X'. Identification code error.
    Sample Input
    6-670-82162-9
    Sample Output
    6-670-82162-X
  3. Modify the first two on the basis of sample 2. PIN correctly.
    Sample input
    0-770-82162-6
    Sample Output
    Right
  4. 5th revised on the basis of a set of test cases. PIN correctly.
    Sample input
    0-770-92162-0
    Sample Output
    Right

summary

  1. Skilled string operations, to answer this question clear.
  2. By altering the existing test cases, generate new test cases, it is a good way. This approach is worth using software capability in the CCF certification exam.
Published 85 original articles · won praise 86 · views 310 000 +

Guess you like

Origin blog.csdn.net/yedouble/article/details/104104373