-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path21_WCS_Functions.py
More file actions
150 lines (112 loc) · 3.72 KB
/
21_WCS_Functions.py
File metadata and controls
150 lines (112 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""21_WCS_Functions.py
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1JCPoKjFr4pvPBlvwsS3ed147nXWOcIZB
## Mathematical functions that need coding

**Now the statisticians are begging you to code ready-made math functions for them. Below are the functions they use most often. Code the functions in the cells below:**
- Square of a number: Create a function that returns the square of a number.
- Cube of a number: Create a function that returns the cube of a number.
- Absolute value: Create a function that returns the absolute value of a number.
- Factorial of a number: Create a function that returns the factorial of a number.
- Mode of a list of numbers: Create a function that returns the mode of a list of numbers, for instance [68, 99, 65, 44, 77, 44, 44] --> 44.
- Average of a list of numbers: Create a function that returns the average of a list of numbers.
- Minimum of a list of numbers: Create a function that returns the minimum from within a list of numbers.
- Maximum of a list of numbers: Create a function that returns the maximum from within a list of numbers.
**Yes, all those functions already exist in dedicated modules, and you may not reuse them, unfortunately: you may only use loops, conditionals, basic structures, `len`, addition, subtraction, multiplication, division and powers.**
**Don't forget to test your functions with at least two possible cases!**
Optional Question: Create a function that returns the mode of a list of numbers when more than one modal value is present in a given data set. For example [68, 68, 68, 99, 65, 44, 77, 44, 44] --> [68, 44]
"""
## Question 1
def square(n):
return n*n
i = input()
if '.' in i:
i = float(i)
else:
i = int(i)
square(i)
## Question 2
def cube(n):
return n*n*n
i = input()
if '.' in i:
i = float(i)
else:
i = int(i)
cube(i)
## Question 3
def absolute(n):
if n >= 0:
result = n
elif n < 0 :
result = n*(-1)
return result
i = input()
if '.' in i:
i = float(i)
else:
i = int(i)
absolute(i)
## Question 4
def fac(n):
fact = 1
for i in range (1, n+1):
fact = i*fact
return fact
print(fac(int(input())))
## Question 5
def itirating(size):
list = []
a = 0
for i in range (size):
n = int(input("Enter a number to the list "))
list.append(n)
frequency = {}
for n in list:
frequency.setdefault(n, 0)
frequency[n]+=1
highestFrequency = max(frequency.values())
highestFreqlist = []
for n, freq in frequency.items():
if freq == highestFrequency:
highestFreqlist.append(n)
return (f'The mode of your list: {highestFreqlist}')
print(itirating(int(input("Enter the size of list :"))))
## Question 6
def average(size):
list = []
total = 0
for i in range (size):
n = int(input("Enter a number to the list "))
list.append(n)
total = n + total
result = total / size
print("Your list :", list)
return (f'The average of your list: {result}')
print(average(int(input("Enter the size of list :"))))
## Question 7
def minimum(size):
list = []
for i in range (size):
n = input("Enter a number to the list ")
if '.' in n:
n = float(n)
else:
n = int(n)
list.append(n)
return (f'The mimimum of your list: {min(list)}')
print(minimum(int(input("Enter the size of list :"))))
## Question 8
def maximum(size):
list = []
for i in range (size):
n = input("Enter a number to the list ")
if '.' in n:
n = float(n)
else:
n = int(n)
list.append(n)
return (f'The maximum of your list: {max(list)}')
print(maximum(int(input("Enter the size of list :"))))