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/25] 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/25] 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/25] 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/25] [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/25] 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/25] [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/25] 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/25] [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/25] 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/25] 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/25] [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/25] 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/25] [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/25] 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/25] [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/25] 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/25] [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() From 364b1daae912c1818144baa1e66e6f7a4f452116 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Thu, 5 Oct 2023 18:45:05 -0700 Subject: [PATCH 18/25] Delete hashes/fletcher32.py --- hashes/fletcher32.py | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 hashes/fletcher32.py diff --git a/hashes/fletcher32.py b/hashes/fletcher32.py deleted file mode 100644 index 0470777f6ac0..000000000000 --- a/hashes/fletcher32.py +++ /dev/null @@ -1,40 +0,0 @@ -""" -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() From b64f1de3450cd15f99ccbd4a1bd1cf79280ae9c5 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Sat, 7 Oct 2023 18:23:43 -0700 Subject: [PATCH 19/25] Update stewart.py --- maths/stewart.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/maths/stewart.py b/maths/stewart.py index c657ebefc2c3..edae5f61e959 100644 --- a/maths/stewart.py +++ b/maths/stewart.py @@ -8,7 +8,7 @@ """ -def stewart(a: float, b: float, c: float, n: float, m: float) -> float: +def stewart(lena: float, lenb: float, lenc: float, lenn: float, lenm: 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, @@ -27,6 +27,11 @@ def stewart(a: float, b: float, c: float, n: float, m: float) -> float: >>> stewart(3,2,4,1.7,1.3) 2.9308701779505686 """ + a = lena + b = lenb + c = lenc + n = lenn + m = lenm if a + b <= c or b + c <= a or a + c <= b: raise ValueError("This triangle violates the triangle inequality") if n + m != a: From c1ccfb8478a05c66e00a153f88bd09370e64f83a Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:08:56 -0700 Subject: [PATCH 20/25] Update primelib.py added tests --- maths/primelib.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/maths/primelib.py b/maths/primelib.py index cf01750cf912..b6a9eb80a634 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -38,7 +38,7 @@ """ from math import sqrt - +import doctest from maths.greatest_common_divisor import gcd_by_iterative @@ -46,6 +46,15 @@ def is_prime(number: int) -> bool: """ input: positive integer 'number' returns true if 'number' is prime otherwise false. + + >>> is_prime(97) + True + >>> is_prime(9991) + False + >>> is_prime(-3) + Traceback (most recent call last): + ... + AssertionError: 'number' must been an int and positive """ # precondition @@ -588,4 +597,7 @@ def fib(n): ans += fib1 fib1 = tmp - return ans + return and + +if __name__ == "__main__": + doctest.testmod() From c5de3ff7686edeae169e86e897aa15a3817dfd20 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 06:09:22 +0000 Subject: [PATCH 21/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/primelib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/primelib.py b/maths/primelib.py index b6a9eb80a634..92619d61ac71 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -598,6 +598,6 @@ def fib(n): fib1 = tmp return and - + if __name__ == "__main__": doctest.testmod() From 24f86be26ffc2d91af1c66a9c08d9958c07edbe4 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:09:40 -0700 Subject: [PATCH 22/25] Update primelib.py --- maths/primelib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/primelib.py b/maths/primelib.py index 92619d61ac71..e4037e31c176 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -597,7 +597,7 @@ def fib(n): ans += fib1 fib1 = tmp - return and + return ans if __name__ == "__main__": doctest.testmod() From 74381937ae2c61e53acf09912f125b7718ae7344 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 06:10:15 +0000 Subject: [PATCH 23/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/primelib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/primelib.py b/maths/primelib.py index e4037e31c176..92ebb5006627 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -599,5 +599,6 @@ def fib(n): return ans + if __name__ == "__main__": doctest.testmod() From ef337c2ff7f6705074b33e94d1d0908325504f66 Mon Sep 17 00:00:00 2001 From: rtang09 <49603415+rtang09@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:27:14 -0700 Subject: [PATCH 24/25] Update primelib.py moved the "import doctest" line into the if name == main statement --- maths/primelib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/primelib.py b/maths/primelib.py index 92ebb5006627..263317616b5f 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -38,7 +38,6 @@ """ from math import sqrt -import doctest from maths.greatest_common_divisor import gcd_by_iterative @@ -601,4 +600,5 @@ def fib(n): if __name__ == "__main__": + import doctest doctest.testmod() From f89f7cee152c3c4ccbd961b1433a5dc9adf6cdc4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 06:27:46 +0000 Subject: [PATCH 25/25] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/primelib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/primelib.py b/maths/primelib.py index 263317616b5f..bb5c21078f5b 100644 --- a/maths/primelib.py +++ b/maths/primelib.py @@ -601,4 +601,5 @@ def fib(n): if __name__ == "__main__": import doctest + doctest.testmod()