Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Nah,
push()
becomes redundant whenclass
changed. Also, instructions ask to try to usepush()
inbuild_one_two_three()
, so directions not really followed.Then I stand corrected, thank you.
I still don't like that it puts the condition after the if_true code
Ternary operator in Python is less "functional", not less "powerfull".
Yes, you can't do such stuff with ternary, but you can call functions using ternary as well.
I should have explained myself better:
It doesn't use the classic
condition ? if_true : if_false
format but theA = X if true else Y
which I find less explicit and much less powerful as it's only useful for value assignment, you can't for instance docondition ? x += 1 : callFoo()
What?
@monobrawl
Is
value if value >= last else -value
a "false" ternary?I personally considered it but opted out as I find it less readable, too bad python doesn't have a true ternary operator i.e:
total += (value >= last ? value : -value)
...wait I thought it said 2 weeks not 2 years, sorry :)
The if/elsif/else can be condensed down to simply:
total += dict[c] if dict[c] >= last else -dict[c]
To start the loop you know dict[c] can't be less than last. This removes the redundant code.
It creates a new head for the given linked list or creates a new list itself, what's wrong? Seems to be correct and neat for me.
This doesn't really solve the Kata according to the instructions. What is the point of the push function in your implementation?
As bkaes said, you don't want to surprise the user by modifying the input list. Lists and dictionaries are examples of data types that are mutable, so when you change an input list in the local function space, you are also changing the input list in the caller space. This can be a useful feature of these data types, but requires special care sometimes.
Additionally, you could have just returned [lst[0], lst[-1]] instead of creating the tempor variable.
Since
min_max
isn't documented, you expect the argumentlst
to hold the following property:After all,
min_max
extracts information fromlst
, so it's not necessary to modify the list. Changing the list would surprise the user.That being said, your solution runs in O(n log n) compared to the optimal O(n). If you don't know the O-notation: if the list has
n
elements, your solution needs abouts * (n * log n)
operations to get the minimum (wheres
is some constant), whereas an optimal solution only usess' * n
operations (wheres'
is another constant). If you're interested in O-notation, search for "Big O notation" or "Landau notation".And why is it dangerous? Do you think I could lost some part of imput or is it a question of Best Practices?
I'm not really experienced programmer so thank you in advance for your answer.
A bit dangerous because you have modified the input list using the .sort() method. Sorting a copy or creating a new list using sorted avoids this situation.