AmitDiwan has Published 10744 Articles

Rearranging digits to form the greatest number using JavaScript

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:47:38

719 Views

ProblemWe are required to write a JavaScript function that takes in one positive three-digit integer and rearranges its digits to get the maximum possible number.ExampleFollowing is the code − Live Democonst num = 149; const maxRedigit = function(num) {    if(num < 100 || num > 999)       return ... Read More

Python dictionary, set and counter to check if frequencies can become same

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:47:20

345 Views

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 ... Read More

Python counter and dictionary intersection example

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:46:54

313 Views

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 ... Read More

Python dictionary with keys having multiple inputs

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:46:29

1K+ Views

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] ... Read More

Longest string consisting of n consecutive strings in JavaScript

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:46:12

395 Views

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", ... Read More

Sort Dictionary key and values List in Python

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:45:51

474 Views

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 ... Read More

Sort Python Dictionaries by Key or Value

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:40:48

220 Views

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 ... Read More

Check order of character in string using OrderedDict( ) in Python

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:40:18

703 Views

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():     ... Read More

Insertion at the beginning in OrderedDict using Python

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:39:17

529 Views

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 = ... Read More

Ways to sort list of dictionaries by values in Python Using itemgetter

AmitDiwan

AmitDiwan

Updated on 17-Apr-2021 12:38:42

697 Views

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}, ... Read More

Advertisements