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 #19

Open
essepuntato opened this issue Oct 28, 2021 · 22 comments
Open

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

essepuntato opened this issue Oct 28, 2021 · 22 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.

@ManuSrivastava1
Copy link

ManuSrivastava1 commented Oct 31, 2021

Here is my version of enumerate function.

def my_enumerate(input_list):
    x = len(input_list)
    y= 0
    output_list = []
    while y <= x-1:
        output_tuple = (y,input_list[y])
        output_list.append(output_tuple)
        y += 1
    return output_list

Here is the visualization of how the function works

Below is the test code for the above written algorithm -

def test_my_enumerate(input_list,output_list,expected):
    n=0
    while n < len(input_list):
        item_from_list = output_list[n]
        if item_from_list[1]==input_list[n] and item_from_list[0]==n:
            result = True
        else:
            result = False
            break
        n+= 1
    if expected == result:
        return True
    else:
        return False
        
g = test_my_enumerate(['f','g','h'],[(0,'f'),(1,'g'),(2,'h')],True)
k = test_my_enumerate(['f','g','h'],[(0,'f'),(1,'h'),(2,'h')],False) 

Here you can see the test code in action

note: I don't think this is the most elegant or even the complete method for testing the algorithm, but its the best I could come up with.

@RebeccaJillianBeattie
Copy link

enumerate function

@chloeppd
Copy link

chloeppd commented Nov 2, 2021

Screenshot 2021-11-02 at 12 02 51 PM

@CarmenSantaniello
Copy link

CarmenSantaniello commented Nov 2, 2021

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

my_list = ["Gryffindor", "Hufflepuff", "Ravenclaw", "Slytherin"]

def my_enumerate(input_list):
    output_list = list()
    for item in input_list:
        my_tuple = (input_list.index(item), item)
        output_list.append(my_tuple)
    return output_list

print(test_my_enumerate(my_list, [(0, "Gryffindor"), (1, "Hufflepuff"), (2, "Ravenclaw"), (3, "Slytherin")]))
print(my_enumerate(my_list))

It returns:

True
[(0, 'Gryffindor'), (1, 'Hufflepuff'), (2, 'Ravenclaw'), (3, 'Slytherin')]


@federicabonifazi
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):
    enumerated_list = list()
    for item in input_list:
        enumerated_list.append ((input_list.index(item), item))
    return enumerated_list

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

@olgagolgan
Copy link

image
image

@martasoricetti
Copy link

image

@ManueleVeggi
Copy link

ManueleVeggi commented Nov 3, 2021

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

#Code of the algorithm
def my_enumerate(input_list):
    output_list = []
    for element in input_list:
        position = input_list.index(element)
        singletuple = (position, element)
        output_list.append(singletuple)     
    return output_list

#Test 1. Expect: T
in_list1 = list(["a","b","c"])
ex_list1 = list([(0, "a"), (1, "b"), (2, "c")])
print(test_my_enumerate(in_list1, ex_list1))

#Test 2. Expect: F
in_list2 = list(["a","b","d"])
ex_list2 = list([(0, "a"), (1, "b"), (2, "c")])
print(test_my_enumerate(in_list2, ex_list2))

#Test 3. Expect: F
in_list3 = list(["a","b","c"])
ex_list3 = list([(1, "a"), (2, "b"), (3, "c")])
print(test_my_enumerate(in_list3, ex_list3))

If the code is run, the results True, False, False: as a consequence, the code should be correct

@OrsolaMBorrini
Copy link

OrsolaMBorrini commented Nov 4, 2021

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):
    result = list()
    for item in input_list:
        my_tuple = (input_list.index(item),item)
        result.append(my_tuple)
    return result

# Test 1
example_list = ["Alpha","Gamma","Beta"]
example_expected = [(0,"Alpha"),(1,"Gamma"),(2,"Beta")]
print(test_my_enumerate(example_list,example_expected)) # It should return True

print(my_enumerate(example_list))
# This correctly returns [(0, "Alpha"), (1, "Gamma"), (2, "Beta")]

# Test 2
example_list2 = ["Long John Silver", "Flint", "Jim Hawkins"]
example_expected2 = [(0,"Long John Silver"), (1,"Flint")]
print(test_my_enumerate(example_list2,example_expected2)) # It should return False, as one item is missing

@tommasobattisti
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_p_list = []
    for i in input_list:
        output_p_list.append((input_list.index(i), i))
    return output_p_list

print(test_my_enumerate(['a', 'b', 'c', 'd'],[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]))
print(test_my_enumerate(['red', 'purple', 'black', 'green'],[(0, 'red'), (1, 'purple'), (2, 'black'), (3, 'green')]))

@MaddaGh
Copy link

MaddaGh commented Nov 4, 2021

me_en

@NoraPs
Copy link

NoraPs commented Nov 4, 2021

Cattura 2

@katya-avem
Copy link

image

