Samual Sam has Published 2492 Articles

How to comment each condition in a multi-line if statement in Python?

Samual Sam

Samual Sam

Updated on 17-Jun-2020 12:01:22

190 Views

You can do this directly if you are surrounding your multiline if statements conditions in a parenthesis. For example, if (cond1 == 'val1' and    cond2 == 'val2' and # Some comment    cond3 == 'val3' and # Some comment    cond4 == 'val4'):However, this is not possible if you ... Read More

What is python .. ("dot dot") notation syntax?

Samual Sam

Samual Sam

Updated on 17-Jun-2020 11:58:35

330 Views

There is no special .. ("dot dot") notation syntax in python. You can, however, see this in case of floats accessing their properties. For example, f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8))This will give the output:0.125What we have is a float literal without the trailing zero, which ... Read More

How to create Python dictionary from the value of another dictionary?

Samual Sam

Samual Sam

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

2K+ Views

You can do this by merging the other dictionary to the first dictionary. In Python 3.5+, you can use the ** operator to unpack a dictionary and combine multiple dictionaries using the following syntax −Syntaxa = {'foo': 125} b = {'bar': "hello"} c = {**a, **b} print(c)OutputThis will give the ... Read More

Magic Square

Samual Sam

Samual Sam

Updated on 17-Jun-2020 10:16:55

5K+ Views

The magic square is a square matrix, whose order is odd and where the sum of the elements for each row or each column or each diagonal is same. The sum of each row or each column or each diagonal can be found using this formula. n(n2+ 1)/2Here are the rules ... Read More

How can I remove items out of a Python tuple?

Samual Sam

Samual Sam

Updated on 17-Jun-2020 10:12:50

1K+ Views

Tuples in python are immutable. If you want to remove items out of a Python tuple, you can use index slicing to leave out a particular index. For example, a = (1, 2, 3, 4, 5) b = a[:2] + a[3:] print(b)This will give the output:(1, 2, 4, 5)Or you ... Read More

Polynomial Time Approximation Scheme

Samual Sam

Samual Sam

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

771 Views

Polynomial Time Approximation schemeWe can find some polynomial time solution for NP-Complete problems like 0-1 Knapsack problem or Subset sum problem. These problems are very popular in the real world, so there must be some ways to handle these problems.The Polynomial Time Approximation Scheme (PTAS) is a type to approximate ... Read More

Lexicographically minimum string rotation

Samual Sam

Samual Sam

Updated on 17-Jun-2020 10:03:27

473 Views

Let us consider a string is given, we know that the string is a sequence of characters. The Lexicographical rotation is the rotation of string, to convert characters in lexicographical order.The solution is simple, we simply concatenate the given string with itself, then in another array, all rotation of strings ... Read More

Nuts and Bolt Problem

Samual Sam

Samual Sam

Updated on 17-Jun-2020 09:56:52

1K+ Views

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

Travelling Salesman Problem

Samual Sam

Samual Sam

Updated on 17-Jun-2020 09:50:16

1K+ Views

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

Check if two line segments intersect

Samual Sam

Samual Sam

Updated on 17-Jun-2020 09:40:21

4K+ Views

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

Advertisements