

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A python programming assignment for a computer science course. Students are required to write a python module containing four functions: collatzsequence, collatzsearch, isalphabetic, and gettokens. The first function generates a collatz sequence given a starting number. The second function searches for collatz sequences of a minimum length with a specified number of examples. The third function checks if a character is alphabetic or an apostrophe. The fourth function tokenizes a string into a list of words and punctuation. The assignment includes instructions for reading related text and provides examples and test cases.
Typology: Assignments
1 / 2
This page cannot be seen from the preview
Don't miss anything!


For this assignment you are to create and submit a single Python module; that is, a file with a “.py” extension containing nothing but Python code (which, of course, includes comments, such as the standard six-line header). The file must include four (4) function definitions with precisely these names (including case):
Your file, which must be named “hw2.py,” may also contain other functions and global variables, but you will be graded exclusively on the correctness and clarity of the four functions named above (as well as those that they may call). Here is what you need to do:
an+1 =
undefined if an = 1 an/ 2 if an is even 3 an + 1 if an is odd
where a 0 > 1 is specified (i.e. set to anything you like), and the sequence terminates as soon as the value 1 is attained (which, as far as anybody knows, will always happen eventually). For example, starting with a 0 = 3, we get the following sequence: 3, 10, 5, 16, 8, 4, 2, 1. Starting with a 0 = 5, we get the following sequence: 5, 16, 8, 4, 2, 1. This type of sequence has come to be called a Collatz sequence. Note that the length of the Collatz sequence varies wildly and is impossible to predict from the starting value. For this part of the assignment you are to define a Python function called CollatzSequence that takes a single argument, call it N , and returns a list containing the entire Collatz sequence that begins with a 0 = N. For example, CollatzSequence( 5 ) should return the list [ 5, 16, 8, 4, 2, 1 ]. (Do not worry about catching erroneous input, such as non-integer values of N. You may simply assume that N will always be an integer greater than 1, and that the sequence will always terminate.)
3, 8 ) should return {3: [3, 10, 5, 16, 8, 4, 2, 1], 6: [6, 3, 10, 5, 16, 8, 4, 2, 1], 7: [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]}, be- cause 3, 6, and 7 are the first three integers that produce a Collatz sequence of length 8 or longer.
Note that the tokens "it’s" and "Shakespeare’s" each contain an apostrophe, which is why we included apostrophes among the alphabetic characters. It’s also why Python decided to print those tokens out using double quotes rather than single quotes (apostrophes). We will build upon this function in subsequent assignments to do some interesting processing of English text.
More test cases for you to check your functions with will be posted for you on the downloads page. Be sure to test your functions thoroughly. Also, remember to document your functions internally.
You will need to understand strings, lists, dictionaries, the append method, the len function, if-elif-else tests, while loops, and the in keyword (among other things) to do this assignment.