-
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 5 #20
Comments
|
def my_reversed(input_list):
result= list()
for i in range(len(input_list)):
result.insert(i-1, input_list[-i])
return result
#Test case
def test_reversed(input_list, expected):
if my_reversed(input_list)== expected:
return True
else:
return False
# Three test runs
print(test_reversed([0, 1, 2, 3], [3, 2, 1, 0]))
print(test_reversed(["Alice", "Bob", "Carl", "Danielle"], ["Danielle", "Carl", "Bob", "Alice"]))
print(test_reversed([0, 1, 2, 3], [1, 2, 3, 0])) Second try: def my_reversed(input_list):
result= list()
for item in input_list:
result.insert(0, item)
return result
#Test case
def test_reversed(input_list, expected):
if my_reversed(input_list)== expected:
return True
else:
return False
# Three test runs
print(test_reversed([0, 1, 2, 3], [3, 2, 1, 0]))
print(test_reversed(["Alice", "Bob", "Carl", "Danielle"], ["Danielle", "Carl", "Bob", "Alice"]))
print(test_reversed([0, 1, 2, 3], [1, 2, 3, 0])) Third try: def my_reversed(input_list):
result=list()
for item in input_list:
result= [item]+ result
return result |
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 = []
i = len(input_list)-1
while i >= 0:
output_list.append(input_list[i])
i -= 1
return output_list
#Test case
print(test_my_reversed([0, 1, 2, 3], [3, 2, 1, 0]))
#Console output
True |
|
def test_my_reversed(input_list, expected): def my_reversed(input_list): print(test_my_reversed([1, 2, 3, 4], [4, 3, 2, 1])) |
|
def my_reversed(input_list):
output_list = []
for i in range(len(input_list)):
output_list.append(input_list[len(input_list)-i-1])
return output_list
def test_my_reversed(input_list, expected):
if my_reversed(input_list) == expected:
return True
else:
return False
print(test_my_reversed(['a','b','c','d'],['d', 'c', 'b', 'a']))
print(test_my_reversed(['1','2','3','4'],['4', '3', '2', '1'])) Or: def my_reversed(input_list):
output_list = []
for item in input_list:
output_list.insert(0, item)
return output_list
def test_my_reversed(input_list, expected):
if my_reversed(input_list) == expected:
return True
else:
return False
print(test_my_reversed(['a','b','c','d'],['d', 'c', 'b', 'a']))
print(test_my_reversed(['1','2','3','4'],['4', '3', '2', '1'])) |
|
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):
my_iterator = []
i = len(input_list)
for item in input_list:
current_item = input_list[i-1]
my_iterator.append(current_item)
i -= 1
return my_iterator
print(test_my_reversed(["Harry", "Ron", "Hermione", "Hagrid"], ["Hagrid", "Hermione", "Ron", "Harry"]))
print(test_my_reversed([1, 2, "tre", 4], [4, "tre", 2, 1]))
print(test_my_reversed([], [])) |
|
|
Write in Python the function
def my_reversed(input_list)
, which behaves 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.The text was updated successfully, but these errors were encountered: