Lecture "Brute-force algorithms", exercise 5
essepuntato opened this issue · 12 comments
Write in Python the function def my_reversed(input_list)
which behave like the built-in function reversed()
introduced in Section "Insertion sort" and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function reversed()
in the implementation.
def test_reverse_item(items,expected):
if reverse_item(items) == expected:
return True
else:
return False
def reverse_item(items):
last_position= len(items) - 1
new_list= []
for position, item in enumerate(items):
new_list.append(items[last_position])
ast_position = last_position - 1
return new_list
print(test_reverse_item([1,2,4],[4,2,1]))
print(test_reverse_item(["a",5,2],[2,5,"a"]))
I was a little stuck and @Hizkie's answer was very helpful. I did not see the need for enumerating the original list, and it worked with doing so. But I can definitely see how doing so is "safer" because it introduces the constraint that Hizkiel defined as " if(position>=0): ".
def my_reversed(input_list):
result = list() # new empty list where to store the resulting list
position = len(input_list) - 1 # the position of the last item in original input_list -> the item that should be first in the new result list
for item in input_list: # for each item in the original input_list -> this means it will run the same number of times as there are items in the original list
result.append(input_list[position]) # append the item that is in the position specified (in first iteration, in last position)
position -= 1 # decrease the position by one
return result
# accompany the function with the related test case
# Test case for the algorithm
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
# Different test runs
print(test_my_reversed([15, 38, 56, 4], [4, 56, 38, 15]))
print(test_my_reversed(["A", "B"], ["B", "A"]))
print(test_my_reversed(["A", 78, "Hola"], ["Hola", 78, "A"]))
print(test_my_reversed([10, 78, "Sol"], ["Sol", 10, 78]))
# True
# True
# True
# False
Thanks to @delfimpandiani for suggesting me to use "input_list[new_position]"!
def test_my_reverse(input_list, expected):
if expected == my_reverse(input_list):
return True
else:
return False
def my_reverse(input_list):
result = []
new_position = len(input_list) - 1
for item in input_list:
result.append(input_list[new_position])
new_position -= 1
return result
print(test_my_reverse(["a", "b", "c"], ["c", "b", "a"]))
print(test_my_reverse(["e", "f", "g"], ["f", "e", "g"]))
True
False
# Test case for the algorithm
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
# Code of the algorithm
def my_reversed(input_list):
# return a list with the elements of the input_list
# sorted in the opposite order
result = list() # the list to return
for item in input_list:
result.append(item)
result.reverse()
return result
# Run some tests
print(test_my_reversed([0, 1, 2], [2, 1, 0]))
print(test_my_reversed([0, 1, 2], [2, 1]))
True
False
# Testing Algorithm
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
# Algorithm
def my_reversed(input_list):
reversed_list = []
reversed_list.extend(input_list)
tmp_value = len(input_list) - 1
for item in input_list:
position = tmp_value - input_list.index(item)
reversed_list[position] = item
return reversed_list
# Testing Input
print(test_my_reversed([0,1,2], [2,1,0]))
print(test_my_reversed([0,1,2,3], [3,2,1,0]))
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
def my_reversed(input_list):
reverse_list = []
max_value = len(input_list) - 1
for position, item in enumerate(input_list):
if position >= 0:
reverse_list.append(input_list[max_value])
max_value = max_value - 1
return reverse_list
print(test_my_reversed([1, 2, 3, 4], [4, 3, 2, 1])) #true
print(test_my_reversed(["a", "b", "c", "d"], ["d", "c", "b", "a"])) #true
print(test_my_reversed(["a", 2, "c", 4], [4, "c", 2, "a"])) #true
print(test_my_reversed(["a", 1, 2, 4], ["a", "b", 3, "a"])) #false
thanks @delfimpandiani for taking time adding code's explanation
#Write in Python the function def my_reversed(input_list)
#which behave like the built-in function reversed()
#introduced in Section "Insertion sort" and returns a proper list,
# and accompany the function with the related test case.
#It is not possible to use the built-in function reversed() in the implementation.
#test
def test_myreverse(oldlist,expected):
result= myreverse(oldlist)
if result == expected:
return True
else:
return False
#code
def myreverse(oldlist):
newlist = []
j = len(oldlist)-1
for i in lst:
newlist.append(oldlist[j])
j -=1
return newlist
print(test_myreverse(["a","b","c","d"],['d', 'c', 'b', 'a'])) #true
return my_reversed(input_list) == expected
def my_reversed(input_list):
reversed_list = []
for i in range(len(input_list)):
pos_wanted = len(input_list) - i - 1
item_wanted = input_list[pos_wanted]
reversed_list.append(item_wanted)
return reversed_list
l1 = [1,3,5,7,9]
expected = list(reversed(l1))
print(test_my_reversed(l1,expected))
def my_reversed(input_list):
if input_list(items,expected):
result = my_reversed(input_list)
return True
else:
return False
def my_reversed(input_list) : []
def my_reversed(input_list) :
return input_list[::-1]
print(my_reversed([1,3,5]))
output = [5,3,1] True
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
# Test case for the algorithm
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if expected == result:
return True
else:
return False
# Code of the algorithm
def my_reversed(input_list):
l = list()
for item in input_list:
l.insert(0, item)
return l
print(test_my_reversed([], []))
print(test_my_reversed([1, 2, 4, 3, 4, 7, 2], [2, 7, 4, 3, 4, 2, 1]))
print(test_my_reversed(["a", "b", "c", "d"], ["d", "c", "b", "a"]))
Some comments:
-
Some of you have specified an
if
block within the foreach loop with an enumeration of the element of the list, e.g.:for position, item in enumerate(input_list): if position >= 0: # do something
The point is that
position
will be always greater than or equal to 0 becauseenumerate
will anumerate the various positions in the list starting from 0, which is the very first position. If the list is empty, then no iteration of the foreach loop will be executed at all. -
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.
-
tests on different, even unusual, situations: writing the right tests is very important since, as you know, it would allow you to catch specific wrong behaviours of the algorithm in presence of specific input values. Just to introduce an example for this algorithm, try to test your function by using different kinds of inputs, including the unusual ones, such as the empty list and a list in which some elements are repeated (which is a typical characteristic of lists), and see if your function is still working as expected.