0
In list [5,8,2,3,1] sort without user defined functions ?
Note .... Even len() also don't use
18 Respuestas
+ 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)
+ 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
+ 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
+ 4
Do you mean 'without built-in functions'?
+ 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
+ 3
I think for this process INSERTION SHOT would be more easy and fast
+ 2
Ok thanks
+ 2
If you don't need the output to be a list, you can use set([5,8,2,3,1])
+ 2
Good question
+ 1
Yes
+ 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)
+ 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.
+ 1
It's my first post i will learn all ..
+ 1
Hm, so you already have a working version. You only need to get rid of the len?
+ 1
len_ = 0
for i in lst:
len_+=1
lol.
I did some serious code babble up there, Lothar. 😅🤣
+ 1
Lothar fantastic solution. Very Pythonic!
0
How to get the input as character
0
Yes we can sort using <list or list_variable>.sort() function which is inbulit function