Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string
- # takes a non-empty string and returns a lowercase string that contains the most frequently
- # occurring letter(s) in s in alphabetical order (ignoring case in the original string)
- # you will likely need a nested loop for this...
- def mostFrequentLetter(s):
- d = {}
- for c in s.lower():
- #Ignore other non-alphabetic characters.
- if c in string.ascii_lowercase:
- d[c] = d.get(c, 0) + 1
- #Make a list of tuples.
- t = [(v, k) for k, v in d.items()]
- #Sort the list by the elements in tuples.
- t.sort(key = lambda x: (-x[0], x[1]))
- if not t:
- return ''
- max_count = t[0][0]
- return ''.join(c[1] for c in t if c[0] == max_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement