The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
help needed python through linux
Discuss help needed python through linux in the Python Programming forum on Dev Shed. help needed python through linux Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 28th, 2003, 05:48 PM
|
|
Junior Member
|
|
Join Date: Nov 2003
Location: USA
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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??
|

November 29th, 2003, 12:31 AM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
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, 06:29 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
__________________
programming language development: www.netytan.com – Hula
|

November 29th, 2003, 06:45 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

November 29th, 2003, 10:25 AM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
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
|

November 29th, 2003, 02:20 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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, 08:01 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
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, 09:32 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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, 09:58 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
|
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.
|

November 30th, 2003, 07:38 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

December 1st, 2003, 02:12 AM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
|
what on earth does this do ??
for s in range(len(list))[::-1]
|

December 1st, 2003, 06:28 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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, 03:21 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 325
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
|
|
Ahhhhhh .. nicely done!
Bubble sort in one line
Cheers,
Eli
|

December 1st, 2003, 05:47 PM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 35
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
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).
|

December 1st, 2003, 05:55 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
"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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|