From d705e8598c2c468bd3fe9ca79e063dc35aebb721 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 19 Apr 2023 03:25:47 +0800 Subject: [PATCH 1/2] Fix typo in Arrays.py line 31 --- Data Structures - Arrays/Arrays.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Data Structures - Arrays/Arrays.py b/Data Structures - Arrays/Arrays.py index 7a2b464..f350db1 100644 --- a/Data Structures - Arrays/Arrays.py +++ b/Data Structures - Arrays/Arrays.py @@ -28,7 +28,7 @@ #pop() Removes the element at the specified position #remove() Removes the first item with the specified value #reverse() Reverses the order of the list -#ort() Sorts the list +#sort() Sorts the list #List objects are implemented as arrays. #They are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) From fc447a2493764ee5dc8c0646f09241a948fe2a0f Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 22 Apr 2023 02:00:45 +0800 Subject: [PATCH 2/2] Fix First Recurring Character.py --- .../First Recurring Character.py | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Data Structures - Hashtables/First Recurring Character.py b/Data Structures - Hashtables/First Recurring Character.py index 27f36d7..b7910b2 100644 --- a/Data Structures - Hashtables/First Recurring Character.py +++ b/Data Structures - Hashtables/First Recurring Character.py @@ -1,21 +1,28 @@ -def func(mylist): +def first_recurring_character(array): - for i in range(0,len(mylist)): - for j in range(i+1,len(mylist)): - if mylist[i] == mylist[j]: - return mylist[i] - return 0 + total_items = len(array)-1 + first_RC = None + for i in range(total_items): + for j in range(i+1, total_items): + if array[i] == array[j]: + if first_RC is None or first_RC > j: + first_RC = j + if first_RC: + return array[first_RC] + return None -def hashtable(mylist): - mydict = {} - for i in range(0,len(mylist)): - if mylist[i] in mydict: - return mylist[i] +def first_recurring_character_hashtable(array): + my_dict = {} + for i in range(0,len(array)): + if array[i] in my_dict: + return array[i] else: - mydict[mylist[i]]=i - return 0 + my_dict[array[i]]=i + return None -mylist = [2,1,1,2,3,4,5] -x = hashtable(mylist) -print(x) +my_list = [2,1,1,2,3,4,5] # Output 1 instead of 2 +first_RC = first_recurring_character(my_list) +first_RC_hashtable = first_recurring_character_hashtable(my_list) +print(first_RC) +print(first_RC_hashtable)