Lecture "Brute-force algorithms", exercise 3
Opened this issue · 15 comments
Write in Python the function def my_enumerate(input_list)
which behaves like the built-in function enumerate()
introduced in Section "Linear search" and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function enumerate()
in the implementation.
def test_my_enumerate(input_list):
for tuple in my_enumerate(input_list):
item = input_list[tuple[0]]
if tuple[1] == item:
continue
return False
return True
def my_enumerate(input_list):
enumerate_list = []
for item in input_list:
index = input_list.index(item)
tuple = (index, item)
enumerate_list.append(tuple)
return enumerate_list
# This is the function that enumerates without the use of "enumerate"
def my_enumerate(input_list):
myenum=list()
n=0
for item in input_list:
myenum.append((n,item))
n=n+1
return myenum
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if expected == result:
return True
else:
return False
print(test_my_enumerate(["Rick","Morty","Mr.Poopybutthole","Mr.Meeseeks"],[(0,"Rick"),(1,"Morty"),(2,"Mr.Poopybutthole"),(3,"Mr.Meeseeks")]))
def test_my_enumerate(input_list,expected):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False
def my_enumerate(input_list):
result = []
for item in input_list:
list = []
idx = input_list.index(item)
list.append(idx)
list.append(item)
tupl = tuple(list)
result.append(tupl)
return result
print(test_my_enumerate(["cat", "dog", "lion","fish"],[(0,"cat"), (1,"dog"), (2,"lion"), (3,"fish")]))
print(test_my_enumerate(["cat", "dog", "lion","fish"],[(3,"cat"), (2,"dog"), (1,"lion"), (0,"fish")]))
def my_enumerate(input_list):
list_item = []
my_tuple = ()
for item in input_list:
my_tuple = 0
my_tuple = (input_list.index(item), item)
list_item.append(my_tuple)
return list_item
print(my_enumerate(['cane', 'gatto', 'asino','topo']))
me and Giorgia Sampò did it together
👭
# Write in Python the function def my_enumerate(input_list)
# which behaves like the built-in function enumerate() introduced
# in Section "Linear search" and returns a proper list, and
# accompany the function with the related test case. It is not
# possible to use the built-in function enumerate() in the implementation.
# enumerate has input = list and output [(position, value), (position, value)]
def my_enumerate(input_list):
enum_object = [] # creates empty enum obj which will store result
position = 0 # sets index to 0 to start the count
for item in input_list: # loops through items in list
iteration_value = tuple((position, item)) # creates a tuple with index, value
enum_object.append(iteration_value) # appends tuple to final result
position += 1 # adds 1 to the indexing
return enum_object # returns final result in list format
listOne = [1, 2, 3, 4]
listTwo = ["a", "b", "c", "d"]
print(my_enumerate(listOne)) # output: [(0, 1), (1, 2), (2, 3), (3, 4)]
print(my_enumerate(listTwo)) # output: [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
def test_my_enumerate (input_list, expected):
result = my_enumerate(input_list)
if expected == result:
return True
else:
return False
def my_enumerate(input_list):
output_list = list()
for item in input_list:
output_list.append(input_list.index(item))
output_list.append(item)
return output_list
print(test_my_enumerate(["Wild Light", "No Man's Sky", "The Fall of Math", "replicr"], [0, "Wild Light", 1, "No Man's Sky",2, "The Fall of Math", 3, "replicr"]))
print(test_my_enumerate(["Nostalghia", "Stalker", "The Mirror", "Solaris"], [0,"Nostalghia", 1, "Stalker", 2, "The Mirror", 3, "Solaris"]))
#This is the test for the function
def test_enumeration(input_list, expected):
result = enumeration(input_list)
if result == expected:
return True
else:
return False
print(test_enumeration(list1, [(0, 'Harry'), (1, 'Ron'), (2, 'Hermione'), (3, 'Snape')]))
#This is the function that enumerates
def enumeration(input_list):
my_numeration=list()
n=0
for item in input_list:
my_numeration.append((n,item))
n=n+1
return my_numeration
list1= ['Harry', 'Ron', 'Hermione', 'Snape']
print(my_numeraton(list1))
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False
def my_enumerate(input_list):
position = 0
output_list = list()
for item in input_list:
output_list.append((position, item))
position += 1
return output_list
print(test_my_enumerate(list(["Minerva", "Ginny", "Hermione", "Luna", "Hedwig"]),
[(0, "Minerva"), (1, "Ginny"), (2, "Hermione"), (3, "Luna"), (4, "Hedwig")]))
print(test_my_enumerate(list(["Leonardo", "Michelangelo", "Donatello", "Raffaello"]),
[(0, 'Leonardo'), (1, 'Michelangelo'), (2, 'Donatello'), (3, 'Raffaello')]))
def test_my_enumerate(input_list, expected):
res = my_enumerate(input_list)
if expected == res:
return True
else:
return False
def my_enumerate(input_list):
my_tuple = list()
position = 0
for item in input_list:
my_tuple.append((position , item))
position +=1
return my_tuple
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")])) #returns True
#test
def test_my_enumerate (input_list, output_list):
result = my_enumerate (input_list)
if result == output_list:
return True
else:
return False
#algorithm
def my_enumerate (input_list):
output_list = list()
position = 0
for item in input_list:
my_tuple = (position, item)
output_list.append(my_tuple)
position += 1
return output_list
#variables for short
my_list = ['Slater', 'Murgi', 'Giammei', 'Fleur', 'Virginia', 'Chiru']
my_enumeration = [(0, 'Slater'), (1, 'Murgi'), (2, 'Giammei'), (3, 'Fleur'), (4, 'Virginia'), (5, 'Chiru')]
#some test runs
print(test_my_enumerate(my_list, my_enumeration))
print(test_my_enumerate(['Pink Floyd', 'Bach', 'Haendel', 'Vivaldi', 'Monteverdi', 'Marenzio'], [(0, 'Pink Floyd'), (1, 'Bach'), (2, 'Haendel'), (3, 'Vivaldi'), (4, 'Monteverdi'), (5, 'Marenzio')]))
print(test_my_enumerate(list(), list()))
print(test_my_enumerate(['Marte'], [(0, 'Marte')]))
#execution algorithm
print(my_list)
print(my_enumerate(my_list))
# test
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False
# code
def my_enumerate(input_list):
output_list = list()
for item in input_list:
index = input_list.index(item) # it returns the position od the item that is running in the algorithm
output_tuple = (index, item)
output_list.append(output_tuple)
return output_list
print(test_my_enumerate(["a", "b", "c"], [(0, 'a'), (1, 'b'), (2, 'c')])) # return true
print(test_my_enumerate(["chico"], [(0, 'chico')])) #return True
print(test_my_enumerate([""], [(0, 'a')])) #return False ```
# function
def my_enumerate(input_list):
my_enumerated_list=list()
position = 0
for item in input_list:
my_enumerated_list.append((position, item))
position=position + 1
return my_enumerated_list
# test function
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if result == expected:
print(True)
else:
print(False)
#test that function works properly
test_my_enumerate(['William', 'Sarah', 'Lilly'], [(0, 'William'), (1, 'Sarah'), (2, 'Lilly')])
#print various enumerated lists
print(my_enumerate(['William', 'Sarah', 'Lilly']))
print(my_enumerate(['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']))
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if result == expected:
return True
return False
def my_enumerate(input_list):
enumerate_list = [(number, input_list[number]) for number in range(len(input_list))]
return enumerate_list
print(test_my_enumerate(['a', 'b'], [(0, 'a'), (1, 'b')]))
print(test_my_enumerate(['ciao', 'anche', 'a', 'te'], [(0, 'ciao'), (1, 'anche'), (2, 'a'), (3, 'te')]))
print(test_my_enumerate([70, 26], [(0, 70), (1, 26)]))
`color_list = ["green", "red", "black", "blue", "orange", "yellow"]
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False
def my_enumerate(input_list):
my_enumerate_list = []
for item in input_list:
ind = input_list.index(item)
new_item = ind, item
my_enumerate_list.append(new_item)
return my_enumerate_list
print(my_enumerate(color_list))
print(test_my_enumerate(color_list, [(0, 'green'), (1, 'red'), (2, 'black'), (3, 'blue'), (4, 'orange'), (5, 'yellow')]))
`
Hi all,
A general comment: for all of you that have used the method index
, can you please try your function by using the following input, to see if it works correctly?
["a", "b", "a", "b", "a", "b", "c"]