-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ex_Sheet_2_Num_3.py
66 lines (49 loc) · 1.53 KB
/
Ex_Sheet_2_Num_3.py
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
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 23-Mar-2020
File name: Ex_Sheet_2_Num_3.py
Description: This script generate three different arrays.
"""
import numpy as np
def initArray(value, length):
"""
Function initArray(value, length) initializes a NumPy array of len 'length'
filled with the same value 'value'
Parameters:
value (float): scalar value to fill the array.
length (int): length of the array.
Returns:
None
"""
if value == 1:
return np.ones(length)
else:
return np.zeros(length) + value
# alternative version: this version is equivalent, but an addition is in
# general more efficient than a multiplication, that's why I would slightly
# prefer the choice above.
#
# if value == 0:
# return np.zeros(length)
# else:
# return np.ones(length)*value
def main():
"""
Function main() defines a three Numy arrays, the first made of 10 zeros,
the second made of 10 ones and the last one made of 10 fives.
Parameters:
None
Returns:
None
"""
# arrays
tenZeros = initArray(0, 10)
tenOnes = initArray(1, 10)
tenFives = initArray(5, 10)
# Print section
print("Array of 10 zeros = {}".format(tenZeros))
print("Array of 10 ones = {}".format(tenOnes))
print("Array of 10 fives = {}".format(tenFives))
if __name__ == "__main__":
main()