Suppose we have a list of data say x, represents a domain and a list of data y (size of y is same as size of x), represents a range. We have to check whether x -> y is a function or not. Here we are considering all elements in x and y are positive.So, if the input is like x = [1, 3, 2, 6, 5] y = [1, 9, 4, 36, 25], then the output will be True, because for each x, the corresponding y is its square value here, so this is a function.To solve this, we ... Read More
Suppose we have two sides of a right angled triangle, these sides are AB and BC. Consider the midpoint of hypotenuse AC is M. We have to find the angle between M and BC.So, if the input is like ab = 6 bc = 4, then the output will be 56.309932474020215 because arc_tan of ab/bc is 0.9828 but in degrees it is 56.31.To solve this, we will follow these steps −ans := arc-tan(ab/bc)return ans in degreesExampleLet us see the following implementation to get better understandingfrom math import atan, pi def solve(ab, bc): def deg(rad): return 180/pi ... Read More
Suppose we have two times in this format "Day dd Mon yyyy hh:mm:ss +/-xxxx", where Day is three letter day whose first letter is in uppercase. Mon is the name of month in three letters and finally + or - xxxx represents the timezone for example +0530 indicates it is 5 hours 30 minutes more than GMT (other formats like dd, hh, mm, ss are self-explanatory). We have to find absolute difference between two timestamps in seconds.To solve this using python we will use the datetime library. There is a function called strptime() this will convert string formatted date to ... Read More
Suppose we have a string s and and a value k. The value of k is factor of the length of s, say the length is n. We can split s into n/k different substrings called t_i of size k. Then use these t_i to make u_i such thatThe characters present in u_i are subsequence of characters in t_iAny repeat characters will be removed from these string such that frequency of each character in u_i is 1We have to find these u_i stringsSo, if the input is like s = "MMPQMMMRM" k = 3, then the output will be ["MP", ... Read More
Suppose there are two players Amal and Bimal. They are playing a game. The game rules are as follows −Both players have a same string s.Both of them have to make substrings using the letters of s.Bimal has to make words starting with consonants.Amal has to make words starting with vowels.The game will end when both players have made all possible substrings.Now the scoring criteria is like: a player gains 1 point for each occurrence of the substring in the string s. We have to find winner of this game and his score.So, if the input is like s = ... Read More
Suppose we have a list of numbers called nums with positive and negative numbers. We have to update this list so that the final list will only hold the absolute value of each element.So, if the input is like nums = [5, -7, -6, 4, 6, -9, 3, -6, -2], then the output will be [5, 7, 6, 4, 6, 9, 3, 6, 2]To solve this, we will follow these steps −Solve this by map and list operationsdefine one anonymous function say l, that takes x as argument and returns abs(x)using map() method convert each element e from nums to ... Read More
Suppose we have a number n. We have to find divisor of n which one is better based on these conditions: We have two numbers p and q, the one whose digits sum to a larger number is called better than the other one. When the sum of digits is same, then the smaller number is the better one.So, if the input is like n = 180, then the output will be 9 because the divisors are [1, 2, 3, 4, 5, 6, 9, 10, 12, 15, 18, 20, 30, 36, 45, 60, 90, 180]. So the number whose digit ... Read More
Suppose we have a list nums. We have to find the length of this list but without using any length(), size() or len() type of functions.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be 9.To solve this, we will follow these steps −Solve this by map and list operationsx := a list which contains all elements in numsconvert all elements in x to 1find sum of x by using sum() methodIn this example we have used the map() method to convert all into 1 by defining an ... Read More
Suppose we have a list of n elements called nums. We have to reverse this list by list slicing operations.So, if the input is like nums = [5, 7, 6, 4, 6, 9, 3, 6, 2], then the output will be [2, 6, 3, 9, 6, 4, 6, 7, 5]To solve this, we will follow these steps −list slicing takes at most three parameters separated by colon. First one is start, second one is end and third one is stephere as we start from 0 we do not pass first parameter, as we end at n, we also not providing ... Read More
Suppose we have a number n. We have to create a list of elements of size n, the elements are from 1 to n.So, if the input is like n = 5, then the output will be [1, 2, 3, 4, 5]To solve this, we will follow these steps −use python list comprehension strategy to solve this problemcreate a list with i for each i from 1 to n, for this we use range() function. This will take lower bound which is n here, and upper bound n+1 because we want to generate up to n.ExampleLet us see the following ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP