Lecture "Brute-force algorithms", exercise 5
Opened this issue · 15 comments
essepuntato commented
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.
diegochillo commented
def my_reversed(input_list):
my_rev=list()
n=len(input_list)-1
c=0
while n>=0:
my_rev.insert(c,input_list[n])
c=c+1
n=n-1
return my_rev
def test_my_reversed(input_list,expected):
result=my_reversed(input_list)
return result==expected
print(test_my_reversed(["Birdperson","Tammy","Squanchy","Gazorpazorpfield"],["Gazorpazorpfield","Squanchy","Tammy","Birdperson"]))
gabrielefiorenza commented
def test_my_reversed(input_list,expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
def my_reversed(input_list):
result= []
idx = len(input_list) -1
for item in input_list:
change = input_list[idx]
result.append(change)
idx -=1
return result
print (test_my_reversed([0,1,2,3],[3, 2, 1, 0]))
print (test_my_reversed([0,1,2,3],[1, 3, 2, 0]))
ChiaraCati commented
# test
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
# function
def my_reversed(input_list):
r_list = []
count = len(input_list)
while count > 0:
count = count - 1
r_list.append(input_list[count])
return r_list
print(test_my_reversed(['cane', 'gatto', 'asino','topo'], ['topo', 'asino', 'gatto', 'cane'])) # output: True
print(test_my_reversed(['cane', 'gatto', 'asino',''], ['', 'asino', 'gatto', 'cane'])) # output: True
print(test_my_reversed(['cane', 1, 'asino', 2], [2, 'asino', 1, 'cane'])) # output: True
print(test_my_reversed(['cane', 1, 'asino', (2,3)], [2,3, 'asino', 1, 'cane'])) # output: False
print(test_my_reversed(['cane', 1, 'asino', (2,3)], [(2,3), 'asino', 1, 'cane'])) # output: True
me and Giorgia Sampò did it together
👭
fcagnola commented
# 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.
# defining a test function
def test(input_list, expected):
result = my_reversed(input_list)
return result == expected
def my_reversed(input_list):
result_list = [] # creates an empty list object to store the result
index = -1 # uses negative indexing to start from the last item in the input list
for item in input_list: # loops through input list
result_list.append(input_list[index]) # appends each item to the result
index -= 1 # continues negative indexing until it reaches the beginning of the input list
return result_list # returns the result
print(test(["a", "b", "c", "d", "e"], ['e', 'd', 'c', 'b', 'a'])) # returns True
print(test([1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1])) # returns True
print(test([], [])) # returns True
edoardodalborgo commented
from collections import deque
def test_my_reversed(input_list):
deque_check = deque()
deque_check.extend(input_list)
for item in my_reversed(input_list):
item_input_list = deque_check.pop()
if item == item_input_list:
continue
else: return False
return True
def my_reversed(input_list):
output_deque = deque()
output_reversed_list = []
for item in input_list:
output_deque.appendleft(item)
output_reversed_list.extend(output_deque)
return output_reversed_list
input_list = input("Enter a list of objects divided by commas: ").split(", ")
print(test_my_reversed(input_list))
AleRosae commented
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
def my_reversed(input_list):
output_list = list()
n = -1
for item in input_list:
output_list.append(input_list[n])
n = n - 1
return output_list
print(test_my_reversed(([0,1,2,3,4,5]), ([5,4,3,2,1,0])))
print(test_my_reversed(([5,4,3,2,1,0]), ([0,1,2,3,4,5])))
print(test_my_reversed(([0]), ([0])))
AlessandraFa commented
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
def my_reversed(input_list):
reversed_list = []
a = len(input_list) - 1
for position, item in enumerate(input_list):
reversed_list.append(input_list[a-position])
return reversed_list
print(test_my_reversed([0, 1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1, 0]))```
yunglong28 commented
#This is my reversed function
def my_reversed(input_list):
reversed_list = []
c = len(input_list)
while c > 0:
c = c - 1
reversed_list.append(input_list[c])
return reversed_list
print(my_reversed([1, 2, 3]))
#This is my test
def test_my_reversed (input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
print(test_my_reversed([1, 2, 3], [3, 2, 1]))
SusannaPinotti commented
# negative indexing: -1 to refer to the last item of a list
def test_my_reverse(input_list, expected):
res = my_reverse(input_list)
if expected == res:
return True
else:
return False
def my_reverse(input_list):
reversed_list= list()
n = -1
for item in input_list:
reversed_list.append(input_list[n])
n -= 1
return reversed_list
print(test_my_reverse(["a", "c", "c", "o", "r", "r", "a"], ["a", "r", "r", "o", "c", "c", "a"])) #returns True
print(test_my_reverse(["", 0, "a"], ["a", 0, ""])) #returns True
IlaRoss commented
#test
def test_my_reversed(input_list, reversed_list):
result = my_reversed(input_list)
if result == reversed_list:
return True
else:
return False
#algorithm
def my_reversed(input_list):
reversed_list = list()
for item in input_list:
reversed_list.insert(0, item)
return reversed_list
#variables
my_list = ['Slater', 'Murgi', 'Giammei', 'Fleur', 'Virginia', 'Chiru']
my_reversed_list = ['Chiru', 'Virginia', 'Fleur', 'Giammei', 'Murgi', 'Slater']
#test runs
print(test_my_reversed(my_list, my_reversed_list))
print(test_my_reversed([3, 3, 5, 5, 6, 3, 4, 2, 9, 6], [6, 9, 2, 4, 3, 6, 5, 5, 3, 3]))
print(test_my_reversed(['Pink Floyd'], ['Pink Floyd']))
#algorithm execution
print(my_reversed(my_list))
print(my_reversed([6, 5, 4, 3, 2, 1]))
print(['Monteverdi'])
vanessabonanno commented
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
def my_reversed(input_list):
rev_list = []
index = len(input_list) - 1
while index >= 0:
element = input_list.pop(index)
index -= 1
rev_list.append(element)
return rev_list
# first test gives False as output because there is a not expected empty string
print(test_my_reversed(list(['e', 'n', 'g', 'a', 's', 'a','l']), ['l', 'a', 's', 'a', 'g', ' ', 'n', 'e']))
print(test_my_reversed(list([4, 5, 2, 3, 0]), [0, 3, 2, 5, 4])) # output is True
print(my_reversed(list(['e', 'i', 'v', 'o', 'm']))) # output is ['m', 'o', 'v', 'i', 'e']
AlessandroBertozzi commented
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
return False
def my_reversed(input_list):
reversed_list = [input_list[-(elemento + 1)] for elemento in range(len(input_list))]
return reversed_list
print(test_my_reversed([0, 1, 2], [2, 1, 0]))
print(test_my_reversed([0, 1, 2, 3, 4, 5, 6], [6, 5, 4, 3, 2, 1, 0]))
print(test_my_reversed(['c', 'i', 'a', 'o'], ['o', 'a', 'i', 'c']))
LuisAmmi commented
def test_my_reversed(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
# code
def my_reversed(input_list):
output_list = list()
position = -1 #initialize position
for item in input_list:
output_list.insert(position, item)
position -= 1
return output_list
# return True
print(test_my_reversed(["0", "1", "2", "3"], ["3", "2", "1", "0"]))
print(test_my_reversed(["c", "d", "e"], ["e", "d", "c"]))
print(test_my_reversed([""], [""]))
SarahTew commented
def my_reversed(input_list):
my_reversed_list=list()
position=len(input_list)
while position>0:
position=position-1
my_reversed_list.append(input_list[position])
return(my_reversed_list)
print(my_reversed(["William","Sarah", "Lilly"]))
print(my_reversed(["First", "Middle", "Last"]))
print(my_reversed([3, 27.5, "November", ""]))
def test_my_reversed(input_list, expected):
if my_reversed(input_list)==expected:
return True
else:
return False
print(test_my_reversed(["William", "Sarah", "Lilly"], ["Lilly", "Sarah", "William"]))
print(test_my_reversed(["First", "Middle", "Last"], ["Last", "Middle", "First"]))
print(test_my_reversed([3, 27.5, "November", ""], ["", "November", 27.5, 3]))
SofiBar commented
`def test_my_reversed(input_list, expected):
if expected == my_reversed(input_list):
return True
else:
return False
def my_reversed(input_list):
output_list = []
for item in input_list:
output_list.insert(0, item)
return output_list
input_list = [1, 2, 3, 4, 5, 6]
rev_list = [6, 5, 4, 3, 2, 1]
input_list2 = ["cerchio", "quadrato", "triangolo"]
rev_list2 = ["triangolo", "quadrato", "cerchio"]
print(test_my_reversed(input_list, rev_list))
print(test_my_reversed(input_list2, rev_list2))`