Skip to content

Commit 1a1ffbd

Browse files
authored
Merge branch 'master' into add-point-in-polygon
2 parents 2e1a484 + 3f8772c commit 1a1ffbd

57 files changed

Lines changed: 4663 additions & 118 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

DIRECTORY.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@
9797
* [Is Power Of Two](bit_manipulation/is_power_of_two.py)
9898
* [Largest Pow Of Two Le Num](bit_manipulation/largest_pow_of_two_le_num.py)
9999
* [Missing Number](bit_manipulation/missing_number.py)
100+
* [Next Power Of Two](bit_manipulation/next_power_of_two.py)
100101
* [Numbers Different Signs](bit_manipulation/numbers_different_signs.py)
102+
* [Parity](bit_manipulation/parity.py)
101103
* [Power Of 4](bit_manipulation/power_of_4.py)
102104
* [Reverse Bits](bit_manipulation/reverse_bits.py)
105+
* [Rotate Bits](bit_manipulation/rotate_bits.py)
103106
* [Single Bit Manipulation Operations](bit_manipulation/single_bit_manipulation_operations.py)
104107
* [Swap All Odd And Even Bits](bit_manipulation/swap_all_odd_and_even_bits.py)
105108

@@ -519,6 +522,7 @@
519522

520523
## [Fractals](fractals)
521524
* [Barnsley Fern](fractals/barnsley_fern.py)
525+
* [Hilbert Curve](fractals/hilbert_curve.py)
522526
* [Julia Sets](fractals/julia_sets.py)
523527
* [Koch Snowflake](fractals/koch_snowflake.py)
524528
* [Mandelbrot](fractals/mandelbrot.py)
@@ -545,6 +549,7 @@
545549
* [Ramer Douglas Peucker](geometry/ramer_douglas_peucker.py)
546550
* [Rotating Calipers](geometry/rotating_calipers.py)
547551
* [Segment Intersection](geometry/segment_intersection.py)
552+
* [Shoelace](geometry/shoelace.py)
548553
* Tests
549554
* [Test Graham Scan](geometry/tests/test_graham_scan.py)
550555
* [Test Jarvis March](geometry/tests/test_jarvis_march.py)
@@ -690,13 +695,14 @@
690695
* Forecasting
691696
* [Run](machine_learning/forecasting/run.py)
692697
* [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py)
698+
* [Gaussian Mixture Model](machine_learning/gaussian_mixture_model.py)
693699
* [Gaussian Naive Bayes](machine_learning/gaussian_naive_bayes.py)
694700
* [Gradient Boosting Classifier](machine_learning/gradient_boosting_classifier.py)
695701
* [Gradient Boosting Regressor](machine_learning/gradient_boosting_regressor.py)
696702
* [Gradient Descent](machine_learning/gradient_descent.py)
697703
* [K Means Clust](machine_learning/k_means_clust.py)
698704
* [K Medoids](machine_learning/k_medoids.py)
699-
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
705+
* [K Nearest Neighbors](machine_learning/k_nearest_neighbors.py)
700706
* [Linear Discriminant Analysis](machine_learning/linear_discriminant_analysis.py)
701707
* [Linear Regression](machine_learning/linear_regression.py)
702708
* [Linear Regression Vectorized](machine_learning/linear_regression_vectorized.py)
@@ -707,9 +713,11 @@
707713
* Lstm
708714
* [Lstm Prediction](machine_learning/lstm/lstm_prediction.py)
709715
* [Mfcc](machine_learning/mfcc.py)
716+
* [Mini Batch Gradient Descent](machine_learning/mini_batch_gradient_descent.py)
710717
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
711718
* [Polynomial Regression](machine_learning/polynomial_regression.py)
712719
* [Principle Component Analysis](machine_learning/principle_component_analysis.py)
720+
* [Q Learning](machine_learning/q_learning.py)
713721
* [Random Forest Classifier](machine_learning/random_forest_classifier.py)
714722
* [Random Forest Regressor](machine_learning/random_forest_regressor.py)
715723
* [Scoring Functions](machine_learning/scoring_functions.py)
@@ -735,10 +743,12 @@
735743
* [Bailey Borwein Plouffe](maths/bailey_borwein_plouffe.py)
736744
* [Base Neg2 Conversion](maths/base_neg2_conversion.py)
737745
* [Basic Maths](maths/basic_maths.py)
746+
* [Bearing](maths/bearing.py)
738747
* [Binary Exponentiation](maths/binary_exponentiation.py)
739748
* [Binary Multiplication](maths/binary_multiplication.py)
740749
* [Binomial Coefficient](maths/binomial_coefficient.py)
741750
* [Binomial Distribution](maths/binomial_distribution.py)
751+
* [Binomial Expansion](maths/binomial_expansion.py)
742752
* [Ceil](maths/ceil.py)
743753
* [Chebyshev Distance](maths/chebyshev_distance.py)
744754
* [Check Polygon](maths/check_polygon.py)
@@ -753,9 +763,11 @@
753763
* [Decimal To Fraction](maths/decimal_to_fraction.py)
754764
* [Derangement](maths/derangement.py)
755765
* [Digital Root](maths/digital_root.py)
766+
* [Disarium](maths/disarium.py)
756767
* [Dodecahedron](maths/dodecahedron.py)
757768
* [Double Factorial](maths/double_factorial.py)
758769
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)
770+
* [Ear Clipping Polygon Triangulation](maths/ear_clipping_polygon_triangulation.py)
759771
* [Entropy](maths/entropy.py)
760772
* [Euclidean Distance](maths/euclidean_distance.py)
761773
* [Euler Method](maths/euler_method.py)
@@ -781,6 +793,7 @@
781793
* [Interquartile Range](maths/interquartile_range.py)
782794
* [Is Int Palindrome](maths/is_int_palindrome.py)
783795
* [Is Ip V4 Address Valid](maths/is_ip_v4_address_valid.py)
796+
* [Is Ipv6 Address Valid](maths/is_ipv6_address_valid.py)
784797
* [Is Square Free](maths/is_square_free.py)
785798
* [Jaccard Similarity](maths/jaccard_similarity.py)
786799
* [Joint Probability Distribution](maths/joint_probability_distribution.py)
@@ -806,12 +819,15 @@
806819
* [Modular Exponential](maths/modular_exponential.py)
807820
* [Monte Carlo](maths/monte_carlo.py)
808821
* [Monte Carlo Dice](maths/monte_carlo_dice.py)
822+
* [Ncr Combinations](maths/ncr_combinations.py)
823+
* [Next Prime Number](maths/next_prime_number.py)
809824
* [Number Of Digits](maths/number_of_digits.py)
810825
* Numerical Analysis
811826
* [Adams Bashforth](maths/numerical_analysis/adams_bashforth.py)
812827
* [Bisection](maths/numerical_analysis/bisection.py)
813828
* [Bisection 2](maths/numerical_analysis/bisection_2.py)
814829
* [Brent Method](maths/numerical_analysis/brent_method.py)
830+
* [Gauss Seidel Method](maths/numerical_analysis/gauss_seidel_method.py)
815831
* [Integration By Simpson Approx](maths/numerical_analysis/integration_by_simpson_approx.py)
816832
* [Intersection](maths/numerical_analysis/intersection.py)
817833
* [Nevilles Method](maths/numerical_analysis/nevilles_method.py)
@@ -853,6 +869,7 @@
853869
* [Radix2 Fft](maths/radix2_fft.py)
854870
* [Recursive Digit Sum](maths/recursive_digit_sum.py)
855871
* [Remove Digit](maths/remove_digit.py)
872+
* [Reverse Factorial Recursive](maths/reverse_factorial_recursive.py)
856873
* [Segmented Sieve](maths/segmented_sieve.py)
857874
* Series
858875
* [Arithmetic](maths/series/arithmetic.py)
@@ -863,6 +880,7 @@
863880
* [Hexagonal Numbers](maths/series/hexagonal_numbers.py)
864881
* [Logarithmic Series](maths/series/logarithmic_series.py)
865882
* [P Series](maths/series/p_series.py)
883+
* [Shoelace Area](maths/shoelace_area.py)
866884
* [Sieve Of Atkin](maths/sieve_of_atkin.py)
867885
* [Sieve Of Eratosthenes](maths/sieve_of_eratosthenes.py)
868886
* [Sigmoid](maths/sigmoid.py)
@@ -874,11 +892,14 @@
874892
* [Solovay Strassen Primality Test](maths/solovay_strassen_primality_test.py)
875893
* [Spearman Rank Correlation Coefficient](maths/spearman_rank_correlation_coefficient.py)
876894
* Special Numbers
895+
* [Abundant Numbers](maths/special_numbers/abundant_numbers.py)
877896
* [Armstrong Numbers](maths/special_numbers/armstrong_numbers.py)
878897
* [Automorphic Number](maths/special_numbers/automorphic_number.py)
879898
* [Bell Numbers](maths/special_numbers/bell_numbers.py)
880899
* [Carmichael Number](maths/special_numbers/carmichael_number.py)
881900
* [Catalan Number](maths/special_numbers/catalan_number.py)
901+
* [Deficient Numbers](maths/special_numbers/deficient_numbers.py)
902+
* [Disarum Number](maths/special_numbers/disarum_number.py)
882903
* [Hamming Numbers](maths/special_numbers/hamming_numbers.py)
883904
* [Happy Number](maths/special_numbers/happy_number.py)
884905
* [Harshad Numbers](maths/special_numbers/harshad_numbers.py)
@@ -891,6 +912,7 @@
891912
* [Pronic Number](maths/special_numbers/pronic_number.py)
892913
* [Proth Number](maths/special_numbers/proth_number.py)
893914
* [Triangular Numbers](maths/special_numbers/triangular_numbers.py)
915+
* [Trimorphic Number](maths/special_numbers/trimorphic_number.py)
894916
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
895917
* [Weird Number](maths/special_numbers/weird_number.py)
896918
* [Sum Of Arithmetic Series](maths/sum_of_arithmetic_series.py)
@@ -900,6 +922,7 @@
900922
* [Sumset](maths/sumset.py)
901923
* [Sylvester Sequence](maths/sylvester_sequence.py)
902924
* [Tanh](maths/tanh.py)
925+
* [Test Abs](maths/test_abs.py)
903926
* [Test Factorial](maths/test_factorial.py)
904927
* [Test Prime Check](maths/test_prime_check.py)
905928
* [Three Sum](maths/three_sum.py)
@@ -1005,21 +1028,28 @@
10051028
* [Altitude Pressure](physics/altitude_pressure.py)
10061029
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
10071030
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
1031+
* [Bragg Angle](physics/bragg_angle.py)
10081032
* [Casimir Effect](physics/casimir_effect.py)
10091033
* [Center Of Mass](physics/center_of_mass.py)
10101034
* [Centripetal Force](physics/centripetal_force.py)
10111035
* [Coulombs Law](physics/coulombs_law.py)
10121036
* [Doppler Frequency](physics/doppler_frequency.py)
10131037
* [Escape Velocity](physics/escape_velocity.py)
1038+
* [Faraday Lenz Law](physics/faraday_lenz_law.py)
1039+
* [First Law Of Thermodynamics](physics/first_law_of_thermodynamics.py)
1040+
* [Fresnel Diffract](physics/fresnel_diffract.py)
10141041
* [Grahams Law](physics/grahams_law.py)
10151042
* [Hamiltonian](physics/hamiltonian.py)
1043+
* [Hookes Law](physics/hookes_law.py)
10161044
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
10171045
* [Hubble Parameter](physics/hubble_parameter.py)
10181046
* [Ideal Gas Law](physics/ideal_gas_law.py)
10191047
* [In Static Equilibrium](physics/in_static_equilibrium.py)
10201048
* [Kinetic Energy](physics/kinetic_energy.py)
10211049
* [Lens Formulae](physics/lens_formulae.py)
1050+
* [Light Aberration](physics/light_aberration.py)
10221051
* [Lorentz Transformation Four Vector](physics/lorentz_transformation_four_vector.py)
1052+
* [Magnetic Flux](physics/magnetic_flux.py)
10231053
* [Malus Law](physics/malus_law.py)
10241054
* [Mass Energy Equivalence](physics/mass_energy_equivalence.py)
10251055
* [Maxwells Equations](physics/maxwells_equations.py)
@@ -1032,9 +1062,12 @@
10321062
* [Photoelectric Effect](physics/photoelectric_effect.py)
10331063
* [Potential Energy](physics/potential_energy.py)
10341064
* [Rainfall Intensity](physics/rainfall_intensity.py)
1065+
* [Relativistic Kinetic Energy](physics/relativistic_kinetic_energy.py)
1066+
* [Relativistic Velocity Summation](physics/relativistic_velocity_summation.py)
10351067
* [Reynolds Number](physics/reynolds_number.py)
10361068
* [Rms Speed Of Molecule](physics/rms_speed_of_molecule.py)
10371069
* [Shear Stress](physics/shear_stress.py)
1070+
* [Snells Law](physics/snells_law.py)
10381071
* [Speed Of Sound](physics/speed_of_sound.py)
10391072
* [Speeds Of Gas Molecules](physics/speeds_of_gas_molecules.py)
10401073
* [Terminal Velocity](physics/terminal_velocity.py)
@@ -1450,6 +1483,7 @@
14501483
* [Autocomplete Using Trie](strings/autocomplete_using_trie.py)
14511484
* [Barcode Validator](strings/barcode_validator.py)
14521485
* [Bitap String Match](strings/bitap_string_match.py)
1486+
* [Boyer Moore Horspool](strings/boyer_moore_horspool.py)
14531487
* [Boyer Moore Search](strings/boyer_moore_search.py)
14541488
* [Camel Case To Snake Case](strings/camel_case_to_snake_case.py)
14551489
* [Can String Be Rearranged As Palindrome](strings/can_string_be_rearranged_as_palindrome.py)

