In: Computer Science
Create a python program that will:
prompt a user for a command
Command
get_data
Level 1: Take one of the commands
my_max
my_min
my_range
my_sum
mean
median
Requirements:
Your commands should be case-insensitive
You should use python lists to store data
You should NOT use built-in python math functions, or math libraries to compute these values
Tips:
Write one function that will convert a string with comma-separated numbers into a python list with the numbers. You can use this in multiple other functions.
Don't name any of your functions "min", "max", or "range" or "sum". These are built-in functions.
Details
Load command details
Get_data: prompts users for a list of numbers, separated by commas
(please also print the list, for testing)
Level 1 details (these functions work with the loaded data list)
my_max
my_min
my_range: Computes the range (difference of greatest value and least value)
my_sum
mean: Computes and prints the arithmetic mean (average)
Level 2
median: show the median (middle value, or average of two middle values)
mode: show the mode, or modes
Level 3 details (these functions should use the first number in the data list)
prime: determine if a number is prime
factorize: get the prime factors of a number
fib: get the nth number in the fibonacci sequence
Here is my code so far:
#hehe my sum and stuff
f = open("input.txt",'r')
def input(prompt=''):
print(prompt, end='')
return f.readline().strip()
#get data
def get_data():
pass
line = input().strip().split(',')
nums=[]
for n in line:
nums.append(float(n))
return nums
#get sum
def my_sum(data):
s=0
for n in data:
s += n
return s
pass
#get max
def my_max(data):
m=0
for n in data:
m >= n
return m
pass
#get min
def my_min(data):
i=0
for n in data:
i < n
return i
pass
#get range
def my_range(data):
r=0
for n in data:
m - i
return r
pass
#get mean
def mean(data):
e=0
for n in data:
????????
#get median
def median(data):
d=0
for n in data:
??????
#get mode
def mode(data):
o=0
for n in data:
???
#get prime
def prime(data):
p=0
for n in data:
???
#get factorization
def factorization(data):
f=0
for n in data:
????
#get fib
def fib(data):
b=0
for n in data:
??
#run it
def run_cli():
while(1):
cmd = input()
if cmd=='exit':
break
elif cmd=='get_data':
data = get_data()
print(data)
elif cmd=='my_sum':
s = my_sum(data)
print(s)
elif cmd=='my_max':
m = my_max(data)
print(m)
elif cmd=='my_min':
i = my_min(data)
print(i)
elif cmd=='my_range':
r = my_range(data)
print(r)
elif cmd=='mean':
e = mean(data)
print(e)
elif cmd=='median':
d = median(data)
print(d)
elif cmd=='mode':
o = mode(data)
print(o)
elif cmd=='prime':
p = prime(data)
print(p)
elif cmd=='factorization':
f = factorization(data)
print(f)
elif cmd=='fib':
b = fib(data)
print(b)
pass
#ha ahsbas
def main():
run_cli()
main()
Heres the missing functions, please do as needed and if you need any further help you can always reach out to me in comments
f = open("input.txt",'r')
def input(prompt=''):
print(prompt, end='')
return f.readline().strip()
#get data
def get_data():
pass
line = input().strip().split(',')
nums=[]
for n in line:
nums.append(float(n))
return nums
#get sum
def my_sum(data):
s=0
for n in data:
s += n
return s
pass
#get max
def my_max(data):
m=0
for n in data:
m >= n
return m
pass
#get min
def my_min(data):
i=0
for n in data:
i < n
return i
pass
#get range
def my_range(data):
r=0
for n in data:
m - i
return r
pass
#get mean
def mean(data):
e=0
for n in data:
#????????
e+=n
return e/len(data)
#get median
def median(data):
data.sort()
return data[len(data)/2]
#get mode
def mode(data):
n = len(data)
data = Counter(data)
get_mode = dict(data)
mode = [k for k, v in get_mode.items() if v == max(list(data.values()))]
if len(mode) == n:
get_mode = "No mode found"
else:
get_mode = "Mode is / are: " + ', '.join(map(str, mode))
print(get_mode)
#check_prime
def check_prime(num):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
return False
else:
return True
else:
return True
#get prime
def prime(data):
for n in data:
if(check_prime(n)==True) print(n)
#get factorization
def factorization(data):
for i in range(1:data+1):
if(data%i==0) print(i)
#get fib
def fib(data):
if(data == 0):
return 0
elif(data == 1):
return 1
else:
return (Fibonacci_series(data - 2) + Fibonacci_series(data - 1))
#run it
def run_cli():
while(1):
cmd = input()
if cmd=='exit':
break
elif cmd=='get_data':
data = get_data()
print(data)
eliif cmd=='my_sum':
s = my_sum(data)
prnt(s)
elif cmd=='my_max':
m = my_max(data)
print(m)
elif cmd=='my_min':
i = my_min(data)
print(i)
elif cmd=='my_range':
r = my_range(data)
print(r)
elif cmd=='mean':
e = mean(data)
print(e)
elif cmd=='median':
d = median(data)
print(d)
elif cmd=='mode':
o = mode(data)
print(o)
elif cmd=='prime':
p = prime(data)
print(p)
elif cmd=='factorization':
f = factorization(data)
print(f)
elif cmd=='fib':
b = fib(data)
print(b)
pass
#ha ahsbas
def main():
run_cli()
main()