-3

sort a list let's say list_1 of numbers in increasing order using the random library.

Import the randint definition of the random library of python.

how to use randint

  • 1
    is there any code that you have written that you need help with? – Ma0 Sep 04 '18 at 06:39
  • i dont know how to swap element – Rohitjojo09 Sep 04 '18 at 06:40
  • `temp = list_1[i]; list_1[i] = list_1[j]; list_1[j] = temp` – Ma0 Sep 04 '18 at 06:43
  • I had this exact question - formatted like this as well just a few days ago. It was put on hold then IIRC. Either you are taking the same course or you posted a dupe of a deleted question ... – Patrick Artner Sep 04 '18 at 06:47
  • 2
    A search for **python swap list elements** would have answered your questions with this post: [how-to-switch-position-of-two-items-in-a-python-list](https://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list) or [how-to-switch-position-of-two-items-in-a-python-list](https://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list) or [fastest-way-to-swap-elements-in-python-list](https://stackoverflow.com/questions/4554130/fastest-way-to-swap-elements-in-python-list) – Patrick Artner Sep 04 '18 at 06:50
  • 2
    Possible duplicate of [How to switch position of two items in a Python list?](https://stackoverflow.com/questions/2493920/how-to-switch-position-of-two-items-in-a-python-list) – John M Sep 04 '18 at 07:08

2 Answers2

0

For your question's title:

As a full program

from random import sample
print(sample((s:=eval(input())),len(s)))

Explanation

from random import sample             Imports the sample function only in order to preserve speed.
print(                                Print out ...
      sample(                         Shuffle randomly this list...
             (s:=                     Python 3.8's assignment eval (assign to s while still evaling)
                  eval(input())       Evalulate the input, which is a Python list
             ),
             len(                     Take the length of ...
                  s                   the input
             )
      )
)

Try it online!

As an anonymous lambda

lambda x:sample(x, len(x))
from random import sample

Explanation

lambda x:                             Define a lambda that takes in one argument named x
         sample(                      Shuffle randomly
                 x,                   The one argument, which is a list
                 len(                 The length of ...
                     x                The one argument, which is a list
                 ) 
         )
from random import sample             Import the sample function

Try it online!

As a function

def e(x):return sample(x, len(x))
from random import sample

Explanation

def e(x):                            Define a function named e with one argument named x
         return                      Set the functions value to be ...
               sample(               Shuffle a list
                      x,             The one argument, which is a list
                      len(           Length of ...
                          x          The one argument; x
                      )
               )
from random import sample            Import the sample function

Try it online!

For your first question in the body of your question:

You cannot sort a list using the random module, as it is for random functions, not for sorting. However, you can use Python's sorted() function, which sorts a list.

As a full program

print(sorted(eval(input())))

Explanation

print(                          Print out ...
      sorted(                   The sorted version of ...
             eval(              The evaluated version of ...
                  input()       The input
             )
      )
)

Try it online!

As an anonymous lambda

lambda x:sorted(x)

Explanation

lambda x:                        Declare a lambda with one argument, x
         sorted(                 Return the sorted value of...
                x                The lambda's argument
         )

Try it online!

MilkyWay90
  • 2,023
  • 1
  • 9
  • 21
-1
import random
n=int(input())
list_1 = []
for i in range(n):
    list_1.append(int (input()))
list_2=[] 
while list_1:
    minimum = list_1[0]
    for x in list_1: 
        if x < minimum:
            minimum = x
    list_2.append(minimum)
    list_1.remove(minimum)    
sarr = [str(a) for a in list_2]
print(' '.join(sarr))
idontknow700
  • 13
  • 1
  • 5