Skip to content

Commit a0b0f41

Browse files
MaximSmolskiygithub-actionspoyea
authored
Add Project Euler problem 116 solution 1 (TheAlgorithms#6305)
* Add solution * updating DIRECTORY.md * Fix pre-commit * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <[email protected]>
1 parent 91c671e commit a0b0f41

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

DIRECTORY.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@
446446
* [Scoring Functions](machine_learning/scoring_functions.py)
447447
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
448448
* [Similarity Search](machine_learning/similarity_search.py)
449+
* [Support Vector Machines](machine_learning/support_vector_machines.py)
449450
* [Word Frequency Functions](machine_learning/word_frequency_functions.py)
450451

451452
## Maths
@@ -859,6 +860,8 @@
859860
* [Sol1](project_euler/problem_114/sol1.py)
860861
* Problem 115
861862
* [Sol1](project_euler/problem_115/sol1.py)
863+
* Problem 116
864+
* [Sol1](project_euler/problem_116/sol1.py)
862865
* Problem 119
863866
* [Sol1](project_euler/problem_119/sol1.py)
864867
* Problem 120
@@ -983,7 +986,7 @@
983986
* [Recursive Quick Sort](sorts/recursive_quick_sort.py)
984987
* [Selection Sort](sorts/selection_sort.py)
985988
* [Shell Sort](sorts/shell_sort.py)
986-
* [Shrink Shell](sorts/shrink_shell.py)
989+
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
987990
* [Slowsort](sorts/slowsort.py)
988991
* [Stooge Sort](sorts/stooge_sort.py)
989992
* [Strand Sort](sorts/strand_sort.py)
@@ -1005,6 +1008,7 @@
10051008
* [Check Pangram](strings/check_pangram.py)
10061009
* [Credit Card Validator](strings/credit_card_validator.py)
10071010
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)
1011+
* [Dna](strings/dna.py)
10081012
* [Frequency Finder](strings/frequency_finder.py)
10091013
* [Hamming Distance](strings/hamming_distance.py)
10101014
* [Indian Phone Validator](strings/indian_phone_validator.py)

matrix/inverse_of_matrix.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:
2929

3030
D = Decimal # An abbreviation for conciseness
3131

32-
# Check if the provided matrix has 2 rows and 2 columns, since this implementation only works for 2x2 matrices
32+
# Check if the provided matrix has 2 rows and 2 columns
33+
# since this implementation only works for 2x2 matrices
3334
if len(matrix) != 2 or len(matrix[0]) != 2 or len(matrix[1]) != 2:
3435
raise ValueError("Please provide a matrix of size 2x2.")
3536

project_euler/problem_116/__init__.py

Whitespace-only changes.

project_euler/problem_116/sol1.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Project Euler Problem 116: https://projecteuler.net/problem=116
3+
4+
A row of five grey square tiles is to have a number of its tiles
5+
replaced with coloured oblong tiles chosen
6+
from red (length two), green (length three), or blue (length four).
7+
8+
If red tiles are chosen there are exactly seven ways this can be done.
9+
10+
|red,red|grey|grey|grey| |grey|red,red|grey|grey|
11+
12+
|grey|grey|red,red|grey| |grey|grey|grey|red,red|
13+
14+
|red,red|red,red|grey| |red,red|grey|red,red|
15+
16+
|grey|red,red|red,red|
17+
18+
If green tiles are chosen there are three ways.
19+
20+
|green,green,green|grey|grey| |grey|green,green,green|grey|
21+
22+
|grey|grey|green,green,green|
23+
24+
And if blue tiles are chosen there are two ways.
25+
26+
|blue,blue,blue,blue|grey| |grey|blue,blue,blue,blue|
27+
28+
Assuming that colours cannot be mixed there are 7 + 3 + 2 = 12 ways
29+
of replacing the grey tiles in a row measuring five units in length.
30+
31+
How many different ways can the grey tiles in a row measuring fifty units in length
32+
be replaced if colours cannot be mixed and at least one coloured tile must be used?
33+
34+
NOTE: This is related to Problem 117 (https://projecteuler.net/problem=117).
35+
"""
36+
37+
38+
def solution(length: int = 50) -> int:
39+
"""
40+
Returns the number of different ways can the grey tiles in a row
41+
of the given length be replaced if colours cannot be mixed
42+
and at least one coloured tile must be used
43+
44+
>>> solution(5)
45+
12
46+
"""
47+
48+
different_colour_ways_number = [[0] * 3 for _ in range(length + 1)]
49+
50+
for row_length in range(length + 1):
51+
for tile_length in range(2, 5):
52+
for tile_start in range(row_length - tile_length + 1):
53+
different_colour_ways_number[row_length][tile_length - 2] += (
54+
different_colour_ways_number[row_length - tile_start - tile_length][
55+
tile_length - 2
56+
]
57+
+ 1
58+
)
59+
60+
return sum(different_colour_ways_number[length])
61+
62+
63+
if __name__ == "__main__":
64+
print(f"{solution() = }")

0 commit comments

Comments
 (0)