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

[Marc Folch] lab-functions #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

marcfolchp
Copy link

No description provided.

@sh-ih
Copy link

sh-ih commented Oct 13, 2023

Marc,
Great job on this lab!

Some comments:

On question 2, your function passes the test but there is a case were setting largest=0 would create some issues: when your array has only negative numbers. If you run: greatest([-1,-5,-800]) you’ll get 0 as result, instead of -1.

A way to deal with this cases is to set largest=+inf
In Python, +inf is an unbounded upper value.

On question 5, you could simplify your code by calling the functions you created on the previous two questions:

def oper_all(arr, oper):
    if oper == "+":
        return sum_all(arr)
    elif oper == "*":
        return mult_all(arr)

On question 12, when naming variables, try using names that show more clearly what each variable is: instead of y, f, z, use things like separated_words, sorted_words. That way, anyone that sees your function can quickly understand your code.

If you test your function running something like: sort_alpha("dog,cat,bird") the output is 'dgo,act,bdir' so you are doing an extra unnecessary sorting of the characters inside each word, instead of sorting the complete words.

Here is how your code would be without that extra sorting:

def sort_alpha(string):
    y = []
    f = ""
    z = []
    n = ""
    l = []
    final = ""

#Here you are creating a list of the words contained in the string
    string = string + ","
    for i in string:
        if i != ",":
            f = f + i
        elif i == ",":
            y.append(f)
            f = ""

#Here we sort the list of words    
    y.sort()

#We comment this code because it is sorting the letters inside each word and we don't need to do that
#     for i in y:
#         for j in i:
#             z.append(j)
#             z.sort()

#         for k in z:
#             n = n + k

#         l.append(n)
#         n = ""
#         z = []

#Here you iterate over the list of sorted words to create a string with each word followed by a comma
    for i in y:
        final = final + i + ","
    
#     return final[0:-1]
    return final[:-1] #we don't return the last character because it is an extra comma

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