Skip to content

Commit 58d3a8d

Browse files
committed
Fix: Issue TheAlgorithms#9588
2 parents 115703e + 2b096d9 commit 58d3a8d

File tree

5 files changed

+190
-1
lines changed

5 files changed

+190
-1
lines changed

DIRECTORY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,6 @@
639639
* [Quadratic Equations Complex Numbers](maths/quadratic_equations_complex_numbers.py)
640640
* [Radians](maths/radians.py)
641641
* [Radix2 Fft](maths/radix2_fft.py)
642-
* [Relu](maths/relu.py)
643642
* [Remove Digit](maths/remove_digit.py)
644643
* [Runge Kutta](maths/runge_kutta.py)
645644
* [Segmented Sieve](maths/segmented_sieve.py)
@@ -710,6 +709,7 @@
710709
* [Exponential Linear Unit](neural_network/activation_functions/exponential_linear_unit.py)
711710
* [Leaky Rectified Linear Unit](neural_network/activation_functions/leaky_rectified_linear_unit.py)
712711
* [Scaled Exponential Linear Unit](neural_network/activation_functions/scaled_exponential_linear_unit.py)
712+
* [Rectified Linear Unit](neural_network/activation_functions/rectified_linear_unit.py)
713713
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
714714
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
715715
* [Perceptron](neural_network/perceptron.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
author: Sanket Kittad
3+
Given a string s, find the longest palindromic subsequence's length in s.
4+
Input: s = "bbbab"
5+
Output: 4
6+
Explanation: One possible longest palindromic subsequence is "bbbb".
7+
Leetcode link: https://leetcode.com/problems/longest-palindromic-subsequence/description/
8+
"""
9+
10+
11+
def longest_palindromic_subsequence(input_string: str) -> int:
12+
"""
13+
This function returns the longest palindromic subsequence in a string
14+
>>> longest_palindromic_subsequence("bbbab")
15+
4
16+
>>> longest_palindromic_subsequence("bbabcbcab")
17+
7
18+
"""
19+
n = len(input_string)
20+
rev = input_string[::-1]
21+
m = len(rev)
22+
dp = [[-1] * (m + 1) for i in range(n + 1)]
23+
for i in range(n + 1):
24+
dp[i][0] = 0
25+
for i in range(m + 1):
26+
dp[0][i] = 0
27+
28+
# create and initialise dp array
29+
for i in range(1, n + 1):
30+
for j in range(1, m + 1):
31+
# If characters at i and j are the same
32+
# include them in the palindromic subsequence
33+
if input_string[i - 1] == rev[j - 1]:
34+
dp[i][j] = 1 + dp[i - 1][j - 1]
35+
else:
36+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
37+
38+
return dp[n][m]
39+
40+
41+
if __name__ == "__main__":
42+
import doctest
43+
44+
doctest.testmod()

maths/bell_numbers.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Bell numbers represent the number of ways to partition a set into non-empty
3+
subsets. This module provides functions to calculate Bell numbers for sets of
4+
integers. In other words, the first (n + 1) Bell numbers.
5+
6+
For more information about Bell numbers, refer to:
7+
https://en.wikipedia.org/wiki/Bell_number
8+
"""
9+
10+
11+
def bell_numbers(max_set_length: int) -> list[int]:
12+
"""
13+
Calculate Bell numbers for the sets of lengths from 0 to max_set_length.
14+
In other words, calculate first (max_set_length + 1) Bell numbers.
15+
16+
Args:
17+
max_set_length (int): The maximum length of the sets for which
18+
Bell numbers are calculated.
19+
20+
Returns:
21+
list: A list of Bell numbers for sets of lengths from 0 to max_set_length.
22+
23+
Examples:
24+
>>> bell_numbers(0)
25+
[1]
26+
>>> bell_numbers(1)
27+
[1, 1]
28+
>>> bell_numbers(5)
29+
[1, 1, 2, 5, 15, 52]
30+
"""
31+
if max_set_length < 0:
32+
raise ValueError("max_set_length must be non-negative")
33+
34+
bell = [0] * (max_set_length + 1)
35+
bell[0] = 1
36+
37+
for i in range(1, max_set_length + 1):
38+
for j in range(i):
39+
bell[i] += _binomial_coefficient(i - 1, j) * bell[j]
40+
41+
return bell
42+
43+
44+
def _binomial_coefficient(total_elements: int, elements_to_choose: int) -> int:
45+
"""
46+
Calculate the binomial coefficient C(total_elements, elements_to_choose)
47+
48+
Args:
49+
total_elements (int): The total number of elements.
50+
elements_to_choose (int): The number of elements to choose.
51+
52+
Returns:
53+
int: The binomial coefficient C(total_elements, elements_to_choose).
54+
55+
Examples:
56+
>>> _binomial_coefficient(5, 2)
57+
10
58+
>>> _binomial_coefficient(6, 3)
59+
20
60+
"""
61+
if elements_to_choose in {0, total_elements}:
62+
return 1
63+
64+
if elements_to_choose > total_elements - elements_to_choose:
65+
elements_to_choose = total_elements - elements_to_choose
66+
67+
coefficient = 1
68+
for i in range(elements_to_choose):
69+
coefficient *= total_elements - i
70+
coefficient //= i + 1
71+
72+
return coefficient
73+
74+
75+
if __name__ == "__main__":
76+
import doctest
77+
78+
doctest.testmod()
File renamed without changes.

physics/photoelectric_effect.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
The photoelectric effect is the emission of electrons when electromagnetic radiation ,
3+
such as light, hits a material. Electrons emitted in this manner are called
4+
photoelectrons.
5+
6+
In 1905, Einstein proposed a theory of the photoelectric effect using a concept that
7+
light consists of tiny packets of energy known as photons or light quanta. Each packet
8+
carries energy hv that is proportional to the frequency v of the corresponding
9+
electromagnetic wave. The proportionality constant h has become known as the
10+
Planck constant. In the range of kinetic energies of the electrons that are removed from
11+
their varying atomic bindings by the absorption of a photon of energy hv, the highest
12+
kinetic energy K_max is :
13+
14+
K_max = hv-W
15+
16+
Here, W is the minimum energy required to remove an electron from the surface of the
17+
material. It is called the work function of the surface
18+
19+
Reference: https://en.wikipedia.org/wiki/Photoelectric_effect
20+
21+
"""
22+
23+
PLANCK_CONSTANT_JS = 6.6261 * pow(10, -34) # in SI (Js)
24+
PLANCK_CONSTANT_EVS = 4.1357 * pow(10, -15) # in eVs
25+
26+
27+
def maximum_kinetic_energy(
28+
frequency: float, work_function: float, in_ev: bool = False
29+
) -> float:
30+
"""
31+
Calculates the maximum kinetic energy of emitted electron from the surface.
32+
if the maximum kinetic energy is zero then no electron will be emitted
33+
or given electromagnetic wave frequency is small.
34+
35+
frequency (float): Frequency of electromagnetic wave.
36+
work_function (float): Work function of the surface.
37+
in_ev (optional)(bool): Pass True if values are in eV.
38+
39+
Usage example:
40+
>>> maximum_kinetic_energy(1000000,2)
41+
0
42+
>>> maximum_kinetic_energy(1000000,2,True)
43+
0
44+
>>> maximum_kinetic_energy(10000000000000000,2,True)
45+
39.357000000000006
46+
>>> maximum_kinetic_energy(-9,20)
47+
Traceback (most recent call last):
48+
...
49+
ValueError: Frequency can't be negative.
50+
51+
>>> maximum_kinetic_energy(1000,"a")
52+
Traceback (most recent call last):
53+
...
54+
TypeError: unsupported operand type(s) for -: 'float' and 'str'
55+
56+
"""
57+
if frequency < 0:
58+
raise ValueError("Frequency can't be negative.")
59+
if in_ev:
60+
return max(PLANCK_CONSTANT_EVS * frequency - work_function, 0)
61+
return max(PLANCK_CONSTANT_JS * frequency - work_function, 0)
62+
63+
64+
if __name__ == "__main__":
65+
import doctest
66+
67+
doctest.testmod()

0 commit comments

Comments
 (0)