-3

I have an assignment, where I'm to sort a list from low to high. But my program gives out an inverse of the expected order. This is my futile attempt at the assignment. Please help me I am genuinely stuck. Hopefully it is a noob mistake.

from random import *

def main():
  # initializing variables
  numList = []; #the list of integers
  numListLength = 25; #the number of integers in numList array

  maxShuffles = 1000; #the number of times numList is to be shuffled

  #populating numList
  while len(numList) < numListLength :
    randomElement = randint(-100, 100)
    numList.append(randomElement)

  printNow("List before shuffling: " )
  printNow(numList)

  #shuffle the list multiple times
  shuffleCount = 0
  while shuffleCount < maxShuffles :
    i = randrange( 0, len(numList) )
    j = randrange( 0, len(numList) )

    if i < j :
      #compare the contents  of those locations
      if numList[i] < numList[j] :
        #swap the contents
        original_i = numList[i]
        original_j = numList[j]

        numList[i] = original_j
        numList[j] = original_i

    elif j < i :
      if numList[j] < numList[i] :
          #swap the contents
          original_i = numList[i]
          original_j = numList[j]

          numList[i] = original_j
          numList[j] = original_i 

    #increment shuffleCounter
    shuffleCount = shuffleCount + 1

  #shuffling done, display results
  printNow("List after shuffling: ")
  printNow(numList)
main()
  • Please elaborate your question. Read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and edit your question. – vishes_shell Mar 20 '18 at 07:00
  • 1
    This is actually pretty awful Python code. How much of it did your teacher write? Looks like the assignment was ported from java – John La Rooy Mar 20 '18 at 07:17
  • Do you have a `printNow` function? It seems that your code won't print. – kiyah Mar 20 '18 at 07:22

3 Answers3

1
from random import randint, randrange

def main():
  #  initializing variables
  num_list_length = 25  # the number of integers in num_list array

  max_shuffles = 1000  # the number of times num_list is to be shuffled

  # populating num_list
  num_list = [randint(-100,100) for i in range(num_list_length)]

  print("List before shuffling: " )
  print(num_list)

  # shuffle the list multiple times
  for shuffle_count in range(max_shuffles):
    i = randrange(0, len(num_list))
    j = randrange(0, len(num_list))

    if i > j:  # ensure i is always smaller than j
        i, j = j, i

    #compare the contents of those locations
    if num_list[i] < num_list[j]:
      num_list[i], num_list[j] = num_list[j], num_list[i]

  #shuffling done, display results
  print("List after shuffling: ")
  print(num_list)

if __name__ == "__main__":
  main()
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0
from random import *

def main():
  # initializing variables
  numList = []; #the list of integers
  numListLength = 25; #the number of integers in numList array

  maxShuffles = 1000; #the number of times numList is to be shuffled

  #populating numList
  while len(numList) < numListLength :
    randomElement = randint(-100, 100)
    numList.append(randomElement)

  printNow("List before shuffling: " )
  printNow(numList)

  #shuffle the list multiple times
  shuffleCount = 0
  while shuffleCount < maxShuffles :
    i = randrange( 0, len(numList) )
    j = randrange( 0, len(numList) )

    if i < j :
      #compare the contents  of those locations
      if numList[i] > numList[j] : #Inverted < sign
        #swap the contents
        original_i = numList[i]
        original_j = numList[j]

        numList[i] = original_j
        numList[j] = original_i

    elif j < i :
      if numList[j] > numList[i] : #Inverted < sign
          #swap the contents
          original_i = numList[i]
          original_j = numList[j]

          numList[i] = original_j
          numList[j] = original_i 

    else: #Handling i==j case
          continue

    #increment shuffleCounter
    shuffleCount = shuffleCount + 1

  #shuffling done, display results
  printNow("List after shuffling: ")
  printNow(numList)
main()
DZurico
  • 637
  • 4
  • 11
0

In order to print the reverse order of what your program prints, change your i < j to i > j and j < i to i < j. Do the same for your numList[i] > numList[j].

Your code will now print something like:

List before shuffling: 
[26, 52, -58, 48, -91, -53, -20, -7, 78, -74, 10, -8, 29, -57, 31, 80, -76, -48, 45, -59, -46, -23, 33, -64, -89]
List after shuffling: 
[-91, -89, -76, -74, -64, -59, -58, -57, -53, -46, -48, -23, -20, -8, -7, 10, 26, 29, 31, 33, 45, 48, 52, 78, 80]
kiyah
  • 1,502
  • 2
  • 18
  • 27