@AnastasiyaSopyryaeva
Copy link

Task_3

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

def my_enumerate(input_list):
    output = []
    for item in input_list:
        output.append((input_list.index(item), item))
    return output

print(testing(['a', 'b', 'c'], [(0, 'a'), (1, 'b'), (2, "c")]))
print(testing(['Italy', 'Spain', 'Germany', 'Norway'], [(0, 'Italy'), (1, 'Spain'), (2, 'Norway'), (3, 'Germany')]))

Returns
True
False

@giorgimariachiara
Copy link

giorgimariachiara commented Nov 6, 2021

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

my_list = ("a", "b", "c")

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

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

True
[(0, 'a'), (1, 'b'), (2, 'c')]

@angstigone
Copy link

Schermata 2021-11-01 alle 17 21 46

@elizastuglik
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:
output_list.append(input_list.index(item), item))

return output_list

print(my_enumerate_test([1, 2, 3, 4], [(0, 1), (1,2), (2,3), (3,4)]))

@Bianca-LM
Copy link

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

from collections import deque

def my_enumerate(input_list): 
    result = list ()
    a = 0
    for item in input_list: 
        each_item = (a, item)
        result.append(each_item)
        a += 1
    return result

print (test_my_enumerate(["Qui", "Quo", "Qua"], [(0, "Qui"), (1, "Quo"), (2, "Qua")]))

@essepuntato
Copy link
Contributor Author

Hi all,

Just a few general and specific comments:

  • try to run your algorithm by using the following list as input: ["a", "a", "b", "a"]. Is still working as expected?
  • @MaddaGh, does it change something if I remove the while loop?
  • @giorgimariachiara please indent the code, otherwise, it is very difficult for anyone (including an electronic computer) to understand how to execute it
  • @elizastuglik, please indent the code and, once done, try to run the algorithm using Python before answering since the current version contains syntactic errors (e.g. it is True not true)

@AmeliaLamargese
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):
    enum_list = list()
    for position in range(len(input_list)):
        enum_list.append(position, input_list[position])
    return enum_list 

print(test_my_enumerate([], []))   
print(test_my_enumerate(["ciao", "hello", "salut"], [(0, "ciao"), (1, "hello"), (2, "salut")])) 
print(test_my_enumerate([1, 2, 3], [(0, 1), (1, 2), (2, 3)])) 
print(test_my_enumerate(["ciao", 1, "hello", 2], [(0, "ciao"), (1, 1), (2, "hello"), (3, 2)])) 

@OrsolaMBorrini
Copy link

Hello, @essepuntato

I tried to run my algorithm with the input list ['a', 'a', 'b', 'a'] and of course I found out it didn't really work. The output was: [(0, 'a'), (0, 'a'), (2, 'b'), (0, 'a')] when it should have been [(0,'a'),(1,'a'),(2,'b'),(3,'a')].

At first I thought to delete the item analysed at every iteration, but again, it did not seem to work, as the other elements shifted (changing all the indexes). The output was: [(0, 'a'), (1, 'b')].

I found this solution, that seems to work!
I thought to "scroll" through the list using the index of each item, initialising it at value 0.
The procedure is:

  1. I create the result list (empty)
  2. I create a new variable for the index and I initialise it to 0 (as I start to scroll through the list starting from the first item, with index 0)
  3. I use a while instruction with condition (index < length of the input list) and in which I:
  • Create a new variable with value the value of the element with that index
  • Create a tuple (index, value of the element with that index)
  • Add the tuple to my result list
  • "Update" the value of the index +1 (modifying the condition of the while instruction)
  1. Return the result list

I think it's much easier to understand by looking at the code! I really hope I made no mistakes...

def new_my_enumerate(input_list):
	result = list()
	i = 0
	while i < len(input_list):
		item = input_list[i]
		my_tuple = (i,item)
		result.append(my_tuple)
		i += 1
	return result

example_list = ['Alpha','Gamma','Beta']
new_list = ['a', 'a', 'b', 'a']
print(new_my_enumerate(example_list)) #This returns [(0, 'Alpha'), (1, 'Gamma'), (2, 'Beta')]
print(new_my_enumerate(new_list)) #This returns [(0, 'a'), (1, 'a'), (2, 'b'), (3, 'a')]

@sarabecchi
Copy link

sarabecchi commented Nov 28, 2021

esercizio 3

OUTPUT:

esercizio 3 - output

from collections import deque

def my_enumerate(input_list):
    result = deque()
    
    for i in range(len(input_list)):
        #print(range(len(input_list)))
        #print(i)
        #print(input_list[i])
        result.append((i, input_list[i]))
    return result

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

print(test_my_enumerate([0, 1, 2, 3], deque([(0, 0), (1, 1), (2, 2), (3, 3)])))
print(test_my_enumerate([], deque([])))
print(test_my_enumerate(["a"], deque([(0, "a")])))

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