Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1870c0c
feat: Introduce hamming code generator
AyushChakraborty Oct 6, 2024
aac46ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
195144d
fix: Add descriptive naming
AyushChakraborty Oct 6, 2024
9312ec6
Merge branch 'branch1' of
AyushChakraborty Oct 6, 2024
03ef987
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
598a01a
fix: Update changes indicated by failed PR
AyushChakraborty Oct 6, 2024
b720b7a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2024
4de7668
fix: Update to more readable code
AyushChakraborty Oct 8, 2024
e9d35aa
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
46a2eee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
4ff8ddc
fix: Add return statement in elif block
AyushChakraborty Oct 8, 2024
e790a90
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
a51f39a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
e9a65c7
fix: Update elif to else for more general case
AyushChakraborty Oct 8, 2024
2dcd836
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
140d8c9
fix: Update primary if statement
AyushChakraborty Oct 8, 2024
0a87aa2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
49325d1
fix: Modify code to pass tests
AyushChakraborty Oct 8, 2024
4fc77ef
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
6d5b137
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
827d9d4
fix: Update to pass test cases
AyushChakraborty Oct 8, 2024
bca27fc
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
172b9bf
fix: Update to pass checks
AyushChakraborty Oct 8, 2024
6817870
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
ac9983e
fix: Update for more readability of code
AyushChakraborty Oct 8, 2024
2f15194
Merge branch 'branch1' of github.com:AyushChakraborty/TheAlgorithms i…
AyushChakraborty Oct 8, 2024
dbea02c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
3e4ed8a
fix: Update for readability
AyushChakraborty Oct 8, 2024
54e8005
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
b34076c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2024
f0ae8f3
fix: Update for readability
AyushChakraborty Oct 8, 2024
d3649da
Merge branch 'branch1' of
AyushChakraborty Oct 8, 2024
defaba9
fix: Update variable names
AyushChakraborty Oct 8, 2024
68e8480
fix: Update to remove unecessary variable (r)
AyushChakraborty Oct 9, 2024
9259f5a
Merge branch 'master' into branch1
cclauss Sep 15, 2026
9d3417c
updating DIRECTORY.md
cclauss Sep 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* [Generate Parentheses Iterative](backtracking/generate_parentheses_iterative.py)
* [Hamiltonian Cycle](backtracking/hamiltonian_cycle.py)
* [Knight Tour](backtracking/knight_tour.py)
* [M Coloring Problem](backtracking/m_coloring_problem.py)
* [Match Word Pattern](backtracking/match_word_pattern.py)
* [Minimax](backtracking/minimax.py)
* [N Queens](backtracking/n_queens.py)
Expand Down Expand Up @@ -91,6 +92,7 @@
* [Find Previous Power Of Two](bit_manipulation/find_previous_power_of_two.py)
* [Find Unique Number](bit_manipulation/find_unique_number.py)
* [Gray Code Sequence](bit_manipulation/gray_code_sequence.py)
* [Hamming Code Generator](bit_manipulation/hamming_code_generator.py)
* [Highest Set Bit](bit_manipulation/highest_set_bit.py)
* [Index Of Rightmost Set Bit](bit_manipulation/index_of_rightmost_set_bit.py)
* [Is Even](bit_manipulation/is_even.py)
Expand All @@ -108,6 +110,9 @@

## [Blockchain](blockchain)
* [Diophantine Equation](blockchain/diophantine_equation.py)
* [Merkle Tree](blockchain/merkle_tree.py)
* [Simple Blockchain](blockchain/simple_blockchain.py)
* [Simple Proof Of Work](blockchain/simple_proof_of_work.py)

## [Boolean Algebra](boolean_algebra)
* [And Gate](boolean_algebra/and_gate.py)
Expand Down Expand Up @@ -166,6 +171,7 @@
* [Porta Cipher](ciphers/porta_cipher.py)
* [Rabin Miller](ciphers/rabin_miller.py)
* [Rail Fence Cipher](ciphers/rail_fence_cipher.py)
* [Rc4](ciphers/rc4.py)
* [Rot13](ciphers/rot13.py)
* [Rsa Cipher](ciphers/rsa_cipher.py)
* [Rsa Factorization](ciphers/rsa_factorization.py)
Expand All @@ -180,6 +186,7 @@
* [Vernam Cipher](ciphers/vernam_cipher.py)
* [Vigenere Cipher](ciphers/vigenere_cipher.py)
* [Xor Cipher](ciphers/xor_cipher.py)
* [Xtea](ciphers/xtea.py)

## [Computer Vision](computer_vision)
* [Cnn Classification](computer_vision/cnn_classification.py)
Expand All @@ -198,13 +205,17 @@
## [Conversions](conversions)
* [Astronomical Length Scale Conversion](conversions/astronomical_length_scale_conversion.py)
* [Binary To Decimal](conversions/binary_to_decimal.py)
* [Binary To Excess3](conversions/binary_to_excess3.py)
* [Binary To Gray](conversions/binary_to_gray.py)
* [Binary To Gray Code](conversions/binary_to_gray_code.py)
* [Binary To Hexadecimal](conversions/binary_to_hexadecimal.py)
* [Binary To Octal](conversions/binary_to_octal.py)
* [Convert Number To Words](conversions/convert_number_to_words.py)
* [Decimal To Any](conversions/decimal_to_any.py)
* [Decimal To Binary](conversions/decimal_to_binary.py)
* [Decimal To Hexadecimal](conversions/decimal_to_hexadecimal.py)
* [Decimal To Octal](conversions/decimal_to_octal.py)
* [Endianness](conversions/endianness.py)
* [Energy Conversions](conversions/energy_conversions.py)
* [Excel Title To Column](conversions/excel_title_to_column.py)
* [Hex To Bin](conversions/hex_to_bin.py)
Expand Down Expand Up @@ -689,6 +700,7 @@
* [Astar](machine_learning/astar.py)
* [Automatic Differentiation](machine_learning/automatic_differentiation.py)
* [Data Transformations](machine_learning/data_transformations.py)
* [Dbscan](machine_learning/dbscan.py)
* [Decision Tree](machine_learning/decision_tree.py)
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
* [Federated Averaging](machine_learning/federated_averaging.py)
Expand All @@ -712,15 +724,18 @@
* [Loss Functions](machine_learning/loss_functions.py)
* Lstm
* [Lstm Prediction](machine_learning/lstm/lstm_prediction.py)
* [Mean Shift](machine_learning/mean_shift.py)
* [Mfcc](machine_learning/mfcc.py)
* [Mini Batch Gradient Descent](machine_learning/mini_batch_gradient_descent.py)
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
* [Naive Bayes Text Classification](machine_learning/naive_bayes_text_classification.py)
* [Ordinary Least Squares Regression](machine_learning/ordinary_least_squares_regression.py)
* [Polynomial Regression](machine_learning/polynomial_regression.py)
* [Principle Component Analysis](machine_learning/principle_component_analysis.py)
* [Q Learning](machine_learning/q_learning.py)
* [Random Forest Classifier](machine_learning/random_forest_classifier.py)
* [Random Forest Regressor](machine_learning/random_forest_regressor.py)
* [Rmsprop](machine_learning/rmsprop.py)
* [Scoring Functions](machine_learning/scoring_functions.py)
* [Self Organizing Map](machine_learning/self_organizing_map.py)
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
Expand All @@ -737,6 +752,7 @@
* [Arc Length](maths/arc_length.py)
* [Area](maths/area.py)
* [Area Under Curve](maths/area_under_curve.py)
* [Autocorrelation](maths/autocorrelation.py)
* [Average Absolute Deviation](maths/average_absolute_deviation.py)
* [Average Mean](maths/average_mean.py)
* [Average Median](maths/average_median.py)
Expand Down Expand Up @@ -782,6 +798,7 @@
* [Fibonacci](maths/fibonacci.py)
* [Find Max](maths/find_max.py)
* [Find Min](maths/find_min.py)
* [First Fundamental Form](maths/first_fundamental_form.py)
* [Floor](maths/floor.py)
* [Gamma](maths/gamma.py)
* [Gaussian](maths/gaussian.py)
Expand Down Expand Up @@ -844,6 +861,7 @@
* [Square Root](maths/numerical_analysis/square_root.py)
* [Weierstrass Method](maths/numerical_analysis/weierstrass_method.py)
* [Odd Sieve](maths/odd_sieve.py)
* [Padovan Sequence](maths/padovan_sequence.py)
* [Pell Number](maths/pell_number.py)
* [Perfect Cube](maths/perfect_cube.py)
* [Perfect Number](maths/perfect_number.py)
Expand Down Expand Up @@ -873,6 +891,8 @@
* [Reverse Factorial Recursive](maths/reverse_factorial_recursive.py)
* [Segmented Sieve](maths/segmented_sieve.py)
* Series
* [Alternate Harmonic Series](maths/series/alternate_harmonic_series.py)
* [Alternating Harmonic Series](maths/series/alternating_harmonic_series.py)
* [Arithmetic](maths/series/arithmetic.py)
* [Geometric](maths/series/geometric.py)
* [Geometric Series](maths/series/geometric_series.py)
Expand Down Expand Up @@ -912,6 +932,7 @@
* [Polygonal Numbers](maths/special_numbers/polygonal_numbers.py)
* [Pronic Number](maths/special_numbers/pronic_number.py)
* [Proth Number](maths/special_numbers/proth_number.py)
* [Spy Number](maths/special_numbers/spy_number.py)
* [Triangular Numbers](maths/special_numbers/triangular_numbers.py)
* [Trimorphic Number](maths/special_numbers/trimorphic_number.py)
* [Ugly Numbers](maths/special_numbers/ugly_numbers.py)
Expand Down
99 changes: 99 additions & 0 deletions bit_manipulation/hamming_code_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
Author: Ayush Chakraborty
Date: 6th October 2024

