Challenge to write a Python function that can sort a list in-place
Пікірлер: 14
@parkamark13 жыл бұрын
@heyandy889 I mostly agree with you. However, a function by definition returns a value or object. A subroutine doesn't. As it turns out, all functions and methods in Python return the object 'None', if no explicit 'return' statement is used within the function or method to return something. So in Python terms, they are the same thing. The intention of my comment was to highlight the semantic difference between both, within the realms of imperative and object orientated language paradigms.
@parkamark13 жыл бұрын
@JeremyNasmith min() returns the minimum value from a list/set, yes. The code is short, but it is far from efficient. Python takes the niceties out of having to traverse arrays/lists by hiding all the real work from the coder (when you call min() on a list, it's having to go away and call a lengthy subroutine to work this out). Based on the data structures Python uses to store lists/sets etc, this will normally be pretty fast, most times. As for Lambda functions, Google is your friend.
@rockingamingwiththesahit21453 жыл бұрын
👍👍👍👍
@heyandy88913 жыл бұрын
@parkamark "function" and "method" mean almost the same thing. Another synonym is "subroutine."
@jreylbc113 жыл бұрын
YES YES MORE CS MORE CS!!!!
@JeremyNasmith13 жыл бұрын
@parkamark Nice concise code: but for us noobs: what is lambda in python? And I suppose min(l) returns the smallest/first value in list l? if min works this way, it's a very nice solution for having to traverse the list, and remember progressively smaller encountered values...
@AQWorldAceChronozap13 жыл бұрын
and first view
@parkamark13 жыл бұрын
a.sort() is not a function, it is a method which operates on list objects. But I appreciate what you are saying about a.sort() being in-line. My attempt at sorting a list without using in-built Python sort (non in-line) is this (yes, it is the most naive sorting algorithm but it works): def my_remove(l,v): l.remove(v) return l my_sort = lambda l: l and [min(l)] + my_sort(my_remove(l,min(l))) my_sort([7,3,5,9,3,5,45,1,3,764745,42354,3]) [1, 3, 3, 3, 3, 5, 5, 7, 9, 45, 42354, 764745]
@thelight9492 Жыл бұрын
Methods are functions within a class.
@parkamark13 жыл бұрын
@AkrionXxarr My comments are in relation to Python, not C/C++. Interestingly, you have written, and I quote, 'a function of type void doesn't return anything' - well, it does, doesn't it? It returns an object, of type 'void'. Python returns objects of type 'None'. WikiPedia: 'The void type, in several programming languages derived from C and Algol68, is the type for the result of a function that returns normally, but does not provide a result value to its caller.' ... ???
@Kramer796913 жыл бұрын
Somebody should write a function to sort the comments, the first comment is in the second spot! :)