46 Python Problems

From http://www.ling.gu.se/~lager/python_exercises.html

Files Code and Result

poblem19.py

problem1.py

problem2.py

problem3.py

problem4.py

problem5.py

problem6.py

problem7.py

problem8.py

problem9.py

problem10.py

problem11.py

problem12.py

problem13.py

problem14.py

problem15.py

problem16.py

problem17.py

problem18.py

problem20.py

problem21.py

problem22.py

problem23.py

problem24.py

problem25.py

problem26.py

problem27.py

problem28.py

problem29.py

problem30.py

problem31.py

problem32.py

problem33.py

problem34.py

problem36.py

problem37.py

problem38.py

problem42.py

problem43.py

problem44.py

problem45.py

problem46.py

problem33.py

# requires semordnilap.txt

def semordnilap():
            f = open('semordnilap.txt','r')
            word_store = []
            sorted_in_pairs = []
            for line in f:
                    line = line.strip().split()
                    for word in line:
                            word_store.append(word)
            for x in range(0, len(word_store)):
                    candidate = word_store[x]
                    target = candidate[::-1]
                    if target in word_store:
                            sorted_in_pairs.append((target,candidate))
            # remove duplicates
            for x in sorted_in_pairs:
                    for y in sorted_in_pairs:
                            if x[0] == y[1]:
                                    sorted_in_pairs.remove(x)
            print(len(sorted_in_pairs))
            return (sorted_in_pairs)



ans = semordnilap()
print(ans)

Result

76
[('ACETONE', 'ENOTECA'), ('DESSERTS', 'STRESSED'), ('DIORAMAS', 'SAMAROID'), ('MARGARET', 'TERAGRAM'), ('REDRAWER', 'REWARDER'), ('REWARDER', 'REDRAWER'), ('DIORAMAS', 'SAMAROID'), ('DESSERTS', 'STRESSED'), ('MARGARET', 'TERAGRAM'), ('ALUCARD', 'DRACULA'), ('ANIMATS', 'STAMINA'), ('DEIFIED', 'DEIFIED'), ('DEIFIER', 'REIFIED'), ('DELEVEL', 'LEVELED'), ('DELIVER', 'REVILED'), ('DEMINED', 'DENIMED'), ('DENIMED', 'DEMINED'), ('DENNETS', 'STENNED'), ('DEPARTS', 'STRAPED'), ('net', 'ten'), ('DESSERT', 'TRESSED'), ('DEVIVED', 'DEVIVED'), ('DILUTOR', 'ROTULID'), ('ALUCARD', 'DRACULA'), ('DRAGGIN', 'NIGGARD'), ('ENIMINE', 'ENIMINE'), ('ACETONE', 'ENOTECA'), ('ten', 'net'), ('ENROBER', 'REBORNE'), ('GATEMAN', 'NAMETAG'), ('GLENELG', 'GLENELG'), ('HADEDAH', 'HADEDAH'), ('HALALAH', 'HALALAH'), ('DELEVEL', 'LEVELED'), ('LEVELER', 'RELEVEL'), ('MURDRUM', 'MURDRUM'), ('GATEMAN', 'NAMETAG'), ('NAURUAN', 'NAURUAN'), ('NIELLOS', 'SOLLEIN'), ('DRAGGIN', 'NIGGARD'), ('OGOPOGO', 'OGOPOGO'), ('QAANAAQ', 'QAANAAQ'), ('RACECAR', 'RACECAR'), ('ENROBER', 'REBORNE'), ('REFERER', 'REREFER'), ('DEIFIER', 'REIFIED'), ('REIFIER', 'REIFIER'), ('REKNITS', 'STINKER'), ('REKNOTS', 'STONKER'), ('LEVELER', 'RELEVEL'), ('RELIVER', 'REVILER'), ('REPAPER', 'REPAPER'), ('REFERER', 'REREFER'), ('DELIVER', 'REVILED'), ('RELIVER', 'REVILER'), ('REVIVER', 'REVIVER'), ('ROTANES', 'SENATOR'), ('ROTATOR', 'ROTATOR'), ('DILUTOR', 'ROTULID'), ('SAGENES', 'SENEGAS'), ('SALLETS', 'STELLAS'), ('SEITIES', 'SEITIES'), ('SEMEMES', 'SEMEMES'), ('ROTANES', 'SENATOR'), ('SAGENES', 'SENEGAS'), ('SENINES', 'SENINES'), ('SENONES', 'SENONES'), ('NIELLOS', 'SOLLEIN'), ('ANIMATS', 'STAMINA'), ('SALLETS', 'STELLAS'), ('DENNETS', 'STENNED'), ('REKNITS', 'STINKER'), ('REKNOTS', 'STONKER'), ('DEPARTS', 'STRAPED'), ('SUMMIST', 'TSIMMUS'), ('DESSERT', 'TRESSED')]