```python=
url = "www.google.com?search=cats&results=fast&search=dogs"
def extract(url):
parameter = url.split("?")
parameter = parameter[-1]
# parmater = "search=cats&results=fast"
terms = parameter.split("&")
# terms = ["search=cats", "results=fast", "search=dogs"]
hashmap = defaultdict(list)
for item in terms:
key,val = item.split("=")
hashmap[key].append(val)
# {"search":["cats","dogs"],
# "results":["fast"]}
return hashmap
d = extract(url)
print(d)
'''
Parameter: my_str: a string - input string
shouldCountLetters: Boolean (True/False) - True - Count the letters ; False - count the words
Output: Counter dictionary with frequency (number of occurances) of each word/letter.
'''
def countThings(my_str, shouldCountLetters):
# Check if the given input is a string, if not throw Exception
if type(my_str) != str:
raise Exception('Not a string')
# If shouldCountLetters is True - split the string by letters; False - split the string into words.
if not shouldCountLetters:
lst_strs = my_str.split(" ") # make it a list
else:
lst_strs = my_str
TC = dict()
# Loop over the list of words / letters
for i in range(len(lst_strs)):
singleStr = lst_strs[i]
# Convert each word/letter to lower case
singleStr = singleStr.lower()
# If the word/letter is present in the dictionary, then increment the count, else add it to the dictionary
if singleStr in TC:
TC[singleStr] += 1
else:
TC[singleStr] = 1
# Return the counter dictionary
return TC
# Example expected input/output
countThings("sweet caroline Bah bah bah good times never seemed so good", False)
# Output: {'sweet': 1, 'caroline': 1, 'bah': 3, 'good': 2,
# 'times': 1, 'never': 1, 'seemed': 1, 'so': 1}
countThings('Hello World', True)
# Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1}
# ------------------------------------------------------------------------ #
'''
Input is a counter dictionary of letters in a string
Output is a list of lists with each elements having 2 items: letter, frequency
The function will loop over the input dictionary and append the data into a list based on the frequency in decreasing order.
'''
def listOfHighestCounts(dict_data):
listOfKeyPlus_total_occurences = []
for key, value in dict_data.items():
if len(listOfKeyPlus_total_occurences) == 0:
listOfKeyPlus_total_occurences.append([key,value])
else:
len_list = len(listOfKeyPlus_total_occurences)
for i in range(len_list):
existing_key = listOfKeyPlus_total_occurences[i][0]
existingValue = listOfKeyPlus_total_occurences[i][1]
if value > existingValue:
listOfKeyPlus_total_occurences.insert(i, [key, value])
break
elif i + 1 == len_list:
listOfKeyPlus_total_occurences.append([key, value])
return listOfKeyPlus_total_occurences
# Example input/output
listOfHighestCounts(countNumLetters('Hello World'))
# Output: [['l', 3], ['o', 2], ['h', 1], ['e', 1],
# [' ', 1], ['w', 1], ['r', 1], ['d', 1]]
[[],['w','r','d'],['o'],['l']]
# ------------------------------------------------------------------------ #
def get_topFiveMost_used_words_andLetters(strData):
number_toReturn = 5
tot_words = dict()
TOtalLets = dict()
tot_words = countThings(strData, shouldCountLetters=True)
TOtalLets = countThings(strData, shouldCountLetters=False)
maxLetterList = listOfHighestCounts(tot_words)
maxWords = listOfHighestCounts(TOtalLets)
return maxLetterList[:5], maxWords[:5]
# Example input/output
get_topFiveMost_used_words_andLetters('sweet caroline Bah bah bah good times never seemed so good')
# Output: ([[' ', 10], ['e', 9], ['o', 6], ['s', 4], ['a', 4]],
# [['bah', 3], ['good', 2], ['sweet', 1], ['caroline', 1], ['times', 1]])
```