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 3 #18

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

Lecture "Brute-force argorithms", exercise 3 #18

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

Comments

@essepuntato
Copy link
Contributor

Write in Python the function def my_enumerate(input_list), which behaves like the built-in function enumerate() introduced in Section "Linear search" and returns a proper list, and accompany the function with the related test case. It is not possible to use the built-in function enumerate() in the implementation.

@n1kg0r
Copy link

n1kg0r commented Nov 9, 2022

def test_my_enumerate(input_list, expected):
    return input_list == expected
    
def my_enumerate(input_list):
    output_list = []
    for i in range(len(input_list)):
        output_list.append((i, input_list[i]))
    return output_list

# Expect True
print(test_my_enumerate(my_enumerate([1,2,3]), [(0, 1), (1, 2), (2, 3)]))
print(test_my_enumerate(my_enumerate(['1','2','3']), list(enumerate(['1','2','3']))))
# Console output: True

@delete4ever
Copy link

delete4ever commented Nov 9, 2022

I'm still confused and only have some rough ideas about it. Anyway I took my first second try like this:

def my_enumerate(input_list):
    result= list()
    for item in input_list:
        position_of_item= input_list.index(item)
        result.append((position_of_item, item))
    return result
# Test case
def test(input_list, expected):
    if my_enumerate(input_list)== expected:
        return True
    else:
        return False
# Three test runs
print(test(["Siamese", "Ragdoll", "Chartreux", "Persian"], [(0, "Siamese"), (1, "Ragdoll"), (2, "Chartreux"), (3, "Persian")]))
print(test(["Coliie", "Doberman", "Akita", "Shiba"], [(0, "Coliie"), (1, "Doberman"), (2, "Akita"), (3, "Shiba")]))
print(test(["Coliie", "Doberman", "Akita", "Shiba"], [(2, "Coliie"), (3, "Doberman"), (4, "Akita"), (5, "Shiba")]))

@alka2696
Copy link

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


# Code of the function
def my_enumerate(input_list):
    result = []
    for item in range(len(input_list)):
        result.append((item, input_list[item]))
    return result

print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))
print(test_my_enumerate(["Coraline", "American Gods","The Graveyard Book", "Good Omens"], [(0, "American Gods"), (1, "The Graveyard Book"), (2, "Coraline")]))

@lucia1299
Copy link

def test_my_enumerate (input_list, expected):
    result = my_enumerate(input_list)
    if  expected == result:
        return True
    else: 
        return False 

def my_enumerate (input_list):
    output_list = list ()
    for string in input_list:
        output_list.append(string)
        output_list.append(input_list.index(string))
    return output_list

#Two test runs
print (test_my_enumerate((["a", "b", "c"]), (["a", 0, "b", 1, "c", 2])))
print (test_my_enumerate((["Artic Monkeys", "The Radiohead", "The Beatles"]), (["Artic Monkeys", 0, "The Radiohead", 1, "The Beatles", 2])))
#Console output
True
True

@giorgiacrosilla
Copy link

giorgiacrosilla commented Nov 10, 2022

#Test case for the algorithm
def test_my_enumerate(input_list, expected):
    result = my_enumerate(input_list)
    if expected == result:
        return True
    else:
        return False

def my_enumerate(input_list):
    output_list = list()
    for i in range(len(input_list)):
        output_list.append(input_list[i])
        output_list.append(i)
        
    return output_list

print(test_my_enumerate((["a", "b", "c"]), (["a", 0, "b", 1, "c", 2])))
print(test_my_enumerate((["a", "a", "b"]), (["a", 0, "a", 1, "b", 2])))

@EricaAndreose
Copy link

def test_my_enumerate(input_list, expected):
    result = my_enumerate(input_list)
    if expected == result:
        return True
    else:
        return False

# My very first try
def my_enumerate(input_list):
    output_list = list() 

    for item in input_list:
        output_list.append(input_list.index(item))
        output_list.append(item)
    return output_list

# Test runs that returns True
print(test_my_enumerate(["Zio", "Zia"], [0, "Zio", 1, "Zia"]))
print(test_my_enumerate([0, 1, 2, 3, 4], [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]))
print(test_my_enumerate(["Harry", "Ron", "Hermione", "Draco", "Luna"], [0, "Harry", 1, "Ron", 2, "Hermione", 3, "Draco", 4, "Luna"]))

# My second idea
def my_enumerate(input_list):
    output_list = list() 
   
    for item in range(len(input_list)):
        output_list.append((item, input_list[item]))
    return output_list

