0

printing sorted list, minimum and maximum value from the list, duplicate numbers from a list, and even numbers from the list in python

Task

Create a program to generate a list called num_list that has n elements from a random integer number between the start to end, and the output of this program should print the following:

  • The original generate list (num_list).
  • The sorted num_list.
  • The minimum and maximum value in num_list.
  • The list of duplication number from the num_list.
  • The only even numbers without duplication from the num_list.

Specifications

  • The program inputs are:
    • n is length of the list, ….                          (Must be integer and greater than 40)
    • start of random generation number.
    • end of random generation number.          (Must be integer and end greater than start)

Note:

  • You need to show the output result for all exception error output. For example, input the n is -90 or n is 30.
  • You can use this command to generate random number

Code

from random import randrange
n = int(input("Enter N : "))
num_list = []
even = set()

#checks if n is greater than 40
if n > 40:
    #taking start and end from user
    start = int(input("Enter Starting Number : "))
    end = int(input("Enter Ending Number : "))
    #checks if end is less than start
    if start < end:
        #generating num_list with size equal to n with random values between start and end
        for i in range(n):
            num = randrange(start,end)
            num_list.append(num)
            #if number generated is even then adding it to even set
            if num%2 == 0:
                even.add(num)

        #converting set to list
        even = list(even)
        seen = set()
        #finding duplicates items
        for x in num_list:
            if num_list.count(x) > 1:
                seen.add(x)
        dup = list(seen)
        #printing results
        print("Original List       : ",num_list)
        num_list.sort()
        print("Sorted List         : ",num_list)
        print("Max Value           : ",max(num_list))
        print("Min Value           : ",min(num_list))
        print("Duplication Numbers : ",dup)
        print("Even Numbers in List: ",even)

    else:
        print("Start must be less than End")
else:
    print("Value of n must be greater than 40")

Output

Enter N : 42
Enter Starting Number : 60
Enter Ending Number : 80
Original List : [72, 62, 61, 77, 66, 74,
66, 71, 66, 69, 76, 60,
77, 78, 61, 79, 61, 65,
60, 61, 77, 79, 74, 79,
64,63, 76, 69, 67, 66,
62, 78, 71, 69, 66, 76, 75,
63, 77, 61, 75]
Sorted List : [60, 60, 61, 61, 61, 61, 61,
62, 62, 63, 63, 64, 65, 66,
66, 66, 66, 66, 67, 69, 69,
69, 71, 71, 72, 74, 74, 75,
75, 76, 76, 76, 77, 77, 77,
77, 77, 78, 78, 79, 79, 79]
Max Value : 79
Min Value : 60
Duplication Numbers : [66, 69, 71, 74, 75, 76, 77,
78, 79, 60, 61, 62, 63]
Even Numbers in List: [64, 66, 72, 74, 76, 78, 60, 62]

keep visiting : shaadcoding

shaad

Leave a Reply

Your email address will not be published. Required fields are marked *