apter 4. we developed álgorithm čonverting fröm Binary to děčimal. You čan generalize this algorithm to work for a representation in any base. Instead of using a power of 2. you will write a program that converts any base between 2 and 16. Recall that for numbers 1o - 15 we use letters A - F. In convert.py, define a function named repToDecimal that expects two arguments, a string, and an integer. The second argument should be the base. For example. repToDecimal("10", 8) returns 8, whereas repToDecimal("10", 16) returns 16. • The function should use a lookup table to find the value of any digit. Make sure that this table (it is actually a dictionary) is initialized before the function is defined. • For its keys, use the 10 decimal digits (all strings) and the letters A...F (all uppercase). The value stored with each key should be the integer that the digit represents. (The letter A associates with the integer value 10, and so on.) The main loop of the function should convert each digit to uppercase, look up its value in the table, and use this value in the computation. A main function that tests the conversion function with numbers in several bases has been provided. An example of main and correct output is shown below: def main(): print(repToDecimal('10', 10)) print(repToDecimal('10', 8)) print(repToDecimal('10', 2)) print(repToDecimal('10', 16)) 10 8 16

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 22PE
icon
Related questions
Question
In Chapter 4. we developed an algorithm for converting from binary to decimal. You can
generalize this algorithm to work for a representation in any base. Instead of using a
power of 2. you will write a program that converts any base between 2 and 16. Recall
that for numbers 10 - 15 we use letters A - F.
In convert.py. define a function named repToDecimal that expects two arguments, a
string. and an integer. The second argument should be the base. For example.
repToDecimal ("10", 8) returns 8, whereas repToDecimal("10", 16) returns
16.
• The function should use a lookup table to find the value of any digit. Make sure that
this table (it is actually a dictionary) is initialized before the function is defined.
• For its keys, use the 10 decimal digits (all strings) and the letters A...F (all
uppercase). The value stored with each key should be the integer that the digit
represents. (The letter A associates with the integer value 10, and so on.)
The main loop of the function should convert each digit to uppercase. look up its
value in the table. and use this value in the computation.
A main function that tests the conversion function with numbers in several bases has
been provided.
An example of main and correct output is shown below:
def main():
print(repToDecimal('10', 10))
print(repToDecimal ('10', 8))
print(repToDecimal('10', 2))
print(repToDecimal('10', 16))
10
16
Transcribed Image Text:In Chapter 4. we developed an algorithm for converting from binary to decimal. You can generalize this algorithm to work for a representation in any base. Instead of using a power of 2. you will write a program that converts any base between 2 and 16. Recall that for numbers 10 - 15 we use letters A - F. In convert.py. define a function named repToDecimal that expects two arguments, a string. and an integer. The second argument should be the base. For example. repToDecimal ("10", 8) returns 8, whereas repToDecimal("10", 16) returns 16. • The function should use a lookup table to find the value of any digit. Make sure that this table (it is actually a dictionary) is initialized before the function is defined. • For its keys, use the 10 decimal digits (all strings) and the letters A...F (all uppercase). The value stored with each key should be the integer that the digit represents. (The letter A associates with the integer value 10, and so on.) The main loop of the function should convert each digit to uppercase. look up its value in the table. and use this value in the computation. A main function that tests the conversion function with numbers in several bases has been provided. An example of main and correct output is shown below: def main(): print(repToDecimal('10', 10)) print(repToDecimal ('10', 8)) print(repToDecimal('10', 2)) print(repToDecimal('10', 16)) 10 16
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr