FUNCTIONS:1. Define the SplitHex function
Here we define a function SplitHex which takes as input a single positive integer input n. The function then calculates a positive integer output as follows:
(a)Convert n to hexadecimal form, call it H. You may use the hex( ) function.
- Split H into two “halves�, call them F and S, where if the length of H is odd, then include the extra digit in the first half. For example, if H = 1B5AC9, then the first half is F = 1B5 and the second half is S = AC9. As another example, if B = 6CB, then the first half is F = 6C and the second half is S = B. Note: If H has only one digit, then F will be that single digit and S = 0.
- Convert F and S to decimal form, add 16 to the decimal form of F and 1 to the decimal form of S, and then return the product of these new values.
As a full example, consider n = 62.
(a)Converting n to hexadecimal form, we have H = 3E
(b)Then F = 3 and S = E
- Converting them back to decimal form, we have the decimal values 3 and 14. Finally, we return the product (3+16)*(14+1) = 285.
Therefore SplitHex(62) should return 285. At the bottom, there are further examples of the
SplitHex function.
- Define the SplitHexSequence function
- The final step!
- If the list created by SplitHexSequence has 20 elements in it, then the function will stop adding more elements and return the list with those 20 elements; or
- If the last element added to the list is a duplicate of one of the previous terms in the list, then that duplicated element will be added to the list and the list will be returned in its current form.
- ADD COMMENTS to clarify your choice of variables and to indicate what is happening at different steps of the program. A good rule of thumb is one line of comments for every four to five lines of code. Your comments will be checked to see that you understand how your code is structured and what it is doing in the big picture. Code lacking in comments will be penalized!
We can use SplitHex( ) to turn one integer into another. We can iterate this process over and over, generating a sequence of positive integers. For example, if we start with the positive inte- ger 62, then SplitHex(62) = 285. Then SplitHex(285) = 462, and SplitHex(462) = 660, and so on. This creates the sequence 62, 285, 462, 660,…………………………………….. This sequence we will call
the Split Hex Sequence starting with 62.
In your project1.py script, you will create a function called SplitHexSequence which takes one positive integer input n. The function then returns a list of the elements in the Split Hex Sequence (defined above) with the following conditions:
Using the example above, the Split Hex Sequence for 62 begins as 62, 285, 462, 660, 285 and since the last element is a duplicate of a previous entry, then SplitHexSequence(62) would return the list [62, 285, 462, 660, 285]. Further examples of the SplitHexSequence func- tion are given below.
EXAMPLES To test that your SplitHex function is working properly, here are some examples of the output:
n |
SplitHex(n) |
4 |
20 |
16 |
17 |
22 |
119 |
29 |
238 |
62 |
285 |
67 |
80 |
79 |
320 |
80 |
21 |
100 |
110 |
To test that your SplitHexSequence function is working properly, here are some examples of the output:
n |
SplitHexSequence(n) |
4 |
[4, 20, 85, 126, 345, 370, 117, 138, 264, 288, 34, 54, 133, 144, 25, 170, 286, 495, 736, 62, 285] |
16 |
[16, 17, 34, 54, 133, 144, 25, 170, 286, 495, 736, 62, 285, 462, 660, 285] |
22 |
[22, 119, 184, 243, 124, 299, 408, 369, 78, 300, 442, 473, 450, 132, 120, 207, 448, 44, 234, 330] |
29 |
[29, 238, 450, 132, 120, 207, 448, 44, 234, 330, 396, 520, 432, 43, 216, 261, 192, 28, 221, 406] |
62 |
[62, 285, 462, 660, 285] |
67 |
[67, 80, 21, 102, 154, 275, 132, 120, 207, 448, 44, 234, 330, 396, 520, 432, 43, 216, 261, 192] |
79 |
[79, 320, 36, 90, 231, 240, 31, 272, 33, 36] |
80 |
[80, 21, 102, 154, 275, 132, 120, 207, 448, 44, 234, 330, 396, 520, 432, 43, 216, 261, 192, 28] |
100 |
[100, 110, 330, 396, 520, 432, 43, 216, 261, 192, 28, 221, 406, 287, 528, 49, 38, 126, 345, 370] |
BONUS This part should only be completed after you have completed the project1.py file described above. You are going to alter the code for the SplitHexSequence function so it is advised that you create a separate Python file than project1.py so you don’t mess up your code that you will submit to Gradescope.
In the project1.py file, the SplitHexSequence function halted after 20 elements were added to the list, but the sequence could continue past that until it found a duplicate.
Assume the SplitHexSequence function would keep adding elements to the list until a duplicate was found instead of stopping at 20 elements. If we adjust the SplitHexSequence function to account for this change, then it turns out that the first positive integer whose Split Hex Sequence contains exactly 20 elements is the input 11.
Here are the bonus questions:
- (+2 points) What is the smallest positive integer where the length of the SplitHexSequence
- (+5 points) What is the smallest positive integer where the length of the SplitHexSequence
- (+5 points) If you create a list from the final element of each SplitHexSequence from 1 to 100,000, then you will get a list that contains 21 elements. What are those 21 elements (in increasing order)?
is exactly 30 elements?
is exactly 80 elements?
If you partake in the Bonus questions, you will re-submit to Gradescope your original project1.py file along with one text file called project1-bonus.txt. This new text file document should have an honesty statement at the top:
Then it should have your answers (bogus answers are used below; these are not the correct answers):
Question 1:10
Question 2:60
Question 3:[1,2,3,…,21]
GRADING SCHEME We will use the following grading scheme for project 1:
- project1.py submitted with the functions SplitHex and SplitHexSequences (10%): You will receive 10 points for defining these two functions in the right file.
- SplitHex examples (35%): Your program will be tested with random inputs to the function
- SplitHexSequence examples (45%) Your program will be tested with random inputs to the function
- Manual Grading (10%): The program will be checked manually for docstrings and com- ments. In particular both functions defined must have a docstring describing what the func- tion’s arguments are and what the function returns.