Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import string d = string.digits + string.ascii _ letters import math def base _ q _ to _ p ( s , q , p

import string
d = string.digits + string.ascii_letters
import math
def base_q_to_p(s,q,p): # base_q_to_p method which takes a string, two bases p and q where number stored in string is in base q and we need to convert it to base p
no = int(s) # Converting string to integer
po=0; # Setting p and decimal to 0
decimal=0
while(no!=0): # Running a while loop to convert number from base q to decimal
decimal+=(no%10)*int(pow(q,po))
po+=1
no = int(math.floor(no/10));
res =[] # Creating a list to store the remainders
while decimal: # Running a while loop to convert number from decimal to base p
res.append(d[decimal % p]) # Appending every remainder to list res
decimal = decimal // p
res.reverse() # Reversing the list res
x = int(''.join(res)) # Converting list to integer
return x # Returning the required base q to base p number
print(base_q_to_p(10001,2,8)) # Calling base_q_to_p method here

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

Question

gpt 6 9 9 .

Answered: 1 week ago