# Test runs that returns True
print(test_my_enumerate(["Miao", "Ciao"], [(0, "Miao"), (1, "Ciao")]))
print(test_my_enumerate([1, 2, 3, 4], [(0, 1), (1, 2), (2, 3), (3, 4)]))
print(test_my_enumerate(["Harry", "Ron", "Hermione", "Draco", "Luna"], [(0, "Harry"), (1, "Ron"), (2, "Hermione"), (3, "Draco"), (4, "Luna")]))


@matteo-guenci
Copy link

def test_my_enumerate(input_list, expected):
    result = my_enumerate(input_list)
    if result == expected:
        return True
    else:
        return False

def my_enumerate(input_list):
    enumerated_list = []
    for _ in range(len(input_list)):
        enumerated_list.append((_, input_list[_]))
    return enumerated_list

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

@SpedicatiDaniele
Copy link

def test_my_enumerate (input_list, expected):
result = my_enumerate(input_list)
if result == expected:
return True
else:
return False

def my_enumerate(input_list):
output_list = list()

for item in input_list:
    mytuple = (input_list.index(item), item)
    output_list.append(mytuple)
return output_list

print(test_my_enumerate(["a", "b", "c"], [(0, 'a'), (1, 'b'), (2, 'c')]))
print(test_my_enumerate(["Alice", "Catherine", "Bob", "Charles"], [(0, 'Alice'), (1, 'Catherine'), (2,'Bob'), (3, 'Charles')]))

@ranacoskun
Copy link

def my_enumerate(input_list):
  output_list = []
  for i in range(len(input_list)):
      output_list.append((i, input_list[i]))

  return output_list     

def test_my_enumerate(input_list,expected):
  if my_enumerate(input_list)== expected:
    return True
  else:
    return False

print(test_my_enumerate(['a','b','c','d'],[(0,'a'),(1,'b'),(2,'c'),(3,'d')]))
print(test_my_enumerate(['1','2','3','4'],[(0,'1'),(1,'2'),(2,'3'),(3,'4')]))

@corrado877
Copy link

# Test case algorithm
def test_my_enumerate_list (input_list, expected):
    result= my_enumerate_list(input_list)
    if result==expected:
        return True
    else:
        return False

# Code of algorithm
def my_enumerate_list(input_list):
    result_list=[]
    for item in range(len(input_list)):
        result_list.append ((item, input_list[item]))
    return result_list

# Three test runs

print (test_my_enumerate_list(['The Godfather', 'Goodfellas', 'The Departed','Inception', 'Shutter Island'], [(0, 'The Godfather'), (1, 'Goodfellas'), 
                             (2, 'The Departed'), (3, 'Inception'), (4, 'Shutter Island')]))
print (test_my_enumerate_list(["abc", "def", "ghi"], [(0, "abc"), (1,"def"), (2, "ghi")]))
print (test_my_enumerate_list(["Moon", "Earth", "Venus", "Mars"], [(0, "Moon"), (1, "Earth"), (2, "Venus"), (3, "Mars")]))

@irematmar
Copy link

def my_enumerate(input_list):
  output_list = []
  for item in range(len(input_list)):
    output_list.append((item, input_list[item]))
  
  return output_list

def test_my_enumerate(input_list, expected):
  if my_enumerate(input_list) == expected:
    return True
  else:
    return False

print(test_my_enumerate(["x", "y", "z"], [(0, "x"), (1, "y"), (2, "z")]))
print(test_my_enumerate(["apple", "laptop", "carrot"], [(0, "x"), (1, "y"), (2, "z")]))

@ChiaraParravicini
Copy link

def test_my_enumerate(input_list, expected):
    result = my_enumerate(input_list)
    if expected == result:
        return True
    else:
        return False
#First try
def my_enumerate(input_list):
    new_list = []
    i = -1
    for item in input_list:
        i += 1
        new_tuple = (i, item)
        new_list.append(new_tuple)
    return new_list
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))

#Second try
def my_enumerate(input_list):
    new_list = []
    for i in range(len(input_list)):
        new_tuple = (i, input_list[i])
        new_list.append(new_tuple)
    return new_list
print(test_my_enumerate(["a", "b", "c"], [(0, "a"), (1, "b"), (2, "c")]))

@essepuntato
Copy link
Contributor Author

Hi all,

Some hits on how to run some tests (try on your code and see if everything still works:

  • Input list: ["a", "a", "b", "c"]
  • Output list: [(0, "a"), (1, "a"), (2, "b"), (3, "c")]

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