Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, string
- d = {}
- def isletterAthroughZ(letter):
- if 65 <= ord(letter) <= 90 or 97 <= ord(letter) <= 122:
- return True
- else:
- return False
- ## Read lines, split words and for every word add one to count
- ## negative definitino didnt work with 6th test so I will try positive definition
- ## go through all characters in line and is if they are letter build newword untill you hit first nonletter
- newword = ''
- for line in sys.stdin:
- #print(line)
- #clearline = ''
- for i in line:
- if isletterAthroughZ(i):
- newword = newword + f'{i}'
- elif newword != '':
- d[newword] = d.get(newword, 0) +1
- newword = ''
- ## create a list with nested lists each containing word at [0] and word count at [1]
- ## save the biggest count
- ArciList = []
- biggest = 0
- for key,val in d.items(): ## loop over simoltaneously over keys and items
- if val > biggest:
- biggest = val
- a = []
- a.append(key)
- a.append(val)
- ArciList.append(a)
- ## Go through all Arcilist[x][1] and for each count biggest:
- ## 1) print(f'{Arcilist[x][0]} {Arcilist[x][1]}')
- ## 2) add one to printcount
- ## 3) after every print check if printcount == 20, if so exit()
- ## 4) after the loop for count biggest ends, biggest -= 1 and repeat
- printcount = 0
- for i in range(biggest,0,-1):
- for j in range(len(ArciList)):
- if int(ArciList[j][1]) == i:
- print(f'{ArciList[j][0]} {ArciList[j][1]}')
- printcount += 1
- if printcount == 20:
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement