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

PLEASE CODE IN PYTHON You’ve heard of Roman numerals, but have you heard of Moro

ID: 3864966 • Letter: P

Question

PLEASE CODE IN PYTHON

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 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, Return Value

moronic2arabic(’SIE’), 71

moronic2arabic(’SSSSSEESIIII’), 372

moronic2arabic(’FSSFIIII’), 900

moronic2arabic(’FFFIIE’), 1542

moronic2arabic(’FFFSFEIE’), 1999

Explanation / Answer

dictionary = {'F': 512, 'S': 64, 'E': 8, 'I':1};


def moronicToDecimal(moronicValue) :
    decimalValue = 0.0;
    length = len(moronicValue);

    i=0;
    while (i<length) :
        j = i + 1;
        isAdditive = True;

        # Check whether it is additive or substractive digit
        while(j<length) :
            if (dictionary[moronicValue[j]] < dictionary[moronicValue[i]]) :
                break;
            elif (dictionary[moronicValue[j]] > dictionary[moronicValue[i]]) :
                isAdditive = False;
                break;
            j = j + 1;

        # Check the flag and decide whether to substract or add  
        if (isAdditive) :
            decimalValue += dictionary[moronicValue[i]];
        else :
            decimalValue -= dictionary[moronicValue[i]];

        i = i+1;

    return decimalValue;

value = moronicToDecimal("FFFSFEIE");
print value;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote