Add Project Euler problem 116 solution 1 (#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 <johnlaw.po@gmail.com>
This commit is contained in:
Maxim Smolskiy 2022-09-24 20:04:00 +03:00 committed by GitHub
parent 91c671ebab
commit a0b0f414ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 2 deletions

View File

@ -446,6 +446,7 @@
* [Scoring Functions](machine_learning/scoring_functions.py) * [Scoring Functions](machine_learning/scoring_functions.py)
* [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py) * [Sequential Minimum Optimization](machine_learning/sequential_minimum_optimization.py)
* [Similarity Search](machine_learning/similarity_search.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) * [Word Frequency Functions](machine_learning/word_frequency_functions.py)
## Maths ## Maths
@ -859,6 +860,8 @@
* [Sol1](project_euler/problem_114/sol1.py) * [Sol1](project_euler/problem_114/sol1.py)
* Problem 115 * Problem 115
* [Sol1](project_euler/problem_115/sol1.py) * [Sol1](project_euler/problem_115/sol1.py)
* Problem 116
* [Sol1](project_euler/problem_116/sol1.py)
* Problem 119 * Problem 119
* [Sol1](project_euler/problem_119/sol1.py) * [Sol1](project_euler/problem_119/sol1.py)
* Problem 120 * Problem 120
@ -983,7 +986,7 @@
* [Recursive Quick Sort](sorts/recursive_quick_sort.py) * [Recursive Quick Sort](sorts/recursive_quick_sort.py)
* [Selection Sort](sorts/selection_sort.py) * [Selection Sort](sorts/selection_sort.py)
* [Shell Sort](sorts/shell_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) * [Slowsort](sorts/slowsort.py)
* [Stooge Sort](sorts/stooge_sort.py) * [Stooge Sort](sorts/stooge_sort.py)
* [Strand Sort](sorts/strand_sort.py) * [Strand Sort](sorts/strand_sort.py)
@ -1005,6 +1008,7 @@
* [Check Pangram](strings/check_pangram.py) * [Check Pangram](strings/check_pangram.py)
* [Credit Card Validator](strings/credit_card_validator.py) * [Credit Card Validator](strings/credit_card_validator.py)
* [Detecting English Programmatically](strings/detecting_english_programmatically.py) * [Detecting English Programmatically](strings/detecting_english_programmatically.py)
* [Dna](strings/dna.py)
* [Frequency Finder](strings/frequency_finder.py) * [Frequency Finder](strings/frequency_finder.py)
* [Hamming Distance](strings/hamming_distance.py) * [Hamming Distance](strings/hamming_distance.py)
* [Indian Phone Validator](strings/indian_phone_validator.py) * [Indian Phone Validator](strings/indian_phone_validator.py)

View File

@ -29,7 +29,8 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]:
D = Decimal # An abbreviation for conciseness 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: if len(matrix) != 2 or len(matrix[0]) != 2 or len(matrix[1]) != 2:
raise ValueError("Please provide a matrix of size 2x2.") raise ValueError("Please provide a matrix of size 2x2.")

View File

View File

@ -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() = }")