Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How would you set up these functions in python? Iumtobase (N,B) str: a. This function accepts as input a non-negative integer N and a base

How would you set up these functions in python?
image text in transcribed
image text in transcribed
image text in transcribed
Iumtobase (N,B) str: a. This function accepts as input a non-negative integer N and a base B (no error checking); it should return a string representing the number N in base B. The string should always be of length multiple of 8 unless the number is 0 . Your function must output the empty string when the input value of N is 0 . (This avoids leading zeros!) a. Parameters: N (int), B (int) b. Returns : str c. The function displays nothing. d. Steps to convert decimal to other base system: Step 1 - Divide the decimal number to be converted by the value of the new base. Step 2 - Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 - Divide the quotient of the previous divide by the new base. Step 4 - Record the remainder from Step 3 as the next digit (to the left) of the new base number. Step 5 - Repeat Steps 3 and 4, getting remainders from right to left, until the quotient becomes zero in Step 3. The last remainder thus obtained will be the Most Significant Bit (MSB) of the new base number. Example on how to convert from decimal to binary base system: Decimal Number: 29 Calculating Rinary Fmisialant - Decima1 Number - 29 = Binary Number - 00011101 As you can see, the remainders have to be arranged in the reverse order so that the first remainder becomes the Least Significant Bit (LSB) and the last remainder becomes the Most Significant Bit (MSB). Now ask yourself... what has to change in order to output the number in base B instead of base 2? In [1]: numtobase (29,2) Out {1]:0011101 asetonum (S,B) int: a. This function accepts as input a string S and a base B where S represents a number in base B where is between 2 and 10 inclusive. It should then return an integer in base 10 representing the same numbe as S. It should output 0 when S is the empty string. b. Parameters: S (string), B (int) c. Returns : integer d. The function displays nothing. e. Steps to convert any base system to decimal: Step 1 - Determine the column (positional) value of each digit (this depends on the position of the digit and the base of the number system). Step 2 - Multiply the obtained column values (in Step 1) by the digits in the corresponding columns. Step 3 - Sum the products calculated in Step 2. The total is the equivalent value in decimal. Example Binary Number - 00011101 Calculating Decimal Equivalent - Binary Number - 00011101 - Decimal Number - 29 Again, the key is to ask yourself... what has to change in order to output base B instead of base 2 ? In [1]: basetonum (+00011101', 2) out [1]=29 In (2): basotonum("', 4) out [2]:0 In [3]= barnetionum (00004321,5) Out (3):586 basetobase (B1,B2, S_B1) str: a. Now, we can assemble what we've written to write a function that takes three inputs: a base B1, a base B2 and s_B1, which is a string representing a number in base B1. Then, your function should return a string representing the same number in base B2. S_B1 is always of length multiple of 8 . b. Parameters: B1 (int), B2 (int), S_B1 (str) c. Returns: string d. The function displays nothing. e. Don't rewrite any conversions at all! Instead, convert to decimal and then back to the desired final base! CSE 231

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

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions