Find Greatest and Smallest Number in a Space-Separated String using JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:25:08

323 Views

ProblemWe are required to write a JavaScript function that takes in a string that contains numbers separated by spaces.Our function should return a string that contains only the greatest and the smallest number separated by space.Inputconst str = '5 57 23 23 7 2 78 6';Outputconst output = '78 2';Because 78 is the greatest and 2 is the smallest.ExampleFollowing is the code − Live Democonst str = '5 57 23 23 7 2 78 6'; const pickGreatestAndSmallest = (str = '') => {    const strArr = str.split(' ');    let creds = strArr.reduce((acc, val) => {    let { greatest, ... Read More

Returning Lengthy Words from a String Using JavaScript

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

131 Views

ProblemWe are required to write a JavaScript function that takes in a sentence of words and a number. The function should return an array of all words greater than the length specified by the number.Inputconst str = 'this is an example of a basic sentence'; const num = 4;Outputconst output = [ 'example', 'basic', 'sentence' ];Because these are the only three words with length greater than 4.ExampleFollowing is the code − Live Democonst str = 'this is an example of a basic sentence'; const num = 4; const findLengthy = (str = '', num = 1) => {    const strArr ... Read More

Numbers Obtained During Checking Divisibility by 7 Using JavaScript

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

669 Views

ProblemWe can check for a number to be divisible by 7 if it is of the form 10a + b and a - 2b is divisible by 7.We continue to do this until a number known to be divisible by 7 is obtained; we can stop when this number has at most 2 digits because we are supposed to know if a number of at most 2 digits is divisible by 7 or not.We are required to write a JavaScript function that takes in a number and return the number of such steps required to reduce the number to a ... Read More

Can All Array Elements Mesh Together in JavaScript

AmitDiwan
Updated on 17-Apr-2021 12:16:09

156 Views

ProblemTwo words can mesh together if the ending substring of the first is the starting substring of the second. For instance, robinhood and hoodie can mesh together.We are required to write a JavaScript function that takes in an array of strings. If all the words in the given array mesh together, then our function should return the meshed letters in a string, otherwise we should return an empty string.ExampleFollowing is the code − Live Democonst arr = ["allow", "lowering", "ringmaster", "terror"]; const meshArray = (arr = []) => {    let res = "";    for(let i = 0; i < ... Read More

Largest Index Difference with Increasing Value in JavaScript

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

131 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr. Our function should return the largest difference in indexes j - i such that arr[i] {    const { length: len } = arr;    let res = 0;    for(let i = 0; i < len; i++){       for(let j = i + 1; j < len; j++){          if(arr[i] res){             res = j - i;          };       };    };    return res; }; console.log(findLargestDifference(arr));OutputAnd the output in the console will be −3

Find Missing Number Between Two Arrays in JavaScript

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

628 Views

ProblemWe are required to write a JavaScript function that takes in two arrays arr1 and arr2.arr2 is a shuffled duplicate of arr1 with just one element missing.Our function should find and return that one element.ExampleFollowing is the code − Live Democonst arr1 = [6, 1, 3, 6, 8, 2]; const arr2 = [3, 6, 6, 1, 2]; const findMissing = (arr1 = [], arr2 = []) => {    const obj = {};    for (let i = 0; i < arr1.length; i++) {       if (obj[arr1[i]] === undefined) {          obj[arr1[i]] = 1;     ... Read More

Extract Unique Dictionary Values in Python

AmitDiwan
Updated on 17-Apr-2021 12:09:31

1K+ Views

When it is required to extract unique values from a dictionary, a dictionary is created, and the ‘sorted’ method and dictionary comprehension is used.Below is a demonstration for the same −Example Live Demomy_dict = {'hi' : [5, 3, 8, 0],    'there' : [22, 51, 63, 77],    'how' : [7, 0, 22],    'are' : [12, 11, 45],    'you' : [56, 31, 89, 90]} print("The dictionary is : ") print(my_dict) my_result = list(sorted({elem for val in my_dict.values() for elem in val})) print("The unique values are : ") print(my_result)OutputThe dictionary is : {'hi': [5, 3, 8, 0], ... Read More

Count Lowercase Characters in a String Using Python

AmitDiwan
Updated on 17-Apr-2021 12:08:56

2K+ Views

When it is required to count the number of lower case characters in a string, the ‘islower’ method and a simple ‘for’ loop can be used.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" print("The string is ") print(my_string) my_counter=0 for i in my_string:    if(i.islower()):       my_counter=my_counter+1 print("The number of lowercase characters in the string are :") print(my_counter)OutputThe string is Hi there how are you The number of lowercase characters in the string are : 15ExplanationA string is defined and is displayed on the console.A counter value is initialized to ... Read More

Display the Larger String Without Using Built-in Functions in Python

AmitDiwan
Updated on 17-Apr-2021 12:08:37

849 Views

When it is required to take two strings and display the larger string without using any built-in function, the counter can be used to get the length of the strings, and ‘if’ condition can be used to compare their lengths.Below is the demonstration of the same −Example Live Demostring_1= "Hi there" string_2= "Hi how are ya" print("The first string is :") print(string_1) print("The second string is :") print(string_2) count_1 = 0 count_2 = 0 for i in string_1:    count_1=count_1+1 for j in string_2:    count_2=count_2+1 if(count_1

Find All Connected Components Using DFS in an Undirected Graph

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

2K+ Views

When it is required to find all the connected components using depth first search in an undirected graph, a class is defined that contains methods to initialize values, perform depth first search traversal, find the connected components, add nodes to the graph and so on. The instance of the class can be created and the methods can be accessed and operations and be performed on it.Below is a demonstration of the same −Example Live Democlass Graph_struct:    def __init__(self, V):       self.V = V       self.adj = [[] for i in range(V)]    def DFS_Utililty(self, temp, ... Read More

Advertisements