Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is for python 3 Problem B: Base Conversion (10 points) You are used to seeing numbers represented in base-ten, or decimal form, where each

This is for python 3

Problem B: Base Conversion (10 points)

You are used to seeing numbers represented in base-ten, or decimal form, where each digit can represent one of ten numbers (0 9). You may also be familiar with base-two, or binary form, where the only digits are 1 and 0. In fact, there is a very simple algorithm to convert a base-ten number into any other base representation. Suppose you want to convert the decimal number 13 to base-two. Repeatedly integer divide the number 13 by 2, collecting each remainder, until you get a quotient of 0.

13 // 2 = 6, remainder 1

6 // 2 = 3, remainder 0

3 // 2 = 1, remainder 1

1 // 2 = 0, remainder 1

Now put the remainders together in reverse order and you get 1101, the base-two representation of 13.

Your assignment is to write a recursive function that accepts two int parameters, a decimal number and a new base, and returns a string representing that number in the given base form. Your program must have a main function, so that you can prompt for input.

An example run of the program will look like:

Enter your decimal number: 79

Enter the base you want to convert to: 16

79 in base 16 is 4F >>> Notice how the result has the letter F in it. For bases larger than 10, we need to use a single character to represent numbers greater than 9, so we use the letters A for 10, B for 11, and so on. To make this conversion, you may find the functions ord() and chr() useful. Hint: the modulo operator (%) gives you the remainder of a division.

Constraints:

You may assume that the given base is an int larger than 1 and not larger than 36

You may assume that the given decimal number is a positive int

Your conversion function must be pure; i.e., it may not use print statements or get input from the user

The function must be recursive (loops are not allowed)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions