0

# In list [5,8,2,3,1] sort without user defined functions ?

Note .... Even len() also don't use

15th Feb 2020, 1:43 PM
Rajkumar
18 ответов
+ 13
A bit late as always... lst = [2,6,8,4,3,2,9,15,0] len_ = (sum([1 for i in lst])) # shortest version to get len # or len_ = 0 for i in lst: len_ += 1 res = [] count = 0 while count < len_: res.append(lst.pop(lst.index(min(lst)))) count += 1 print(res)
15th Feb 2020, 7:28 PM
Lothar
+ 7
Dirk, sorted output for integers by using set() is true, but not for float and string: x = [5, 8, 2, 3, 1] print(set(x)) # output is {1, 2, 3, 5, 8} y = [10.5, 3.5, 2.0] print(set(y)) # output is {2.0, 10.5, 3.5} z = ['g', 'a', 'x', 'b'] print(set(z)) # output is {'g', 'b', 'a', 'x'} but can vary
16th Feb 2020, 7:34 AM
Lothar
+ 5
Here a clanky len version, without any functions or methods. ;-) length = 0 while True: try: your_list[length] length += 1 except: break Another version working with slice: length = 0 clone = your_list[:] while clone: clone = clone[1:] length += 1
15th Feb 2020, 2:10 PM
HonFu
+ 4
Do you mean 'without built-in functions'?
15th Feb 2020, 1:48 PM
HonFu
+ 3
I'm only telling you so that you get a good answer more easily. I think the easiest (not the quickest) sorting algorithm will be bubble sort. You can try to implement that in Python. https://www.sololearn.com/learn/649/?ref=app
15th Feb 2020, 1:57 PM
HonFu
+ 3
I think for this process INSERTION SHOT would be more easy and fast
16th Feb 2020, 4:05 PM
Vijay Gunwant
+ 2
Ok thanks
15th Feb 2020, 1:58 PM
Rajkumar
+ 2
If you don't need the output to be a list, you can use set([5,8,2,3,1])
16th Feb 2020, 3:41 AM
David Casson
+ 2
Good question
16th Feb 2020, 2:23 PM
Edward
+ 1
Yes
15th Feb 2020, 1:49 PM
Rajkumar
+ 1
I did using length any other #program to sort the values l = [5,8,2,3,1] for x in range(len(l)): b = 0 a = 1 for i in l: if a == len(l): break if l[b]<l[a]: a += 1 b += 1 continue elif l[b] > l[a]: c = l[a] l[a] = l[b] l[b] = c a += 1 b += 1 elif l[a] == l[b]: a += 1 b += 1 continue print(l)
15th Feb 2020, 1:53 PM
Rajkumar
+ 1
Then you should edit your question text, so that people can see immediately what you really want. Also you could add more meaningful tags, 'sort' as a minimum.
15th Feb 2020, 1:54 PM
HonFu
+ 1
It's my first post i will learn all ..
15th Feb 2020, 1:55 PM
Rajkumar
+ 1
Hm, so you already have a working version. You only need to get rid of the len?
15th Feb 2020, 2:00 PM
HonFu
+ 1
len_ = 0 for i in lst: len_+=1 lol. I did some serious code babble up there, Lothar. 😅🤣
15th Feb 2020, 7:41 PM
HonFu
+ 1
Lothar fantastic solution. Very Pythonic!
16th Feb 2020, 3:51 AM
Tamoghna Saha
0
How to get the input as character
16th Feb 2020, 4:36 AM
Yeswanth S
0
Yes we can sort using <list or list_variable>.sort() function which is inbulit function
17th Feb 2020, 7:25 AM
Tarandeep Singh Gujral