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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education