Remove Letters to Make Adjacent Pairs Different Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:49:04

158 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains only ‘A’, ‘B’ and ‘C’. Our function should find the minimum number of characters needed to be removed from the string so that the characters in each pair of adjacent characters are different.ExampleFollowing is the code − Live Democonst str = "ABBABCCABAA"; const removeLetters = (str = '') => {    const arr = str.split('')    let count = 0    for (let i = 0; i < arr.length; i++) {       if (arr[i] === arr[i + 1]) {          count += 1          arr.splice(i, 1)          i -= 1       }    }    return count } console.log(removeLetters(str));Output3

Counting Rings in Letters Using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:48:45

205 Views

ProblemWe are required to write a JavaScript function that takes in a string of English alphabets.Our function should count the number of rings present in the string.O', 'b', 'p', 'e', 'A', etc. all have one rings whereas 'B' has 2ExampleFollowing is the code − Live Democonst str = 'some random text string'; function countRings(str){    const rings = ['A', 'D', 'O', 'P', 'Q', 'R', 'a', 'b', 'd', 'e', 'g', 'o', 'p', 'q'];    const twoRings = ['B'];    let score = 0;    str.split('').map(x => rings.includes(x)    ? score++    : twoRings.includes(x)    ? score = score + 2    : x    );    return score; } console.log(countRings(str));Output7

Keys Associated with Values in Dictionary in Python

AmitDiwan
Updated on 17-Apr-2021 12:48:00

433 Views

When it is required to find the keys associated with specific values in a dictionary, the ‘index’ method can be used.Below is the demonstration of the same −Example Live Demomy_dict ={"Hi":100, "there":121, "Mark":189} print("The dictionary is :") print(my_dict) dict_key = list(my_dict.keys()) print("The keys in the dictionary are :") print(dict_key) dict_val = list(my_dict.values()) print("The values in the dictionary are :") print(dict_val) my_position = dict_val.index(100) print("The value at position 100 is : ") print(dict_key[my_position]) my_position = dict_val.index(189) print("The value at position 189 is") print(dict_key[my_position])OutputThe dictionary is : {'Hi': 100, 'there': 121, 'Mark': 189} The keys in the dictionary are : ... Read More

Rearranging Digits to Form the Greatest Number Using JavaScript

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

727 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 null    return +num    .toString()    .split('')    .sort((a, b) => b - a)    .join('') }; console.log(maxRedigit(num));Output941

Check if Frequencies Can Become Same using Dictionary, Set, and Counter in Python

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

356 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 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

Python Counter and Dictionary Intersection Example

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

321 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 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

Python Dictionary with Keys Having Multiple Inputs

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] = 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

Longest String Consisting of N Consecutive Strings in JavaScript

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

402 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", "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

Sort Dictionary Keys and Values List in Python

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

481 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 = 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

Sort Python Dictionaries by Key or Value

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

229 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 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

Advertisements