Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Python

Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-2008, 09:09 AM
tarmael tarmael is offline
Newbie
 
Join Date: Aug 2008
Posts: 3
Rep Power: 0
tarmael is on a distinguished road
Default Simple list question

Hey, new to the forum.
Also, new to Python (Loving it).

We're doing exercises in class and I'm really finding this teacher useless.

The exercise is a bubble sort.

I'd love to learn by myself to help practice, but I need help and I know he's (teacher) not going to be able to provide.

Here it is:

Bubble sorting. Sort 5 characters alphanumerically.
In high school I was learning Visual Basic.
an array was easy.
example:
Code:
loop
x = x + 1
my_array(x) = 4 + x
end loop
How do I get a similar thing in Python?

Here's the algorithm for the actual exercise for those playing at home.
If you come up with a working solution, please don't post it, I want to work the bulk for myself.

Code:
sortList = [set of 5 characters] e.g. [1,10,15,3,56]
movedElement = true
while movedElement = true
     movedElement = false
     x = 0
     while x <= 5
          if sortList[x] >sortList[x+1]
                temp = sortList[x]
                sortList[x] = sortList[x+1]
                sortList[x+1] = temp
                movedElement = true
         end if
         x = x + 1
     end while
display sortList
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 08-14-2008, 09:48 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Simple list question

What exactly do you want? Do you want to know how to assign values to a list?

Initializing:
Code:
>>> a1 = []
>>> a1
[]
>>> a2 = [1, 2, 3]
>>> a2
[1, 2, 3]
Appending:
Code:
>>> a
[1, 2, 3]
>>> a.append(3)
>>> a
[1, 2, 3, 3]
Extending:
Code:
>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a.extend([4, 5])
>>> a
[1, 2, 3, 4, 5]
Inserting:
Code:
>>> a = [1, 2, 4, 5]
>>> a
[1, 2, 4, 5]
>>> a.insert(2, 3)
>>> a
[1, 2, 3, 4, 5]
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-15-2008, 03:25 AM
tarmael tarmael is offline
Newbie
 
Join Date: Aug 2008
Posts: 3
Rep Power: 0
tarmael is on a distinguished road
Default Re: Simple list question

The idea is that the user inputs 5 different characters and the program sorts them alphanumerically.

So I need to find a way to get the list to allow for user input.

In VB it would be like this:
Code:
x = 1
loop until x = 6
     my_array(x) = *USERS INPUT METHOD*
     x = x + 1
end loop
so in Python I think I need something like,
Code:
lst = []
count = 0
while 1:
      count += 1 
      char = raw_input("Enter character %i to be sorted" % count)
      lst.append(tmp)
lst.remove(tmp)
print lst
I'm not sure...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-15-2008, 08:28 AM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Simple list question

This will give you an idea of how to do it. Note that I'm not going to make it for you, because like you said, you will solve it yourself.
Code:
myList = []
for x in range(0, 5):
    myList.append(raw_input("Enter: "))
print sorted(myList)
__________________
05-03-2007 - 11-13-2008
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-15-2008, 07:57 PM
tarmael tarmael is offline
Newbie
 
Join Date: Aug 2008
Posts: 3
Rep Power: 0
tarmael is on a distinguished road
Default Re: Simple list question

And here is the result!

Thanks a heap!!

Code:
print "Bubble sort. Sort 5 Numbers"
sortList = []
for x in range(5):
    sortList.append(raw_input("Enter number %i " % (x+1)))
movedElement = True
while movedElement == True:
    movedElement = False
    x = 0
    while x < 4:
        if int(sortList[x]) > int(sortList[x+1]):
            temp = sortList[x]
            sortList[x] = sortList[x+1]
            sortList[x+1] = temp
            movedElement = True
        x+=1
print sortList[0], sortList[1], sortList[2], sortList[3], sortList[4]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 10-27-2008, 11:11 PM
Donovan's Avatar   
Donovan Donovan is offline
Programmer
 
Join Date: Oct 2008
Location: Texas
Age: 14
Posts: 139
Rep Power: 2
Donovan will become famous soon enoughDonovan will become famous soon enough
Send a message via ICQ to Donovan Send a message via MSN to Donovan
Default Re: Simple list question

Very nice If you have more questions try and check this out. I just put it together.

[Detailed Guide] Programming and You!

Cool, 30th post I am now a new rank
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
array, bubble, lists



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download Jordan Community Projects 331 11-26-2008 12:35 PM
C/C++ FAQ: Read this before you post! v0id C and C++ 7 08-05-2008 01:08 PM
Probably a simple class question utdiscant Python 1 06-10-2008 10:10 PM
Animated Ripple effect (Simple) - Flash MX 2004 ahsan16 Photoshop Tutorials 10 05-22-2008 06:54 AM
need some question asked debug C and C++ 4 02-27-2008 08:27 AM


All times are GMT -5. The time now is 08:31 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads