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

Part III: Arabic Numerals Moronic Numerals Converter (5 points) You’ve heard of

ID: 3804005 • Letter: P

Question

Part III: Arabic Numerals Moronic Numerals Converter (5 points) You’ve heard of Roman numerals, but have you heard of Moronic numerals? Probably not because we’ve invented them for this homework assignment. Moronic numerals are similar to Roman numerals in that numbers are formed by combining symbols and adding the values. In Moronic numerals, each numeral has a fixed value representing a power of 8 (rather than a power of 10 or half of a power of 10, as in Roman numerals):

Symbol: I, E, S, F

Value: 1, 8, 64, 512

Symbols are placed from left to right in order of value, starting with the largest. For example, FFFFSSSEEEEEIIII is 2, 284 : (512 × 4) + (64 × 3) + (8 × 5) + (4 × 1). In a few specific cases, to avoid having six or more characters being repeated in succession (such as IIIIII or EEEEEE), subtractive notation is used, as in this table:

Number: 6,7, 48 (64 - 2×8), 56 (64 - 1×8), 384 (512 - 2×64), 448 (512 - 1×64)

Notation: IIE, IE, EES, ES, SSF, SF

Thus, there must never be more than five copies of a single symbol next to each other in a Moronic numeral. Moreover, it is illegal for a symbol to be used in an additive manner after it has been used in a subtractive manner. To see why this is true, consider Roman numerals for a moment. CXC (190) is valid because C is used first in an additive way and then in a subtractive way. But XCXX is invalid because X is first used in a subtractive way and then in an additive way. In Moronic numerals, examples of analogous invalid cases would be expressions like SFSSS, ESEE and IEIII. Write a function arabic2moronic() that takes a positive integer (not a string) in the range [1, 3071] and returns a string consisting of the Moronic representation of that number. (Note that 3, 071 = (512 × 5) + (512 64) + (64 8) + (8 1) is FFFFFSFESIE.) To get started, consider how you could build up the output string piece-by-piece. Start with an empty string for the output. The basic idea is to take the input number, append one or more numerals to the output, and subtract out the value of those numerals from the input. As an example, suppose you start with 2000 as the input value. You subtract out 3 F’s (3 × 512 = 1536), leaving you with 464. Since 448 464 < 512, you then append “SF” to the output and subtract out 448 from the 464, leaving 16. 16 is expressed as EE, so we append “EE” to the result. The final answer is, therefore, FFFSFEE. So in general you proceed in this manner, starting with the input value, appending numerals to the output and correspondingly reducing the value of the input number until it becomes 0. First check if the value is 512 and if so, repeatedly subtract 512 from the input value until it becomes < 512. Each time you subtract 512, append one F to the output. Then look for the special subtractive combinations SF and SSF, appending numerals as needed and decreasing the input value. Next subtract out multiples of 64, appending the letter S to the output. Continue in this manner with smaller numerals and subtractive combinations of numerals until the input value is reduced to zero. Finally, return the representation of the original number as Moronic numerals.

Examples:

Function Call: arabic2moronic(6), arabic2moronic(581), arabic2moronic(935), arabic2moronic(1776), arabic2moronic(2017)

Return Value: ’IIE', ’FSIIIII’, ’FSSFEEEEIE’, ’FFFSSSEES’, ’FFFSFEEEEI’

Part IV: Moronic Numerals Arabic Numerals Converter (5 points)

Write a function moronic2arabic() that takes a string argument consisting of properly-formatted Moronic numerals and returns the integer represented by the Moronic numerals. The converted integer is guaranteed to be in the range [1, 3071]. Process the input string from left-to-right, looking for combinations of numerals (doubles and triples that indicate subtraction) or single numerals that are not involved in subtraction. Add the value of the numeral combination or singleton to a running total and move on to the remainder of the input string. To find combinations of numerals, examine two or three characters simultaneously (e.g., SSF or IE), taking care not to overrun the end of the string while doing this. Proceed in this manner until the rightmost numeral has been processed.

