Home‎ > ‎ProgComp‎ > ‎ProgComp 2012 Sample Tasks‎ > ‎

1 Junior Task





# Junior Task: Plurals

# Open a file
fo = open("plurals_input.txt", "r")
str=fo.read();
fo.close();
inp = str.splitlines(); # creates a list from the file input separated by \n (default)

def doPlural(Q, Word):
    if Q==0: Q="no";

    if Word.endswith(("s","x","z","ch","sh")):
        Word=Word+"es";
    elif(Word.endswith("o") and Word[len(Word)-2] not in ["a","e","i","o","u","y"]):
        Word=Word+"es";
    elif(Word.endswith("y") and Word[len(Word)-2] not in ["a","e","i","o","u","y"]):
        Word=Word[0:len(Word)-1]+"ies";
    elif(Word.endswith("fe") and Word[len(Word)-3] not in ["f"]):
        Word=Word[0:len(Word)-2]+"ves";
    elif(Word.endswith("f") and Word[len(Word)-2] not in ["f"]):
        Word=Word[0:len(Word)-1]+"ves";
    else:
        Word=Word+"s";

    print(Q, Word);

def doSingular(Q, Word):
    print ("one {}".format(Word));
    
for sentence in inp:
    #print (sentence);
    lines = sentence.split(" ");
    Q = int(lines[0]); #get Quantity
    
    if (len(lines)>1):  # must contain an Q and Word in sentence
        Word = lines.pop().lower(); #get the last Word in the sentence and convert to lowercase
        Plural=False;

        if (Q==0 or Q>1):
            Plural = True;
        elif (Q==1):
            Plural=False;

        if Plural:
            doPlural(Q, Word);
        else:
            doSingular(Q, Word);
        count+=1;
    else :
        print (Q);
        maxcount=Q;
        count=0;

if maxcount==count:
    print("Correct number of records processed: ",maxcount);
else:
    print("Incorrect number of records processed: expected {}, got {}".format(maxcount,count));




Comments