```python from collections import defaultdict url = "https://www.google.com//query?abc=d" comps = url.split("/") hmap = defaultdict(list) if "?" in comps[-1]: q = comps[-1].split("?") if "&" in q[1]: queries = q[1].split("&") for query in queries: key, value = query.split("=") hmap[key].append(value) else: key, value = q[1].split("=") hmap[key].append(value) print(hmap) --------- def countThings(my_string, shouldCountLetters): """ returns a dictionary with count of characters or letters based on shouldCountLetters's value Parameters --------------- my_str: string shouldCountLetters: boolean indicate if letters should be counted or characters -------------- Returns ------------- Dictionary with character or letter count """ # Raise exception if input is not a string if type(my_str) != str: raise Exception('Not a string') if not shouldCountLetters: lst_strs = my_str.split(" ") # make it a list else: lst_strs = my_str #TC = dict() TC = defaultdict(int) for i in range(len(lst_strs)): singleStr = lst_strs[i] singleStr = singleStr.lower() '''if singleStr in TC: TC[singleStr] += 1 else: TC[singleStr] = 1''' TC[singleStr] += 1 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} # ------------------------------------------------------------------------ # def listOfHighestCounts(dict_data): """ returns a list of tuples with non-increasing count order from a dictionary """ tuplesOfdictionary = dict_data.items() # ['l', 3] listOfKeyPlus_total_occurences = [] return sorted(key = x[1] for x in tuplesOfdictionary, x[0]) '''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]] # ------------------------------------------------------------------------ # 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]]) ```