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
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
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
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return an array showing all the ways of balancing n parenthesis.For example, for n = 3, the output will be −["()()()","(())()","()(())","(()())","((()))"]ExampleFollowing is the code − Live Democonst res = []; const buildcombination = (left, right, str) => { if (left === 0 && right === 0) { res.push(str); } if (left > 0) { buildcombination(left-1, right+1, str+"("); } if (right > 0) { buildcombination(left, right-1, str+")"); } } buildcombination(3, 0, ""); console.log(res);OutputFollowing is the console output −[ '((()))', '(()())', '(())()', '()(())', '()()()' ]
When it is required to calculate the length of a string without using library methods, a counter is used to increment every time an element of the string is encountered.Below is the demonstration of the same −Example Live Demomy_string = "Hi Will" print("The string is :") print(my_string) my_counter=0 for i in my_string: my_counter=my_counter+1 print("The length of the string is ") print(my_counter)OutputThe string is : Hi Will The length of the string is 7ExplanationA string is defined, and is displayed on the console.A counter is initialized to 0.The string is iterated over, and after every element is iterated over, the counter ... Read More
When it is required to remove a specific index character from a string which isn’t empty, it can be iterated over, and when the index doesn’t match, that character can be stored in another string.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" print("The string is :") print(my_string) index_removed = 2 changed_string = '' for char in range(0, len(my_string)): if(char != index_removed): changed_string += my_string[char] print("The string after removing ", index_removed, "nd character is : ") print(changed_string)OutputThe string is : Hi there how are you The ... Read More
When it is required to find the maximum sub array using Kadane’s algorithm, a method is defined that helps find the maximum of the sub array. Iterators are used to keep track of the maximum sub array.Below is the demonstration of the same −Example Live Demodef find_max_sub_array(my_list, beg, end): max_end_at_i = max_seen_till_now = my_list[beg] max_left_at_i = max_left_till_now = beg max_right_till_now = beg + 1 for i in range(beg + 1, end): if max_end_at_i > 0: max_end_at_i += my_list[i] else: max_end_at_i = my_list[i] ... Read More
ProblemWe are required to write a JavaScript function that takes in an array of literals that might contain some 0s. Our function should tweak the array such that all the zeroes are pushed to the end and all non-zero elements hold their relative positions.ExampleFollowing is the code − Live Democonst arr = [5, 0, 1, 0, -3, 0, 4, 6]; const moveAllZero = (arr = []) => { const res = []; let currIndex = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(el === 0){ res.push(0); }else{ res.splice(currIndex, undefined, el); currIndex++; }; }; return res; }; console.log(moveAllZero(arr));OutputFollowing is the console output −[ 5, 1, -3, 4, 6, 0, 0, 0 ]
ProblemWe are required to write a JavaScript function that takes in an array of integers arr. Our function should return the string ‘odd’ if the sum of all the elements of the array is odd or ‘even’ if it’s even.ExampleFollowing is the code − Live Democonst arr = [5, 1, 8, 4, 6, 9]; const assignSum = (arr = []) => { const sum = arr.reduce((acc, val) => { return acc + val; }, 0); const isSumEven = sum % 2 === 0; return isSumEven ? 'even' : 'odd'; }; console.log(assignSum(arr));OutputFollowing is the console output −odd
ProblemWe are required to write a JavaScript function that takes in a number n. Our function should construct and return an array of N * N order (2-D array), in which the 1s take all the spiralling positions starting from [0, 0] and all the 0s take non-spiralling positions.Therefore, for n = 5, the output will look like −[ [ 1, 1, 1, 1, 1 ], [ 0, 0, 0, 0, 1 ], [ 1, 1, 1, 0, 1 ], [ 1, 0, 0, 0, 1 ], [ 1, 1, 1, 1, 1 ] ]ExampleFollowing ... Read More