Skip to content

Commit

Permalink
Add Project Euler problem 116 solution 1 (TheAlgorithms#6305)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Sep 24, 2022
1 parent 91c671e commit a0b0f41
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
6 changes: 5 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@
* [Scoring Functions](machine_learning/scoring_functions.py)
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
* [Similarity Search](machine_learning/similarity_search.py)
* [Support Vector Machines](machine_learning/support_vector_machines.py)
* [Word Frequency Functions](machine_learning/word_frequency_functions.py)

## Maths
Expand Down Expand Up @@ -859,6 +860,8 @@
* [Sol1](project_euler/problem_114/sol1.py)
* Problem 115
* [Sol1](project_euler/problem_115/sol1.py)
* Problem 116
* [Sol1](project_euler/problem_116/sol1.py)
* Problem 119
* [Sol1](project_euler/problem_119/sol1.py)
* Problem 120
Expand Down Expand Up @@ -983,7 +986,7 @@
* [Recursive Quick Sort](sorts/recursive_quick_sort.py)
* [Selection Sort](sorts/selection_sort.py)
* [Shell Sort](sorts/shell_sort.py)
* [Shrink Shell](sorts/shrink_shell.py)
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
* [Slowsort](sorts/slowsort.py)
* [Stooge Sort](sorts/stooge_sort.py)
* [Strand Sort](sorts/strand_sort.py)
Expand All @@ -1005,6 +1008,7 @@
* [Check Pangram](strings/check_pangram.py)
* [Credit Card Validator](strings/credit_card_validator.py)
* [Detecting English Programmatically](strings/detecting_english_programmatically.py)
* [Dna](strings/dna.py)
* [Frequency Finder](strings/frequency_finder.py)
* [Hamming Distance](strings/hamming_distance.py)
* [Indian Phone Validator](strings/indian_phone_validator.py)
Expand Down
3 changes: 2 additions & 1 deletion matrix/inverse_of_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:

D = Decimal # An abbreviation for conciseness

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

Expand Down
Empty file.
64 changes: 64 additions & 0 deletions project_euler/problem_116/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Project Euler Problem 116: https://projecteuler.net/problem=116
A row of five grey square tiles is to have a number of its tiles
replaced with coloured oblong tiles chosen
from red (length two), green (length three), or blue (length four).
If red tiles are chosen there are exactly seven ways this can be done.
|red,red|grey|grey|grey| |grey|red,red|grey|grey|
|grey|grey|red,red|grey| |grey|grey|grey|red,red|
|red,red|red,red|grey| |red,red|grey|red,red|
|grey|red,red|red,red|
If green tiles are chosen there are three ways.
|green,green,green|grey|grey| |grey|green,green,green|grey|
|grey|grey|green,green,green|
And if blue tiles are chosen there are two ways.
|blue,blue,blue,blue|grey| |grey|blue,blue,blue,blue|
Assuming that colours cannot be mixed there are 7 + 3 + 2 = 12 ways
of replacing the grey tiles in a row measuring five units in length.
How many different ways can the grey tiles in a row measuring fifty units in length
be replaced if colours cannot be mixed and at least one coloured tile must be used?
NOTE: This is related to Problem 117 (https://projecteuler.net/problem=117).
"""


def solution(length: int = 50) -> int:
"""
Returns the number of different ways can the grey tiles in a row
of the given length be replaced if colours cannot be mixed
and at least one coloured tile must be used
>>> solution(5)
12
"""

different_colour_ways_number = [[0] * 3 for _ in range(length + 1)]

for row_length in range(length + 1):
for tile_length in range(2, 5):
for tile_start in range(row_length - tile_length + 1):
different_colour_ways_number[row_length][tile_length - 2] += (
different_colour_ways_number[row_length - tile_start - tile_length][
tile_length - 2
]
+ 1
)

return sum(different_colour_ways_number[length])


if __name__ == "__main__":
print(f"{solution() = }")

0 comments on commit a0b0f41

Please sign in to comment.