#K1551. Valid ISBN-10 Checker
Valid ISBN-10 Checker
Valid ISBN-10 Checker
You are given a string that is intended to represent an ISBN-10 number. Your task is to determine whether the given ISBN-10 is valid.
An ISBN-10 is valid if it meets the following conditions:
1. It must be exactly 10 characters long.
2. The first 9 characters must be digits (0-9).
3. The last character must be either a digit (0-9) or the uppercase letter X, which represents the value 10.
4. The checksum is calculated by summing the products of each of the first 9 digits with its position index (starting at 1) and then taking the modulus with 11. For the ISBN to be valid, the result should match the value of the last character. If the last character is 'X', the checksum must be equal to 10, otherwise it must equal the numeric value of that character.
Formally, let the ISBN be represented as (d_1d_2\ldots d_9c). Compute
[
\text{checksum} = \left(\sum_{i=1}^{9} i \times d_i\right) \mod 11.
]
The ISBN is valid if and only if
[
\text{checksum} = \begin{cases}10,& \text{if } c = 'X'\ c,& \text{if } c \text{ is a digit}\end{cases}
]Note that all calculations and comparisons should respect the rules above.
inputFormat
The input consists of a single line containing a string which represents the ISBN-10 number. The string may include digits and/or the letter 'X' as described.
outputFormat
Output a single line: print 'True' if the provided ISBN-10 is valid according to the rules, or 'False' otherwise.## sample
0306406152
True
</p>