When it is required to find the closest pair to the Kth index element in a tuple, the ‘enumerate’ method can be used along with ‘abs’ method.Below is the demonstration of the same −Example Live Demomy_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print("The list is : ") print(my_list) my_tuple = (17, 23) print("The tuple is ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) min_diff, my_result = 999999999, None for idx, val in enumerate(my_list): diff = abs(my_tuple[K - 1] - val[K - 1]) if diff ... Read More
When it is required to create a list from a given list that have a number and its cube, the list comprehension can be used.Below is the demonstration of the same −Example Live Demomy_list = [32, 54, 47, 89] print("The list is ") print(my_list) my_result = [(val, pow(val, 3)) for val in my_list] print("The result is ") print(my_result)OutputThe list is [32, 54, 47, 89] The result is [(32, 32768), (54, 157464), (47, 103823), (89, 704969)]ExplanationA list is defined, and is displayed on the console.The list comprehension is used to iterate through the list, and use the ‘pow’ method to get ... Read More
When it is required to find the maximum and minimum K elements in a tuple, the ‘sorted’ method is used to sort the elements, and enumerate over them, and get the first and last elements.Below is the demonstration of the same −Example Live Demomy_tuple = (7, 25, 36, 9, 6, 8) print("The tuple is : ") print(my_tuple) K = 2 print("The value of K has been initialized to ") print(K) my_result = [] my_tuple = list(my_tuple) temp = sorted(my_tuple) for idx, val in enumerate(temp): if idx < K or idx >= len(temp) - K: ... Read More
When it is required to find the size of a tuple, the ‘sizeof’ method can be used.Below is the demonstration of the same −Example Live Demoimport sys tuple_1 = ("A", 1, "B", 2, "C", 3) tuple_2 = ("Java", "Lee", "Code", "Mark", "John") tuple_3 = ((1, "Bill"), ( 2, "Ant"), (3, "Fox"), (4, "Cheetah")) print("The first tuple is :") print(tuple_1) print("The second tuple is :") print(tuple_2) print("The third tuple is :") print(tuple_3) print("Size of first tuple is : " + str(sys.getsizeof(tuple_1)) + " bytes") print("Size of second tuple is : " + str(sys.getsizeof(tuple_2)) + " bytes") print("Size of third tuple is: ... Read More
ProblemWe are required to write a JavaScript function that takes in two strings. The first string specifies the user name and second the owner name.If the user and owner are the same our function should return ‘hello master’, otherwise our function should return ‘hello’ appended with the name of that user.ExampleFollowing is the code − Live Democonst name = 'arnav'; const owner = 'vijay'; function greet (name, owner) { if (name === owner){ return 'Hello master'; } return `Hello ${name}`; }; console.log(greet(name, owner));OutputHello arnav
ProblemWe are required to write a JavaScript function that takes in an array of numbers of length at least three.Our function should simply return the third smallest number from the array.ExampleFollowing is the code − Live Democonst arr = [6, 7, 3, 8, 2, 9, 4, 5]; const thirdSmallest = () => { const copy = arr.slice(); for(let i = 0; i < 2; i++){ const minIndex = copy.indexOf(Math.min(...copy)); copy.splice(minIndex, 1); }; return Math.min(...copy); }; console.log(thirdSmallest(arr));Output4
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
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
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
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