Exercise - Write a Sorting Function

  Рет қаралды 62,489

Khan Academy

Khan Academy

13 жыл бұрын

Challenge to write a Python function that can sort a list in-place

Пікірлер: 14
@parkamark
@parkamark 13 жыл бұрын
@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.
@parkamark
@parkamark 13 жыл бұрын
@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.
@rockingamingwiththesahit2145
@rockingamingwiththesahit2145 3 жыл бұрын
👍👍👍👍
@heyandy889
@heyandy889 13 жыл бұрын
@parkamark "function" and "method" mean almost the same thing. Another synonym is "subroutine."
@jreylbc1
@jreylbc1 13 жыл бұрын
YES YES MORE CS MORE CS!!!!
@JeremyNasmith
@JeremyNasmith 13 жыл бұрын
@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...
@AQWorldAceChronozap
@AQWorldAceChronozap 13 жыл бұрын
and first view
@parkamark
@parkamark 13 жыл бұрын
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
@thelight9492 Жыл бұрын
Methods are functions within a class.
@parkamark
@parkamark 13 жыл бұрын
@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.' ... ???
@Kramer7969
@Kramer7969 13 жыл бұрын
Somebody should write a function to sort the comments, the first comment is in the second spot! :)
@marcportugal1
@marcportugal1 13 жыл бұрын
Khan Rules
@AQWorldAceChronozap
@AQWorldAceChronozap 13 жыл бұрын
FIRST COMMENT
@lifeDotGov
@lifeDotGov 13 жыл бұрын
fourth
Insertion Sort Algorithm
7:52
Khan Academy
Рет қаралды 205 М.
How To Use Functions In Python (Python Tutorial #3)
14:55
CS Dojo
Рет қаралды 2,3 МЛН
Дарю Самокат Скейтеру !
00:42
Vlad Samokatchik
Рет қаралды 3,3 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,7 МЛН
1 or 2?🐄
00:12
Kan Andrey
Рет қаралды 52 МЛН
Insertion Sort in Python
8:36
Khan Academy
Рет қаралды 163 М.
Stepping Through Iterative Fibonacci Function
9:39
Khan Academy
Рет қаралды 40 М.
Quicksort In Python Explained (With Example And Code)
14:13
FelixTechTips
Рет қаралды 137 М.
Stepping Through Insertion Sort Function
11:55
Khan Academy
Рет қаралды 38 М.
Introduction to Programs Data Types and Variables
11:29
Khan Academy
Рет қаралды 1 МЛН
Comparing Iterative and Recursive Factorial Functions
7:48
Khan Academy
Рет қаралды 157 М.
Merge Sort In Python Explained (With Example And Code)
13:35
FelixTechTips
Рет қаралды 199 М.
For Loops in Python
10:15
Khan Academy
Рет қаралды 790 М.