For this assignment, make a folder named pa7 and save your answers in it.
When you are finished, zip up and submit.
As always, you are responsible for protecting your solutions to these problems from being seen by other students, both physically (e.g., by looking over your shoulder) and electronically.
You will create a Python source file containing a Python function(s) implementing each of the problems described below. In implementing these functions, you may not use and built-in data conversion functions that convert between numbers and strings directly. You may, however, use subscripting with [ ] to access a character of a string, and the functions ord (to convert from a character to its ASCII value) and chr (to convert from an ASCII value to the corresponding character).
[3 points] A positive integer \(n\) can be converted to a string of 1's and 0's holding that numbers binary represntation using the following algorithm:
Write a function convert_to_binary(n) (in convert_to_binary.py) which takes a non-negative integer n and returns the a string of 1's and 0's
NOTE THAT: You will have to figure out how to adjust the algorithm to deal with the case where parameter n = 0.
Example:
>>> convert_to_binary(127) '1111111' >>> convert_to_binary(4) '100' >>> convert_to_binary(3) '11' >>> convert_to_binary(0) '0'
[3 points] The value of a number represented with a binary string s can be found by the formula below:
Another way is iterating over the indices of the string and, for each index i, updating value to be the value of the binary number represented by s[0:i+1]. An example is shown in the table below:
s | i | s[i] | value |
---|---|---|---|
"11001" | 0 | "1" | 1 |
"11001" | 1 | "1" | 3 |
"11001" | 2 | "0" | 6 |
"11001" | 3 | "0" | 12 |
"11001" | 4 | "1" | 25 |
Write a function valueOfBinary(s)
(in valueOfBinary.py) that
takes a non-empty string of 1's and 0's and converts it into the
corresponding integer value.
HINT: For each character in the string (from left to right),
if it is a "1", double the current value and add 1. If it is a "0", just
double the current value.
Example:
>>> valueOfBinary("0") 0 >>> valueOfBinary("1") 1 >>> valueOfBinary("101") 5 >>> valueOfBinary("0011101110") 238
For this problem you will convert letters between upper and lower case. This is easy to do, because in the ASCII encoding, the code for each lower-case letter is obtained by adding 32 to the code for the corresponding upper-case letter. But you can't do arithmetic with characters in Python. Instead, you must get the character's code using the ord function, do the arithmetic, and convert the result back to a character using the chr function.
Example:
>>> ord('A') 65 >>> ord('a') 97 >>> ord('Z') 90 >>> ord('z') 122 >>> chr(65) 'A' >>> chr(97) 'a' >>> chr(90) 'Z' >>> chr(122) 'z'
You are not allowed to use any built-in functions that convert between upper and lower case, or any that indicate whether a character is upper or lower case. Instead, use the character codes and numeric operations.
Example:
>>> upperCase('a') 'A' >>> upperCase('m') 'M' >>> upperCase('M') 'M' >>> upperCase('*') '*'
Example:
>>> lowerCase('A') 'a' >>> lowerCase('M') 'm' >>> lowerCase('m') 'm' >>> lowerCase('*') '*'
Example:
>>> capitalize("the quick brown fox") 'The Quick Brown Fox' >>> s = "PYTHON IS A SNAKE" >>> capitalize(s) 'Python Is A Snake' >>> s 'PYTHON IS A SNAKE' >>> capitalize(" abc ") ' Abc ' >>> capitalize("abc") 'Abc'
HINT:
Loop over each character of the string s. If it can be the start of a word, make it upper case. Otherwise, make the character lower case (this will not affect spaces). The big question is: how do you know when you're at the start of a word?
Your pa7.zip should contain
Submit your assignment using Gradescope.