From d0c90f9163880156157e18690be93f45d95ea028 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sat, 15 Oct 2022 20:51:07 +0100 Subject: [PATCH 01/12] refactor: Move constants outside of variable scope --- ciphers/bifid.py | 15 ++++---- ciphers/brute_force_caesar_cipher.py | 4 +- ciphers/polybius.py | 17 +++++---- compression/peak_signal_to_noise_ratio.py | 8 ++-- conversions/binary_to_hexadecimal.py | 39 ++++++++++---------- conversions/decimal_to_any.py | 16 ++++---- conversions/roman_numerals.py | 32 ++++++++-------- geodesy/haversine_distance.py | 7 ++-- geodesy/lamberts_ellipsoidal_distance.py | 9 +++-- hashes/adler32.py | 3 +- physics/n_body_simulation.py | 13 ++++--- project_euler/problem_054/test_poker_hand.py | 6 +-- project_euler/problem_064/sol1.py | 8 ++-- project_euler/problem_097/sol1.py | 6 +-- project_euler/problem_125/sol1.py | 2 +- sorts/radix_sort.py | 4 +- web_programming/fetch_quotes.py | 8 ++-- 17 files changed, 108 insertions(+), 89 deletions(-) diff --git a/ciphers/bifid.py b/ciphers/bifid.py index 54d55574cdca..e4866bde1b12 100644 --- a/ciphers/bifid.py +++ b/ciphers/bifid.py @@ -10,15 +10,16 @@ import numpy as np +SQUARE = [ + ["a", "b", "c", "d", "e"], + ["f", "g", "h", "i", "k"], + ["l", "m", "n", "o", "p"], + ["q", "r", "s", "t", "u"], + ["v", "w", "x", "y", "z"], +] + class BifidCipher: def __init__(self) -> None: - SQUARE = [ # noqa: N806 - ["a", "b", "c", "d", "e"], - ["f", "g", "h", "i", "k"], - ["l", "m", "n", "o", "p"], - ["q", "r", "s", "t", "u"], - ["v", "w", "x", "y", "z"], - ] self.SQUARE = np.array(SQUARE) def letter_to_numbers(self, letter: str) -> np.ndarray: diff --git a/ciphers/brute_force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py index cc97111e05a7..096ad62c36cc 100644 --- a/ciphers/brute_force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -1,3 +1,6 @@ + +LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def decrypt(message: str) -> None: """ >>> decrypt('TMDETUX PMDVU') @@ -28,7 +31,6 @@ def decrypt(message: str) -> None: Decryption using Key #24: VOFGVWZ ROFXW Decryption using Key #25: UNEFUVY QNEWV """ - LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # noqa: N806 for key in range(len(LETTERS)): translated = "" for symbol in message: diff --git a/ciphers/polybius.py b/ciphers/polybius.py index bf5d62f8d33e..b25205df9a51 100644 --- a/ciphers/polybius.py +++ b/ciphers/polybius.py @@ -9,15 +9,18 @@ import numpy as np +SQUARE = [ + ["a", "b", "c", "d", "e"], + ["f", "g", "h", "i", "k"], + ["l", "m", "n", "o", "p"], + ["q", "r", "s", "t", "u"], + ["v", "w", "x", "y", "z"], +] + + class PolybiusCipher: def __init__(self) -> None: - SQUARE = [ # noqa: N806 - ["a", "b", "c", "d", "e"], - ["f", "g", "h", "i", "k"], - ["l", "m", "n", "o", "p"], - ["q", "r", "s", "t", "u"], - ["v", "w", "x", "y", "z"], - ] + self.SQUARE = np.array(SQUARE) def letter_to_numbers(self, letter: str) -> np.ndarray: diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index 66b18b50b028..f213fd974203 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -12,13 +12,15 @@ import numpy as np +PIXEL_MAX = 255.0 + def psnr(original: float, contrast: float) -> float: mse = np.mean((original - contrast) ** 2) if mse == 0: return 100 - PIXEL_MAX = 255.0 # noqa: N806 - PSNR = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) # noqa: N806 - return PSNR + + psnr = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) + return psnr def main() -> None: diff --git a/conversions/binary_to_hexadecimal.py b/conversions/binary_to_hexadecimal.py index 61f335a4c465..89f7af696357 100644 --- a/conversions/binary_to_hexadecimal.py +++ b/conversions/binary_to_hexadecimal.py @@ -1,3 +1,23 @@ +BITS_TO_HEX = { + "0000": "0", + "0001": "1", + "0010": "2", + "0011": "3", + "0100": "4", + "0101": "5", + "0110": "6", + "0111": "7", + "1000": "8", + "1001": "9", + "1010": "a", + "1011": "b", + "1100": "c", + "1101": "d", + "1110": "e", + "1111": "f", +} + + def bin_to_hexadecimal(binary_str: str) -> str: """ Converting a binary string into hexadecimal using Grouping Method @@ -17,25 +37,6 @@ def bin_to_hexadecimal(binary_str: str) -> str: ... ValueError: Empty string was passed to the function """ - BITS_TO_HEX = { # noqa: N806 - "0000": "0", - "0001": "1", - "0010": "2", - "0011": "3", - "0100": "4", - "0101": "5", - "0110": "6", - "0111": "7", - "1000": "8", - "1001": "9", - "1010": "a", - "1011": "b", - "1100": "c", - "1101": "d", - "1110": "e", - "1111": "f", - } - # Sanitising parameter binary_str = str(binary_str).strip() diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index e54fa154a0f7..c703527426cd 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -1,6 +1,15 @@ """Convert a positive Decimal Number to Any Other Representation""" +# fmt: off +ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', # noqa: E501 + '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', + '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', + '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', + '34': 'Y', '35': 'Z'} +# fmt: on + + def decimal_to_any(num: int, base: int) -> str: """ Convert a positive integer to another base as str. @@ -65,13 +74,6 @@ def decimal_to_any(num: int, base: int) -> str: raise ValueError("base must be >= 2") if base > 36: raise ValueError("base must be <= 36") - # fmt: off - ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', # noqa: N806, E501 - '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', - '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', - '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', - '34': 'Y', '35': 'Z'} - # fmt: on new_value = "" mod = 0 div = 0 diff --git a/conversions/roman_numerals.py b/conversions/roman_numerals.py index 960d41342276..61215a0c0730 100644 --- a/conversions/roman_numerals.py +++ b/conversions/roman_numerals.py @@ -1,3 +1,20 @@ +ROMAN = [ + (1000, "M"), + (900, "CM"), + (500, "D"), + (400, "CD"), + (100, "C"), + (90, "XC"), + (50, "L"), + (40, "XL"), + (10, "X"), + (9, "IX"), + (5, "V"), + (4, "IV"), + (1, "I"), +] + + def roman_to_int(roman: str) -> int: """ LeetCode No. 13 Roman to Integer @@ -29,21 +46,6 @@ def int_to_roman(number: int) -> str: >>> all(int_to_roman(value) == key for key, value in tests.items()) True """ - ROMAN = [ # noqa: N806 - (1000, "M"), - (900, "CM"), - (500, "D"), - (400, "CD"), - (100, "C"), - (90, "XC"), - (50, "L"), - (40, "XL"), - (10, "X"), - (9, "IX"), - (5, "V"), - (4, "IV"), - (1, "I"), - ] result = [] for (arabic, roman) in ROMAN: (factor, number) = divmod(number, arabic) diff --git a/geodesy/haversine_distance.py b/geodesy/haversine_distance.py index b601d2fd1983..266ecdd5dff7 100644 --- a/geodesy/haversine_distance.py +++ b/geodesy/haversine_distance.py @@ -1,6 +1,10 @@ from math import asin, atan, cos, radians, sin, sqrt, tan +AXIS_A = 6378137.0 +AXIS_B = 6356752.314245 +RADIUS = 6378137 + def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: """ Calculate great circle distance between two points in a sphere, @@ -30,9 +34,6 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl """ # CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System # Distance in metres(m) - AXIS_A = 6378137.0 # noqa: N806 - AXIS_B = 6356752.314245 # noqa: N806 - RADIUS = 6378137 # noqa: N806 # Equation parameters # Equation https://en.wikipedia.org/wiki/Haversine_formula#Formulation flattening = (AXIS_A - AXIS_B) / AXIS_A diff --git a/geodesy/lamberts_ellipsoidal_distance.py b/geodesy/lamberts_ellipsoidal_distance.py index d36d399538de..e995ac5f597e 100644 --- a/geodesy/lamberts_ellipsoidal_distance.py +++ b/geodesy/lamberts_ellipsoidal_distance.py @@ -3,6 +3,11 @@ from .haversine_distance import haversine_distance +AXIS_A = 6378137.0 +AXIS_B = 6356752.314245 +EQUATORIAL_RADIUS = 6378137 + + def lamberts_ellipsoidal_distance( lat1: float, lon1: float, lat2: float, lon2: float ) -> float: @@ -45,10 +50,6 @@ def lamberts_ellipsoidal_distance( # CONSTANTS per WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System # Distance in metres(m) - AXIS_A = 6378137.0 # noqa: N806 - AXIS_B = 6356752.314245 # noqa: N806 - EQUATORIAL_RADIUS = 6378137 # noqa: N806 - # Equation Parameters # https://en.wikipedia.org/wiki/Geographical_distance#Lambert's_formula_for_long_lines flattening = (AXIS_A - AXIS_B) / AXIS_A diff --git a/hashes/adler32.py b/hashes/adler32.py index 80229f04620a..611ebc88b80f 100644 --- a/hashes/adler32.py +++ b/hashes/adler32.py @@ -8,6 +8,8 @@ source: https://en.wikipedia.org/wiki/Adler-32 """ +MOD_ADLER = 65521 + def adler32(plain_text: str) -> int: """ @@ -20,7 +22,6 @@ def adler32(plain_text: str) -> int: >>> adler32('go adler em all') 708642122 """ - MOD_ADLER = 65521 # noqa: N806 a = 1 b = 0 for plain_chr in plain_text: diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index 2f8153782663..347895e7b62a 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -20,6 +20,13 @@ from matplotlib import pyplot as plt +# Frame rate of the animation +INTERVAL = 20 + +# Time between time steps in seconds +DELTA_TIME = INTERVAL / 1000 + + class Body: def __init__( self, @@ -219,12 +226,6 @@ def plot( Utility function to plot how the given body-system evolves over time. No doctest provided since this function does not have a return value. """ - # Frame rate of the animation - INTERVAL = 20 # noqa: N806 - - # Time between time steps in seconds - DELTA_TIME = INTERVAL / 1000 # noqa: N806 - fig = plt.figure() fig.canvas.set_window_title(title) ax = plt.axes( diff --git a/project_euler/problem_054/test_poker_hand.py b/project_euler/problem_054/test_poker_hand.py index bf5a20a8e862..5735bfc37947 100644 --- a/project_euler/problem_054/test_poker_hand.py +++ b/project_euler/problem_054/test_poker_hand.py @@ -185,12 +185,12 @@ def test_compare_random(hand, other, expected): def test_hand_sorted(): - POKER_HANDS = [PokerHand(hand) for hand in SORTED_HANDS] # noqa: N806 - list_copy = POKER_HANDS.copy() + poker_hands = [PokerHand(hand) for hand in SORTED_HANDS] + list_copy = poker_hands.copy() shuffle(list_copy) user_sorted = chain(sorted(list_copy)) for index, hand in enumerate(user_sorted): - assert hand == POKER_HANDS[index] + assert hand == poker_hands[index] def test_custom_sort_five_high_straight(): diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py index 9edd9a1e7a64..81ebcc7b73c3 100644 --- a/project_euler/problem_064/sol1.py +++ b/project_euler/problem_064/sol1.py @@ -33,13 +33,13 @@ def continuous_fraction_period(n: int) -> int: """ numerator = 0.0 denominator = 1.0 - ROOT = int(sqrt(n)) # noqa: N806 - integer_part = ROOT + root = int(sqrt(n)) + integer_part = root period = 0 - while integer_part != 2 * ROOT: + while integer_part != 2 * root: numerator = denominator * integer_part - numerator denominator = (n - numerator**2) / denominator - integer_part = int((ROOT + numerator) / denominator) + integer_part = int((root + numerator) / denominator) period += 1 return period diff --git a/project_euler/problem_097/sol1.py b/project_euler/problem_097/sol1.py index 94a43894ee07..2807e893ded0 100644 --- a/project_euler/problem_097/sol1.py +++ b/project_euler/problem_097/sol1.py @@ -34,9 +34,9 @@ def solution(n: int = 10) -> str: """ if not isinstance(n, int) or n < 0: raise ValueError("Invalid input") - MODULUS = 10**n # noqa: N806 - NUMBER = 28433 * (pow(2, 7830457, MODULUS)) + 1 # noqa: N806 - return str(NUMBER % MODULUS) + modulus = 10**n + number = 28433 * (pow(2, 7830457, modulus)) + 1 + return str(number % modulus) if __name__ == "__main__": diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py index 1812df36132e..2383217ef745 100644 --- a/project_euler/problem_125/sol1.py +++ b/project_euler/problem_125/sol1.py @@ -13,6 +13,7 @@ be written as the sum of consecutive squares. """ +LIMIT = 10**8 def is_palindrome(n: int) -> bool: """ @@ -35,7 +36,6 @@ def solution() -> int: Returns the sum of all numbers less than 1e8 that are both palindromic and can be written as the sum of consecutive squares. """ - LIMIT = 10**8 # noqa: N806 answer = set() first_square = 1 sum_squares = 5 diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index a496cdc0c743..7f90fad7f3e1 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -6,6 +6,9 @@ from __future__ import annotations +RADIX = 10 + + def radix_sort(list_of_ints: list[int]) -> list[int]: """ Examples: @@ -19,7 +22,6 @@ def radix_sort(list_of_ints: list[int]) -> list[int]: >>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000]) True """ - RADIX = 10 # noqa: N806 placement = 1 max_digit = max(list_of_ints) while placement <= max_digit: diff --git a/web_programming/fetch_quotes.py b/web_programming/fetch_quotes.py index a45f6ea0eaf1..eb8f58b4d0e6 100644 --- a/web_programming/fetch_quotes.py +++ b/web_programming/fetch_quotes.py @@ -12,13 +12,13 @@ def quote_of_the_day() -> list: - API_ENDPOINT_URL = "https://zenquotes.io/api/today/" # noqa: N806 - return requests.get(API_ENDPOINT_URL).json() + api_endpoint_url = "https://zenquotes.io/api/today/" + return requests.get(api_endpoint_url).json() def random_quotes() -> list: - API_ENDPOINT_URL = "https://zenquotes.io/api/random/" # noqa: N806 - return requests.get(API_ENDPOINT_URL).json() + api_endpoint_url = "https://zenquotes.io/api/random/" + return requests.get(api_endpoint_url).json() if __name__ == "__main__": From 6b77e2a38e4b608c9fb7ab0de2c865b0ff333c75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Oct 2022 19:52:49 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ciphers/bifid.py | 2 +- ciphers/brute_force_caesar_cipher.py | 2 +- ciphers/polybius.py | 5 ++--- compression/peak_signal_to_noise_ratio.py | 2 +- geodesy/haversine_distance.py | 2 +- geodesy/lamberts_ellipsoidal_distance.py | 1 - physics/n_body_simulation.py | 1 - project_euler/problem_125/sol1.py | 1 + sorts/radix_sort.py | 1 - 9 files changed, 7 insertions(+), 10 deletions(-) diff --git a/ciphers/bifid.py b/ciphers/bifid.py index e4866bde1b12..c005e051a6ba 100644 --- a/ciphers/bifid.py +++ b/ciphers/bifid.py @@ -9,7 +9,6 @@ import numpy as np - SQUARE = [ ["a", "b", "c", "d", "e"], ["f", "g", "h", "i", "k"], @@ -18,6 +17,7 @@ ["v", "w", "x", "y", "z"], ] + class BifidCipher: def __init__(self) -> None: self.SQUARE = np.array(SQUARE) diff --git a/ciphers/brute_force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py index 096ad62c36cc..6542c6859687 100644 --- a/ciphers/brute_force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -1,6 +1,6 @@ - LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + def decrypt(message: str) -> None: """ >>> decrypt('TMDETUX PMDVU') diff --git a/ciphers/polybius.py b/ciphers/polybius.py index b25205df9a51..c81c1d39533f 100644 --- a/ciphers/polybius.py +++ b/ciphers/polybius.py @@ -8,7 +8,6 @@ import numpy as np - SQUARE = [ ["a", "b", "c", "d", "e"], ["f", "g", "h", "i", "k"], @@ -16,11 +15,11 @@ ["q", "r", "s", "t", "u"], ["v", "w", "x", "y", "z"], ] - + class PolybiusCipher: def __init__(self) -> None: - + self.SQUARE = np.array(SQUARE) def letter_to_numbers(self, letter: str) -> np.ndarray: diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index f213fd974203..f09cc0854552 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -11,9 +11,9 @@ import cv2 import numpy as np - PIXEL_MAX = 255.0 + def psnr(original: float, contrast: float) -> float: mse = np.mean((original - contrast) ** 2) if mse == 0: diff --git a/geodesy/haversine_distance.py b/geodesy/haversine_distance.py index 266ecdd5dff7..93e625770f9d 100644 --- a/geodesy/haversine_distance.py +++ b/geodesy/haversine_distance.py @@ -1,10 +1,10 @@ from math import asin, atan, cos, radians, sin, sqrt, tan - AXIS_A = 6378137.0 AXIS_B = 6356752.314245 RADIUS = 6378137 + def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> float: """ Calculate great circle distance between two points in a sphere, diff --git a/geodesy/lamberts_ellipsoidal_distance.py b/geodesy/lamberts_ellipsoidal_distance.py index e995ac5f597e..62ce59bb476f 100644 --- a/geodesy/lamberts_ellipsoidal_distance.py +++ b/geodesy/lamberts_ellipsoidal_distance.py @@ -2,7 +2,6 @@ from .haversine_distance import haversine_distance - AXIS_A = 6378137.0 AXIS_B = 6356752.314245 EQUATORIAL_RADIUS = 6378137 diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index 347895e7b62a..2b701283f166 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -19,7 +19,6 @@ from matplotlib import animation from matplotlib import pyplot as plt - # Frame rate of the animation INTERVAL = 20 diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py index 2383217ef745..616f6f122f97 100644 --- a/project_euler/problem_125/sol1.py +++ b/project_euler/problem_125/sol1.py @@ -15,6 +15,7 @@ LIMIT = 10**8 + def is_palindrome(n: int) -> bool: """ Check if an integer is palindromic. diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 7f90fad7f3e1..832b6162f349 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -5,7 +5,6 @@ """ from __future__ import annotations - RADIX = 10 From db1007b2b55a88d9feb53a13505bacaad67778f6 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sat, 15 Oct 2022 20:54:20 +0100 Subject: [PATCH 03/12] chore: Fix visual indentation --- conversions/decimal_to_any.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index c703527426cd..d8d4ba1f0b53 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -3,10 +3,10 @@ # fmt: off ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', # noqa: E501 - '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', - '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', - '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', - '34': 'Y', '35': 'Z'} + '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', + '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', + '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', + '34': 'Y', '35': 'Z'} # fmt: on From 698cf30f14a600de788a9d2ab252fa1f68cc421a Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 00:26:55 +0100 Subject: [PATCH 04/12] refactor: Replace char constant with `string.ascii_uppercase` --- ciphers/brute_force_caesar_cipher.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ciphers/brute_force_caesar_cipher.py b/ciphers/brute_force_caesar_cipher.py index 6542c6859687..458d08db2628 100644 --- a/ciphers/brute_force_caesar_cipher.py +++ b/ciphers/brute_force_caesar_cipher.py @@ -1,4 +1,4 @@ -LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +import string def decrypt(message: str) -> None: @@ -31,15 +31,15 @@ def decrypt(message: str) -> None: Decryption using Key #24: VOFGVWZ ROFXW Decryption using Key #25: UNEFUVY QNEWV """ - for key in range(len(LETTERS)): + for key in range(len(string.ascii_uppercase)): translated = "" for symbol in message: - if symbol in LETTERS: - num = LETTERS.find(symbol) + if symbol in string.ascii_uppercase: + num = string.ascii_uppercase.find(symbol) num = num - key if num < 0: - num = num + len(LETTERS) - translated = translated + LETTERS[num] + num = num + len(string.ascii_uppercase) + translated = translated + string.ascii_uppercase[num] else: translated = translated + symbol print(f"Decryption using Key #{key}: {translated}") From b8bef862a98c61384e9c23253c647757c01c2c98 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Sun, 16 Oct 2022 11:03:31 +0530 Subject: [PATCH 05/12] chore: remove fmt: on/off --- conversions/decimal_to_any.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index d8d4ba1f0b53..74d344dcae22 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -1,13 +1,11 @@ """Convert a positive Decimal Number to Any Other Representation""" -# fmt: off -ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', # noqa: E501 +ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', '34': 'Y', '35': 'Z'} -# fmt: on def decimal_to_any(num: int, base: int) -> str: From d8d712de39c8952874bc1f4d16b4145fb92ce5f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 05:34:22 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/decimal_to_any.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index 74d344dcae22..4617f954d4bc 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -1,11 +1,34 @@ """Convert a positive Decimal Number to Any Other Representation""" -ALPHABET_VALUES = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F', - '16': 'G', '17': 'H', '18': 'I', '19': 'J', '20': 'K', '21': 'L', - '22': 'M', '23': 'N', '24': 'O', '25': 'P', '26': 'Q', '27': 'R', - '28': 'S', '29': 'T', '30': 'U', '31': 'V', '32': 'W', '33': 'X', - '34': 'Y', '35': 'Z'} +ALPHABET_VALUES = { + "10": "A", + "11": "B", + "12": "C", + "13": "D", + "14": "E", + "15": "F", + "16": "G", + "17": "H", + "18": "I", + "19": "J", + "20": "K", + "21": "L", + "22": "M", + "23": "N", + "24": "O", + "25": "P", + "26": "Q", + "27": "R", + "28": "S", + "29": "T", + "30": "U", + "31": "V", + "32": "W", + "33": "X", + "34": "Y", + "35": "Z", +} def decimal_to_any(num: int, base: int) -> str: From 93f75d1c93b2a4a68346de54a9f7b4e7d171be5f Mon Sep 17 00:00:00 2001 From: Caeden Date: Sun, 16 Oct 2022 10:03:06 +0100 Subject: [PATCH 07/12] Update compression/peak_signal_to_noise_ratio.py Co-authored-by: Christian Clauss --- compression/peak_signal_to_noise_ratio.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index f09cc0854552..ce5513e40c52 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -19,8 +19,7 @@ def psnr(original: float, contrast: float) -> float: if mse == 0: return 100 - psnr = 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) - return psnr + return 20 * math.log10(PIXEL_MAX / math.sqrt(mse)) def main() -> None: From e048d017fb7e89ebdebd08729dd76bffb2cae67f Mon Sep 17 00:00:00 2001 From: Caeden Date: Sun, 16 Oct 2022 10:03:16 +0100 Subject: [PATCH 08/12] Update compression/peak_signal_to_noise_ratio.py Co-authored-by: Christian Clauss --- compression/peak_signal_to_noise_ratio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index ce5513e40c52..4c775271e920 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -14,7 +14,7 @@ PIXEL_MAX = 255.0 -def psnr(original: float, contrast: float) -> float: +def peak_signal_to_noise_ratio(original: float, contrast: float) -> float: mse = np.mean((original - contrast) ** 2) if mse == 0: return 100 From 974476d46d1d085881c250e17e2f4460573f5cb5 Mon Sep 17 00:00:00 2001 From: Caeden Date: Sun, 16 Oct 2022 10:03:26 +0100 Subject: [PATCH 09/12] Update conversions/decimal_to_any.py Co-authored-by: Christian Clauss --- conversions/decimal_to_any.py | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index 4617f954d4bc..908c89e8fb6b 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -1,34 +1,8 @@ """Convert a positive Decimal Number to Any Other Representation""" +from string import ascii_uppercase -ALPHABET_VALUES = { - "10": "A", - "11": "B", - "12": "C", - "13": "D", - "14": "E", - "15": "F", - "16": "G", - "17": "H", - "18": "I", - "19": "J", - "20": "K", - "21": "L", - "22": "M", - "23": "N", - "24": "O", - "25": "P", - "26": "Q", - "27": "R", - "28": "S", - "29": "T", - "30": "U", - "31": "V", - "32": "W", - "33": "X", - "34": "Y", - "35": "Z", -} +ALPHABET_VALUES = {str(ord(c) - 55): c for c in ascii_uppercase} def decimal_to_any(num: int, base: int) -> str: From 9a167fa06e50d9be6519cd1b3dacbe8a51e24fe5 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 10:12:03 +0100 Subject: [PATCH 10/12] refactor: Create constant variable from url --- web_programming/fetch_quotes.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/web_programming/fetch_quotes.py b/web_programming/fetch_quotes.py index eb8f58b4d0e6..cd21c2eed8d9 100644 --- a/web_programming/fetch_quotes.py +++ b/web_programming/fetch_quotes.py @@ -11,14 +11,15 @@ import requests +API_ENDPOINT_URL = "https://zenquotes.io/api" + + def quote_of_the_day() -> list: - api_endpoint_url = "https://zenquotes.io/api/today/" - return requests.get(api_endpoint_url).json() + return requests.get(API_ENDPOINT_URL + "/today").json() def random_quotes() -> list: - api_endpoint_url = "https://zenquotes.io/api/random/" - return requests.get(api_endpoint_url).json() + return requests.get(API_ENDPOINT_URL + "/random").json() if __name__ == "__main__": From 9cdecf87426499639f98e797f6cf6af97c628a65 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 09:13:05 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/fetch_quotes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/web_programming/fetch_quotes.py b/web_programming/fetch_quotes.py index cd21c2eed8d9..d557e2d95e74 100644 --- a/web_programming/fetch_quotes.py +++ b/web_programming/fetch_quotes.py @@ -10,7 +10,6 @@ import requests - API_ENDPOINT_URL = "https://zenquotes.io/api" From 34c3fd2e7c2671c32fd0d729d3ebd13b84f879b0 Mon Sep 17 00:00:00 2001 From: CaedenPH Date: Sun, 16 Oct 2022 10:16:04 +0100 Subject: [PATCH 12/12] refactor: Rename `psnr` to `peak_signal_to_noise_ratio` --- compression/peak_signal_to_noise_ratio.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compression/peak_signal_to_noise_ratio.py b/compression/peak_signal_to_noise_ratio.py index 4c775271e920..284f2904a21d 100644 --- a/compression/peak_signal_to_noise_ratio.py +++ b/compression/peak_signal_to_noise_ratio.py @@ -35,11 +35,11 @@ def main() -> None: # Value expected: 29.73dB print("-- First Test --") - print(f"PSNR value is {psnr(original, contrast)} dB") + print(f"PSNR value is {peak_signal_to_noise_ratio(original, contrast)} dB") # # Value expected: 31.53dB (Wikipedia Example) print("\n-- Second Test --") - print(f"PSNR value is {psnr(original2, contrast2)} dB") + print(f"PSNR value is {peak_signal_to_noise_ratio(original2, contrast2)} dB") if __name__ == "__main__":