-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Brute-force argorithms", exercise 3 #18
Comments
|
I'm still confused and only have some rough ideas about it. Anyway I took my def my_enumerate(input_list):
result= list()
for item in input_list:
position_of_item= input_list.index(item)
result.append((position_of_item, item))
return result
# Test case
def test(input_list, expected):
if my_enumerate(input_list)== expected:
return True
else:
return False
# Three test runs
print(test(["Siamese", "Ragdoll", "Chartreux", "Persian"], [(0, "Siamese"), (1, "Ragdoll"), (2, "Chartreux"), (3, "Persian")]))
print(test(["Coliie", "Doberman", "Akita", "Shiba"], [(0, "Coliie"), (1, "Doberman"), (2, "Akita"), (3, "Shiba")]))
print(test(["Coliie", "Doberman", "Akita", "Shiba"], [(2, "Coliie"), (3, "Doberman"), (4, "Akita"), (5, "Shiba")])) |
|
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 string in input_list:
output_list.append(string)
output_list.append(input_list.index(string))
return output_list
#Two test runs
print (test_my_enumerate((["a", "b", "c"]), (["a", 0, "b", 1, "c", 2])))
print (test_my_enumerate((["Artic Monkeys", "The Radiohead", "The Beatles"]), (["Artic Monkeys", 0, "The Radiohead", 1, "The Beatles", 2])))
#Console output
True
True |
#Test case for the algorithm
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 i in range(len(input_list)):
output_list.append(input_list[i])
output_list.append(i)
return output_list
print(test_my_enumerate((["a", "b", "c"]), (["a", 0, "b", 1, "c", 2])))
print(test_my_enumerate((["a", "a", "b"]), (["a", 0, "a", 1, "b", 2])))
|
|
|
def test_my_enumerate (input_list, expected): def my_enumerate(input_list):
print(test_my_enumerate(["a", "b", "c"], [(0, 'a'), (1, 'b'), (2, 'c')])) |
def my_enumerate(input_list):
output_list = []
for i in range(len(input_list)):
output_list.append((i, input_list[i]))
return output_list
def test_my_enumerate(input_list,expected):
if my_enumerate(input_list)== expected:
return True
else:
return False
print(test_my_enumerate(['a','b','c','d'],[(0,'a'),(1,'b'),(2,'c'),(3,'d')]))
print(test_my_enumerate(['1','2','3','4'],[(0,'1'),(1,'2'),(2,'3'),(3,'4')])) |
|
|
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if expected == result:
return True
else:
return False
#First try
def my_enumerate(input_list):
new_list = []
i = -1
for item in input_list:
i += 1
new_tuple = (i, item)
new_list.append(new_tuple)
return new_list
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))
#Second try
def my_enumerate(input_list):
new_list = []
for i in range(len(input_list)):
new_tuple = (i, input_list[i])
new_list.append(new_tuple)
return new_list
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")])) |
Hi all, Some hits on how to run some tests (try on your code and see if everything still works:
|
Write in Python the function
def my_enumerate(input_list)
, which behaves like the built-in functionenumerate()
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 functionenumerate()
in the implementation.The text was updated successfully, but these errors were encountered: