Skip to content

Em : functions lab without exercise 12 - me petaba ya la cabeza#11

Open
emmacunill wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
trainwithlabs:main
Open

Em : functions lab without exercise 12 - me petaba ya la cabeza#11
emmacunill wants to merge 1 commit intoIronhack-data-bcn-oct-2023:mainfrom
trainwithlabs:main

Conversation

@emmacunill
Copy link
Copy Markdown

No description provided.

@bripollc
Copy link
Copy Markdown

Emma:)

Las funciones pueden ser mindblowinggggg pero te ahorrarán mucho tiempo y simplificación de código. EPPPP muy muy muy buen trabajo 🌻 Felicitats!!

  • Now combine those two ideas and write a function that receives a list and either "+" or "*" and outputs acordingly.
  def oper_all(arr, oper):
    sum_ = 0
    mult_ = 1
    for i in arr:
        if oper == "+":
            sum_ += i
        elif oper == "*":
            mult_ *= i
            
    if oper == "+":    -- Incluir esta parte
        return sum_
    elif oper == "*":
        return mult_

Si ejecutas tu código varias veces, aveces pasa el test y aveces no:) El problema está en la línea return sum_ or mult_. Utilizando el or lo que estás haciendo es: si el primer valor sum_ es verdadero, se devuelve ese valor. Si el primer valor es falso, se devuelve el segundo valor mult_ sin importar su valor. Modificando la parte que te especifico arriba debería funcionarte.

Te dejo también algunas manera de resolver el bonus:

  • Write a function that returns the mode of a list, i.e.: the element that appears the most times.
def mode_counter(arr):
    counter = {} 

    for i in arr:
        if i in counter:
            counter[i] += 1  
        else:
            counter[i] = 1

    mode = max(counter, key=counter.get)

    return mode
  • 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):
    word_list = string.split(", ") 
    word_list.sort()  
    sorted_string = ", ".join(word_list)
    return sorted_string
a_string = "call, you, later"
sort_alpha(a_string)

Fins demà!!!!!!!

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