Python Articles

Page 34 of 855

How to wrap python object in C/C++?

Gireesha Devara
Gireesha Devara
Updated on 12-Mar-2026 655 Views

To wrap existing C or C++ functionality in Python, there are number of options available, which are: Manual wrapping using PyMethodDef and Py_InitModule, SWIG, Pyrex, ctypes, SIP, Boost.Python, and pybind1. Using the SWIG Module Let’s take a C function and then tune it to python using SWIG. The SWIG stands for “Simple Wrapper Interface Generator”, and it is capable of wrapping C in a large variety of languages like python, PHP, TCL etc. Example Consider simple factorial function fact() in example.c file. /* File : example.c */ #include // calculate factorial int fact(int n) { ...

Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 374 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Read More

Maximum Nesting Depth of Two Valid Parentheses Strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 388 Views

Suppose we have a string, that string is a valid parentheses string (denoted VPS) if and only if it consists of "(" and ")" characters only, and it satisfies these properties −It is the empty string, orIt can be written as AB, where A and B are VPS's, orIt can be written as (A), where A is a VPS.We can also define the nesting depth depth(S) of any VPS S like below −depth("") = 0depth(A + B) = max of depth(A), depth(B), where A and B are VPS'sdepth("(" + A + ")") = 1 + depth(A), where A is a ...

Read More

Program to check whether one value is present in BST or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.So, if the input is likeval = 7, then the output will be True, as 7 is present in the tree.To solve this, we will follow these steps−Define a function solve() . This will take root, valif root is null, thenreturn Falseif data of root is same as val, thenreturn Trueif data of root < val, thenreturn solve(left of root, val)return solve(right of root, val)Let us see the following implementation to get better understanding−class TreeNode:   ...

Read More

Program to find contiguous intervals of a unique array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 747 Views

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 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 11-Mar-2026 162 Views

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 More

Python Program to Filter Rows with a specific Pair Sum

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 249 Views

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 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 11-Mar-2026 687 Views

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 More

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

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 251 Views

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 More

Python – Check if any list element is present in Tuple

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 992 Views

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 More
Showing 331–340 of 8,547 articles
« Prev 1 32 33 34 35 36 855 Next »
Advertisements