Jump to content

Simple list question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
tarmael

tarmael

    Newbie

  • Members
  • Pip
  • 3 posts
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:
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.

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


#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
What exactly do you want? Do you want to know how to assign values to a list?

Initializing:
>>> a1 = []
>>> a1
[]
>>> a2 = [1, 2, 3]
>>> a2
[1, 2, 3]

Appending:
>>> a
[1, 2, 3]
>>> a.append(3)
>>> a
[1, 2, 3, 3]

Extending:
>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a.extend([4, 5])
>>> a
[1, 2, 3, 4, 5]

Inserting:
>>> a = [1, 2, 4, 5]
>>> a
[1, 2, 4, 5]
>>> a.insert(2, 3)
>>> a
[1, 2, 3, 4, 5]


#3
tarmael

tarmael

    Newbie

  • Members
  • Pip
  • 3 posts
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:
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,
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...

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.
myList = []
for x in range(0, 5):
    myList.append(raw_input("Enter: "))
print sorted(myList)


#5
tarmael

tarmael

    Newbie

  • Members
  • Pip
  • 3 posts
And here is the result!

Thanks a heap!!


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]



#6
Donovan

Donovan

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 798 posts
Very nice:) If you have more questions try and check this out. I just put it together.

http://forum.codecal...amming-you.html

Cool, 30th post I am now a new rank:)
Posted Image
+Friend Me | My Graphics | Forum Rules | Help Forum | Forum FAQ