From da6f1676d89dc5751fb29358c85911a4249fd489 Mon Sep 17 00:00:00 2001 From: Shanjidul Islam Sadhin Date: Thu, 10 Oct 2024 20:08:36 +0000 Subject: [PATCH 1/2] Hacktoberfest contribution. Added Faster way to revers a list in python with proper docstring explaination. --- easyPythonpi/methods/array.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/easyPythonpi/methods/array.py b/easyPythonpi/methods/array.py index 5722421..a9576d4 100644 --- a/easyPythonpi/methods/array.py +++ b/easyPythonpi/methods/array.py @@ -34,3 +34,26 @@ def arrayrev(array:'list')->'list': return reversed_array + +def fast_arrayrev(array: 'list') -> 'list': + """ + Reverses the elements of the input list. + + Parameters: + array (list): The list to be reversed. + + Returns: + list: A new list containing the elements of the input list in reverse order. + + Steps: + 1. The function takes a single argument, `array`, which is expected to be a list. + 2. It uses Python's slicing feature to reverse the list: + - The slice notation `array[::-1]` means: + - Start from the end of the list (indicated by the negative step). + - Move backwards through the list. + - The result is a new list that contains the elements of `array` in reverse order. + 3. The reversed list is stored in the variable `reversed_array`. + 4. Finally, the function returns `reversed_array`, which is the reversed version of the input list. + """ + reversed_array = array[::-1] + return reversed_array \ No newline at end of file From fcfcba67cd9e0263a6326f96de0ca59aeea1cbb1 Mon Sep 17 00:00:00 2001 From: Shanjidul Islam Sadhin Date: Thu, 10 Oct 2024 20:11:20 +0000 Subject: [PATCH 2/2] Hacktoberfest contribution. Update the docstring --- easyPythonpi/methods/array.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/easyPythonpi/methods/array.py b/easyPythonpi/methods/array.py index a9576d4..c5aee41 100644 --- a/easyPythonpi/methods/array.py +++ b/easyPythonpi/methods/array.py @@ -49,11 +49,12 @@ def fast_arrayrev(array: 'list') -> 'list': 1. The function takes a single argument, `array`, which is expected to be a list. 2. It uses Python's slicing feature to reverse the list: - The slice notation `array[::-1]` means: - - Start from the end of the list (indicated by the negative step). + - Start[start::] from the end [:end:] of the list (indicated by the negative step)[start : end : -1]. - Move backwards through the list. - The result is a new list that contains the elements of `array` in reverse order. 3. The reversed list is stored in the variable `reversed_array`. 4. Finally, the function returns `reversed_array`, which is the reversed version of the input list. + source: https://www.geeksforgeeks.org/python-reversed-vs-1-which-one-is-faster/ """ reversed_array = array[::-1] return reversed_array \ No newline at end of file