Open essepuntato opened 6 years ago
If I understood the prompt correctly, the point of the exercise is to create a new function called my_enumerate(), which should behave just like the built-in function enumerate()
def my_enumerate(input_list):
result = list() # new empty list where to store the resulting list
position = 0 # initialize position to the first one, 0
for item in input_list: # for each item in the original input_list
new_item = position, item
result.append(new_item) # apend the item and its position to the new list
position += 1 # increase the position by one
return result
# accompany the function with the related test case
# 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
# Three different test runs
print(test_my_enumerate([15, 38, 56, 4], [(0, 15), (1, 38), (2, 56), (3, 4)]))
print(test_my_enumerate(["A", "B", "C", "D"], [(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]))
print(test_my_enumerate(["A", 78, "Hola", 6], [(0, 'A'), (1, 78), (2, 'Hola'), (3, 6)]))
I tried to use len() as in the Insertion sort algorithm, and for once it worked! YEY!
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):
result = list()
for item in input_list:
position = len(result)
new_item = item, position
result.append(new_item)
return result
print(test_my_enumerate((["a", "b", "c"]), [('a', 0), ('b', 1), ('c', 2)]))
print(test_my_enumerate((["a", "b", "c"]), [('a', 3), ('b', 1), ('c', 2)]))
Thanks to @delfimpandiani for the patience and the help (you're a very good teacher):
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):
res_list = list()
n = 0
for item in input_list:
new_item = n, item
res_list.append(new_item)
n += 1
return res_list
input_list = ["a", "b", "c", "d"]
print(my_enumerate(input_list))
print(test_my_enumerate(["a", "b", "c", "d"], [(0, "a"), (1, "b"), (2, "c"), (3, "d")])) #true
print(test_my_enumerate(["a", "b", "c", "d"], [(0, "a"), (2, "b"), (1, "c"), (3, "d")])) #false
print(test_my_enumerate(["a", "b", "c", "d"], [(0, "a"), (1, "c"), (2, "b"), (3, "d")])) #false
Thank you @delfimpandiani and @federicafaithbologna hugs and kisses :)
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):
result = list()
for item in input_list:
position = len(result)
new_item = item, position
result.append(new_item)
return result
print(test_my_enumerate((['Miami', 'Heat', '23']), [('Miami', 0), ('Heat', 1), ('23', 2)]))
# Testing Algorithm
def test_my_enumerate(input_list, expected):
result = my_enumerate(input_list)
if expected == result:
return True
else:
return False
# Algorithm
def my_enumerate(input_list):
enumerate_object = []
for item in input_list:
position = input_list.index(item)
tmp_tuple = (position, item)
enumerate_object.append(tmp_tuple)
return enumerate_object
# Testing Input
print(test_my_enumerate(["the","ultimate","question"], [(0,"the"),(1,"ultimate"),(2,"question")]))
I had to look at my colleagues examples, but in the end I've understood the logic behind the following example
def test_my_enumerate(oldlist,expected):
result = my_enumerate(oldlist)
if result == expected:
return True
else:
return False
def my_enumerate(oldlist):
newlist = list()
position = 0
for item in oldlist:
new_list_item = position, item
newlist.append(new_list_item)
position += 1
return newlist
print(test_my_enumerate(["Ron","Hermione","Draco"],[(0,"Ron"),(1,"Hermione"),(2,"Draco")]))
def test_my_enumerate(input_list, expected):
return my_enumerate(input_list) == expected
def my_enumerate(input_list):
enumerate_list = list()
for i in range(len(input_list)):
enumerate_list.append(i)
enumerate_list.append(input_list[i])
return enumerate_list
list1 = ["Vettel", "Hamilton", "Bottas"]
list2 = ["Rossi", "Marquez", "Dovizioso"]
list3 = ["Peroni", "Italia", "Tamburini"]
expected1 = [0, "Vettel", 1, "Hamilton", 2, "Bottas"]
expected2 = [0, "Rossi", 1, "Marquez", 2, "Dovizioso"]
expected3 = [0, "Peroni", 1, "Italia", 2, "Tamburini"]
print(test_my_enumerate(list1, expected1))
print(test_my_enumerate(list2, expected2))
print(test_my_enumerate(list3, expected3))
@essepuntato shouldn't we put the built in function enumerate() as expected result?
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 = []
position = 0
for item in input_list:
new_list = position, item
result.append(new_list)
position += 1
return result
print(test_my_enumerate(["T", "R", "S", "U"], [(0, "T"), (1, "R"), (2, "S"), (3, "U")]))
print(test_my_enumerate(["Bologna", "Roma", "Napoli", "Sicilia"], [(0, "Bologna"), (3, "Roma"), (2, "Napoli"), (1, "Sicilia")]))
print(test_my_enumerate(["pizza", "pasta", "cannoli", "lasagna"], [(0, "pizza"), (1, "cannoli"), (2, "lasagna"), (3, "pasta")]))
True
False
False
def test_my_enumerate(input_list: object, expected: object):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False
def my_enumerate(input_list):
results = list()
position = 0
for item in input_list:
new_item = position, item
results.append(new_item)
position += 1
return results
print(test_my_enumerate(["red", "blue", "yellow", "pink"], [(0, "red"), (1, "blue"), (2, "yellow"), (3, "pink")])) #output: True
print(test_my_enumerate(["A", "B", "C", "D"], [(1, 'A'), (1, 'B'), (2, 'C'), (3, 'D')])) #output: False
Hi all,
here my take on the exercise (available also as Python file in the GitHub repository), with some comments:
# 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
# Code of the algorithm
def my_enumerate(input_list):
l = list()
for i in range(len(input_list)):
l.append((i, input_list[i]))
return l
print(test_my_enumerate([], []))
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))
Some comments:
@mangiafrangette I would suggest not to use enumerate
in the test, since actually that function does not return a proper list (i.e. what I've asked you to return) but rather an enumeration – which is similar to list but it is not.
test-driven development: all the tests must be passed in order to claim that an algorithm returns what it is expected. If a test execution return False, the test is not passed. If you need to check the non-compliancy of the execution of a function on purpose, then you have to create an additional testing function that returns True if the condition of the test is not passed. Remeber: first write the test, then develop the algorithm/function.
Python code and indentation: please, in your answers to the various questions, if you have to write down a Python code, be sure that the correct indent is preserved by previewing your post before to publish it. You can use the ```
environment for defining your Python code, e.g.:
```
write your Python code here
```
Write in Python the function
def my_enumerate(input_list)
which behave 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.