Assignment 3: due 11:59 pm on Saturday, Oct 19, 2024
To clarify Part 1 specifications, I have provided sample tests in Section 3. The behaviour implied by the sample tests should be considered as required specifications in addition to what is explained below.
Summary of Instructions
Note Read the instructions carefully and follow them exactly
Assignment Weight 4% of your course grade
Due Date and time 11:59 pm on Saturday, Oct 19, 2024
Important
As outlined in the syllabus, late submissions will not be accepted
Any files with syntax errors will automatically be excluded from grading. Be sure to test your code before you submit it
For all functions, both in Part 1 and 2, make sure you’ve written good docstrings that include type contract, function description and the preconditions if any.
This is an individual assignment. Please review the Plagiarism and Academic Integrity policy presented in the first class, i.e. read in detail pages 16-20 of course outline (i.e. slides of Lecture 1). You can find that file on Brightspace under Lecture 1. While at it, also review Course Policy on missing assignments on page 14.
The goal of this assignment is to learn and practice the concepts covered thus far, in particular: strings (including indexing, slicing and string methods), control structures (if statements and for-loops), use of range function, function design and function calls.
The only collection you can use are strings. You may not use any other collection (such as a list/array, tuple, set or dictionary) in this assignment. Using any of these in a solution to a question constitutes changing that question. Consequently, that question will not be graded.
Your grade will partially be determined by automatic (unit) tests that will test your functions. All the specified requirements below are mandatory (including function names). Any requirement that is specified and not met may/will result in deduction of points.
Submit your assignment by the deadline via Brightspace (as instructed and practiced in the first lab.) You can make multiple submissions, but only the last submission before the deadline will be graded. For this assignment you must submit 5 files as described below. For each missing file there will be a grade deduction:
The assignment has two parts. Each part explains what needs to be submitted. Put all those required documents into a folder called a3_xxxxxx where you changed xxxxxx to your student number, zip that folder (do not use rar compression format) and submit it as explained in Lab 1. In particular, the folder must have the following 5 files:
Part 1: a3_part1_xxxxxx.py, a3_part1_xxxxxx.txt
Part 2: a3_part2_xxxxxx.py and a3_part2_xxxxxx.txt and
references-YOUR-FULL-NAME .txt
Both of your programs must run without syntax errors. In particular, when grading your assignment, TAs will first open your file a3_part1_xxxxxx.py with IDLE and press Run Module. If pressing Run Module causes any syntax error, the grade for Part 1 becomes zero. The same applies to Part 2, when they open and run file a3_part2_xxxxxx.py.
Furthermore, for each of the functions (in Part 1 and Part 2), I have provided one or more tests to test your functions with. For example, you should test the function split_tester from Part 1 by making a call in Python shell with split_tester ("12311234","4") . To obtain a partial mark your function may not necessarily give the correct answer on these tests. But if your function gives any kind of python error when run on the tests provided below, that question will be marked with zero points.
Section 3, contains tests for Part 1. Tests for Part 2 are provided after each question in Part 2. To determine your grade, your functions will be tested both with examples provided in Part 2 and Section 3 and with some other examples. Thus you too should test your functions with more examples than what I provided in Part 2 and Section 3.
Use of global variables inside function bodies is not allowed. If you do not know what that means, interpret this to mean that inside of your functions you can only use variables that are created in that function. For example, this is not allowed, since variable x is not a parameter of function a_times(a) nor is it a variable created in function a_times(a). It is a global variable created outside of all functions.
def a_times(a):
result=x*a
return result
x=float(input("Give me a number: "))
print(a_times(10))
About references-YOUR-FULL-NAME . txt file:
The file must be a plain text file. The file must contain references to any code you used that you did not write yourself, including any code you got from a friend, internet, AI engines like chatGPT, social media/forums (including Stack Overflow and discord) or any other source or a person. The only exclusion from that rule is the code that we did in class, the code done as part the lab work, or the code in your textbook. So here is what needs to be written in that file. For every question where you used code from somebody else:
• Write the question number
• . Copy-paste all parts of the code that were written by somebody else. That includes the code you found/were-given and that you then slightly modified.
• Source of the copied code: name of the person or the place on the internet/book where you found it.
While you may not get points for copied parts of the question, you will not be in the position of being accused of plagiarism. Any student caught in plagiarism will receive zero for the whole assignment and will be reported to the dean.
Showing/giving any part of your assignment code to a friend also constitutes plagiarism and the same penalties will apply. If you have nothing to declare/reference, then just write a sentence stating that and put your first and last name under that sentence in your references-YOUR-FULL-NAME.txt file.
Not including references-YOUR-FULL-NAME.txt file, will be taken as you declaring that all the code in the assignment was written by you. Recall though that not submitting that file, comes with a grade penalty.
1 Part 1: increasing-splits tester - 40 points
To clarify Part 1 specifications, I have provided sample tests in Section 3. The behaviour implied by the sample tests should be considered as required specifications in addition to what is explained below.
Description: Some positive integers N can be split into smaller pieces, such that the pieces are comprised of consecutive digits of N and such that each piece contains the same number of digits. Given an integer N and given a number of digits d, one can then ask if N can be split into pieces such that each pieces has d digits and such that the resulting sequence of numbers is strictly increasing.
Examples:
- For N = 154152 and d = 2, the answer is yes, since d = 2 implies the following sequence 15, 41, 52 which is increasing.
- For N = 154152 and d = 3, the answer is no, since d = 2 implies the following sequence 154, 152 which is not increasing.
- For N = 154152 and d = 5, the answer is no, since 154152 cannot be split into five-digit pieces.
- For N = 137 and d = 1, the answer is yes, since since d = 1 implies the following sequence 1, 3, 7 which is increasing.
- For N = 137 and d = 2, the answer is no, since 137 cannot be split into two-digit pieces.
- For N = 113 and d = 1, the answer is no, since since d = 1 implies the following sequence 1, 1, 3 which is not increasing.
- For N = 113 and d = 3, the answer is yes, since d = 3 implies the following sequence 113 which is increasing.
Design, implement and test a Python program which checks to see if a user-supplied positive integer N and a user- specified split d, are such that N can be split into pieces with d digits such that the resulting sequence is strictly increasing. Here are the steps that your program should have: (Note that since no collection other than strings is allowed you will be working with N and d as strings)
0. The program greets the user.
1. The program asks the user if they want to test if a number admits an increasing-split of give size.
2. The program prompts the user to enter a positive integer. If the user enters an invalid input (anything other than a positive integer), the program will repeat the questions starting with step 1.
3. The program prompts the user to enter the number of digits in each piece. The program will verify that the input is valid (a positive integer which is a proper divisor of the number of digits in the first input); If the input is invalid, the program will repeat the questions starting with step 1.
4. Once the valid input is obtained, the program splits the number into pieces of specified length and displays/prints those pieces in one line (separated by commas).
5. Finally, the program reports whether or not the obtained sequence of numbers is in strictly increasing order. If there is only one piece, it is defined to be in strictly increasing order.
For this part, I provided you with starter code in file called part1_xxxxxx.py. Begin by replacing xxxxxx in the file name with your student number. Then open the file. Your solution (code) for this part must go into that file in the clearly indicated spaces only. You are not allowed to delete or comment-out or change any parts of the provided code except for the keyword pass.
For this part you need to submit two files: a3_part1_xxxxxx.py and a3_part1_xxxxxx.txt
a3_part1_xxxxxx.py needs to contain your program for Part 1 as explained above and a3_part1_xxxxxx.txt needs to contain the proof that you tested your two core function from this part, namely split_tester.
1.1 The Core Function
Your solution in a3_part1_xxxxxx.py must have a function called: split_tester. You should design and test function split_tester before moving onto designing and coding the main part of the program. Here are specifications for that function:
split_tester This function has two parameters, a string N and a string d. These two strings can be assume to be such that each looks like a positive integer and such that the number of digits in N is divisible by the integer represented by d.
Since N and d are assumed to be valid i.e. meet the preconditions, they define a sequence of numbers (as specified in the examples above). The function should print that sequence of numbers in such a way that the consecutive numbers are separated by commas as shown in the test cases. Note that there cannot be a comma after the last number in the sequence.
The function should then return True if the sequences is strictly increasing and False otherwise. (Reporting, i.e. printing, whether or not the obtained sequence of numbers is strictly increasing has to be done in the main and not in the body of the split_tester function).
Here are two approaches that may help you design this function:
You may find it easier to process the number N, which is given as a string, when you split it into pieces.
One approach:
start with an empty string to hold the substring, i .e. the split
as you loop through the whole number one digit at a time
append digits to the substring
if the substring reaches the desired length
do something with the substring
reset the substring to empty to collect the next substring
reset the substring length count to zero
Another approach:
figure out how many substrings you want
loop that number of times
use slices to create the substrings
Other suggestions:
-You can use the isdigit() string method to determine if a string contains only digits. Type help(str.isdigit) in the Python shell for more information.
- You can use the len() function to determine the length of a string i.e. the number of digits in N.
- If you find getting the commas right in the output tricky (because only commas between numbers are allowed), leave that until everything else is working correctly.
1.2 The User Interaction i.e. the main part of the program
Now that you have the function that performs the core functionality, you want to code the communication with the user in the main. It is also in the main that you should print the message about whether the sequence is increasing or not.
Advice: Leave error checking of the input for last. Begin by building your function first, and then the main assuming perfect input. Make sure though to leave enough time at the end to get the input checking details right.
The specifications on how your program needs to behave when communicating with the user should be inferred from the test examples. You will notice that the program is required to display greetings surrounded with stars. One of your functions from Assignment 1, may be helpful for that. You can copy paste that function from your Assignment 1 solution (or mine) to your solution in a3_part1_xxxxxx.py.
2 Part 2: A Library of Functions
For this part of the assignment, you are required to write and test several functions (as you did in Assignment 1). You need to save all functions in a3_part2_xxxxxx.py where you replace xxxxxx by your student number. You need to test your functions (like you did in Assignment 1) and copy/paste your tests in a3_part2_xxxxxx.txt. Thus, for this part you need to submit two files: a3_part2_xxxxxx.py and a3_part2_xxxxxx.txt
2.1 sum_odd_divisors(n) - 5 points
Write a function called sum_odd_divisors that takes a integer n as input. If n is zero, sum_odd_divisors returns None. Else, sum_odd_divisors returns the sum of all the postive odd divisors of n.
>>> sum_odd_divisors(-9)
13
>>> sum_odd_divisors(1)
1
>>> sum_odd_divisors(2)
1
>>> sum_odd_divisors(3)
4
>>> sum_odd_divisors(7)
8
>>> sum_odd_divisors(-2001)
2880
2.2 series_sum() - 5 points
Write a function called series_sum() that prompts the user for an non-negative integer n. If the user enters a negative integer the function should return None otherwise the function should return the sum of the following series 1000 + 1/12 + 1/22 + 1/32 + 1/42 + ... + 1/n2 for the given integer n. For example, for n = 0, the function should return 1000, for n = 1, the function should return 1001, for n = 2, the function should return 1001.25, for n = 3, the function should return 1001.3611111111111, etc.
>>> series_sum()
Please enter a non-negative integer: -10
>>> series_sum()
Please enter a non-negative integer: 0
1000
>>> series_sum()
Please enter a non-negative integer: 5
1001.463611111111
2.3 pell(n) - 5 points
Pell numbers are a mathematical sequence of numbers that help approximate the value of √2 (check out the Wikipedia page on Pell Numbers:
https://en.wikipedia.org/wiki/Pell__number). The sequence is defined recursively as shown in eq.
(1)
Write a function called pell that takes one integer parameter n, of type int. If n is negative, pell returns None. Else, pell returns the nth Pell number (i.e. Pn ).
>>> pell(0)
0
>>> pell(1)
1
>>> pell(2)
2
>>> pell(3)
5
>>> pell(-45)
>>> pell(6)
70
>>> pell(1743)
532690007842574349527112009340648584206039420554652235605917968301878560204391358977288970768088948931512979242757675577993 290392759079609635614256071618406298994532643385226638853102443318071341757012888307627361739142916789755393486051687770688 826270892256294622920292002671354119783127549690735964749390914433090966928985482211622995711178437113046183531202406642424 338067044223749234565988260287660191555741946139008489540633861672714251246514572808657156171836659007522426709678946690131 627254854511941937501064402371005017444986539854908064857327597139176688814079527858978412577793058938555244277033473983250 7514635151419472104046900160626484896467099596437905
2.4 countMembers(s) - 5 points
In this question you are not allowed to use any of the Python,s string methods (i.e. those string functions that you call with dot operator).
Say a character is extraordinary if it is one of the following: the lower case letter between e and j (inclusive), the upper case letters between F and X (inclusive), numerals between 2 and 6 (inclusive), and the exclamation point ( !), comma ( ,), and backslash (\)
You are required to count how many times these characters appear in a string.
Write a function called countMembers that takes a single input parameter s, of type str. countMembers then returns the number of characters in s, that are extraordinary. Therefore, if there are two Xs in s, countMembers must count two extraordinary characters (one for each occurrence of X in s).
>>> countMembers("\\")
1
>>> countMembers("2\ ' ")
1
>>> countMembers("1\ ' ")
0
>>> countMembers("2aAb3?eE' _13")
4
>>> countMembers("one, Two") 3
2.5 casual_number(s) - 5 points
Imagine a customer in a bank is asked to enter a whole number representing their approximate monetary worth. In that case, some people use commas to separate thousands, millions etc, and some don,t. In order to perform a some calculations the bank needs that number as na integer. Write a function, called casual_number(s) that has one parameter, s, as input where s is a string. Function casual_number(s) should return an integer representing a number in s. If s does not look like a number the function should return None. Note that if a string in s looks like a number but with commas, you may assume that commas are in meaningful places i.e. you may assume that s will not be a string like ,1; 1; 345,.
>>> casual_number("251")
251
>>> casual_number("1,aba,251")
>>> casual_number("1,251")
1251
>>>casual_number("1251")
1251
>>> casual_number("-97,251")
-97251
>>> casual_number("-97251")
-97251
>>> casual_number("-,,,,")
>>> casual_number("--97,251")
>>> casual_number("-")
>>> casual_number("-1,000,001")
-1000001
>>> casual_number("999,999,999,876") 999999999876
>>> casual_number("-2")
-2
2.6 alienNumbers(s) - 5 points
Strange communication has been intercepted between two alien species at war. NASAs top linguists have determined that these aliens use a weird numbering system. They have symbols for various numeric values, and they just add all the values for the symbols in a given numeral to calculate the number. NASA’s linguists have given you the following table of symbols and their numeric values. Since they have a lot of these numbers to compute, they want you to write a function that they can use to automate this task.
Thus a!ya!U!NaU represents a value of 1095 (see table below for an explanation).
Write a function called alienNumbers that takes one string parameter s, and returns the integer value represented by
s. Since aliens only know these characters you may assume that no character in s outside of this set: {T,y, !,a, N, U}.
Callenge: try to make the whole body of this function only one line long.
>>> alienNumbers("a!ya!U!NaU")
1095
>>> alienNumbers("aaaUUU")
129
>>> alienNumbers("") 0
2.7 alienNumbersAgain(s) - 5 points
NASA is very pleased with your proof-of-concept solution in the previous question. Now, they’ve designed a small chip that runs a very restricted version of python - it doesn’t have any of the string methods that you are used to. They want you to now rewrite your alienNumbers function to run without using any of those string methods you may have otherwise used. Basically, you can use a loop of some sort and any branching you’d like, but none of the string methods. Use accumulator variable as we have seen in class. ’
Write a function called alienNumbersAgain, that takes a single string parameter s, and returns the numeric value of the number that s represents in the alien numbering system.
>>> alienNumbersAgain("a!ya!U!NaU")
1095
>>> alienNumbersAgain("aaaUUU")
129
>>> alienNumbersAgain("")
0
The last three questions may be a more challenging to solve especially the last one.