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/3] 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/3] 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 From 5634198af61b4efafea715266d5f0b73a4e5bdd5 Mon Sep 17 00:00:00 2001 From: Shanjidul Islam Sadhin Date: Tue, 15 Oct 2024 15:34:27 +0000 Subject: [PATCH 3/3] Hacktoberfest contribution: Add function to reverse a linked list --- easyPythonpi/methods/linkedlist.py | 55 +++++++++++++++++++----------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/easyPythonpi/methods/linkedlist.py b/easyPythonpi/methods/linkedlist.py index fd49a7c..e0d851c 100644 --- a/easyPythonpi/methods/linkedlist.py +++ b/easyPythonpi/methods/linkedlist.py @@ -2,41 +2,56 @@ #-*- coding: utf-8 -*- -#Linked list - -def create_node(data:'int')->'list': - class node: - def __init__(self,data): - self.data=data - self.next=None - - a=node(data) +#Linked list + +class Node: + def __init__(self,data): + self.data=data + self.next=None + +def create_node(data:'int')->'Node': + a=Node(data) return a + # to link a node with another node -def node_link(a:'int',b:'int'): +def node_link(a:'Node', b:'Node'): a.next=b b.next=None #a=node(data1) - - -# to count number of nodes -def count_node(head:'node')->'int': + + +# to count number of nodes +def count_node(head:'Node')->'int': + count=0 if head is None: - return 0 + return count else: temp=head - count=0 while(temp!=None): - count=count+1 - temp=temp.next - return count + count=count+1 + temp=temp.next + return count # to diplay a linked list whose header node is passed as an argument. -def display_nodes(head:'node')->'int': +def display_nodes(head:'Node')->None: t=head while t is not None: print(t.data,"->",end="") t=t.next print("NULL") +# re revrese a lined list +def revrese_lined_list(head: 'Node')->'Node': + prev = None + # a -> b -> c -> d -> e -> f -> g -> h -> None + current = head + while current is not None: + next = current.next # b -> c -> d -> so one... + + current.next = prev # a -> None + prev = current # b -> a -> None + current = next # c -> d -> e -> f -> g -> h -> None + head=prev # h -> g -> f -> e -> d -> c -> b -> a -> None + + return head \ No newline at end of file