Ad
  • Default User Avatar

    Nah, push() becomes redundant when class changed. Also, instructions ask to try to use push() in build_one_two_three(), so directions not really followed.

  • Custom User Avatar

    Then I stand corrected, thank you.
    I still don't like that it puts the condition after the if_true code

  • Custom User Avatar

    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.

  • Custom User Avatar

    I should have explained myself better:
    It doesn't use the classic condition ? if_true : if_false format but the A = 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 do condition ? x += 1 : callFoo()

  • Custom User Avatar

    too bad python doesn't have a true ternary operator

    What?

  • Default User Avatar

    @monobrawl
    Is value if value >= last else -value a "false" ternary?

  • Custom User Avatar

    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 :)

  • Custom User Avatar

    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.

  • Custom User Avatar

    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.

  • Default User Avatar

    This doesn't really solve the Kata according to the instructions. What is the point of the push function in your implementation?

  • Default User Avatar

    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.

  • Custom User Avatar

    Since min_max isn't documented, you expect the argument lst to hold the following property:

    old_list = list(lst)
    min_max(lst)
    old_list == lst # element wise equal with same order
    

    After all, min_max extracts information from lst, 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 about s * (n * log n) operations to get the minimum (where s is some constant), whereas an optimal solution only uses s' * n operations (where s' is another constant). If you're interested in O-notation, search for "Big O notation" or "Landau notation".

  • Custom User Avatar

    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.

  • Default User Avatar

    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.