Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    USA
    Posts
    1
    Rep 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??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    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.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 07:49 AM.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    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
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    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)
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    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 11:03 PM.
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    what on earth does this do ??

    for s in range(len(list))[::-1]
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    325
    Rep Power
    12
    Ahhhhhh .. nicely done!
    Bubble sort in one line

    Cheers,
    Eli
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    35
    Rep Power
    12
    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).
  28. #15
  29. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    "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.
    programming language development: www.netytan.com Hula

Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo