Open essepuntato opened 3 years ago
unlike reversed(), with [::-1] code reverse the list without modifying it:
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):
reversed_list = input_list[::-1]
return reversed_list
print(test_my_reversed([], []))
print(test_my_reversed([1], [1]))
print(test_my_reversed([1, 2, 3, 4], [4, 3, 2, 1]))
print(test_my_reversed([1, 2, 3, 4, "5", " ", 7], [7, " ", "5", 4, 3, 2, 1]))
Reverse with slicing:
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):
output_list = input_list[::-1]
return output_list
my_list = [0, 1, 2, 3, 4, 5]
print(test_my_reversed(my_list, [5, 4, 3, 2, 1, 0]))
print(my_reversed(my_list))
It returns:
True
[5, 4, 3, 2, 1, 0]
def my_reversed(input_list):
my_stack = deque()
for item in input_list:
my_stack.appendleft(item)
return my_stack
#Test code for the algorithm
def test_my_reverse(input_list, expected_reverse):
result = my_reverse(input_list)
if result == expected_reverse:
return True
else:
return False
#Code for the algorithm
def my_reverse (input_list):
olist = list()
while len(input_list) > 0:
item = input_list[-1]
olist.append(item)
del input_list[-1]
return olist
#Some of the possible tests
print(test_my_reverse(["a", "b", "c", "d"], ["d", "c", "b", "a"])) # N. 1, expect: T
print(test_my_reverse(["a", "b", "c", "d"], ["d", "b", "c", "a"])) # N. 2, expect: F
print(test_my_reverse(["a", "b", "c", "d"], ["a", "b", "c", "d"])) # N. 3, expect: F
print(test_my_reverse(["a", "b", "c", "d"], ["a", "b", "c"])) # N. 4, expect: F
print(test_my_reverse(["a", "b", "c", "d"], ["a", "a", "c", "d"])) # N. 5, expect: F
If code is run, the results coincide with our expectations: therefore the code should be correct
def test_my_reversed(input_list2, expected):
result = my_reversed(input_list2)
if result == expected:
return True
else:
return False
def my_reversed(input_list2):
output_p_list2 = []
while len(input_list2) > 0:
output_p_list2.append(input_list2.pop())
return output_p_list2
print(test_my_reversed([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]))
print(test_my_reversed(['a', 'b', 'c', 'd', 'e'], ['e', 'd', 'c', 'b', 'a']))
def test_my_reversed(input_list,expected):
my_result = my_reversed(input_list)
if my_result == expected:
return True
else:
return False
def my_reversed(input_list):
output_list = list()
for item in input_list:
output_list.insert(0,item)
return output_list
example_list = [0,1,2]
print(test_my_reversed(example_list,[2,1,0])) #This should return True
print(my_reversed(["A","B","D","K","C"]))
Task_5
def testing(input_list, expected):
result = my_reversed(input_list)
if result == expected:
return True
else:
return False
def my_reversed(input_list):
output = []
number = 0
maxnumber = len(input_list)
while number < maxnumber:
output.append(input_list.pop())
number +=1
return output
print(testing([0, 1, 2], [2, 1, 0]))
print(testing([0, 1, 2, 5, 6], [2, 1, 0]))
Returns True False
Hi all,
Several different approaches were used, that's wonderful indeed! Just a few comments:
[1, 2, 3, 1]
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()
position = len(input_list) -1
while position >= 0:
output_list.append(input_list[position])
position -= 1
return output_list
print(test_my_reversed([], []))
print(test_my_reversed([1, 2, 3], [3, 2, 1]))
print(test_my_reversed(["ciao", "hello", "salut"], ["salut", "hello", "ciao"]))
print(test_my_reversed(["ciao", 1, "hello", 2], [2, "hello", 1, "ciao"]))
def my_reversed(input_list):
l = list()
for item in input_list:
l.insert(0, item)
return l
list_1 = [1, 2, 3, 4, 5, 6, 7, 8]
print(my_reversed(list_1))
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]))
print(test_my_reversed([3, 2, 1], [1, 2, 3]))
print(test_my_reversed(["a", "b", "c"], ["c", "b", "a"]))
print(test_my_reversed(["c", "b", "a"], ["a", "b", "c"]))
print(test_my_reversed([], []))
OUTPUT:
[8, 7, 6, 5, 4, 3, 2, 1]
True
True
True
True
True
Write in Python the function
def my_reversed(input_list)
, which behave like the built-in functionreversed()
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 functionreversed()
in the implementation.