A list of different locks and another list of keys are given. Our task is to find the correct match of lock and key from the given list, and assign that key with the lock when it is correct.In this approach we will traverse all of the locks and create a hash-map, after that, each key is searched in the hash-map. When the key is matches, then that is marked as a valid key and assigned with a lock.Input and OutputInput: The lists of locks and keys. lock = { ), @, *, ^, (, %, !, $, &, #} ... Read More
A list of different nuts and another list of bolts are given. Our task is to find the correct match of nuts and bolts from the given list, and assign that nut with the Bolt, when it is matched.This problem is solved by the quick-sort technique. By taking the last element of the bolt as a pivot, rearrange the nuts list and get the final position of the nut whose bolt is the pivot element. After partitioning the nuts list, we can partition the bolts list using the selected nut. The same tasks are performed for left and right sub-lists ... Read More
Printing all permutations of a given string is an example of backtracking problem. We will reduce the size of the substring to solve the sub-problems, then again backtrack to get another permutation from that section.For an example, if the string is ABC, the all permutations will be ABC, ACB, BAC, BCA, CAB, CBA.The complexity of this algorithm is O(n!). It is a huge complexity. When the string size increases, it takes a longer time to finish the task.Input and OutputInput: A string “ABC” Output: All permutations of ABC is: ABC ACB BAC BCA CBA CABAlgorithmstringPermutation(str, left, right)Input: The string and left ... Read More
Parity of a number is based on the number of 1’s present in the binary equivalent of that number. When the count of present 1s is odd, it returns odd parity, for an even number of 1s it returns even parity.As we know that the numbers in computer memory are stored in binary numbers, so we can shift numbers easily. In this case, by shifting the bits, we will count a number of 1’s present in the binary equivalent of the given number.Input and OutputInput: A number: 5 Binary equivalent is (101) Output: Parity of 5 is Odd.AlgorithmfinParity(n)Input: The number ... Read More
Every class is an object. It's an instance of something called a metaclass. The default metaclass is typed. You can check this using the is instance function. For example,class Foo: pass foo = Foo() isinstance(foo, Foo) isinstance(Foo, type)This will give the output:True TrueA metaclass is not part of an object's class hierarchy whereas base classes are. These classes are used to initialize the class and not its objects.You can read much more in-depth about Metaclasses and inheritance on https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/
The Reservoir sampling is a randomized algorithm. In this algorithm, k items are chosen from a list with n different items.We can solve it by creating an array as a reservoir of size k. Then randomly pick one element from the main list and placed that item in the reservoir list. When one item is selected once, it will not be selected for next time. But his approach is not effective, we can increase the complexity by this method.In the reservoir list, copy first k items from the list, now one by one from the (k+1)th number in the list, ... Read More
One sales-person is in a city, he has to visit all other cities those are listed, the cost of traveling from one city to another city is also provided. Find the route where the cost is minimum to visit all of the cities once and return back to his starting city.The graph must be complete for this case, so the sales-person can go from any city to any city directly.Here we have to find minimum weighted Hamiltonian Cycle.Input and OutputInput: Cost matrix of the matrix. 0 20 42 25 30 20 0 30 34 15 42 30 0 10 10 ... Read More
A list of given strings is sorted in alphanumeric order or Dictionary Order. Like for these words: Apple, Book, Aim, they will be sorted as Aim, Apple, Book.If there are some numbers, they can be placed before the alphabetic strings.Input and OutputInput: A list of strings: Ball Apple Data Area 517 April Man 506 Output: Strings after sort: 506 517 Apple April Area Ball Data ManAlgorithmsortStr(strArr, n)Input: The list of all strings, number of elements.Output − Strings in alphanumeric sorted order.Begin for round := 1 to n-1, do for i := 0 to n-round, do ... Read More
Zeller’s Algorithm is used to find the weekday from a given date. The formula to find weekday using Zeller’s Algorithm is here:The formula is containing some variables; They are −d − The day of the date.m: It is the month code. From March to December it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then given year will be decreased by 1.y − Last two digits of the yearc − first two digits of the yearw − The weekday. When it is 0, it is Saturday, when ... Read More
Let two line-segments are given. The points p1, p2 from the first line segment and q1, q2 from the second line segment. We have to check whether both line segments are intersecting or not.We can say that both line segments are intersecting when these cases are satisfied:When (p1, p2, q1) and (p1, p2, q2) have a different orientation and(q1, q2, p1) and (q1, q2, p2) have a different orientation.There is another condition is when (p1, p2, q1), (p1, p2, q2), (q1, q2, p1), (q1, q2, p2) are collinear.Input and OutputInput: Points of two line-segments Line-segment 1: (0, 0) to (5, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP