0

working with functions/methods in python

Task

Create two functions (generate_data and process_data) in python, where:

  • generate_data function will generate a sequence list from a random integer number based on the following specifications:
    • The function inputs are Length of list, start and end random number.
  • process_data function will process the input list data into this function to produce new output based on the following specifications:
    • The function inputs are data_list as a sequence list, operation_type as a string.
    • There will be three operation’s type:
      • “Sorting”, this will return an ascending sorted list
      • “Max”, this will return the maximum value in the input list
      • “Min”, this will return the minimum value in the input list

Code

from random import randrange
from datetime import date

today = date.today().strftime("%b %d, %Y")


def generate_data(n,start,end):
    list = []
    for i in range(n):
        num = randrange(start,end)
        list.append(num)
    return list

def process_data(data_list,operation_type):
    if operation_type.lower() == "sorting":
        data_list.sort()
        return data_list
    elif operation_type.lower() == "max":
        return max(data_list)
    elif operation_type.lower() == "min":
        return min(data_list.min())
    else:
        return data_list


n = int(input("length of list: "))
start = int(input("Start generating list: "))
end = int(input("End of generating list: "))
type_operation = input("Type of operation: ")

gen_list = generate_data(n,start,end)
print("The generated list is: ",gen_list)
proc_list = process_data(gen_list,type_operation)
print("The processed list is: ",proc_list)

Output

length of list: 10
Start generating list: 5
End of generating list: 25
Type of operation: max
The generated list is: [20, 7, 14, 10, 10, 13, 21, 24, 17, 5]
The processed list is: 24
n = int(input("length of list: "))

keep visiting : shaadcoding

shaad

Leave a Reply

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