generate (15,11) hamming encoded bits gives 11 bit data

input: 11 bit binary number with type string
return: 16 bit binary hamming encoded number returned in string form
Comment thread
AyushChakraborty marked this conversation as resolved.
"""


def hamming_15_11(number: str) -> str:
"""
Performs parity checks to assign values to the redundant bits,
in the 16 bit number
returned, bits at index 0, 1, 2, 4, 8 are redundant bits used for checking

Hamming generated 16 bits generated from the 11 bit binary number can only detect
and change a single bit change, but can only detect more than a
single bit change

for more theoretical knowledege about Hamming codes, refer:
https://www.youtube.com/watch?v=X8jsijhllIA&t=0s
by 3B1B YT channel

Wikipedia:
https://en.wikipedia.org/wiki/Hamming_code

>>> hamming_15_11("00110001110")
'0010101110001110'
>>> hamming_15_11("10110000110")
'0101001100000110'
>>> hamming_15_11("10111100110")
'0011001101100110'
>>> hamming_15_11("10001000010")
'0001100001000010'
>>> hamming_15_11("00010abcdef")
"Input must be an 11-bit binary string containing only '0's and '1's."

"""
is_bin = True # assuming it's binary initially
Comment thread
AyushChakraborty marked this conversation as resolved.
for i in number:
if i not in ("0", "1"):
is_bin = False
break

if len(number) == 11 and is_bin:
digits = [int(number[i]) for i in range(len(number))]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
digits = [int(number[i]) for i in range(len(number))]
digits = [int(digit) for digit in number]


total_ones = sum(digits)
hamming_digits = [0] * 16

parity_positions = {
1: [0, 1, 3, 4, 6, 8, 10],
2: [0, 2, 3, 5, 6, 9, 10],
4: [1, 2, 3, 7, 8, 9, 10],
8: [4, 5, 6, 7, 8, 9, 10],
}

redundant_bits = [0] * 5

redundant_bits_index = 1
for positions in parity_positions.values():
parity = 0
for idx in positions:
parity ^= digits[idx]
redundant_bits[redundant_bits_index] = parity
redundant_bits_index += 1

redundant_bits[0] = (
total_ones % 2
^ redundant_bits[1]
^ redundant_bits[2]
^ redundant_bits[3]
^ redundant_bits[4]
)

data_index = 0
redundant_bit_locations = [0, 1, 2, 4, 8]
for k in range(16):
if k in redundant_bit_locations:
hamming_digits[k] = redundant_bits.pop(0)
else:
hamming_digits[k] = digits[data_index]
data_index += 1

return "".join([str(i) for i in hamming_digits])

elif len(number) != 11 or not is_bin:
return "Input must be an 11-bit binary string containing only '0's and '1's."

else:
return "Invalid input"
Comment on lines +90 to +93

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These must be raised exceptions.



if __name__ == "__main__":
import doctest

doctest.testmod()