Reference no: EM132395778
Assignment
1 Part 1: increasing-splits tester -
To clarify Part 1 specifications, You have provided sample tests in Section 3 and a video. The behaviour implied by the sample tests and the video 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
Design, implement and test a Python program which checks to see if a user-supplied positive integer N and a userspecified 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.
1.1 The Core Function
Your solution in a2_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).
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 and the video. 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.
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 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 part2_xxxxxx.txt. Thus, for this part you need to submit two files: a2_part2_xxxxxx.py and a2_part2_xxxxxx.txt
2.1 min_enclosing_rectangle(radius, x, y)
Computing a smallest (axis-aligned) rectangle that encloses a set of objects in the plane is a very common computational problem arising in graphics and thus game development too. Write a function, called min_enclosing_rectangle, that has 3 input parameters. The first is a number representing a radius of a circle and the next two are two numbers representing the x- and y-coordinates of its center. Consider the smallest axis-aligned rectangle that contains that circle. The function should return the x- and y-coordinates of the bottom-left corner of that rectangle. If radius is a negative number, min_enclosing_rectangle should return None.
2.2 series_sum()
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 sumer of the following series 1000 +
1/1
2 + 1/2
2 + 1/3
2 + 1/4
2 + ... + 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.
2.3 pell(n)
Pell numbers are a mathematical sequence of numbers that help approximate the value of √ 2 (check out the Wikipedia page on Pell Numbers.
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).
2.4 countMembers(s)
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 returnsthe 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).
2.5 casual_number(s)
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’.
2.6 alienNumbers(s)
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.
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.
2.7 alienNumbersAgain(s)
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.
2.8 encrypt(s)
You want to send a note to your friend in class, and because you want to be respectful in class, you don’t want to whip out your phone and send a text or an email (or any other digital communication). Instead, you choose to go old school and write it out on a piece of paper and pass it along to your friend. The problem is, you don’t want anyone else to read the note as they pass it along. Luckily, your friend and you have come up with an encryption system so that nobody else can understand your message. Here’s how it works: you write out your message backwards (so, Hello, world becomes dlrow ,olleH). But you don’t stop there, because that’s too easy to crack - anyone can figure that out!
Now that you’ve written in backwards, Then you start on either side of the string and bring the characters together. So the first and the last characters become the first and the second character in the encrypted string, and the second and the second last characters become the third and the fourth characters in the string, and so on. Thus, Hello, world ultimately becomes dHlerlolwo , (notice how all punctuation, special characters, spaces, etc are all treated the same) and 0123456789 becomes 9081726354.
Write a function called encrypt, that has one parameter s where s is a string and encrypt returns a string which is the encrypted version of s.
2.9 oPify(s)
Write a function called oPify, that takes a single string parameter (s) and returns a string. This function considers every pair of consecutive characters in s. It returns a string with the letters o and p inserted between every pair of consecutive characters of s, as follows. If the first character in the pair is uppercase, it inserts an uppercase O. If however, it is lowercase, it inserts the lowercase o. If the second character is uppercase, it inserts an uppercase P. If however, it is lowercase, it inserts the lowercase p. If at least one of the character is not a letter in the alphabet, it does not insert anything between that pair. Finally, if s has one or less characters, the function returns the same string as s.
2.10 nonrepetitive(s)
This may be quite a challenging question to solve. Slicing and remembering that you can ask if two strings (s1==s2) are the same are key to a short solution to this problem.
A nonrepetitive word is a word that does not contain any subword twice in a row.
Examples: ana is nonrepetitive.
borborygmus is not nonrepetitive, since it has subword, bor twice in a row.
abracadabra is nonrepetitive.
repetitive is not nonrepetitive since subword ti is repeated twice in a row.
grammar is not nonrepetitive since subword m is repeated twice in a row.
gaga is not s nonrepetitive since subword ga is repeated twice in a row.
rambunctious is nonrepetitive.
abcab is nonrepetitive.
abacaba is nonrepetitive.
zrtzghtghtghtq is not nonrepetitive since subword ght is repeated twice (in fact three times, but it is enough to find two repetitions to conclude that the word is not nonrepetitive).
aa is not nonrepetitive since subword a is repeated twice.
zatabracabrac is not nonrepetitive since subword abrac is repeated twice in a row.
Write a function, called nonrepetitive, that has one parameter, s, where s is a string. The function returns True if s is nonrepetitive and False otherwise.
Attachment:- Assignment.rar