From f872398d0ca308a315eb52eae80e5610854d2417 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Mon, 3 Oct 2022 02:41:24 +0530 Subject: [PATCH 01/11] added shuffled array --- other/shuffled_array.py | 71 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 other/shuffled_array.py diff --git a/other/shuffled_array.py b/other/shuffled_array.py new file mode 100644 index 000000000000..72f2c57d211b --- /dev/null +++ b/other/shuffled_array.py @@ -0,0 +1,71 @@ +# To create a shuffled array. Generate a pseudo random number +# Swap that number with any other element in the list run this swapping len(Array) +# times to get desired shuffled array + +# To Understand more of random number generation +# follow https://en.wikipedia.org/wiki/Lagged_Fibonacci_generator + +# import doctest +import time + + +class Solution: + + """ + >>> Solution().__init__(array=[1,2,3]) + Traceback (most recent call last): + .... + TypeError: __init__() missing 1 required positional argument: 'array' + """ + + def __init__(self, array: list) -> None: + self.arr = array + self.seed = int(str(time.time())[-1:-5:-1]) + + # generating a 4 digit number randomly + # by taking the last four numbers of the system generated time + # pseudo random number generator + def prng(self, num: int) -> int: + """ + >>> Solution([56]).prng(1) + 0 + """ + if num == 1: + return 0 + self.seed *= self.seed + any = str(self.seed) + if any != "0": + self.seed = int(any[-1:-5:-1]) + else: + any = any[:] + str(len(self.arr) // 2) + if int(any[-1]) < num: + return int(any[-1]) + return self.prng(num) + + def reset(self) -> list: + # it will return the original given array + """ + >>> Solution([2,3,4,56]).reset() + [2, 3, 4, 56] + """ + return self.arr + + def shuffle(self) -> list: + # generated a pseudo number for each traversal + # and swapped the traversing value with that number + """ + >>> Solution([2]).shuffle() + [2] + """ + temp = self.arr.copy() + for i in range(len(self.arr)): + a = self.prng(len(self.arr)) + temp[a], temp[i] = temp[i], temp[a] + return temp + + +solclass = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) +shuffled_arr = solclass.shuffle() +print(shuffled_arr) + +# doctest.testmod() From e5f0cdbd355d403181ed9025c17107a990b16d30 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Mon, 3 Oct 2022 03:02:29 +0530 Subject: [PATCH 02/11] added shuffled_array.py --- other/shuffled_array.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index 72f2c57d211b..e8402819a110 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -11,12 +11,12 @@ class Solution: - """ - >>> Solution().__init__(array=[1,2,3]) - Traceback (most recent call last): - .... - TypeError: __init__() missing 1 required positional argument: 'array' - """ + # """ + # >>> Solution().__init__(array=[1,2,3]) + # Traceback (most recent call last): + # .... + # TypeError: __init__() missing 1 required positional argument: 'array' + # """ def __init__(self, array: list) -> None: self.arr = array From 922a79458cd2cd1e940110df583a320d83ea199b Mon Sep 17 00:00:00 2001 From: manuprakash Date: Mon, 3 Oct 2022 03:56:01 +0530 Subject: [PATCH 03/11] shuffled array made sugested changes --- other/shuffled_array.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index e8402819a110..c33909c3d193 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -5,7 +5,7 @@ # To Understand more of random number generation # follow https://en.wikipedia.org/wiki/Lagged_Fibonacci_generator -# import doctest +import doctest import time @@ -45,8 +45,8 @@ def prng(self, num: int) -> int: def reset(self) -> list: # it will return the original given array """ - >>> Solution([2,3,4,56]).reset() - [2, 3, 4, 56] + >>> Solution([-2, 0, 140, 15, 220]).reset() + [-2, 0, 140, 15, 220] """ return self.arr @@ -63,9 +63,9 @@ def shuffle(self) -> list: temp[a], temp[i] = temp[i], temp[a] return temp +if __name__ == '__main__': + solclass = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) + shuffled_arr = solclass.shuffle() + print(shuffled_arr) -solclass = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) -shuffled_arr = solclass.shuffle() -print(shuffled_arr) - -# doctest.testmod() + doctest.testmod() From bee580038e485573e28023c537116ea6ec5229bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 2 Oct 2022 22:27:24 +0000 Subject: [PATCH 04/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/shuffled_array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index c33909c3d193..6fb451b0b103 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -63,7 +63,8 @@ def shuffle(self) -> list: temp[a], temp[i] = temp[i], temp[a] return temp -if __name__ == '__main__': + +if __name__ == "__main__": solclass = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) shuffled_arr = solclass.shuffle() print(shuffled_arr) From 64e406276f564934d1d468deacf369cbf28e5981 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Sat, 25 Jan 2025 00:50:51 +0530 Subject: [PATCH 05/11] Update reset method docstring with new example --- other/shuffled_array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index c33909c3d193..add4dcdbd32a 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -45,8 +45,8 @@ def prng(self, num: int) -> int: def reset(self) -> list: # it will return the original given array """ - >>> Solution([-2, 0, 140, 15, 220]).reset() - [-2, 0, 140, 15, 220] + >>> Solution([-7, 0, 4, -56.7]).reset() + [-7, 0, 4, -56.7] """ return self.arr From 2ad72d24d8684cc3f4c074fa08640236e154b448 Mon Sep 17 00:00:00 2001 From: manu-prakash-choudhary Date: Fri, 24 Jan 2025 19:26:33 +0000 Subject: [PATCH 06/11] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 941e30dfe721..a3583ad941fa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -866,6 +866,7 @@ * [Quine](other/quine.py) * [Scoring Algorithm](other/scoring_algorithm.py) * [Sdes](other/sdes.py) + * [Shuffled Array](other/shuffled_array.py) * [Tower Of Hanoi](other/tower_of_hanoi.py) * [Word Search](other/word_search.py) From 78d2ffd8351541ca0ab81e9c57d72146c0c82910 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Sat, 25 Jan 2025 01:09:20 +0530 Subject: [PATCH 07/11] Rename prng method to pseudo_random_number_generator for clarity and update references --- other/shuffled_array.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index c6fd5a2ec962..a352c6c53dd6 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -25,9 +25,9 @@ def __init__(self, array: list) -> None: # generating a 4 digit number randomly # by taking the last four numbers of the system generated time # pseudo random number generator - def prng(self, num: int) -> int: + def pseudo_random_number_generator(self, num: int) -> int: """ - >>> Solution([56]).prng(1) + >>> Solution([56]).pseudo_random_number_generator(1) 0 """ if num == 1: @@ -40,7 +40,7 @@ def prng(self, num: int) -> int: any = any[:] + str(len(self.arr) // 2) if int(any[-1]) < num: return int(any[-1]) - return self.prng(num) + return self.pseudo_random_number_generator(num) def reset(self) -> list: # it will return the original given array @@ -59,14 +59,14 @@ def shuffle(self) -> list: """ temp = self.arr.copy() for i in range(len(self.arr)): - a = self.prng(len(self.arr)) + a = self.pseudo_random_number_generator(len(self.arr)) temp[a], temp[i] = temp[i], temp[a] return temp if __name__ == "__main__": - solclass = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) - shuffled_arr = solclass.shuffle() + solution_class = Solution([18, 2, 3, 4, 5, 7, 8, 10, 21]) + shuffled_arr = solution_class.shuffle() print(shuffled_arr) doctest.testmod() From 72559cfd54d4eaff5715280a17fd4b93f4fc8e75 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Jan 2025 19:46:17 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/shuffled_array.py | 1 - 1 file changed, 1 deletion(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index a352c6c53dd6..aa34c7c839d7 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -10,7 +10,6 @@ class Solution: - # """ # >>> Solution().__init__(array=[1,2,3]) # Traceback (most recent call last): From d902683df74181f5bd9d52ad9850f1c18ad87016 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Sat, 25 Jan 2025 01:23:52 +0530 Subject: [PATCH 09/11] Refactor shuffled_array.py to improve variable naming for clarity --- other/shuffled_array.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index a352c6c53dd6..8ac4f67de64d 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -1,5 +1,5 @@ # To create a shuffled array. Generate a pseudo random number -# Swap that number with any other element in the list run this swapping len(Array) +# Swap that number with str_square_seed other element in the list run this swapping len(Array) # times to get desired shuffled array # To Understand more of random number generation @@ -33,13 +33,13 @@ def pseudo_random_number_generator(self, num: int) -> int: if num == 1: return 0 self.seed *= self.seed - any = str(self.seed) - if any != "0": - self.seed = int(any[-1:-5:-1]) + str_square_seed = str(self.seed) + if str_square_seed != "0": + self.seed = int(str_square_seed[-1:-5:-1]) else: - any = any[:] + str(len(self.arr) // 2) - if int(any[-1]) < num: - return int(any[-1]) + str_square_seed = str_square_seed[:] + str(len(self.arr) // 2) + if int(str_square_seed[-1]) < num: + return int(str_square_seed[-1]) return self.pseudo_random_number_generator(num) def reset(self) -> list: From 47493ea0f33bde4aede960f42359a0f607c5e13d Mon Sep 17 00:00:00 2001 From: manuprakash Date: Sat, 25 Jan 2025 01:25:20 +0530 Subject: [PATCH 10/11] line tuning to short --- other/shuffled_array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index dbc5b8d43618..c8192a6f0b0a 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -1,5 +1,6 @@ # To create a shuffled array. Generate a pseudo random number -# Swap that number with str_square_seed other element in the list run this swapping len(Array) +# Swap that number with str_square_seed other element in the list run this +# swapping len(Array) # times to get desired shuffled array # To Understand more of random number generation From 608dbef31402d9a1d1452c1f8d117fc71f7e7572 Mon Sep 17 00:00:00 2001 From: manuprakash Date: Sat, 25 Jan 2025 01:25:55 +0530 Subject: [PATCH 11/11] Remove redundant comments in shuffled_array.py for clarity --- other/shuffled_array.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/other/shuffled_array.py b/other/shuffled_array.py index c8192a6f0b0a..0a2bb66a3451 100644 --- a/other/shuffled_array.py +++ b/other/shuffled_array.py @@ -1,7 +1,6 @@ # To create a shuffled array. Generate a pseudo random number # Swap that number with str_square_seed other element in the list run this -# swapping len(Array) -# times to get desired shuffled array +# swapping len(Array) times to get desired shuffled array # To Understand more of random number generation # follow https://en.wikipedia.org/wiki/Lagged_Fibonacci_generator