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
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
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ffc57c835-1d53-4996-bad6-d745c4d23541%2F29a10291-61fe-4fbf-9829-e56571f2084c%2Fp44rtp_processed.png&w=3840&q=75)
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
![](/static/compass_v2/shared-icons/check-mark.png)
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Recommended textbooks for you
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
![C++ Programming: From Problem Analysis to Program…](https://www.bartleby.com/isbn_cover_images/9781337102087/9781337102087_smallCoverImage.gif)
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
![C++ for Engineers and Scientists](https://www.bartleby.com/isbn_cover_images/9781133187844/9781133187844_smallCoverImage.gif)
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr