Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!