Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

3
Can anyone help with some beginner python?
Post Body

Problem Description: Most books are assigned a number called the international standard book number, or ISBN. This sequence typically contains 10 decimal digits, though sometimes the last digit is replaced with the capital letter X. Hyphens are sometimes embedded in between the digits to make the ISBN number more readable. The first nine digits uniquely identify a book, and the tenth character is a check digit that is used to verify the correctness of the first nine digits - the check digit is chosen so that a weighted sum calculated from digits of the ISBN number is divisible by 11. Since the check digits sometimes needs to be 10 in order to make this value divisible by 11, we use X to represent 10.

How to compute the weighted sums: Form two lists of integers based on the digits in the ISBN. The first list, sum1, contains partial sums of the digits from the ISBN number. The second list, sum2, contains partial sums of the values in the sum1 list. The ISBN is correct if the last entry in sum2 is divisible by 11.

Example: Consider the ISBN number 0-321-53711-4. First, we remove (or ignore) the hyphens. So we get:

digits in ISBN 0 3 2 1 5 3 7 1 1 4 list sum1 entries 0 3 5 6 11 14 21 22 23 27 list sum2 entries 0 3 8 14 25 39 60 82 105 132

The ith entry in sum1 is the sum of the digits in positions 1 through i in the ISBN number, if you consider the leftmost digit to be in position 1.

The ith entry in sum2 is the sum of the integers in positions 1 through i in the sum1 list.

Since 132 is divisible by 11, the ISBN is valid.

Covered topics: functions, lists, file I/O, loops, string library functions.

You must use several functions to decompose the problem into smaller problems. All code except import statements and a call to main() must be contained in a function. No function should be very long or complicated, and each function should have a well-defined action.

Your program will process a file called isbn.txt that contains one ISBN per line. Some of the ISBNs will be valid, and some will be invalid. The ISBNs may contain one or more hyphens, and some of the invalid ISBNs may contain extra characters, or too few digits. The file that we will use to test your program will be different from the sample file. You will read one line at a time as a string, then store the digits (and possibly the character X) in a list. Do NOT include extraneous characters or hyphens in this list. Note that the ISBN is invalid if: there are characters other than the digits '0' through '9' and the characters 'x', 'X', and '-', if there are more than 9 digits 0 through 9 before the last character, if the last character (other than a hyphen) is not a digit from 0 to 9, 'x' or 'X'. You will use three lists in this project: One for the digits (and check character) in the ISBN number, one for the list sum1 described above, and another for sum2.

Open a file isbnValid.txt for writing, and write one line for each entry in the file isbn.txt. Each line should contain one ISBN number from the isbn.txt file, plus either the word "valid" or "invalid". For the sample isbn.txt file, isbnValid.txt looks like this:

0-321-53711-4 valid -0-131-62959-X valid 88-10001202 invalid 0--1014114578a- invalid

Author
Account Strength
90%
Account Age
13 years
Verified Email
Yes
Verified Flair
No
Total Karma
65
Link Karma
65
Comment Karma
n/a
Profile updated: 1 day ago
Posts updated: 4 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
13 years ago