From 57815ee43caab3c16c4216d0b3af062a8c54d320 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Sun, 7 Aug 2022 14:41:53 +0300 Subject: [PATCH 1/4] Add solution --- project_euler/problem_116/__init__.py | 0 project_euler/problem_116/sol1.py | 64 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 project_euler/problem_116/__init__.py create mode 100644 project_euler/problem_116/sol1.py diff --git a/project_euler/problem_116/__init__.py b/project_euler/problem_116/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_116/sol1.py b/project_euler/problem_116/sol1.py new file mode 100644 index 000000000000..efa13ee3f25a --- /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() = }") From 37770a3a682261639ee83a61b2e3491dd2979cd2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 7 Aug 2022 11:42:26 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index b37bb35ec619..9cfbe54b9a67 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -858,6 +858,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 From c0b2f523d07274fa6704d9ee01a1e38f828450ee Mon Sep 17 00:00:00 2001 From: John Law Date: Sat, 24 Sep 2022 17:57:51 +0100 Subject: [PATCH 3/4] Fix pre-commit --- matrix/inverse_of_matrix.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 9deca6c3c08e..92780e656ea1 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -27,14 +27,22 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: [[0.25, -0.5], [-0.3, 1.0]] """ - D = Decimal # An abbreviation to be 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 + if len(matrix) != 2 or len(matrix[0]) != 2 or len(matrix[1]) != 2: + raise ValueError("Please provide a matrix of size 2x2.") + # Calculate the determinant of the matrix determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) if determinant == 0: raise ValueError("This matrix has no inverse.") + # Creates a copy of the matrix with swapped positions of the elements swapped_matrix = [[0.0, 0.0], [0.0, 0.0]] swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0] swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1] + # Calculate the inverse of the matrix return [[float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix] From 6bba0dec864877919e32b723c8a0f411c082c18f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 24 Sep 2022 16:58:40 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3b9564ad663b..1d9e6eff75c6 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 @@ -985,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) @@ -1007,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)