comp-think / 2021-2022

The GitHub repository containing all the material related to the Computational Thinking and Programming course of the Digital Humanities and Digital Knowledge degree at the University of Bologna (a.a. 2021/2022).
17 stars 9 forks source link

Lecture "Brute-force argorithms", exercise 5 #21

Open essepuntato opened 2 years ago

essepuntato commented 2 years ago

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.

ghasempouri1984 commented 2 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]))
RebeccaJillianBeattie commented 2 years ago

reversed function

federicabonifazi commented 2 years ago

image

CarmenSantaniello commented 2 years ago

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]
11051620 commented 2 years ago

def my_reversed(input_list):

my_stack = deque()

for item in input_list:
    my_stack.appendleft(item)

return my_stack
olgagolgan commented 2 years ago

image image

martasoricetti commented 2 years ago

image

ManueleVeggi commented 2 years ago
#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

tommasobattisti commented 2 years ago
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']))
katya-avem commented 2 years ago

image

OrsolaMBorrini commented 2 years ago
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"]))
chloeppd commented 2 years ago
Screenshot 2021-11-05 at 7 10 02 AM
MaddaGh commented 2 years ago

my_reversed

AnastasiyaSopyryaeva commented 2 years ago

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

angstigone commented 2 years ago
Schermata 2021-11-01 alle 16 46 48
essepuntato commented 2 years ago

Hi all,

Several different approaches were used, that's wonderful indeed! Just a few comments:

AmeliaLamargese commented 2 years ago
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"]))
sarabecchi commented 2 years ago
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