When it is required to check if the frequency of a dictionary, set and counter are same, the Counter package is imported and the input is converted into a ‘Counter’. The values of a dictionary are converted to a ‘set’ and then to a list. Based on the length of the input, the output is displayed on the console.Below is the demonstration of the same −Example Live Demofrom collections import Counter def check_all_same(my_input): my_dict = Counter(my_input) input_2 = list(set(my_dict.values())) if len(input_2)>2: print('The frequencies are not same') elif len (input_2)==2 and input_2[1]-input_2[0]>1: ... Read More
When it is required to demonstrate a counter and dictionary intersection, the Counter and dictionary can be used.Below is the demonstration of the same −Example Live Demofrom collections import Counter def make_string(str_1, str_2): dict_one = Counter(str_1) dict_two = Counter(str_2) result = dict_one & dict_two return result == dict_one string_1 = 'Hi Mark' string_2 = 'how are yoU' print("The first string is :") print(string_1) print("The second string is :") print(string_2) if (make_string(string_1, string_2)==True): print("It is possible") else: print("It is not possible")OutputThe first string is : Hi Mark The second string is : how ... Read More
When it is required to create a dictionary where keys have multiple inputs, an empty dictionary can be created, and the value for a specific key can be specified.Below is the demonstration of the same −Example Live Demomy_dict = {} a, b, c = 15, 26, 38 my_dict[a, b, c] = a + b - c a, b, c = 5, 4, 11 my_dict[a, b, c] = a + b - c print("The dictionary is :") print(my_dict)OutputThe dictionary is : {(15, 26, 38): 3, (5, 4, 11): -2}ExplanationThe required packages are imported.An empty dictionary is defined.The values for ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of strings. Our function should create combinations by combining all possible n consecutive strings in the array and return the longest such string that comes first.ExampleFollowing is the code − Live Democonst arr = ["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"]; const num = 2; function longestConsec(strarr, k) { if (strarr.length == 0 || k > strarr.length || k longStr.length ){ longStr = newStr.join(''); } } return longStr; } console.log(longestConsec(arr, num));Outputabigailtheta
When it is required to sort the key and values in a dictionary, the ‘sorted’ method can be used.Below is the demonstration of the same −Example Live Demomy_dict = {'Hi': [1, 6, 3], 'there': [2, 9, 6], 'Mark': [16, 7]} print("The dictionary is : ") print(my_dict) my_result = dict() for key in sorted(my_dict): my_result[key] = sorted(my_dict[key]) print("The sorted dictionary is : " ) print(my_result)OutputThe dictionary is : {'Hi': [1, 6, 3], 'there': [2, 9, 6], 'Mark': [16, 7]} The sorted dictionary is : {'Hi': [1, 3, 6], 'Mark': [7, 16], 'there': [2, 6, 9]}ExplanationA dictionary ... Read More
When it is required to sort the dictionaries in Python using a key or a value, a dictionary can be defined, and key value pairs can be inserted into it. A ‘for’ loop can be used to iterate through the key value pair, and sort it using ‘sort’ method. This method can be called.Below is the demonstration of the same −Exampledef my_dict(): my_key_value_pair ={} my_key_value_pair[2] = 56 my_key_value_pair[1] = 2 my_key_value_pair[5] = 12 my_key_value_pair[4] = 24 my_key_value_pair[6] = 18 my_key_value_pair[3] = 323 print ("The keys and values after sorting in alphabetical order based on the key are ... Read More
When it is required to check the order of the character in the string, the ‘OrderedDict’ method can be used.Below is the demonstration of the same −Example Live Demofrom collections import OrderedDict def check_order(my_input, my_pattern): my_dict = OrderedDict.fromkeys(my_input) pattern_length = 0 for key, value in my_dict.items(): if (key == my_pattern[pattern_length]): pattern_length = pattern_length + 1 if (pattern_length == (len(my_pattern))): return 'The order of pattern is correct' return 'The order of pattern is incorrect' my_input = 'Hi Mark' input_pattern = 'Ma' ... Read More
When it is required to insert the elements at the beginning of an ordered dictionary, the ‘update’ method can be used.Below is the demonstration of the same −Example Live Demofrom collections import OrderedDict my_ordered_dict = OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) print("The dictionary is :") print(my_ordered_dict) my_ordered_dict.update({'Mark':'7'}) my_ordered_dict.move_to_end('Mark', last = False) print("The resultant dictionary is : ") print(my_ordered_dict)OutputThe dictionary is : OrderedDict([('Will', '1'), ('James', '2'), ('Rob', '4')]) The resultant dictionary is : OrderedDict([('Mark', '7'), ('Will', '1'), ('James', '2'), ('Rob', '4')])ExplanationThe required packages are imported.An ordered dictionary is created using OrderedDict’.It is displayed on the console.The ‘update’ method is used ... Read More
When it is required to sort the list of dictionary based on values, the itemgetter attribute can be used.Below is the demonstration of the same −Example Live Demofrom operator import itemgetter my_list = [{ "name" : "Will", "age" : 56}, { "name" : "Rob", "age" : 20 }, { "name" : "Mark" , "age" : 34 }, { "name" : "John" , "age" : 24 }] print("The list sorted ... Read More
When it is required to sort the list of dictionaries based on values, the lambda function can be used.Below is the demonstration of the same −Example Live Demofrom operator import itemgetter my_list = [{ "name" : "Will", "age" : 56}, { "name" : "Rob", "age" : 20 }, { "name" : "Mark" , "age" : 34 }, { "name" : "John" , "age" : 24 }] print("The list sorted ... Read More