Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix First Recurring Character.py array implementation and typo in Arrays.py line 31 #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Data Structures - Arrays/Arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 23 additions & 16 deletions Data Structures - Hashtables/First Recurring Character.py
Original file line number Diff line number Diff line change
@@ -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)