|
||||||
| Python Discussion forum for Python, a high-level language with simple syntax, but yet powerful. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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 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
|
| Sponsored Links |
|
|
|
|||
|
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
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
|
|
|||
|
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]
|
| Sponsored Links |
|
|
|
|||||
|
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 ![]()
__________________
|
![]() |
| Tags |
| array, bubble, lists |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
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 |
| 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 |