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 algorithms", exercise 3 #18

Open
essepuntato opened this issue Nov 4, 2019 · 6 comments
Open

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

essepuntato opened this issue Nov 4, 2019 · 6 comments
Labels

Comments

@essepuntato
Copy link
Collaborator

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.

@arcangelo7
Copy link

def my_enumerate(input_list):
    output_enumerate_object = []

    for index in range(len(input_list)):
        tuple = (index, input_list[index])
        output_enumerate_object.append(tuple)

    return output_enumerate_object

def test_my_enumerate(input_list, expected):
    result = my_enumerate(input_list)

    if result == expected:
        return True
    else:
        return False

print(test_my_enumerate(["Remember", "the", "fifth", "of", "November"], [(0, 'Remember'), (1, 'the'), (2, 'fifth'), (3, 'of'), (4, 'November')]))
# It prints True

@sntcristian
Copy link

Screenshot (7)

@NoonShin
Copy link

NoonShin commented Nov 5, 2019

def my_enumerate(input_list):
    input_length = len(input_list)
    output = [(index, input_list[index]) for index in range(input_length)]
    return output
    
    
def test_my_enumerate(input, expected)
    return my_enumerate(input) == expected
    
    
volumes = ['Preludes and Nocturnes', 'The Dolls House', 'Dream Country', 'Season of Mists', 'A Game of You', 'Fables and Reflections', 'Brief Lives', 'Worlds End', 'The Kindly Ones', 'The Wake']

print(test_my_enumerate(volumes, [(0, 'Preludes and Nocturnes'), (1, 'The Dolls House'), (2, 'Dream Country'), (3, 'Season of Mists'), (4, 'A Game of You'), (5, 'Fables and Reflections'), (6, 'Brief Lives'), (7, 'Worlds End'), (8, 'The Kindly Ones'), (9, 'The Wake')]))

@FrancescoFernicola
Copy link

Screenshot 2019-11-07 17 25 21

@essepuntato
Copy link
Collaborator Author

Hi all,

please find attached my personal solution – also available online:

# 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):
    l = list()
    for i in range(len(input_list)):
        l.append((i, input_list[i]))
    return l


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

An important point: the rationale of using TDD to test the code is that all the tests must be passed (and this, somehow, guarantees the correctness of the code). If a test is not passed, it means that there is something wrong in the code. Thus, please, avoid using tests that fail on purpose since this does not demonstrate the correctness of your code.

@aschimmenti
Copy link

def my_enumerate(input_list):
#implement the enumerate action without using enumerate()
output_list = []
for i in range(len(input_list)):
output_list.append((i, input_list[i]))
return output_list

print(my_enumerate([1, 2, 3, 4])==list(enumerate([1, 2, 3, 4])))
#It prints True

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

6 participants