Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Python Articles
Page 4 of 854
Program to find contiguous intervals of a unique array in Python
Suppose we have a list of unique numbers called nums. We have to find a sorted 2D matrix of numbers where each list represents an inclusive interval summarizing number that are contiguous in nums.So, if the input is like nums = [10, 11, 12, 15, 16, 17, 28, 30], then the output will be [[10, 12], [15, 17], [28, 28], [30, 30]], as in the list [10 to 12], [15 to 17] are contiguous, and 28 and 30 are there, they are represented as [28 to 28] and [30 to 30].To solve this, we will follow these steps−sort the list ...
Read MoreProgram to check if number of compass usage to get out of a maze is enough in Python
Suppose, we are playing a game where we are trapped in a maze. We have to find our way out of the maze. The maze can be presented as an x m matrix, where n is the number of rows and m is the number of columns. Each cell/element of the matrix contains any of the symbols 'O', 'D', 'S', or '-'. 'O' means that the path is blocked, 'D' is the way out from the maze, 'S' is our starting position, and '-' means we can move through the path. We can move freely through any of the '-' ...
Read MorePython Program to Filter Rows with a specific Pair Sum
When it is required to filter rows with a specific pair sum, a method is defined. It checks to see if elements in a specific index is equal to key, and returns output based on this.Below is a demonstration of the same −Exampledef find_sum_pair(val, key): for index in range(len(val)): for ix in range(index + 1, len(val)): if val[index] + val[ix] == key: return True return False my_list = [[71, 5, 21, 6], [34, 21, 2, 71], [21, 2, 34, 5], [6, 9, 21, ...
Read MoreProgram to find out the minimum number of moves for a chess piece to reach every position in Python
Suppose, we have a chessboard and a special knight piece K, that moves in an L shape within the board. If the piece is in position (x1, y1) and if it moves to (x2, y2) the movement can be described as x2 = x1 ± a ; y2 = y1 ± b or x2 = x1 ± b ; y2 = y1 ± a ; where a and b are integer numbers. We have to find out the minimum number of moves for that chess piece to reach to reach position (n-1, n-1) on the chessboard from the position (0, ...
Read MorePython Program to sort rows of a matrix by custom element count
When it is required to sort rows of a matrix by custom element count, a method is defined that uses the list comprehension and ‘len’ method to find the output.Below is a demonstration of the same −Exampledef get_count_matrix(my_key): return len([element for element in my_key if element in custom_list]) my_list = [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] print("The list is :") print(my_list) custom_list = [31, 85, 7] my_list.sort(key=get_count_matrix) print("The resultant list is :") print(my_list)OutputThe list is : [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] The resultant ...
Read MorePython – Check if any list element is present in Tuple
When it is required to check if any list element is present in a tuple or not, a Boolean value and a simple iteration are used.Below is a demonstration of the same −Examplemy_tuple = (14, 35, 27, 99, 23, 89, 11) print("The tuple is :") print(my_tuple) my_list = [16, 27, 88, 99] print("The list is :") print(my_list) my_result = False for element in my_list: if element in my_tuple : my_result = True break print("The result is :") if(my_result == True): print("The element is present in the tuple") ...
Read MorePython – Remove characters greater than K
When it is required to remove characters, which are greater than ‘K’, a simple iteration is used along with the ‘ord’ (Unicode representation) method.Below is a demonstration of the same −Examplemy_list = ["python", "is", "easy", "to", "learn"] print("The list is :") print(my_list) K = 9 print("The value of K is ") print(K) my_result = [] for element in my_list: result_string = '' for sub in element: if (ord(sub) - 97
Read MorePython – Test if all elements are unique in columns of a Matrix
When it is required to test if all elements are unique in columns of a matrix, a simple iteration and a list comprehension along with the ‘set’ operator are used.Below is a demonstration of the same −Examplemy_list = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list[0])): column = [ele[index] for ele in my_list] if len(list(set(column ))) != len(column ): my_result = False break if(my_result == True): print("All columns are unique") else: ...
Read MorePython Program that filters out non-empty rows of a matrix
When it is required to filter out non-empty rows from a matrix, a simple list comprehension along with the ‘len’ method can be used.Below is a demonstration of the same −Examplemy_list = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] print("The list is :") print(my_list) my_result = [row for row in my_list if len(row) > 0] print("The resultant list is :") print(my_result)OutputThe list is : [[21, 52, 4, 74], [], [7, 8, 4, 1], [], []] The resultant list is : [[21, 52, 4, 74], [7, 8, 4, 1]]ExplanationA list of list with integers ...
Read MorePython – Test if Rows have Similar frequency
When it is required to check if rows have similar frequency, the ‘all’ operator, the ‘Counter’ method and a simple iteration is used.Below is a demonstration of the same −Examplefrom collections import Counter my_list = [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] print("The list is :") print(my_list) my_result = all(dict(Counter(row)) == dict(Counter(my_list[0])) for row in my_list ) if(my_result == True): print("All rows have similar frequency") else: print("All rows do not have similar frequency")OutputThe list is : [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], ...
Read More