bit_manipulation/binary_and_operator.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
"""Bitwise AND helper.
2+
3+
Return a zero-padded binary string representing ``a & b`` where the width is the
4+
maximum bit length of the inputs. Only non-negative integers are accepted.
5+
6+
>>> binary_and(25, 32)
7+
'0b000000'
8+
>>> binary_and(37, 50)
9+
'0b100000'
10+
>>> binary_and(21, 30)
11+
'0b10100'
12+
>>> binary_and(58, 73)
13+
'0b0001000'
14+
>>> binary_and(0, 255)
15+
'0b00000000'
16+
>>> binary_and(256, 256)
17+
'0b100000000'
18+
19+
Invalid inputs raise clear exceptions:
20+
21+
>>> binary_and(0, -1)
22+
Traceback (most recent call last):
23+
...
24+
ValueError: inputs must be non-negative integers
25+
>>> binary_and(0, 1.1)
26+
Traceback (most recent call last):
27+
...
28+
TypeError: inputs must be integers
29+
>>> binary_and("0", "1")
30+
Traceback (most recent call last):
31+
...
32+
TypeError: inputs must be integers
33+
"""
34+
135
# https://www.tutorialspoint.com/python3/bitwise_operators_example.htm
236

337

@@ -22,28 +56,32 @@ def binary_and(a: int, b: int) -> str:
2256
>>> binary_and(0, -1)
2357
Traceback (most recent call last):
2458
...
25-
ValueError: the value of both inputs must be positive
59+
ValueError: inputs must be non-negative integers
2660
>>> binary_and(0, 1.1)
2761
Traceback (most recent call last):
2862
...
29-
ValueError: Unknown format code 'b' for object of type 'float'
63+
TypeError: inputs must be integers
3064
>>> binary_and("0", "1")
3165
Traceback (most recent call last):
3266
...
33-
TypeError: '<' not supported between instances of 'str' and 'int'
67+
TypeError: inputs must be integers
68+
>>> binary_and(10, "1")
69+
Traceback (most recent call last):
70+
...
71+
TypeError: inputs must be integers
3472
"""
73+
if not isinstance(a, int) or not isinstance(b, int):
74+
raise TypeError("inputs must be integers")
3575
if a < 0 or b < 0:
36-
raise ValueError("the value of both inputs must be positive")
76+
raise ValueError("inputs must be non-negative integers")
3777

3878
a_binary = format(a, "b")
3979
b_binary = format(b, "b")
4080

4181
max_len = max(len(a_binary), len(b_binary))
4282

43-
return "0b" + "".join(
44-
str(int(char_a == "1" and char_b == "1"))
45-
for char_a, char_b in zip(a_binary.zfill(max_len), b_binary.zfill(max_len))
46-
)
83+
max_len = max(a.bit_length(), b.bit_length())
84+
return f"0b{(a & b):0{max_len}b}"
4785

4886

4987
if __name__ == "__main__":

bit_manipulation/binary_count_trailing_zeros.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
def binary_count_trailing_zeros(a: int) -> int:
55
"""
6-
Take in 1 integer, return a number that is
7-
the number of trailing zeros in binary representation of that number.
6+
Take in 1 integer, return the number of trailing zeros in binary representation.
87
98
>>> binary_count_trailing_zeros(25)
109
0
@@ -17,25 +16,37 @@ def binary_count_trailing_zeros(a: int) -> int:
1716
>>> binary_count_trailing_zeros(4294967296)
1817
32
1918
>>> binary_count_trailing_zeros(0)
20-
0
19+
Traceback (most recent call last):
20+
...
21+
ValueError: Trailing zeros for 0 are undefined
2122
>>> binary_count_trailing_zeros(-10)
2223
Traceback (most recent call last):
2324
...
2425
ValueError: Input value must be a positive integer
2526
>>> binary_count_trailing_zeros(0.8)
2627
Traceback (most recent call last):
2728
...
28-
TypeError: Input value must be a 'int' type
29+
TypeError: Input value must be an integer
2930
>>> binary_count_trailing_zeros("0")
3031
Traceback (most recent call last):
3132
...
32-
TypeError: '<' not supported between instances of 'str' and 'int'
33+
TypeError: Input value must be an integer
3334
"""
35+
36+
# Type check
37+
if not isinstance(a, int):
38+
raise TypeError("Input value must be an integer")
39+
40+
# Edge case: zero
41+
if a == 0:
42+
raise ValueError("Trailing zeros for 0 are undefined")
43+
44+
# Negative numbers not allowed
3445
if a < 0:
3546
raise ValueError("Input value must be a positive integer")
36-
if isinstance(a, float):
37-
raise TypeError("Input value must be a 'int' type")
38-
return 0 if (a == 0) else int(log2(a & -a))
47+
48+
# Core logic
49+
return int(log2(a & -a))
3950

4051

4152
if __name__ == "__main__":

0 commit comments

Comments
 (0)