Skip to content

Commit 216d606

Browse files
committed
Merge remote-tracking branch 'upstream/master' into jenia90/2128_add_typehints_cyphers_and_bool_alg
2 parents 1144496 + ca2d269 commit 216d606

File tree

39 files changed

+1683
-3058
lines changed

39 files changed

+1683
-3058
lines changed

Diff for: .github/CODEOWNERS

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# This is a comment.
2+
# Each line is a file pattern followed by one or more owners.
3+
4+
# More details are here: https://help.github.com/articles/about-codeowners/
5+
6+
# The '*' pattern is global owners.
7+
8+
# Order is important. The last matching pattern has the most precedence.
9+
10+
/.travis.yml @cclauss @dhruvmanila
11+
12+
/.pre-commit-config.yaml @cclauss @dhruvmanila
13+
14+
/.github/ @cclauss
15+
16+
# /arithmetic_analysis/
17+
18+
# /backtracking/
19+
20+
# /bit_manipulation/
21+
22+
# /blockchain/
23+
24+
# /boolean_algebra/
25+
26+
# /cellular_automata/
27+
28+
/ciphers/ @cclauss
29+
30+
# /compression/
31+
32+
# /computer_vision/
33+
34+
/conversions/ @cclauss
35+
36+
/data_structures/ @cclauss
37+
38+
# /digital_image_processing/
39+
40+
# /divide_and_conquer/
41+
42+
/dynamic_programming/ @Kush1101
43+
44+
# /file_transfer/
45+
46+
# /fuzzy_logic/
47+
48+
# /genetic_algorithm/
49+
50+
# /geodesy/
51+
52+
# /graphics/
53+
54+
# /graphs/
55+
56+
# /greedy_method/
57+
58+
# /hashes/
59+
60+
# /images/
61+
62+
# /linear_algebra/
63+
64+
# /machine_learning/
65+
66+
/maths/ @Kush1101
67+
68+
# /matrix/
69+
70+
# /networking_flow/
71+
72+
# /neural_network/
73+
74+
/other/ @cclauss
75+
76+
/project_euler/ @dhruvmanila @Kush1101
77+
78+
# /quantum/
79+
80+
# /scheduling/
81+
82+
# /scripts/
83+
84+
# /searches/
85+
86+
# /sorts/
87+
88+
/strings/ @cclauss
89+
90+
# /traversals/
91+
92+
/web_programming/ @cclauss

Diff for: .travis.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ jobs:
1212
- pytest --doctest-modules --ignore=project_euler/ --durations=10 --cov-report=term-missing:skip-covered --cov=. .
1313
- name: Project Euler
1414
install:
15-
- pip install pytest-cov pytest-subtests
16-
before_script:
17-
- pytest --tb=no --no-summary --capture=no project_euler/validate_solutions.py || true # fail fast on wrong solution
15+
- pip install pytest-cov
1816
script:
1917
- pytest --doctest-modules --durations=10 --cov-report=term-missing:skip-covered --cov=project_euler/ project_euler/
18+
- name: Project Euler Solution
19+
script:
20+
- pytest --tb=short --durations=10 project_euler/validate_solutions.py
2021
after_success:
2122
- scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md
2223
notifications:

Diff for: CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Algorithms in this repo should not be how-to examples for existing Python packag
5353

5454
We want your work to be readable by others; therefore, we encourage you to note the following:
5555

56-
- Please write in Python 3.7+. __print()__ is a function in Python 3 so __print "Hello"__ will _not_ work but __print("Hello")__ will.
56+
- Please write in Python 3.7+. For instance: __print()__ is a function in Python 3 so __print "Hello"__ will _not_ work but __print("Hello")__ will.
5757
- Please focus hard on naming of functions, classes, and variables. Help your reader by using __descriptive names__ that can help you to remove redundant comments.
5858
- Single letter variable names are _old school_ so please avoid them unless their life only spans a few lines.
5959
- Expand acronyms because __gcd()__ is hard to understand but __greatest_common_divisor()__ is not.
@@ -147,9 +147,9 @@ We want your work to be readable by others; therefore, we encourage you to note
147147
- Avoid importing external libraries for basic algorithms. Only use those libraries for complicated algorithms.
148148
- If you need a third party module that is not in the file __requirements.txt__, please add it to that file as part of your submission.
149149

150-
#### Other Standard While Submitting Your Work
150+
#### Other Requirements for Submissions
151151

152-
- File extension for code should be `.py`. Jupyter notebook files are acceptable in machine learning algorithms.
152+
- The file extension for code files should be `.py`. Jupyter Notebooks should be submitted to [TheAlgorithms/Jupyter](https://github.com/TheAlgorithms/Jupyter).
153153
- Strictly use snake_case (underscore_separated) in your file_name, as it will be easy to parse in future using scripts.
154154
- Please avoid creating new directories if at all possible. Try to fit your work into the existing directory structure.
155155
- If possible, follow the standard *within* the folder you are submitting to.

Diff for: DIRECTORY.md

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
6464
* [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py)
6565
* [Rabin Miller](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rabin_miller.py)
66+
* [Rail Fence Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rail_fence_cipher.py)
6667
* [Rot13](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rot13.py)
6768
* [Rsa Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_cipher.py)
6869
* [Rsa Factorization](https://github.com/TheAlgorithms/Python/blob/master/ciphers/rsa_factorization.py)
@@ -553,6 +554,8 @@
553554
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_11/sol2.py)
554555
* Problem 112
555556
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_112/sol1.py)
557+
* Problem 119
558+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_119/sol1.py)
556559
* Problem 12
557560
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol1.py)
558561
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_12/sol2.py)
@@ -576,6 +579,8 @@
576579
* [Solution](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_18/solution.py)
577580
* Problem 19
578581
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_19/sol1.py)
582+
* Problem 191
583+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py)
579584
* Problem 20
580585
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol1.py)
581586
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_20/sol2.py)
@@ -660,16 +665,25 @@
660665
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_63/sol1.py)
661666
* Problem 67
662667
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_67/sol1.py)
668+
* Problem 69
669+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_69/sol1.py)
663670
* Problem 71
664671
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_71/sol1.py)
672+
* Problem 72
673+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_72/sol1.py)
665674
* Problem 76
666675
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_76/sol1.py)
676+
* Problem 80
677+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_80/sol1.py)
667678
* Problem 97
668679
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_97/sol1.py)
669680
* Problem 99
670681
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_99/sol1.py)
671682
* [Validate Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/validate_solutions.py)
672683

