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

[Pere Martin] entrega funcions... per revisar bonus.. no veig l'error #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

perikoloso
Copy link

No description provided.

@bripollc
Copy link

Pere:)

Olé!!! Buen trabajo! Muy muy buen lab, la verdad! Las funciones te ahorrarán tiempo y simplificación de código así que me alegro de que se te den bien ✨✨

  • Write a function that takes a list and returns a list of the unique values.
def unique(lst_un):
    unique_list = []
    for i in lst_un:
        if i not in unique_list:
            unique_list +=[i]
    return unique_list

Tu código pasa el test y la respuesta es correcta peeeeero... el enunciado te especifica que no utilices set 🙃 Te dejo una opción que funciona sin usarlo.

  • Write a function that receives a string of comma separated words and returns a string of comma separated words sorted alphabetically.
def sort_alpha(string):
    end = 0
    init = 0
    counter = 1
    new_string = ""
    lst_words = []

    while end != -1:
        end = string.find(",", init)
        if end == -1:           -- añadir esta parte :)
            lst_words.append(string[init:])
        else:   
            lst_words.append(string[init:end])
        init = end + 1

    lst_words.sort()

    for i in lst_words:
        if counter == len(lst_words):
            new_string = new_string + i
        else:
            new_string = new_string + i + ","
        counter += 1

    return new_string

El problema en tu código es que cuando llega al último elemento en la lista de palabras, no hay una coma después. Por lo tanto, cuando el código intenta buscar la siguiente coma utilizando string.find(",", init), no la encuentra y devuelve -1. Esto causa que el bucle while se detenga antes de que la última palabra se agregue a la lista lst_words.
Si agregas un if-else statement de esta manera te aseguras de que la última palabra se agregue correctamente. Así debería pasarte el test!!!!! 🙃

A por el siguiente lab!!!!

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

Successfully merging this pull request may close these issues.

2 participants