Examples:

Function Call: moronic2arabic(’SIE’), moronic2arabic(’SSSSSEESIIII’), moronic2arabic(’FSSFIIII’), moronic2arabic(’FFFIIE’), moronic2arabic(’FFFSFEIE’)

Return Value: 71, 372, 900, 1542, 1999

Part V: Moronic Numerals Calculator (5 points) Write a function moronic calc() that takes a string consisting of a mathematical expression written in Moronic numerals, such as ’FFSSEI - FIIE’, and returns the value of the expression in Moronic numerals. In the argument you may assume that there is always exactly one space after the first operand, followed by an operator, followed by a space, followed by the second operand. Negation of numbers is also allowed, but with some limitations mentioned below. If a number is negated, then the negation symbol appears immediately to the left of the leftmost numeral. Your calculator must support the following mathematical operators:

Operation Symbol

Example

Addition + FIE + –SSSEIII

Subtraction – – SIE – –FSSSEIII

Multiplication * –FSFEIII / EIII

Integer (Floor) Division / IIIII * EEEEEIII

Exponentiation ˆ –IIIII ˆ III

Important notes: • Either or both operands could be negative, except for the exponent in an exponentiation operation, which you may assume is positive. • You may assume that the result of an operation is in the range [3071, 3071], with the exception of zero. • If the result is positive, do NOT include a + sign in front of it. • Finally, you will want to have the moronic calc() function call your arabic2moronic() and moronic2arabic() functions as needed to help solve this problem.

Examples:

Function Call:

moronic calc(’FIE + SSSEIII’), moronic calc(’-SIE - -FSSSEIII’), moronic calc(’-FSFEIII / EIII’), moronic calc(’IIIII * EEEEEIII’), moronic calc(’-IIIII ˆ III’)

Return value:

’FSSSEEII’, ’FSSIIII’, ’-SEEEI’, ’SSSEEIE’, ’-SESIIIII’

Explanation / Answer

def moronic_calc(expression):
operand1, operator, operand2 = expression.strip(' ').split(' ')
if operand1[0] == '-':
op1_arabic = -1 * moronic2arabic(operand1[1:len(operand1)])
else:
op1_arabic = moronic2arabic(operand1[1:len(operand1)])

if operand2[0] == '-':
op2_arabic = -1 * moronic2arabic(operand2[1:len(operand2)])
else:
op2_arabic = moronic2arabic(operand2[1:len(operand2)])

if operator == '+':
return op1_arabic + op2_arabic
elif operator == '-':
return op1_arabic - op2_arabic
elif operator == '*':
return op1_arabic * op2_arabic
elif operator == '/':
return op1_arabic / float(op2_arabic)
elif operator == '^':
return op1_arabic ^ op2_arabic


def moronic2arabic(moronic_number):
value = {
"I": 1,
"E": 8,
"S": 64,
"F": 512
}
valueOfNumber = 0
currentCharCount = 1
prevChar = ""
for i in xrange(len(moronic_number)):
currentChar = moronic_number[i]
if currentChar != prevChar:
currentCharCount = 1
if currentCharCount <= 5:
valueOfNumber += value[currentChar]
else:
valueOfNumber -= value[currentChar]
currentCharCount += 1
prevChar = currentChar

return valueOfNumber

if __name__ == "__main__":
print('Testing moronic2arabic() for "FIE + SSSEIII": ' + str(moronic_calc('FIE + SSSEIII')))
print('Testing moronic2arabic() for "-SIE - -FSSSEIII": ' + str(moronic_calc('-SIE - -FSSSEIII')))
print('Testing moronic2arabic() for "-FSFEIII / EIII": ' + str(moronic_calc('-FSFEIII / EIII')))
print('Testing moronic2arabic() for "IIIII * EEEEEIII": ' + str(moronic_calc('IIIII * EEEEEIII')))
print('Testing moronic2arabic() for "-IIIII ^ III": ' + str(moronic_calc('-IIIII ^ III')))