From a0b0f414ae134aa1772d33bb930e5a960f9979e8 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sat, 24 Sep 2022 20:04:00 +0300 Subject: [PATCH] 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 --- DIRECTORY.md | 6 ++- matrix/inverse_of_matrix.py | 3 +- project_euler/problem_116/__init__.py | 0 project_euler/problem_116/sol1.py | 64 +++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 project_euler/problem_116/__init__.py create mode 100644 project_euler/problem_116/sol1.py diff --git a/DIRECTORY.md b/DIRECTORY.md index 25eb0ef0e..1d9e6eff7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index e414ee254..92780e656 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -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.") diff --git a/project_euler/problem_116/__init__.py b/project_euler/problem_116/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/project_euler/problem_116/sol1.py b/project_euler/problem_116/sol1.py new file mode 100644 index 000000000..efa13ee3f --- /dev/null +++ b/project_euler/problem_116/sol1.py @@ -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() = }")