MCPcopy Create free account

hub / github.com/TheAlgorithms/Python / functions

Functions3,909 in github.com/TheAlgorithms/Python

Functionfib_binet
Calculates the first n (0-indexed) Fibonacci numbers using a simplified form of Binet's formula: https://en.m.wikipedia.org/wiki/Fibonacc
maths/fibonacci.py:201
Functionfib_iterative
Calculates the first n (0-indexed) Fibonacci numbers using iteration >>> fib_iterative(0) [0] >>> fib_iterative(1) [0, 1] >>>
maths/fibonacci.py:65
Functionfib_iterative_yield
Calculates the first n (1-indexed) Fibonacci numbers using iteration with yield >>> list(fib_iterative_yield(0)) [0] >>> tuple(fib_it
maths/fibonacci.py:40
Functionfib_matrix_np
Calculates the n-th Fibonacci number using matrix exponentiation. https://www.nayuki.io/page/fast-fibonacci-algorithms#:~:text= Summary:%
maths/fibonacci.py:282
Functionfib_memoization
Calculates the first n (0-indexed) Fibonacci numbers using memoization >>> fib_memoization(0) [0] >>> fib_memoization(1) [0, 1]
maths/fibonacci.py:168
Functionfib_recursive
Calculates the first n (0-indexed) Fibonacci numbers using recursion >>> fib_recursive(0) [0] >>> fib_recursive(1) [0, 1] >>>
maths/fibonacci.py:91
Functionfib_recursive_cached
Calculates the first n (0-indexed) Fibonacci numbers using recursion >>> fib_recursive_cached(0) [0] >>> fib_recursive_cached(1)
maths/fibonacci.py:135
Functionfibonacci_search
A pure Python implementation of a fibonacci search algorithm. Parameters ---------- arr List of sorted elements. val
searches/fibonacci_search.py:58
Functionfibonacci_with_matrix_exponentiation
Returns the nth number of the Fibonacci sequence that starts with f1 and f2 Uses the matrix exponentiation >>> fibonacci_with_matrix_
maths/matrix_exponentiation.py:41
Methodfill_graph_randomly
(self, c=-1)
graphs/directed_and_undirected_weighted_graph.py:76
Methodfill_graph_randomly
(self, c=-1)
graphs/directed_and_undirected_weighted_graph.py:342
Methodfind
Returns the smallest index where the number is. >>> cont = NumberContainer() >>> cont.find(10) -1 >>> cont.c
other/number_container_system.py:157
Functionfind_isolated_nodes
Find the isolated node in the graph Parameters: graph (dict): A dictionary representing a graph. Returns: list: A list of isola
graphs/basic_graphs.py:374
Methodfind_kth_smallest
Return the kth smallest element in a binary search tree
data_structures/binary_tree/binary_search_tree.py:326
Functionfind_lanczos_eigenvectors
Computes the largest eigenvalues and their corresponding eigenvectors using the Lanczos method. Args: graph: The graph as a list of a
graphs/lanczos_eigenvectors.py:165
Functionfind_max_iterative
>>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]): ... find_max_iterative(nums) == max(nums) True True T
maths/find_max.py:4
Functionfind_max_recursive
find max value in list :param nums: contains elements :param left: index of first element :param right: index of last element :re
maths/find_max.py:29
Functionfind_median_sorted_arrays
Find the median of two arrays. Args: nums1: The first array. nums2: The second array. Returns: The median of the tw
data_structures/arrays/median_two_array.py:6
Functionfind_min
>>> find_min([1, 2, 3, 4, 5]) 1 >>> find_min([5, 5, 5, 5, 5]) 5 >>> find_min([5, 5, 5, 5]) 0 >>> find_min([3]) 3
dynamic_programming/minimum_partition.py:6
Functionfind_min_iterative
Find Minimum Number in a List :param nums: contains elements :return: min number in list >>> for nums in ([3, 2, 1], [-3, -2, -1], [
maths/find_min.py:4
Functionfind_min_recursive
find min value in list :param nums: contains elements :param left: index of first element :param right: index of last element :re
maths/find_min.py:32
Functionfind_missing_number
Finds the missing number in a list of consecutive integers. Args: nums: A list of integers. Returns: The missing number
bit_manipulation/missing_number.py:1
Functionfind_mod_inverse
(a: int, m: int)
ciphers/cryptomath_module.py:4
Functionfind_pattern
Example of using z-function for pattern occurrence Given function returns the number of times 'pattern' appears in 'input_str' as a subst
strings/z_function.py:59
Functionfind_previous_power_of_two
Find the largest power of two that is less than or equal to a given integer. https://stackoverflow.com/questions/1322510 >>> [find_previ
bit_manipulation/find_previous_power_of_two.py:1
Functionfind_triplets_with_0_sum
Given a list of integers, return elements a, b, c such that a + b + c = 0. Args: nums: list of integers Returns: list of
data_structures/arrays/find_triplets_with_0_sum.py:4
Functionfind_triplets_with_0_sum_hashing
Function for finding the triplets with a given sum in the array using hashing. Given a list of integers, return elements a, b, c such that a
data_structures/arrays/find_triplets_with_0_sum.py:27
Functionfind_unique_number
Given a list of integers where every element appears twice except for one, this function returns the element that appears only once using bit
bit_manipulation/find_unique_number.py:1
Functionfind_winner
Find the winner of the Josephus problem for num_people and a step_size. Args: num_people (int): Number of people. step_size
maths/josephus_problem.py:80
Methodfirst
Returns the first element of the queue >>> cq = CircularQueueLinkedList() >>> cq.first() Traceback (most recent call
data_structures/queues/circular_queue_linked_list.py:61
Methodfirst
Returns the first element of the queue >>> cq = CircularQueue(5) >>> cq.first() False >>> cq.enqueue("A").fir
data_structures/queues/circular_queue.py:39
Methodfirst
return first element >>> d = LinkedDeque() >>> d.add_first('A').first() 'A' >>> d.add_first('B').first() 'B'
data_structures/linked_list/deque_doubly.py:63
Functionfirst_effusion_rate
Input Parameters: ----------------- effusion_rate: Effustion rate of second gas (m^2/s, mm^2/s, etc.) molar_mass_1: Molar mass of the
physics/grahams_law.py:67
Functionfirst_molar_mass
Input Parameters: ----------------- molar_mass: Molar mass of the first gas (g/mol, kg/kmol, etc.) effusion_rate_1: Effustion rate of
physics/grahams_law.py:139
Methodfive_tree
>>> repr(TreeNode.five_tree()) '1,2,null,null,3,4,null,null,5,null,null'
data_structures/binary_tree/serialize_deserialize_binary_tree.py:73
Functionfizz_buzz
| Plays FizzBuzz. | Prints Fizz if number is a multiple of ``3``. | Prints Buzz if its a multiple of ``5``. | Prints FizzBuzz if its
dynamic_programming/fizz_buzz.py:4
Functionfletcher16
Loop through every character in the data and add to two sums. >>> fletcher16('hello world') 6752 >>> fletcher16('onethousandfou
hashes/fletcher16.py:13
Functionflip_bit
Flip the bit at position. Details: perform bitwise xor for given number and X. Where X is a number with all the bits - zeroes and bit on
bit_manipulation/single_bit_manipulation_operations.py:40
Functionfloor_ceiling
Find the floor and ceiling values for a given key in a Binary Search Tree (BST). Args: root: The root of the binary search tree.
data_structures/binary_tree/floor_and_ceiling.py:36
Functionfloy
(a_and_n)
graphs/basic_graphs.py:259
Functionfocal_length
>>> from math import isclose >>> isclose(focal_length(10, 20), 6.66666666666666) True >>> from math import isclose >>> isclose(fo
physics/mirror_formulae.py:61
Functionfocal_length_of_lens
Doctests: >>> from math import isclose >>> isclose(focal_length_of_lens(10,4), 6.666666666666667) True >>> from math import isclo
physics/lens_formulae.py:51
Methodformat_hash
(self, value: str)
data_structures/hashing/bloom_filter.py:100
Functionfrac_knapsack
>>> frac_knapsack([60, 100, 120], [10, 20, 30], 50, 3) 240.0 >>> frac_knapsack([10, 40, 30, 50], [5, 4, 6, 3], 10, 4) 105.0 >>> f
greedy_methods/fractional_knapsack.py:5
Functionfractional_cover
Solve the Fractional Cover Problem. Args: items: A list of items, where each item has weight and value attributes. capacity:
greedy_methods/fractional_cover_problem.py:33
Functionfractional_knapsack
>>> value = [1, 3, 5, 7, 9] >>> weight = [0.9, 0.7, 0.5, 0.3, 0.1] >>> fractional_knapsack(value, weight, 5) (25, [1, 1, 1, 1, 1])
greedy_methods/fractional_knapsack_2.py:8
Functionfrom_file
Parse a file into a list of strings, separated by sep.
data_structures/arrays/sudoku_solver.py:207
Methodfront
(self)
data_structures/queues/queue_on_pseudo_stack.py:50
Methodfullname
(self)
web_programming/instagram_crawler.py:70
Functiongaussian
>>> float(gaussian(1)) 0.24197072451914337 >>> float(gaussian(24)) 3.342714441794458e-126 >>> float(gaussian(1, 4, 2)) 0.06
maths/gaussian.py:8
Functiongaussian_elimination
This function performs Gaussian elimination method Examples: 1. * 1x1 - 4x2 - 2x3 = -2 * 5x1 + 2x2 - 2x3 = -
linear_algebra/gaussian_elimination.py:46
Functiongaussian_error_linear_unit
Implements the Gaussian Error Linear Unit (GELU) function Parameters: vector (np.ndarray): A numpy array of shape (1, n) consisting
neural_network/activation_functions/gaussian_error_linear_unit.py:28
Methodgbndvi
self.green-self.blue NDVI https://www.indexdatabase.de/db/i-single.php?id=186 :return: index
digital_image_processing/index_calculation.py:251
Methodgdvi
Difference self.nir/self.green self.green Difference Vegetation Index https://www.indexdatabase.de/db/i-single.php?id=27 :ret
digital_image_processing/index_calculation.py:341
Methodgemi
Global Environment Monitoring Index https://www.indexdatabase.de/db/i-single.php?id=25 :return: index
digital_image_processing/index_calculation.py:359
Functiongenerate_parenthesis
Generate valid combinations of balanced parentheses for a given n. :param n: An integer representing the number of pairs of parentheses.
backtracking/generate_parentheses.py:48
Functiongenerate_pascal_triangle_optimized
This function returns a matrix representing the corresponding pascal's triangle according to the given input of number of rows of Pascal's tr
matrix/pascal_triangle.py:111
Methodgenerate_public_key
(self)
ciphers/diffie_hellman.py:225
Functiongenerate_random_hands
(number_of_hands: int = 100)
project_euler/problem_054/test_poker_hand.py:146
Methodgenerate_shared_key
(self, other_key_str: str)
ciphers/diffie_hellman.py:236
Methodgenerate_shared_key_static
( local_private_key_str: str, remote_public_key_str: str, group: int = 14 )
ciphers/diffie_hellman.py:252
Functiongenerate_sum_of_subsets_solutions
The main function. For list of numbers 'nums' find the subsets with sum equal to 'max_sum' >>> generate_sum_of_subsets_solutions(nums=[3
backtracking/sum_of_subsets.py:11
Functiongeometric_mean
return the geometric mean of series >>> geometric_mean([2, 4, 8]) 3.9999999999999996 >>> geometric_mean([3, 6, 12, 24]) 8.485281
maths/series/geometric.py:46
Methodget
Get `item` from the Queue >>> queue = QueueByTwoStacks((10, 20, 30)) >>> queue.get() 10 >>> queue.put(40)
data_structures/queues/queue_by_two_stacks.py:74
Methodget
Get `item` from the Queue >>> queue = QueueByList((10, 20, 30)) >>> queue.get() 10 >>> queue.put(40)
data_structures/queues/queue_by_list.py:72
Functionget_1s_count
Count the number of set bits in a 32 bit integer using Brian Kernighan's way. Ref - https://graphics.stanford.edu/~seander/bithacks.html#Coun
bit_manipulation/count_1s_brian_kernighan_method.py:1
Functionget_altitude_at_pressure
This method calculates the altitude from Pressure wrt to Sea level pressure as reference .Pressure is in Pascals https://en.wikipedia.org
physics/altitude_pressure.py:11
Methodget_array
Get the Normal Array of the Fenwick tree in O(N) Returns: list: Normal Array of the Fenwick tree >>> a = [i for
data_structures/binary_tree/fenwick_tree.py:53
Functionget_bit
Get the bit at the given position Details: perform bitwise and for the given number and X, Where X is a number with all the bits - zeroe
bit_manipulation/single_bit_manipulation_operations.py:77
Methodget_cofactor
(self, row: int, column: int)
matrix/matrix_class.py:190
Functionget_factors
this is a recursive function for get all factors of number >>> get_factors(45) Counter({3: 2, 5: 1}) >>> get_factors(2520) Counte
maths/gcd_of_n_numbers.py:9
Methodget_front
Get the front item from the Queue >>> queue = QueueByList((10, 20, 30)) >>> queue.get_front() 10 >>> queue Qu
data_structures/queues/queue_by_list.py:118
Functionget_gas_stations
This function returns a tuple of gas stations. Args: gas_quantities: Amount of gas available at each station costs: The cost
greedy_methods/gas_station.py:36
Methodget_head_data
(self)
data_structures/linked_list/doubly_linked_list_two.py:68
Functionget_highest_set_bit_position
Returns position of the highest set bit of a number. Ref - https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious >>> get
bit_manipulation/highest_set_bit.py:1
Functionget_index_of_rightmost_set_bit
Take in a positive integer 'number'. Returns the zero-based index of first set bit in that 'number' from right. Returns -1, If no set bit
bit_manipulation/index_of_rightmost_set_bit.py:4
Functionget_item_at_index_zero
(x: tuple)
strings/frequency_finder.py:47
Methodget_list
(self)
data_structures/heap/max_heap.py:63
Methodget_maximum_flow
(self)
graphs/edmonds_karp_multiple_source_and_sink.py:87
Methodget_min
We go deep on the left branch >>> BinarySearchTree().insert(10, 20, 30, 40, 50).get_min() {'10': (None, {'20': (None, {'30':
data_structures/binary_tree/binary_search_tree.py:257
Methodget_name
(self)
other/greedy.py:13
Functionget_prime
Gets the n-th prime number. input: positive integer 'n' >= 0 returns the n-th prime number, beginning at index 0 >>> get_prime(0)
maths/primelib.py:538
Functionget_primes_between
input: prime numbers 'pNumber1' and 'pNumber2' pNumber1 < pNumber2 returns a list of all prime numbers between 'pNumber1' (exclus
maths/primelib.py:587
Methodget_private_key
(self)
ciphers/diffie_hellman.py:222
Functionget_random_key
()
ciphers/simple_substitution_cipher.py:71
Functionget_random_key
()
ciphers/affine_cipher.py:77
Functionget_reverse_bit_string
Return the reverse bit string of a 32 bit integer >>> get_reverse_bit_string(9) '10010000000000000000000000000000' >>> get_reverse_b
bit_manipulation/reverse_bits.py:1
Functionget_right_most
(root: MyNode)
data_structures/binary_tree/avl_tree.py:180
Methodget_shortest_path
Return the shortest distance from start_vertex to finish_vertex in 0-1-graph. 1 1 1 0--------
graphs/breadth_first_search_zero_one_shortest_path.py:63
Methodget_side
>>> Polygon().get_side(0) Traceback (most recent call last): ... IndexError: list index out of range >>>
geometry/geometry.py:202
Methodget_sum
The function returns the sum of array from the start to the end indexes. Runtime : O(1) Space: O(1) >>> PrefixSum([1
data_structures/arrays/prefix_sum.py:21
Methodget_tail_data
(self)
data_structures/linked_list/doubly_linked_list_two.py:73
Functionget_transitions
Running Markov Chain algorithm and calculating the number of times each node is visited >>> transitions = [ ... ('a', 'a', 0.9),
graphs/markov_chain.py:41
Functionget_week_day
Returns the week-day name out of a given date. >>> get_week_day(2020, 10, 24) 'Saturday' >>> get_week_day(2017, 10, 24) 'Tuesday'
other/doomsday.py:17
Methodgli
self.green leaf index https://www.indexdatabase.de/db/i-single.php?id=375 :return: index
digital_image_processing/index_calculation.py:207
Methodgndvi
Normalized Difference self.nir/self.green self.green NDVI https://www.indexdatabase.de/db/i-single.php?id=401 :return: index
digital_image_processing/index_calculation.py:243
Functiongoldbach
Goldbach's assumption input: a even positive integer 'number' > 2 returns a list of two prime numbers whose sum is equal to 'number'
maths/primelib.py:377
Methodgosavi
self.green Optimized Soil Adjusted Vegetation Index https://www.indexdatabase.de/db/i-single.php?id=29 mit Y = 0,16 :
digital_image_processing/index_calculation.py:370
Functiongraham_scan
Pure implementation of graham scan algorithm in Python :param points: The unique points on coordinates. :return: The points on convex hell.
other/graham_scan.py:93
Methodgrandparent
Get the current node's grandparent, or None if it doesn't exist.
data_structures/binary_tree/red_black_tree.py:423
← previousnext →3,101–3,200 of 3,909, ranked by callers