From e300cbfb41ee369c7531af984715912ea1cb0c41 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:45:55 -0700 Subject: [PATCH 01/17] Add files via upload --- hashes/fletcher16.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 hashes/fletcher16.py diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py new file mode 100644 index 000000000000..e4bc07407cc8 --- /dev/null +++ b/hashes/fletcher16.py @@ -0,0 +1,24 @@ +''' +The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques. + +Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum +''' + +def fletcher16(data): + ''' + Loops through every character in the data and adds to two sums + + fletcher16('hello world') == 6752 + ''' + sum1 = 0 + sum2 = 0 + for character in data: + sum1 = (sum1+character)%255 + sum2 = (sum1+sum2)%255 + return (sum2 << 8) | sum1 + + +if __name__ == "__main__": + text = input("Enter a text: ") + stuffs = bytes(text, "ascii") + print(fletcher16(stuffs)) \ No newline at end of file From ac7cdba8e50186ad37d4748d9f46f42b498fd105 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 21:50:37 -0700 Subject: [PATCH 02/17] Update fletcher16.py --- hashes/fletcher16.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index e4bc07407cc8..d825a9b564e5 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -4,12 +4,12 @@ Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum ''' -def fletcher16(data): +def fletcher16(text: str) -> int: ''' Loops through every character in the data and adds to two sums - fletcher16('hello world') == 6752 ''' + data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: @@ -20,5 +20,4 @@ def fletcher16(data): if __name__ == "__main__": text = input("Enter a text: ") - stuffs = bytes(text, "ascii") - print(fletcher16(stuffs)) \ No newline at end of file + print(fletcher16(text)) From 79feadf848642fab37b0b611561225f8b068d87f Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:17:58 -0700 Subject: [PATCH 03/17] Update fletcher16.py --- hashes/fletcher16.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index d825a9b564e5..19d794b6b1bd 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -7,7 +7,15 @@ def fletcher16(text: str) -> int: ''' Loops through every character in the data and adds to two sums - fletcher16('hello world') == 6752 + + + + >>> fletcher16('hello world') + 6752 + >>> fletcher16('onethousandfourhundredthirtyfour') + 28347 + >>> fletcher16('The quick brown fox jumps over the lazy dog.') + 5655 ''' data = bytes(text, "ascii") sum1 = 0 @@ -19,5 +27,5 @@ def fletcher16(text: str) -> int: if __name__ == "__main__": - text = input("Enter a text: ") - print(fletcher16(text)) + import doctest + doctest.testmod() From f4427ba11e5d5d22c014822a438a4d1b2178efb8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 05:24:57 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/fletcher16.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index 19d794b6b1bd..eeb7eafeeafb 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -1,11 +1,12 @@ -''' +""" The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -''' +""" + def fletcher16(text: str) -> int: - ''' + """ Loops through every character in the data and adds to two sums @@ -16,16 +17,17 @@ def fletcher16(text: str) -> int: 28347 >>> fletcher16('The quick brown fox jumps over the lazy dog.') 5655 - ''' + """ data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: - sum1 = (sum1+character)%255 - sum2 = (sum1+sum2)%255 + sum1 = (sum1 + character) % 255 + sum2 = (sum1 + sum2) % 255 return (sum2 << 8) | sum1 if __name__ == "__main__": import doctest + doctest.testmod() From b4ec72f4c875084af41cb5713dd8f82274b9370f Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:27:01 -0700 Subject: [PATCH 05/17] Update fletcher16.py --- hashes/fletcher16.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index eeb7eafeeafb..c4444efd665e 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -1,12 +1,15 @@ -""" -The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques. +''' +The Fletcher checksum is an algorithm for computing a position-dependent checksum +devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] +The objective of the Fletcher checksum was to provide error-detection properties approaching +those of a cyclic redundancy check but with the lower computational effort associated with +summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -""" - +''' def fletcher16(text: str) -> int: - """ + ''' Loops through every character in the data and adds to two sums @@ -17,17 +20,16 @@ def fletcher16(text: str) -> int: 28347 >>> fletcher16('The quick brown fox jumps over the lazy dog.') 5655 - """ + ''' data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: - sum1 = (sum1 + character) % 255 - sum2 = (sum1 + sum2) % 255 + sum1 = (sum1+character)%255 + sum2 = (sum1+sum2)%255 return (sum2 << 8) | sum1 if __name__ == "__main__": import doctest - doctest.testmod() From d141feed7a54afc80e91c4c721bf622b06a43565 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 05:27:34 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/fletcher16.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index c4444efd665e..ec481f38ea0e 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -1,4 +1,4 @@ -''' +""" The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to provide error-detection properties approaching @@ -6,10 +6,11 @@ summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -''' +""" + def fletcher16(text: str) -> int: - ''' + """ Loops through every character in the data and adds to two sums @@ -20,16 +21,17 @@ def fletcher16(text: str) -> int: 28347 >>> fletcher16('The quick brown fox jumps over the lazy dog.') 5655 - ''' + """ data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: - sum1 = (sum1+character)%255 - sum2 = (sum1+sum2)%255 + sum1 = (sum1 + character) % 255 + sum2 = (sum1 + sum2) % 255 return (sum2 << 8) | sum1 if __name__ == "__main__": import doctest + doctest.testmod() From 45e79a788e6d21b778b9858bcbc0849085230b62 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:28:52 -0700 Subject: [PATCH 07/17] Update fletcher16.py --- hashes/fletcher16.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index ec481f38ea0e..dd8ae1685a77 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -1,16 +1,16 @@ -""" -The Fletcher checksum is an algorithm for computing a position-dependent checksum -devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] -The objective of the Fletcher checksum was to provide error-detection properties approaching -those of a cyclic redundancy check but with the lower computational effort associated with -summation techniques. +''' +The Fletcher checksum is an algorithm for computing a position-dependent +checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs +in the late 1970s.[1] The objective of the Fletcher checksum was to +provide error-detection properties approaching those of a cyclic +redundancy check but with the lower computational effort associated +with summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -""" - +''' def fletcher16(text: str) -> int: - """ + ''' Loops through every character in the data and adds to two sums @@ -21,17 +21,16 @@ def fletcher16(text: str) -> int: 28347 >>> fletcher16('The quick brown fox jumps over the lazy dog.') 5655 - """ + ''' data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: - sum1 = (sum1 + character) % 255 - sum2 = (sum1 + sum2) % 255 + sum1 = (sum1+character)%255 + sum2 = (sum1+sum2)%255 return (sum2 << 8) | sum1 if __name__ == "__main__": import doctest - doctest.testmod() From a28d3c72bf686e428a0ab6ccb2d40442af57e5b7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 05:29:25 +0000 Subject: [PATCH 08/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/fletcher16.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index dd8ae1685a77..0609e4d91f5c 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -1,4 +1,4 @@ -''' +""" The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to @@ -7,10 +7,11 @@ with summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -''' +""" + def fletcher16(text: str) -> int: - ''' + """ Loops through every character in the data and adds to two sums @@ -21,16 +22,17 @@ def fletcher16(text: str) -> int: 28347 >>> fletcher16('The quick brown fox jumps over the lazy dog.') 5655 - ''' + """ data = bytes(text, "ascii") sum1 = 0 sum2 = 0 for character in data: - sum1 = (sum1+character)%255 - sum2 = (sum1+sum2)%255 + sum1 = (sum1 + character) % 255 + sum2 = (sum1 + sum2) % 255 return (sum2 << 8) | sum1 if __name__ == "__main__": import doctest + doctest.testmod() From 114e415a71217f18280f4291ff0eeda9cede348d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 5 Oct 2023 08:07:46 +0200 Subject: [PATCH 09/17] Update fletcher16.py --- hashes/fletcher16.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py index 0609e4d91f5c..7c23c98d72c5 100644 --- a/hashes/fletcher16.py +++ b/hashes/fletcher16.py @@ -12,9 +12,7 @@ def fletcher16(text: str) -> int: """ - Loops through every character in the data and adds to two sums - - + Loop through every character in the data and add to two sums. >>> fletcher16('hello world') 6752 From 7a28e8963743043365a5b03946cc586d4f47ee09 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 23:25:19 -0700 Subject: [PATCH 10/17] Add files via upload --- maths/stewart.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 maths/stewart.py diff --git a/maths/stewart.py b/maths/stewart.py new file mode 100644 index 000000000000..978b087c73e6 --- /dev/null +++ b/maths/stewart.py @@ -0,0 +1,40 @@ +''' +In geometry, Stewart's theorem yields a relation between the +lengths of the sides and the length of a cevian in a triangle. +Its name is in honour of the Scottish mathematician Matthew +Stewart, who published the theorem in 1746.[1] + +Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem +''' + +def stewart(a:float,b:float,c:float,n:float,m:float) -> float: + ''' + Given the side lengths of the triangle (a,b,c), where the cevian intersects + the side with side length a and splits it into segments with lengthes n and m, + this formula finds the length of the cevian. + + >>> stewart(1,1,1,0.5,0.5) + 0.8660254037844386 + >>> stewart(1,2,3,4,5) + Traceback (most recent call last): + ... + ValueError: This triangle cannot exist because of the triangle inequality + >>> stewart(1,1,1,1,1) + Traceback (most recent call last): + ... + ValueError: n+m must equal a + >>> stewart(3,2,4,1.7,1.3) + 2.9308701779505686 + ''' + if a+b <= c or b+c <= a or a+c <= b: + raise ValueError("This triangle cannot exist because of the triangle inequality") + if n+m != a: + raise ValueError("n+m must equal a") + if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: + raise ValueError("The side lengths of a triangle have to be positive") + return ((b**2*m+c**2*n-m*a*n)/a)**0.5 + + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 7d7d0c373749b3038748b40864e1e109a655b5c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 06:28:02 +0000 Subject: [PATCH 11/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/stewart.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/maths/stewart.py b/maths/stewart.py index 978b087c73e6..5270f97f240b 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -1,14 +1,15 @@ -''' +""" In geometry, Stewart's theorem yields a relation between the lengths of the sides and the length of a cevian in a triangle. Its name is in honour of the Scottish mathematician Matthew Stewart, who published the theorem in 1746.[1] Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem -''' +""" -def stewart(a:float,b:float,c:float,n:float,m:float) -> float: - ''' + +def stewart(a: float, b: float, c: float, n: float, m: float) -> float: + """ Given the side lengths of the triangle (a,b,c), where the cevian intersects the side with side length a and splits it into segments with lengthes n and m, this formula finds the length of the cevian. @@ -25,16 +26,19 @@ def stewart(a:float,b:float,c:float,n:float,m:float) -> float: ValueError: n+m must equal a >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 - ''' - if a+b <= c or b+c <= a or a+c <= b: - raise ValueError("This triangle cannot exist because of the triangle inequality") - if n+m != a: + """ + if a + b <= c or b + c <= a or a + c <= b: + raise ValueError( + "This triangle cannot exist because of the triangle inequality" + ) + if n + m != a: raise ValueError("n+m must equal a") if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: raise ValueError("The side lengths of a triangle have to be positive") - return ((b**2*m+c**2*n-m*a*n)/a)**0.5 + return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5 if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 7b8ef6c8c59c91c5d3b3a1624cfcccfb1483cef7 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 23:32:08 -0700 Subject: [PATCH 12/17] Update stewart.py --- maths/stewart.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/maths/stewart.py b/maths/stewart.py index 5270f97f240b..0a13ac1fd265 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -1,15 +1,14 @@ -""" +''' In geometry, Stewart's theorem yields a relation between the lengths of the sides and the length of a cevian in a triangle. Its name is in honour of the Scottish mathematician Matthew Stewart, who published the theorem in 1746.[1] Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem -""" +''' - -def stewart(a: float, b: float, c: float, n: float, m: float) -> float: - """ +def stewart(a:float,b:float,c:float,n:float,m:float) -> float: + ''' Given the side lengths of the triangle (a,b,c), where the cevian intersects the side with side length a and splits it into segments with lengthes n and m, this formula finds the length of the cevian. @@ -19,26 +18,23 @@ def stewart(a: float, b: float, c: float, n: float, m: float) -> float: >>> stewart(1,2,3,4,5) Traceback (most recent call last): ... - ValueError: This triangle cannot exist because of the triangle inequality + ValueError: This triangle violates the triangle inequality >>> stewart(1,1,1,1,1) Traceback (most recent call last): ... ValueError: n+m must equal a >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 - """ - if a + b <= c or b + c <= a or a + c <= b: - raise ValueError( - "This triangle cannot exist because of the triangle inequality" - ) - if n + m != a: + ''' + if a+b <= c or b+c <= a or a+c <= b: + raise ValueError("This triangle violates the triangle inequality") + if n+m != a: raise ValueError("n+m must equal a") if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: raise ValueError("The side lengths of a triangle have to be positive") - return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5 + return ((b**2*m+c**2*n-m*a*n)/a)**0.5 if __name__ == "__main__": import doctest - doctest.testmod() From 05cdc34851e0c69fce9889f898a82b5cbd83904f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 06:32:37 +0000 Subject: [PATCH 13/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/stewart.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/maths/stewart.py b/maths/stewart.py index 0a13ac1fd265..e44e6eeab391 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -1,14 +1,15 @@ -''' +""" In geometry, Stewart's theorem yields a relation between the lengths of the sides and the length of a cevian in a triangle. Its name is in honour of the Scottish mathematician Matthew Stewart, who published the theorem in 1746.[1] Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem -''' +""" -def stewart(a:float,b:float,c:float,n:float,m:float) -> float: - ''' + +def stewart(a: float, b: float, c: float, n: float, m: float) -> float: + """ Given the side lengths of the triangle (a,b,c), where the cevian intersects the side with side length a and splits it into segments with lengthes n and m, this formula finds the length of the cevian. @@ -25,16 +26,17 @@ def stewart(a:float,b:float,c:float,n:float,m:float) -> float: ValueError: n+m must equal a >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 - ''' - if a+b <= c or b+c <= a or a+c <= b: + """ + if a + b <= c or b + c <= a or a + c <= b: raise ValueError("This triangle violates the triangle inequality") - if n+m != a: + if n + m != a: raise ValueError("n+m must equal a") if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: raise ValueError("The side lengths of a triangle have to be positive") - return ((b**2*m+c**2*n-m*a*n)/a)**0.5 + return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5 if __name__ == "__main__": import doctest + doctest.testmod() From f4f7fb51ce58e519fc86de4e403040668d3cf23d Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Wed, 4 Oct 2023 23:36:17 -0700 Subject: [PATCH 14/17] Update stewart.py --- maths/stewart.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/maths/stewart.py b/maths/stewart.py index e44e6eeab391..45e25f9bd8ac 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -1,17 +1,16 @@ -""" +''' In geometry, Stewart's theorem yields a relation between the lengths of the sides and the length of a cevian in a triangle. Its name is in honour of the Scottish mathematician Matthew Stewart, who published the theorem in 1746.[1] Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem -""" +''' - -def stewart(a: float, b: float, c: float, n: float, m: float) -> float: - """ +def stewart(a:float,b:float,c:float,n:float,m:float) -> float: + ''' Given the side lengths of the triangle (a,b,c), where the cevian intersects - the side with side length a and splits it into segments with lengthes n and m, + the side with side length a and splits it into segments with lengths n and m, this formula finds the length of the cevian. >>> stewart(1,1,1,0.5,0.5) @@ -26,17 +25,16 @@ def stewart(a: float, b: float, c: float, n: float, m: float) -> float: ValueError: n+m must equal a >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 - """ - if a + b <= c or b + c <= a or a + c <= b: + ''' + if a+b <= c or b+c <= a or a+c <= b: raise ValueError("This triangle violates the triangle inequality") - if n + m != a: + if n+m != a: raise ValueError("n+m must equal a") if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: raise ValueError("The side lengths of a triangle have to be positive") - return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5 + return ((b**2*m+c**2*n-m*a*n)/a)**0.5 if __name__ == "__main__": import doctest - doctest.testmod() From 7864c2a72a27c136e295760032bdc5a7a90d6c11 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 06:37:07 +0000 Subject: [PATCH 15/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/stewart.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/maths/stewart.py b/maths/stewart.py index 45e25f9bd8ac..c657ebefc2c3 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -1,14 +1,15 @@ -''' +""" In geometry, Stewart's theorem yields a relation between the lengths of the sides and the length of a cevian in a triangle. Its name is in honour of the Scottish mathematician Matthew Stewart, who published the theorem in 1746.[1] Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem -''' +""" -def stewart(a:float,b:float,c:float,n:float,m:float) -> float: - ''' + +def stewart(a: float, b: float, c: float, n: float, m: float) -> float: + """ Given the side lengths of the triangle (a,b,c), where the cevian intersects the side with side length a and splits it into segments with lengths n and m, this formula finds the length of the cevian. @@ -25,16 +26,17 @@ def stewart(a:float,b:float,c:float,n:float,m:float) -> float: ValueError: n+m must equal a >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 - ''' - if a+b <= c or b+c <= a or a+c <= b: + """ + if a + b <= c or b + c <= a or a + c <= b: raise ValueError("This triangle violates the triangle inequality") - if n+m != a: + if n + m != a: raise ValueError("n+m must equal a") if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0: raise ValueError("The side lengths of a triangle have to be positive") - return ((b**2*m+c**2*n-m*a*n)/a)**0.5 + return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5 if __name__ == "__main__": import doctest + doctest.testmod() From e2accc789d137af6532b55eac70612ddb262bf75 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:06:13 -0700 Subject: [PATCH 16/17] Add files via upload --- hashes/fletcher32.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 hashes/fletcher32.py diff --git a/hashes/fletcher32.py b/hashes/fletcher32.py new file mode 100644 index 000000000000..bbbb50aedd35 --- /dev/null +++ b/hashes/fletcher32.py @@ -0,0 +1,37 @@ +''' +The Fletcher checksum is an algorithm for computing a position-dependent +checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs +in the late 1970s.[1] The objective of the Fletcher checksum was to +provide error-detection properties approaching those of a cyclic +redundancy check but with the lower computational effort associated +with summation techniques. + +Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum +''' + +def fletcher32(text: str) -> int: + ''' + Turns the data into a list, and then loops them in pairs of two and adds to sums + + >>> fletcher32('Apple') + 705355030 + >>> fletcher32('ABCDEFGHI') + 3220837722 + >>> fletcher32("abcd") + 690407108 + ''' + data = bytes(text, "ascii") + sum1 = 0 + sum2 = 0 + data_list = list(data) + if len(data_list)%2 == 1: + data_list.append(0) + for idx in range(len(data_list)//2): + v = (data_list[idx*2+1] << 8) + data_list[idx*2] + sum1 = (sum1+v)%65535 + sum2 = (sum1+sum2)%65535 + return (sum2 << 16) | sum1 + +if __name__ == "__main__": + import doctest + doctest.testmod() \ No newline at end of file From 1611ec2c8e799f6f9a92fef89aaecd086d54eb80 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 5 Oct 2023 07:06:40 +0000 Subject: [PATCH 17/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/fletcher32.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/hashes/fletcher32.py b/hashes/fletcher32.py index bbbb50aedd35..0470777f6ac0 100644 --- a/hashes/fletcher32.py +++ b/hashes/fletcher32.py @@ -1,4 +1,4 @@ -''' +""" The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to @@ -7,10 +7,11 @@ with summation techniques. Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum -''' +""" + def fletcher32(text: str) -> int: - ''' + """ Turns the data into a list, and then loops them in pairs of two and adds to sums >>> fletcher32('Apple') @@ -19,19 +20,21 @@ def fletcher32(text: str) -> int: 3220837722 >>> fletcher32("abcd") 690407108 - ''' + """ data = bytes(text, "ascii") sum1 = 0 sum2 = 0 data_list = list(data) - if len(data_list)%2 == 1: + if len(data_list) % 2 == 1: data_list.append(0) - for idx in range(len(data_list)//2): - v = (data_list[idx*2+1] << 8) + data_list[idx*2] - sum1 = (sum1+v)%65535 - sum2 = (sum1+sum2)%65535 + for idx in range(len(data_list) // 2): + v = (data_list[idx * 2 + 1] << 8) + data_list[idx * 2] + sum1 = (sum1 + v) % 65535 + sum2 = (sum1 + sum2) % 65535 return (sum2 << 16) | sum1 + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod()