There are scenarios when we need to repeat the values in a list. This duplication of values can be achived in python in the following ways.Using nested for loopIt is a straight forward approach in which pick each element, take through a inner for loop to create its duplicate and then pass both of them to an outer for loop.Example Live Demo# Given list listA = ['Mon', 'Tue', 9, 3, 3] print("Given list : ", listA) # Adding another element for each element Newlist = [i for i in listA for n in (0, 1)] # Result print("New ... Read More
The elements in tow lists can be involved in a division operation for some data manipulation activity using python. In this article we will see how this can be achieved.With zipThe zip function can pair up the two given lists element wise. The we apply the division mathematical operator to each pair of these elements. Storing the result into a new list.Example Live Demo# Given lists list1 = [12, 4, 0, 24] list2 = [6, 3, 8, -3] # Given lists print("Given list 1 : " + str(list1)) print("Given list 2 : " + str(list2)) # Use zip res ... Read More
Suppose we have a list of positive integers; whose value is greater than 1. We will make a binary tree, using these integers, and each number may be used as many times as we want. Each non-leaf node should be the product of its children. So we have to find how many trees can we make? The answer will be returned in modulo 10^9 + 7. So if the input is like [2, 4, 5, 10], then the answer will be 7, as we can make 7 trees like [2], [4], [5], [10], [4, 2, 2], [10, 2, 5], [10, ... Read More
Suppose on a table are N cards, with a positive integer printed on both side of each card (possibly different). We have to flip any number of cards, and after we choose one card. If the number X on the back side of the chosen card is not on the front of any card, then the number X is known as good. We have to find the smallest number that is good? When no number is good, return 0. Here, fronts[i] and backs[i] represent the number on the front and back side of card i. A flip will swap the ... Read More
Suppose we have a list of words, we may encode it by writing a reference string S and a list of indexes A. So for example, let us consider if the list of words is ["time", "me", "bell"], then we can write it as S = "time#bell#" and indexes = [0, 2, 5]. Here for each index, we will recover the word by reading from the reference string from that index until we reach the "#" symbol.So we have to find what is the length of the shortest reference string S possible that encodes the given words? So for the ... Read More
Suppose we have given a head; this is the head node of a linked list containing unique integer values. Now we are also given the list G, a subset of the values in the linked list. We have to find the number of connected components in G, where two values are connected if they appear consecutively in the linked list. So if the list is like [0, 1, 2, 3] and G = [0, 1, 3], then output will be 2, as 0 and 1 are connected, so there are two lists [0, 1] and [3].To solve this, we will ... Read More
Suppose we partition a row of numbers A into at most K adjacent groups, then we will set score as the sum of the average of each group. We have to find that what is the largest score that we can achieve. Suppose A = [9, 1, 2, 3, 9] and K is 3, then the result will be 20, this is because, the best choice is to partition A into [9], [1, 2, 3], [9]. So the answer is 9 + (1 + 2 + 3) / 3 + 9 = 20. We could have also partitioned A into ... Read More
Suppose we are playing a simplified Pacman game. Now we start at the point (0, 0), and our destination is (target[0], target[1]). There are several ghosts on the map, Here the i-th ghost starts at (ghosts[i][0], ghosts[i][1]). In each turn, we and all ghosts simultaneously (may) move in one of 4 cardinal directions − north, east, west, or south, going from the last point to a new point 1 unit of distance away. We can escape if and only if we can reach the target before any ghost reaches us (for any given moves the ghosts may take.) If we ... Read More
Suppose In a forest, each rabbit has some color. Now some subset of rabbits (possibly all of them) will tell us how many other rabbits have the same color as them. Those answers are placed in an array. We have to find the minimum number of rabbits that could be in the forest. So if the input is like [1, 1, 2], then the output will be 5, as the two rabbits that answered "1" that could both be the same color, say white. Now the rabbit than answered "2" can't be white or the answers would be inconsistent. Say ... Read More
Suppose on the first row, we have a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 by 01, and each occurrence of 1 by 10. Suppose we have N rows and index K, we have to find the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). So if N = 4 and K = 5, then the output will be 1. This is because −Row 1: 0Row 2: 01Row 3: 0110Row 4: 01101001To solve this, we will follow these steps −Suppose the name of ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP