Ankith Reddy has Published 1070 Articles

How can we do the basic print formatting for Python numbers?

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 12:39:20

147 Views

You can format a floating number to the fixed width in Python using the format function on the string. For example, nums = [0.555555555555, 1, 12.0542184, 5589.6654753] for x in nums:    print("{:10.4f}".format(x))This will give the output0.5556 1.0000 12.0542 5589.6655Using the same function, you can also format integersnums = [5, ... Read More

How to use multiple for and while loops together in Python?

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 12:36:21

255 Views

You can create nested loops in python fairly easily. You can even nest a for loop inside a while loop or the other way around. For example, for i in range(5):    j = i    while j != 0:       print(j, end=', ')       j ... Read More

How to search Python dictionary for matching key?

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 11:17:59

4K+ Views

If you have the exact key you want to find, then you can simply use the [] operator or get the function to get the value associated with this key. For example, Examplea = {    'foo': 45,    'bar': 22 } print(a['foo']) print(a.get('foo'))OutputThis will give the output:45 45ExampleIf you ... Read More

How we can translate Python dictionary into C++?

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 11:01:41

1K+ Views

A python dictionary is a Hashmap. You can use the map data structure in C++ to mimic the behavior of a python dict. You can use map in C++ as follows:#include #include using namespace std; int main(void) {    /* Initializer_list constructor */    map m1 = { ... Read More

Print Matrix in spiral way

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 10:14:06

919 Views

This algorithm is used to print the array elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row and so on, thus it prints the elements in spiral fashion. The time complexity of ... Read More

Amortized Analysis

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 10:07:00

15K+ Views

Amortize AnalysisThis analysis is used when the occasional operation is very slow, but most of the operations which are executing very frequently are faster. Data structures we need amortized analysis for Hash Tables, Disjoint Sets etc.In the Hash-table, the most of the time the searching time complexity is O(1), but ... Read More

Lock & Key problem using Hash-map

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 09:57:57

676 Views

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 ... Read More

Even Number With Prime Sum

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 09:27:18

993 Views

All even numbers from 4, can be expressed as a sum of two prime numbers. Sometimes a number can have more than one sum of the prime number combination.For an example the number 10 = (5 + 5) and (7 + 3)This algorithm will find all of the combinations of ... Read More

Check Perfect Square or Not

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 09:17:02

2K+ Views

A number is said to be a perfect square number if the square root of that number is an integer. In other words, when a square root is a whole number, then the number is called a perfect square number.We can check perfect square by finding the square root of ... Read More

Check whether a given point lies inside a Triangle

Ankith Reddy

Ankith Reddy

Updated on 17-Jun-2020 09:12:37

2K+ Views

Three points of a triangle are given; another point P is also given to check whether the point P is inside the triangle or not.To solve the problem, let consider the points of the triangle are A, B, and C. When the area of triangle Δ𝐴𝐵𝐶 = Δ𝐴𝐵𝑃 + Δ𝑃𝐵𝐶 ... Read More

Advertisements