MCPcopy Create free account

hub / github.com/TheAlgorithms/Python / functions

Functions3,909 in github.com/TheAlgorithms/Python

↓ 1 callersFunctionarea_parallelogram
Calculate the area of a parallelogram. >>> area_parallelogram(10, 20) 200 >>> area_parallelogram(1.6, 2.6) 4.16 >>> a
maths/area.py:363
↓ 1 callersFunctionarea_rectangle
Calculate the area of a rectangle. >>> area_rectangle(10, 20) 200 >>> area_rectangle(1.6, 2.6) 4.16 >>> area_rectangl
maths/area.py:239
↓ 1 callersFunctionarea_rhombus
Calculate the area of a rhombus. >>> area_rhombus(10, 20) 100.0 >>> area_rhombus(1.6, 2.6) 2.08 >>> area_rhombus(0, 0
maths/area.py:485
↓ 1 callersFunctionarea_square
Calculate the area of a square. >>> area_square(10) 100 >>> area_square(0) 0 >>> area_square(1.6) 2.560000000000
maths/area.py:267
↓ 1 callersFunctionarea_trapezium
Calculate the area of a trapezium. >>> area_trapezium(10, 20, 30) 450.0 >>> area_trapezium(1.6, 2.6, 3.6) 7.56000000000000
maths/area.py:391
↓ 1 callersFunctionarea_triangle
Calculate the area of a triangle given the base and height. >>> area_triangle(10, 10) 50.0 >>> area_triangle(1.6, 2.6) 2.0
maths/area.py:287
↓ 1 callersFunctionarea_triangle_three_sides
Calculate area of triangle when the length of 3 sides are known. This function uses Heron's formula: https://en.wikipedia.org/wiki/Heron%27
maths/area.py:315
↓ 1 callersFunctionarmstrong_number
Return True if n is an Armstrong number or False if it is not. >>> all(armstrong_number(n) for n in PASSING) True >>> any(armstrong_
maths/special_numbers/armstrong_numbers.py:16
↓ 1 callersFunctionascend_tree
Ascend the FP-Tree from a leaf node to its root, adding item names to the prefix path. Args: leaf_node: The leaf node to start a
machine_learning/frequent_pattern_growth.py:213
↓ 1 callersFunctionassemble_transformation
Assembles the transformations based on the ops table. >>> ops = [['0', 'Ic', 'Iu', 'It'], ... ['Dc', 'Cc', 'Iu', 'It'], ...
strings/min_cost_string_conversion.py:76
↓ 1 callersFunctionassign_clusters
(data, centroids)
machine_learning/k_means_clust.py:83
↓ 1 callersFunctionassociativity
Return the associativity of the operator `char`. https://en.wikipedia.org/wiki/Operator_associativity
data_structures/stacks/infix_to_postfix_conversion.py:37
↓ 1 callersFunctionastar
Implementation of a start algorithm. world : Object of the world object. start : Object of the cell as start position. stop : Objec
machine_learning/astar.py:89
↓ 1 callersFunctionatbash
>>> atbash("ABCDEFG") 'ZYXWVUT' >>> atbash("aW;;123BX") 'zD;;123YC'
ciphers/atbash.py:26
↓ 1 callersFunctionaudio_frames
Split an audio signal into overlapping frames. Args: audio: The input audio signal. sample_rate: The sample rate of the audi
machine_learning/mfcc.py:174
↓ 1 callersFunctionautocomplete_using_trie
>>> trie = Trie() >>> for word in words: ... trie.insert_word(word) ... >>> matches = autocomplete_using_trie("de") >>> "
strings/autocomplete_using_trie.py:41
↓ 1 callersFunctionavgpooling
This function is used to perform avgpooling on the input array of 2D matrix(image) Args: arr: numpy array size: size of pooli
computer_vision/pooling_functions.py:64
↓ 1 callersFunctionaxpy
input: a 'scalar' and two vectors 'x' and 'y' output: a vector computes the axpy operation
linear_algebra/src/lib.py:218
↓ 1 callersMethodback_propagation
Function for fine-tuning the weights of the neural net based on the error rate obtained in the previous epoch (i.e., iteration).
neural_network/two_hidden_layers_neural_network.py:96
↓ 1 callersMethodback_propagation
(self, gradient)
neural_network/back_propagation_neural_network.py:81
↓ 1 callersFunctionbacktrack
A recursive function that searches for possible combinations. Backtracks in case of a bigger current combination value than the target value.
backtracking/combination_sum.py:16
↓ 1 callersFunctionbacktrack
Helper function to perform backtracking to find the transformation from the current_word to the end_word. Parameters: current_word (
backtracking/word_ladder.py:15
↓ 1 callersFunctionbacktrack
Helper function that uses backtracking to determine if a valid word segmentation is possible starting from index 'start'. Parameters:
backtracking/word_break.py:10
↓ 1 callersFunctionbacktrack
>>> backtrack(13, 2, 1, 0, 0) (0, 1) >>> backtrack(10, 2, 1, 0, 0) (0, 1) >>> backtrack(10, 3, 1, 0, 0) (0, 0) >>> backtr
backtracking/power_sum.py:10
↓ 1 callersFunctionbacktrack
Generate valid combinations of balanced parentheses using recursion. :param partial: A string representing the current combination. :par
backtracking/generate_parentheses.py:11
↓ 1 callersFunctionbacktrack
>>> backtrack(0, 0) True >>> backtrack(0, 1) True >>> backtrack(0, 4) False
backtracking/match_word_pattern.py:19
↓ 1 callersFunctionbacktrack
(start: int)
data_structures/arrays/permutations.py:29
↓ 1 callersMethodbalance_predators_and_prey
Balances predators and preys so that prey can not dominate the predators, blocking up space for them to reproduce. >
cellular_automata/wa_tor.py:183
↓ 1 callersFunctionbasic
Verify that the target contains no genes besides the ones inside genes variable. >>> from string import ascii_lowercase >>> basic("docte
genetic_algorithm/basic_string.py:97
↓ 1 callersMethodbasis_function
The basis function determines the weight of each control point at time t. t: time value between 0 and 1 inclusive at which to eva
graphics/bezier_curve.py:25
↓ 1 callersFunctionbellman_ford
Returns shortest paths from a vertex src to all other vertices. >>> edges = [(2, 1, -10), (3, 2, 3), (0, 3, 5), (0, 1, 4)] >>> g = [{
graphs/bellman_ford.py:20
↓ 1 callersFunctionbenchmark
Benchmark code for comparing above 2 functions
strings/can_string_be_rearranged_as_palindrome.py:78
↓ 1 callersFunctionbenchmark
Benchmark code comparing different version.
strings/is_pangram.py:77
↓ 1 callersFunctionbenchmark
Benchmark code for comparing 2 functions, with different length int values. Brian Kernighan's algorithm is consistently faster than using mod
bit_manipulation/count_number_of_one_bits.py:63
↓ 1 callersFunctionbenchmark
Benchmark multiple functions, with three different length int values.
maths/number_of_digits.py:92
↓ 1 callersFunctionbenchmark
Benchmark multiple functions, with three different length int values.
maths/sum_of_digits.py:52
↓ 1 callersFunctionbenchmark
()
maths/least_common_multiple.py:37
↓ 1 callersFunctionbenchmark
Benchmarks
maths/euclidean_distance.py:42
↓ 1 callersFunctionbenchmark
Let's benchmark our functions side-by-side...
maths/prime_numbers.py:93
↓ 1 callersFunctionbenchmark
Benchmark our functions next to each other
matrix/count_negative_numbers_in_sorted_matrix.py:129
↓ 1 callersFunctionbenchmark
Benchmark multiple functions, with three different length int values.
matrix/pascal_triangle.py:166
↓ 1 callersFunctionbenchmark
Let's benchmark our functions side-by-side...
ciphers/atbash.py:41
↓ 1 callersFunctionbenchmark
Benchmarks
project_euler/problem_145/sol1.py:132
↓ 1 callersFunctionbenchmark
Benchmarks
project_euler/problem_187/sol1.py:142
↓ 1 callersFunctionbenchmark_a_function
(func: Callable, value: int)
maths/number_of_digits.py:98
↓ 1 callersFunctionbenchmark_a_function
(func: Callable, value: int)
maths/sum_of_digits.py:59
↓ 1 callersFunctionbenchmark_a_function
(func: Callable, value: int)
matrix/pascal_triangle.py:173
↓ 1 callersMethodbezier_curve_function
The function to produce the values of the Bezier curve at time t. t: the value of time t at which to evaluate the Bezier function
graphics/bezier_curve.py:49
↓ 1 callersFunctionbfs
(graph, s, t, parent)
networking_flow/minimum_cut.py:13
↓ 1 callersMethodbfs
(self, s=-2)
graphs/directed_and_undirected_weighted_graph.py:86
↓ 1 callersMethodbfs
(self, s=-2)
graphs/directed_and_undirected_weighted_graph.py:352
↓ 1 callersMethodbfs
>>> g = Graph() >>> g.add_edge(0, 1) >>> g.add_edge(0, 1) >>> g.add_edge(0, 2) >>> g.add_edge(1, 2) >
graphs/breadth_first_search.py:40
↓ 1 callersFunctionbfs_shortest_path
Find the shortest path between `start` and `goal` nodes. Args: graph (dict): node/list of neighboring nodes key/value pairs. start
graphs/breadth_first_search_shortest_path_2.py:21
↓ 1 callersFunctionbfs_shortest_path_distance
Find the shortest path distance between `start` and `target` nodes. Args: graph: node/list of neighboring nodes key/value pairs. s
graphs/breadth_first_search_shortest_path_2.py:72
↓ 1 callersFunctionbilateral_filter
( img: np.ndarray, spatial_variance: float, intensity_variance: float, kernel_size: int, )
digital_image_processing/filters/bilateral_filter.py:43
↓ 1 callersFunctionbinarize
Binarizes a grayscale image based on a given threshold value, setting values to 1 or 0 accordingly. Examples: >>> binarize(np.ar
computer_vision/haralick_descriptors.py:88
↓ 1 callersFunctionbinary_exponentiation
(a: int, n: float, mod: int)
maths/fermat_little_theorem.py:8
↓ 1 callersFunctionbinary_insertion_sort
Sorts a list using the binary insertion sort algorithm. :param collection: A mutable ordered collection with comparable items. :return:
sorts/binary_insertion_sort.py:14
↓ 1 callersFunctionbinary_mask
Apply binary mask, or thresholding based on bit mask value (mapping mask is binary). Returns the mapped true value mask and its compleme
computer_vision/haralick_descriptors.py:195
↓ 1 callersFunctionbinary_search
(lst: list[Any], item: Any, start: int, end: int)
sorts/tim_sort.py:4
↓ 1 callersFunctionbinary_search
>>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] >>> binary_search(test_list, 3) False >>> binary_search(test_list, 13) True
searches/simple_binary_search.py:14
↓ 1 callersFunctionbinary_search
Pure implementation of a binary search algorithm in Python Be careful collection must be ascending sorted otherwise, the result will be unpre
searches/binary_search.py:180
↓ 1 callersFunctionbinary_search
This function carries out Binary search on a 1d array and return -1 if it do not exist array: A 1d sorted array value : the value mea
matrix/binary_search_matrix.py:1
↓ 1 callersFunctionbinary_search_by_recursion
Pure implementation of a binary search algorithm in Python by recursion Be careful collection must be ascending sorted otherwise, the result will
searches/binary_search.py:320
↓ 1 callersFunctionbinary_search_by_recursion
Pure implementation of binary search algorithm in Python using recursion Be careful: the collection must be ascending sorted otherwise, the resul
searches/exponential_search.py:19
↓ 1 callersMethodbinary_search_delete
Removes the item from the sorted array and returns the new array. >>> NumberContainer().binary_search_delete([1,2,3], 2)
other/number_container_system.py:20
↓ 1 callersMethodbinary_search_insert
Inserts the index into the sorted array at the correct position. >>> NumberContainer().binary_search_insert([1,2,3], 2)
other/number_container_system.py:77
↓ 1 callersFunctionbinary_search_tree_example
r""" Example 8 / \ 3 10 / \ \ 1 6 14 /
data_structures/binary_tree/binary_search_tree_recursive.py:546
↓ 1 callersFunctionbinary_tree_count
Return the number of possible of binary trees. :param n: number of nodes :return: Number of possible binary trees >>> binary_tree_co
data_structures/binary_tree/number_of_possible_binary_trees.py:81
↓ 1 callersFunctionbinary_tree_mirror
>>> binary_tree_mirror({ 1: [2,3], 2: [4,5], 3: [6,7], 7: [8,9]}, 1) {1: [3, 2], 2: [5, 4], 3: [7, 6], 7: [9, 8]} >>> binary_tree_mirror(
data_structures/binary_tree/binary_tree_mirror.py:16
↓ 1 callersFunctionbinary_tree_mirror_dict
(binary_tree_mirror_dictionary: dict, root: int)
data_structures/binary_tree/binary_tree_mirror.py:7
↓ 1 callersFunctionbinomial_coefficient
Find binomial coefficient using Pascal's triangle. Calculate C(n, r) using Pascal's triangle. :param n: The total number of items.
maths/binomial_coefficient.py:1
↓ 1 callersFunctionbinomial_coefficient
Since Here we Find the Binomial Coefficient: https://en.wikipedia.org/wiki/Binomial_coefficient C(n,k) = n! / k!(n-k)! :param n: 2 ti
data_structures/binary_tree/number_of_possible_binary_trees.py:20
↓ 1 callersFunctionbinomial_distribution
Return probability of k successes out of n tries, with p probability for one success The function uses the factorial function in ord
maths/binomial_distribution.py:7
↓ 1 callersFunctionbisect_right
Locates the first element in a sorted array that is larger than a given value. It has the same interface as https://docs.python.org/3/li
searches/binary_search.py:59
↓ 1 callersFunctionbisection
finds where function becomes 0 in [a,b] using bolzano >>> bisection(lambda x: x ** 3 - 1, -5, 5) 1.0000000149011612 >>> bisection(lam
maths/numerical_analysis/bisection.py:4
↓ 1 callersFunctionbitonic_sort
This function first produces a bitonic sequence by recursively sorting its two halves in opposite sorting orders, and then calls bitonic_merg
sorts/bitonic_sort.py:65
↓ 1 callersMethodblack_height
Returns the number of black nodes from this node to the leaves of the tree, or None if there isn't one such value (the tree is color
data_structures/binary_tree/red_black_tree.py:321
↓ 1 callersFunctionbogo_sort
Pure implementation of the bogosort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items
sorts/bogo_sort.py:19
↓ 1 callersMethodbottom_to_top
(self, val, index, heap, position)
graphs/minimum_spanning_tree_prims.py:42
↓ 1 callersFunctionbottom_up_cut_rod
Constructs a bottom-up dynamic programming solution for the rod-cutting problem Runtime: O(n^2) Arguments --------- * `n`: int
dynamic_programming/rod_cutting.py:133
↓ 1 callersFunctionbreadth_first_search
This function returns True if there is a node that has not iterated. Args: graph: Adjacency matrix of graph source: Source
networking_flow/ford_fulkerson.py:20
↓ 1 callersFunctionbreadth_first_search
sets every nodes direct parent parent of root node is set to 0 calculates depth of each node from root node >>> level = [-1] * 7
data_structures/binary_tree/lowest_common_ancestor.py:94
↓ 1 callersMethodbreath_first_search
This function is a helper for running breath first search on this graph. >>> g = Graph(graph, "G") >>> g.breath_first_search(
graphs/breadth_first_search_shortest_path.py:29
↓ 1 callersFunctionbrute_force
brute_force =========== Returns all the possible combinations of keys and the decoded strings in the form of a dictionary Param
ciphers/caesar_cipher.py:163
↓ 1 callersFunctionbubble_sort_iterative
Pure implementation of bubble sort algorithm in Python :param collection: some mutable ordered collection with heterogeneous comparable items
sorts/bubble_sort.py:4
↓ 1 callersFunctionbubble_sort_recursive
It is similar iterative bubble sort but recursive. :param collection: mutable ordered sequence of elements :return: the same list in ascendin
sorts/bubble_sort.py:63
↓ 1 callersMethodbuild
(self)
neural_network/back_propagation_neural_network.py:113
↓ 1 callersMethodbuild
Builds a graph from the given set of vertices and edges
graphs/minimum_spanning_tree_boruvka.py:85
↓ 1 callersMethodbuild
(self)
data_structures/binary_tree/non_recursive_segment_tree.py:67
↓ 1 callersMethodbuild
(self, idx, left, right)
data_structures/binary_tree/segment_tree.py:38
↓ 1 callersMethodbuild
( self, idx: int, left_element: int, right_element: int, a: list[int] )
data_structures/binary_tree/lazy_segment_tree.py:39
↓ 1 callersMethodbuild_a_sum_tree
r""" Create a binary tree with the specified structure: 26 / \ 10 3 / \ \ 4 6
data_structures/binary_tree/is_sum_tree.py:134
↓ 1 callersMethodbuild_a_tree
r""" Create a binary tree with the specified structure: 11 / \ 2 29 / \ / \
data_structures/binary_tree/is_sum_tree.py:109
↓ 1 callersMethodbuild_heap
(self, array)
data_structures/heap/min_heap.py:54
↓ 1 callersFunctionbuild_sparse_table
Precompute range minimum queries with power of two length and store the precomputed values in a table. >>> build_sparse_table([8, 1, 0,
data_structures/arrays/sparse_table.py:17
↓ 1 callersMethodbuild_suffix_tree
Builds the suffix tree for the given text by adding all suffixes.
data_structures/suffix_tree/suffix_tree.py:24
↓ 1 callersFunctionbuild_tree
Run through the list of Letters and build the min heap for the Huffman Tree.
data_compression/huffman.py:38
↓ 1 callersFunctionbuild_tree
()
searches/binary_tree_traversal.py:17
↓ 1 callersFunctionbuild_tree
()
data_structures/binary_tree/serialize_deserialize_binary_tree.py:122
← previousnext →801–900 of 3,909, ranked by callers