Find the Size of a Tuple in Python

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

384 Views

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

Sending Personalised Messages to User Using JavaScript

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

276 Views

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

Third Smallest Number in an Array Using JavaScript

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

607 Views

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

Remove Letters to Make Adjacent Pairs Different Using JavaScript

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

190 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

222 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

449 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

742 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

371 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

332 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

Advertisements