Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 28th, 2003, 05:48 PM
rla66 rla66 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: USA
Posts: 1 rla66 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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??

Reply With Quote
  #2  
Old November 29th, 2003, 12:31 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
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.

Reply With Quote
  #3  
Old November 29th, 2003, 06:29 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #4  
Old November 29th, 2003, 06:45 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #5  
Old November 29th, 2003, 10:25 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old November 29th, 2003, 02:20 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #7  
Old November 29th, 2003, 08:01 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #8  
Old November 29th, 2003, 09:32 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old November 29th, 2003, 09:58 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old November 30th, 2003, 07:38 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old December 1st, 2003, 02:12 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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]

Reply With Quote
  #12  
Old December 1st, 2003, 06:28 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #13  
Old December 1st, 2003, 03:21 PM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 11
Ahhhhhh .. nicely done!
Bubble sort in one line

Cheers,
Eli

Reply With Quote
  #14  
Old December 1st, 2003, 05:47 PM
oxygenthief oxygenthief is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 35 oxygenthief User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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).

Reply With Quote
  #15  
Old December 1st, 2003, 05:55 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
"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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > help needed python through linux

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap