Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mathematical functions map naturally to program functions and modules often are

ID: 3688596 • Letter: M

Question

Mathematical functions map naturally to program functions and modules often are used to group such functions for reuse.

In the Bukiyip* language, coconuts, days and fish are counted in base 3. Numbers use only the digits 0-2, such that instead of "tens" and "hundreds", the second and third digits represents multiples of 3 and 9 respectively.

(Reference: http://mentalfloss.com/article/31879/12-mind-blowing-number-systems-otherlanguages)

Write a Python module called 'bukiyip.py' with the following functions for simple Bukiyip arithmetic, assuming that all values have at most 3 digits.

a) bukiyip_to_decimal (a)

Convert a Bukiyip number to decimal.

b) decimal_to_bukiyip (a)

Convert a decimal number to Bukiyip.

c) bukiyip_add (a, b)

Add two Bukiyip numbers.

d) bukiyip_multiply(a, b)

Multiply two Bukiyip numbers.

Sample I/O:

**** Bukiyip test program ****

Available commands:

d <number> : convert given decimal number to base-3.

b <number> : convert given base-3 number to decimal.

a <number> <number> : add the given base-3 numbers.

m <number> <number> : multiply the given base-3 numbers.

q : quit

Enter a command:

d 12

110

Enter a command:

b 20

6

Enter a command:

a 12 11

100

Enter a command:

m 12 11

202

Enter a command:

q

Explanation / Answer

import math

def decimal_to_bukiyip(a):
   bstr = ""
   temp = a
   while(temp > 0):
       bstr = str(temp % 3)+bstr;
       temp = int(temp/3);
   return bstr;
  
def bukiyip_to_decimal (a):
   bstr = 0
   temp = a
   i = 0;
   while(temp > 0):
       bstr = bstr + int(math.pow(3,i)) * (temp % 10);
       temp = int(temp/10);
       i = i+1
   return bstr

def bukiyip_add (a, b):
   num1 = bukiyip_to_decimal(a)
   num2 = bukiyip_to_decimal(b)
   return (decimal_to_bukiyip(num1+num2))

def bukiyip_multiply(a, b):
   num1 = bukiyip_to_decimal(a)
   num2 = bukiyip_to_decimal(b)
   return (decimal_to_bukiyip(num1 * num2))

print("Available commands: ");

print("d <number> : convert given decimal number to base-3. ");

print("b <number> : convert given base-3 number to decimal. ");

print("a <number> <number> : add the given base-3 numbers. ");

print("m <number> <number> : multiply the given base-3 numbers. ");

print("q : quit ");

while(1):
  
   option = input("Enter a command:")
   if(option == "q"):
       break;
   #parse other commands
   tokens = option.split(' ')
   if(tokens[0] == 'd'):
       print (decimal_to_bukiyip(int(tokens[1])))
          
   if(tokens[0] == 'b'):
       print (bukiyip_to_decimal(int(tokens[1])))  
  
   if(tokens[0] == 'a'):
       print (bukiyip_add(int(tokens[1]),int(tokens[2])))
      
   if(tokens[0] == 'm'):
       print (bukiyip_multiply(int(tokens[1]),int(tokens[2])))   

====outpu====================

Available commands:

d <number> : convert given decimal number to base-3.

b <number> : convert given base-3 number to decimal.

a <number> <number> : add the given base-3 numbers.

m <number> <number> : multiply the given base-3 numbers.

q : quit

Enter a command:d 12
110
Enter a command:b 20
6
Enter a command:a 12 11
100
Enter a command:m 12 11
202
Enter a command:q