Programming Articles

Page 358 of 2547

Python – Extract rows with Even length strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 202 Views

When working with nested lists of strings, you may need to extract rows where all strings have even length. This can be accomplished using list comprehension with the all() function and modulus operator %. Syntax result = [row for row in nested_list if all(len(element) % 2 == 0 for element in row)] How It Works The solution uses three key components ? len(element) % 2 == 0 − Checks if string length is even all() − Returns True only if all elements in the row have even length List comprehension − Filters ...

Read More

Python – Test if Rows have Similar frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 214 Views

When checking if rows have similar frequency in a list of lists, you can use Python's Counter class along with the all() function to compare element frequencies across all rows. Example Here's how to test if all rows contain the same elements with identical frequencies ? from 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 ...

Read More

Python Program that filters out non-empty rows of a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 261 Views

When working with matrices (lists of lists), you often need to filter out empty rows. Python provides several approaches to remove non-empty rows from a matrix using list comprehension, the filter() function, or loops. Using List Comprehension List comprehension with len() provides a clean and readable solution ? my_matrix = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], [9, 2]] print("The original matrix is:") print(my_matrix) # Filter out empty rows using list comprehension filtered_matrix = [row for row in my_matrix if len(row) > 0] print("The matrix after filtering empty rows:") print(filtered_matrix) ...

Read More

Python – Test if all elements are unique in columns of a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 527 Views

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 − Example my_matrix = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The matrix is:") print(my_matrix) my_result = True for index in range(len(my_matrix[0])): column = [row[index] for row in my_matrix] if len(list(set(column))) != len(column): ...

Read More

Python – Remove characters greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 246 Views

When working with strings, you may need to filter out characters based on their position in the alphabet. This technique removes characters that appear after a certain position K in the alphabet using the ord() function to get Unicode values. Understanding the Approach The method compares each character's alphabetical position with K. Since lowercase 'a' has ASCII value 97, we subtract 97 from each character's ASCII value to get its alphabetical position (a=0, b=1, c=2, etc.). Example Here's how to remove characters greater than K from a list of strings ? words = ["python", ...

Read More

Python – Check if any list element is present in Tuple

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

When it is required to check if any list element is present in a tuple or not, Python provides several approaches. We can use simple iteration, the any() function, or set intersection for efficient checking. Using Simple Iteration This approach uses a loop to check each list element against the tuple ? my_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: ...

Read More

Python Program to sort rows of a matrix by custom element count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 264 Views

When sorting rows of a matrix based on custom element count, we can define a helper function that counts how many elements from each row match our custom criteria. This approach uses list comprehension with the len() method to determine the sort order. Example Let's sort matrix rows by counting how many elements from each row appear in our custom list ? def 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 ...

Read More

Python Program to Filter Rows with a specific Pair Sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 256 Views

When working with lists of numbers, we often need to filter rows that contain a specific pair sum. This involves checking if any two elements in each row add up to a target value and keeping only those rows that satisfy this condition. Problem Overview Given a list of lists (rows) and a target sum, we want to filter and keep only the rows where at least one pair of elements adds up to the target value ? Method: Using Helper Function with List Comprehension We'll create a helper function to check for pair sums and ...

Read More

Program to check if number of compass usage to get out of a maze is enough in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 175 Views

In this problem, we need to determine if we can escape from a maze using a compass at most k times. The maze is represented as a matrix where 'S' is the starting position, 'D' is the destination (exit), '-' represents walkable paths, and 'O' represents blocked cells. The compass is used when we have multiple path choices at any position. Understanding the Problem The key insight is that we only need to use the compass when we have multiple valid moves from our current position. If there's only one valid path, we can move without using the ...

Read More

Program to find out the minimum number of moves for a chess piece to reach every position in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 706 Views

In chess, different pieces have unique movement patterns. This program solves the problem of finding the minimum number of moves for a special chess piece to reach every position on an n×n chessboard from the starting position (0, 0). The special piece moves in an L-shape pattern defined by parameters a and b. From position (x1, y1), it can move to (x2, y2) where: x2 = x1 ± a, y2 = y1 ± b x2 = x1 ± b, y2 = y1 ± a Algorithm Approach We use Breadth-First Search (BFS) to find the ...

Read More
Showing 3571–3580 of 25,466 articles
« Prev 1 356 357 358 359 360 2547 Next »
Advertisements