684+
## Quantum
685+
* [Single Qubit Measure](https://github.com/TheAlgorithms/Python/blob/master/quantum/single_qubit_measure.py)
686+
673687
## Scheduling
674688
* [First Come First Served](https://github.com/TheAlgorithms/Python/blob/master/scheduling/first_come_first_served.py)
675689
* [Round Robin](https://github.com/TheAlgorithms/Python/blob/master/scheduling/round_robin.py)
@@ -760,6 +774,7 @@
760774
* [Binary Tree Traversals](https://github.com/TheAlgorithms/Python/blob/master/traversals/binary_tree_traversals.py)
761775

762776
## Web Programming
777+
* [Co2 Emission](https://github.com/TheAlgorithms/Python/blob/master/web_programming/co2_emission.py)
763778
* [Covid Stats Via Xpath](https://github.com/TheAlgorithms/Python/blob/master/web_programming/covid_stats_via_xpath.py)
764779
* [Crawl Google Results](https://github.com/TheAlgorithms/Python/blob/master/web_programming/crawl_google_results.py)
765780
* [Current Stock Price](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_stock_price.py)

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### All algorithms implemented in Python (for education)
1414

15-
These implementations are for learning purposes. They may be less efficient than the implementations in the Python standard library.
15+
These implementations are for learning purposes only. Therefore they may be less efficient than the implementations in the Python standard library.
1616

1717
## Contribution Guidelines
1818

Diff for: ciphers/rail_fence_cipher.py

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
""" https://en.wikipedia.org/wiki/Rail_fence_cipher """
2+
3+
4+
def encrypt(input_string: str, key: int) -> str:
5+
"""
6+
Shuffles the character of a string by placing each of them
7+
in a grid (the height is dependent on the key) in a zigzag
8+
formation and reading it left to right.
9+
10+
>>> encrypt("Hello World", 4)
11+
'HWe olordll'
12+
13+
>>> encrypt("This is a message", 0)
14+
Traceback (most recent call last):
15+
...
16+
ValueError: Height of grid can't be 0 or negative
17+
18+
>>> encrypt(b"This is a byte string", 5)
19+
Traceback (most recent call last):
20+
...
21+
TypeError: sequence item 0: expected str instance, int found
22+
"""
23+
grid = [[] for _ in range(key)]
24+
lowest = key - 1
25+
26+
if key <= 0:
27+
raise ValueError("Height of grid can't be 0 or negative")
28+
if key == 1 or len(input_string) <= key:
29+
return input_string
30+
31+
for position, character in enumerate(input_string):
32+
num = position % (lowest * 2) # puts it in bounds
33+
num = min(num, lowest * 2 - num) # creates zigzag pattern
34+
grid[num].append(character)
35+
grid = ["".join(row) for row in grid]
36+
output_string = "".join(grid)
37+
38+
return output_string
39+
40+
41+
def decrypt(input_string: str, key: int) -> str:
42+
"""
43+
Generates a template based on the key and fills it in with
44+
the characters of the input string and then reading it in
45+
a zigzag formation.
46+
47+
>>> decrypt("HWe olordll", 4)
48+
'Hello World'
49+
50+
>>> decrypt("This is a message", -10)
51+
Traceback (most recent call last):
52+
...
53+
ValueError: Height of grid can't be 0 or negative
54+
55+
>>> decrypt("My key is very big", 100)
56+
'My key is very big'
57+
"""
58+
grid = []
59+
lowest = key - 1
60+
61+
if key <= 0:
62+
raise ValueError("Height of grid can't be 0 or negative")
63+
if key == 1:
64+
return input_string
65+
66+
temp_grid = [[] for _ in range(key)] # generates template
67+
for position in range(len(input_string)):
68+
num = position % (lowest * 2) # puts it in bounds
69+
num = min(num, lowest * 2 - num) # creates zigzag pattern
70+
temp_grid[num].append("*")
71+
72+
counter = 0
73+
for row in temp_grid: # fills in the characters
74+
splice = input_string[counter : counter + len(row)]
75+
grid.append([character for character in splice])
76+
counter += len(row)
77+
78+
output_string = "" # reads as zigzag
79+
for position in range(len(input_string)):
80+
num = position % (lowest * 2) # puts it in bounds
81+
num = min(num, lowest * 2 - num) # creates zigzag pattern
82+
output_string += grid[num][0]
83+
grid[num].pop(0)
84+
return output_string
85+
86+
87+
def bruteforce(input_string: str) -> dict:
88+
"""Uses decrypt function by guessing every key
89+
90+
>>> bruteforce("HWe olordll")[4]
91+
'Hello World'
92+
"""
93+
results = {}
94+
for key_guess in range(1, len(input_string)): # tries every key
95+
results[key_guess] = decrypt(input_string, key_guess)
96+
return results
97+
98+
99+
if __name__ == "__main__":
100+
import doctest
101+
102+
doctest.testmod()

Diff for: computer_vision/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
Computer vision is a field of computer science that works on enabling computers to see,
44
identify and process images in the same way that human vision does, and then provide appropriate output.
55
It is like imparting human intelligence and instincts to a computer.
6-
Image processing and computer vision and little different from each other.Image processing means applying some algorithms for transforming image from one form to other like smoothing,contrasting, stretching etc
7-
While in computer vision comes from modelling image processing using the techniques of machine learning.Computer vision applies machine learning to recognize patterns for interpretation of images.
8-
Much like the process of visual reasoning of human vision
6+
Image processing and computer vision are a little different from each other. Image processing means applying some algorithms for transforming image from one form to the other like smoothing, contrasting, stretching, etc.
7+
While computer vision comes from modelling image processing using the techniques of machine learning, computer vision applies machine learning to recognize patterns for interpretation of images (much like the process of visual reasoning of human vision).

Diff for: project_euler/problem_11/sol1.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def largest_product(grid):
6868

6969

7070
def solution():
71-
"""Returns the sum of all the multiples of 3 or 5 below n.
71+
"""Returns the greatest product of four adjacent numbers (horizontally,
72+
vertically, or diagonally).
7273
7374
>>> solution()
7475
70600674

Diff for: project_euler/problem_11/sol2.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929

3030
def solution():
31-
"""Returns the sum of all the multiples of 3 or 5 below n.
31+
"""Returns the greatest product of four adjacent numbers (horizontally,
32+
vertically, or diagonally).
3233
3334
>>> solution()
3435
70600674

Diff for: project_euler/problem_119/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_119/sol1.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Problem 119: https://projecteuler.net/problem=119
3+
4+
Name: Digit power sum
5+
6+
The number 512 is interesting because it is equal to the sum of its digits
7+
raised to some power: 5 + 1 + 2 = 8, and 8^3 = 512. Another example of a number
8+
with this property is 614656 = 28^4. We shall define an to be the nth term of
9+
this sequence and insist that a number must contain at least two digits to have a sum.
10+
You are given that a2 = 512 and a10 = 614656. Find a30
11+
"""
12+
13+
import math
14+
15+
16+
def digit_sum(n: int) -> int:
17+
"""
18+
Returns the sum of the digits of the number.
19+
>>> digit_sum(123)
20+
6
21+
>>> digit_sum(456)
22+
15
23+
>>> digit_sum(78910)
24+
25
25+
"""
26+
return sum([int(digit) for digit in str(n)])
27+
28+
29+
def solution(n: int = 30) -> int:
30+
"""
31+
Returns the value of 30th digit power sum.
32+
>>> solution(2)
33+
512
34+
>>> solution(5)
35+
5832
36+
>>> solution(10)
37+
614656
38+
"""
39+
digit_to_powers = []
40+
for digit in range(2, 100):
41+
for power in range(2, 100):
42+
number = int(math.pow(digit, power))
43+
if digit == digit_sum(number):
44+
digit_to_powers.append(number)
45+
46+
digit_to_powers.sort()
47+
return digit_to_powers[n - 1]
48+
49+
50+
if __name__ == "__main__":
51+
print(solution())

0 commit comments

Comments
 (0)