|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
help needed python through linux
#!/usr/bin/python
def bubble(a): n = len(a) changed = 1 while changed: changed = 0 for i in xrange (n-1): if a[i] > a[i+1]: a[i],a[i+1] = a[i+1],a[i] changed =1 n-= 1 import random a =range(20) random.shuffle(a) print a bubble (a) print a When I run in linux with command ./filename I get error mess ./HW7.py: line5 syntax error near unexpected token 'bubble(a' ./HW7.py: line 5 'def bubble(a):' Why dosn't linux recognize the python code?? |
|
#2
|
|||
|
|||
|
um, if that's what the file looks like (i.e. no tabs or anything), then it needs to be formatted so that it does have proper indentation. If it's not what the file looks like, paste the file contents between code tags ([ code ] and [ /code ] but without spaces between the []s and the tag name) so we can see what the indentation looks like.
|
|
#3
|
||||
|
||||
|
Here's his code for anyone whos interested..
Code:
#!/usr/bin/python
def bubble(a):
n = len(a)
changed = 1
while changed:
changed = 0
for i in xrange (n-1):
if a[i] > a[i+1]:
a[i],a[i+1] = a[i+1],a[i]
changed =1
n-= 1
import random
a =range(20)
random.shuffle(a)
print a
bubble (a)
print a
Mark. |
|
#4
|
||||
|
||||
|
Ok, this works for me, all i've done is reindent your original program..
Code:
#!/usr/bin/python def bubble(a): n = len(a) changed = 1 while changed: changed = 0 for i in xrange (n-1): if a[i] > a[i+1]: a[i],a[i+1] = a[i+1],a[i] changed =1 n -= 1 import random a =range(20) random.shuffle(a) print a bubble(a) print a Just a tip, and i don't know if its just me but you should really limit your import statments to the top of your program, it makes it so much easier to see what modules your using you might also want to give your variables more useful names.One question, whats this fucntion surposed to do? all it seems to do is swap the location or one array element with the one infront :S, the actual randomizing is done by random.shuffle() Mark. Last edited by netytan : November 29th, 2003 at 06:49 AM. |
|
#5
|
|||
|
|||
|
Just though this is cool - BubbleSort in 2 lines with recursion and list comprehension
Code:
#!/usr/bin/python
import random
def swap(nums,x,y):
nums[x],nums[y] = nums[y],nums[x]
def bubbleSort(nums,len):
[ swap(nums,x,x+1) for x in range(len-1) if nums[x] > nums[x+1]]
if len>1: bubbleSort(nums, len-1)
nums = range(20)
random.shuffle(nums)
print nums
bubbleSort(nums,len(nums))
print nums
|
|
#6
|
||||
|
||||
|
Hey Yogi, long time no see
.. i hate to tell you this but your program simply sorts the results (returning a list of 1, 2, 3..)..If this IS what the function was surposed to do this then list.sort() should give you the same results rla ![]() This should work (based on Yogi's program!) Code:
#!/usr/bin/env python from random import * def swap(list, x, y): list[x], list[y] = list[y], list[x] def sort(list): [swap(list, n, n + 1) for n in range(len(list) - 1) if list[n] > list[n + 1]] if __name__ == '__main__': l = range(20) shuffle(l) print l sort(l) print l I dunno , if this doesn't do what you what you wanted rla let us know..Mark. |
|
#7
|
|||
|
|||
|
Hi Mark.
Yes, it's meant to sort it. But by using 'Bubble Sort'. Bubble sort is a specific algorithm for sorting, which is what rla66's algirithm is trying to do. I just thought it's cool that it can be done in 2 lines with python using recursion and list comprehension. By the way, your version doesnt work, it only has one iteration which is why it doesn't sort the result. The algorithm is explained here if you're interested on learning how it works: http://linux.wku.edu/~lamonml/algor/sort/bubble.html It's O(n^2), so you would seldom use it in practice unless its with almost completely sorted data already. I prefer merge sort which is O(n*log(n)) in best and worst case which has been proven to be the fastest a list with no range bound can be sorted. (Bucket sort is the ideal one if you have an reasonable upper bound on the max number) |
|
#8
|
||||
|
||||
|
Curse my lack of theory
, i surpose uni does have its uses! Anyway never heard of bubblesort before lol.. The reason it didn't work is because i wasn't trying to sort the data i was just duplicating the results i got from his code ![]() Anyway fixed, easy as pie.. no recursion required! Just a little fiddle with the list compression Code:
#!/usr/bin/env python from random import * def swap(list, x, y): list[x], list[y] = list[y], list[x] def sort(list): [[swap(list, n, n + 1) for n in range(len(list) - 1) if list[n] > list[n + 1]] for e in list] if __name__ == '__main__': l = range(20) shuffle(l) print l sort(l) print l or you could simply add a for loop.. Code:
def sort(list): for each in list: [swap(list, n, n + 1) for n in range(len(list) - 1) if list[n] > list[n + 1]] or fully padded out (not using list compressions) Code:
def sort(list): for each in list: for num in range(len(list) - 1): if list[num] > list[num + 1]: swap(list, num, num + 1) Anyway you get the idea . I'll look into the links, thanks for the info Yogi!Mark. |
|
#9
|
|||
|
|||
|
hmm ..
you're ones aren't quite bubble sort. In bubble sort, you do n-1 comparisons which gets the largest element to the end, then you sort it on the first n-1 items which gets the second largets element int the second to end psition, etc .. so you sort (list), then sort(list-last item), then sort(list-last 2 items), etc ... your just sorts the whole lot each time. Its not quite bubble sort =/ Eli Edit: And since it does the same thing each loop, it lends itself naturally to recursion =) Last edited by lazy_yogi : November 29th, 2003 at 10:03 PM. |
|
#10
|
||||
|
||||
|
Naw i'm convinced you don't need recursion, that and that you can do it in one line
![]() Anyway last try: Code:
def sort(list): [[swap(list, n, n + 1) for n in range(s - 1) if list[n] > list[n + 1]] for s in range(len(list))[::-1]] Basically the same as before except this time it goes backwards through 19, 18, 17 etc.. so its doing the same thing as your code Yogi , all i've done is replace the recursion with a loop.*wonders weather to go too uni instead of sitting at home* home sounds more fun ![]() Mark. |
|
#11
|
|||
|
|||
|
what on earth does this do ??
for s in range(len(list))[::-1] |
|
#12
|
||||
|
||||
|
Simply iterates backwards over the length of list, the [::-1] is an extended slice which reverses a list or string i.e.
>>> string = 'string' >>> string[::-1] 'gnirts' >>> list = ['l', 'i', 's', 't'] >>> list[::-1] ['t', 's', 'i', 'l'] >>> Mark. |
|
#13
|
|||
|
|||
|
Ahhhhhh .. nicely done!
Bubble sort in one line ![]() Cheers, Eli |
|
#14
|
|||
|
|||
|
Just a note: the reverse slice notation [::-1] is something new that came out in (i think) Python 2.3 (coulda been 2.2--can't find the docs).
|