Skip to content
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

Open
essepuntato opened this issue Nov 9, 2022 · 11 comments
Open

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

essepuntato opened this issue Nov 9, 2022 · 11 comments
Labels

Comments

@essepuntato
Copy link
Contributor

Write in Python the function def my_reversed(input_list), which behaves 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.

@n1kg0r
Copy link

n1kg0r commented Nov 9, 2022

def test_my_reversed(input_reversed_list, expected):
    return input_reversed_list == expected
    
def my_reversed(input_list):
    output_list = []
    for _ in range(len(input_list)):
        output_list.append(input_list.pop())
    return output_list

# Expect True
print(test_my_reversed(my_reversed([0,1,2,3]), [3, 2, 1, 0]))
print(test_my_reversed(my_reversed([0,1,2,3]), list(reversed([0, 1, 2, 3]))))
# Console output: True

@delete4ever
Copy link

delete4ever commented Nov 10, 2022

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

@lucia1299
Copy link

lucia1299 commented Nov 10, 2022

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

@matteo-guenci
Copy link

matteo-guenci commented Nov 12, 2022

def test_my_reverse(input_list, expected):
    result = my_reverse(input_list)
    if result == expected:
        return True
    else:
        return False

def my_reverse(input_list):
    reversed_list = input_list[::-1]
    return reversed_list


print(test_my_reverse([0, 1, 2, 3], [3, 2, 1, 0]))

@SpedicatiDaniele
Copy link

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()
last_index = len(input_list)-1
for item in input_list:
output_list.append(input_list[last_index])
last_index -= 1
return output_list

print(test_my_reversed([1, 2, 3, 4], [4, 3, 2, 1]))
print(test_my_reversed([], []))

@EricaAndreose
Copy link

EricaAndreose commented Nov 13, 2022

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 = []
    pen = len(input_list) - 1
    for item in input_list:
        reverse_list.append(input_list[pen])
        pen -= 1
    return reverse_list

# Test runs that return True
print(test_my_reversed([1, 2, 3, 4, 5], [5, 4, 3, 2, 1]))
print(test_my_reversed([0, 1, 2, 3, 4, 5], [5, 4, 3, 2, 1, 0]))
print(test_my_reversed(["Sherlock", "John", "Microft", "Moriarty"], ["Moriarty", "Microft", "John", "Sherlock"]))


# Another idea come after the night!

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=[]
  for item in input_list:
      reversed_list.insert(0, item)
  return reversed_list


print(test_my_reversed(["a", "b", "c"], ["c", "b", "a"]))
print(test_my_reversed([1, 2, 3], [3, 2, 1]))
    

@ranacoskun
Copy link

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']))

@corrado877
Copy link

def test_case_my_reverse(input_list, expected):
# Test case Algorithm
    result=my_reverse(input_list)
    if result==expected:
        return True
    else:
        return False
    
    
# Code of algorithm
def my_reverse (input_list):
    output_list=[]
    for item in range (len(input_list)):
        output_list.insert(0, input_list [item])
    return output_list

#Three test runs
print (test_case_my_reverse(["ab", "bc","de", "ef"], ['ef', 'de', 'bc', 'ab']))
print (test_case_my_reverse ([1,2,3,4], [4,3,2,1]))
print (test_case_my_reverse (["Moon", "Earth", "Venus", "Mars"], ['Mars', 'Venus', 'Earth', 'Moon']))

@ChiaraParravicini
Copy link

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([], []))

@falaimo99
Copy link

falaimo99 commented Nov 14, 2022

def test_my_reversed(input, expected):
    result = my_reversed(input)
    if result == expected:
        return True
    else:
        return False

def my_reversed(input):
    result = list()

    item = len(input)
    for i in range(item):
        item = item - 1
        result.append(input[item])
            
    return result
    
print(test_my_reversed([0,1,2,3,4,5], [5,4,3,2,1,0]))
print(test_my_reversed(["Aragorn", "Frodo", "Samwise"], ["Samwise", "Frodo", "Aragorn"]))
print(test_my_reversed([0,1,1,2,2,3], [3,2,2,1,1,0]))
print(test_my_reversed(["Uruk-hai", "Saruman", "Sauron", "Uruk-hai"], ["Uruk-hai", "Sauron", "Saruman","Uruk-hai"]))

@alka2696
Copy link

# Test case for the function
def test_my_reversed(input_list, expected):
    result = my_reversed(input_list)
    if expected == result:
        return True
    else:
        return False

# Code of the function
def my_reversed(input_list):
    l = list()
    for item in input_list:
        l.insert(0, item)
    return l


# Tests
print(test_my_reversed([], []))
print(test_my_reversed([1, 2, 5, 6, 4, 7, 2], [2, 7, 4, 6, 5, 2, 1]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests