Question: This Code is in Python Moreover, the use of global variables is prohibited. Description: Some positive integers N can be split into smaller pieces, such
This Code is in Python
Moreover, the use of global variables is prohibited.
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.
Task:
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.
***********************************************************************************************************************************************************
def split_tester(N, d): # Your code for split_tester function goes here (instead of keyword pass) # Your code should include dosctrings and the body of the function pass
# you can add more function definitions here if you like
# main # Your code for the welcome message goes here, instead of name="xxx" name="xxx"
flag=True while flag: question=input(name+", would you like to test if a number admits an increasing-split of give size? ") question=(question.strip()).lower() if question=='no': flag=False #YOUR CODE GOES HERE. The next line should be elif or else.
#finally your code goes here too.
***********************************************************************************************************************************************************
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).
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 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. You can copy paste that function from your Assignment 1 solution (or mine) to your solution in a2_part1_xxxxxx.py.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
