Question: ***** STARTER CODE ***** def changeBase(value, newBase): answer = '' # ADD YOUR CODE HERE return answer if __name__ == __main__: start = int(input(Enter a

***** STARTER CODE *****

def changeBase(value, newBase):

answer = '' # ADD YOUR CODE HERE

return answer

if __name__ == "__main__": start = int(input("Enter a base 10 value to convert: ")) b = int(input("Enter the new base as an integer between 1 and 16: "))

converted = changeBase(start, b) print(start, "(base 10) is", converted, "in base", b) print()

original = int(input("Enter a base 10 integer to convert to two's complement: ")) numBits = int(input("How many bits long should the output be? "))

tc = twosComplement(original, numBits) print(original, " (base 10) is ", tc, " in ", numBits, "-bit two's complement representation.", sep='') # removed inter-part spaces for formatting purposes print()

***** STARTER CODE ***** def changeBase(value, newBase): answer = '' # ADD

Test Cases (the changeBase() function)

28 (base 10) is 1C in base 16

294 (base 10) is 446 in base 8

37 (base 10) is 100101 in base 2

3 (base 10) is 3 in base 4

In class, we discussed binary representation and how to convert a positive integer value from base 10 (decimal) into base 2 (binary) (or any other base) through repeated division. The pseudocode algorithm (for any base up through 16) is as follows Set digits to "0123456789ABCDEF" (extend this with more letters for bigger bases, up to 36) Set value to starting base 10 value Set base to desired base (e.g., 2 for base 2) Set answer to "(the empty string) While value is greater than 0 Set remainder to value % base Set answer to digits[remainder] Set value to value //base answer For example, translating 35 (base 10) into base 5 would work as follows 35 5 = 7, remainder 0, so value becomes 7 and answer becomes "0" 7 5 = 1, remainder 2, so value becomes 1 and answer becomes "20" 1 5 = 0, remainder 1 , so value becomes 0 and answer becomes "120" Upon reaching 0, we stop the division process, so the final answer is 120 (base 5) Follow the steps below to complete a Python program that (a) translates any base 10 number into a user-specified base (up through base 16), and (b) builds on that function to translate any base 10 integer (postive or negative) into a special form of binary representation called two's complement. See the end of the lab write-up for some test cases to verify that your functions work correctly 1. Converting Base 10 Into Any Other Base (4 points) Complete the changeBase() function in the provided starter program (lab10.py") by writing Python code that reproduces the pseudocode algorithm above. This function takes two arguments: a positive base 10 number, and an integer (representing the new base) in the range 1-16 (inclusive); it returns the equivalent value in the new base. Your function should return its final answer (the converted value) as a string, in case the new base is greater than 10. DO NOT simply print out the result! Note: The pseudocode algorithm given above assumes that the initial number is greater than 0. If it is exactly 0, then its value in the new base is 0 as well (the algorithm above doesn't account for this special case, but you should)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!