November 28th, 2003, 06:48 PM

help needed python through linux
#!/usr/bin/python
def bubble(a):
n = len(a)
changed = 1
while changed:
changed = 0
for i in xrange (n1):
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??
November 29th, 2003, 01:31 AM

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.
November 29th, 2003, 07:29 AM

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 (n1):
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.
November 29th, 2003, 07:45 AM

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 (n1):
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 07:49 AM.
November 29th, 2003, 11:25 AM

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(len1) if nums[x] > nums[x+1]]
if len>1: bubbleSort(nums, len1)
nums = range(20)
random.shuffle(nums)
print nums
bubbleSort(nums,len(nums))
print nums
November 29th, 2003, 03:20 PM

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.
November 29th, 2003, 09:01 PM

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)
November 29th, 2003, 10:32 PM

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.
November 29th, 2003, 10:58 PM

hmm ..
you're ones aren't quite bubble sort. In bubble sort, you do n1 comparisons which gets the largest element to the end, then you sort it on the first n1 items which gets the second largets element int the second to end psition, etc ..
so you sort (list), then sort(listlast item), then sort(listlast 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 11:03 PM.
November 30th, 2003, 08:38 AM

December 1st, 2003, 03:12 AM

what on earth does this do ??
for s in range(len(list))[::1]
December 1st, 2003, 07:28 AM

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.
December 1st, 2003, 04:21 PM

Ahhhhhh .. nicely done!
Bubble sort in one line
Cheers,
Eli
December 1st, 2003, 06:47 PM

Just a note: the reverse slice notation [::1] is something new that came out in (i think) Python 2.3 (coulda been 2.2can't find the docs).
December 1st, 2003, 06:55 PM

"Extended Slices" are very new, Python 2.3! Although there are other ways to do the same thing it would require more than this example
You should note that [::1] may work in versions before Python 2.3 since the 3rd slice step has been available since Python 1.4
Mark.