-
Notifications
You must be signed in to change notification settings - Fork 5
/
Ex_Sheet_2_Num_6.py
61 lines (43 loc) · 1.41 KB
/
Ex_Sheet_2_Num_6.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
"""
Author: Gabriele Pompa
Identification Number: ABCXYZ [substitute with your Unisi Identification Number]
Date: 23-Mar-2020
File name: Ex_Sheet_2_Num_6.py
Description: This script generate an array and change sign to some values.
"""
import numpy as np
def changeSign(a, startInd, endInd):
"""
Function changeSign(a, startInd, endInd) change the sign of values of array
'a' between indexes 'startInd' and 'endInd'
Parameters:
a (numpy.ndarray): input array
startInd (int): first index to be changed in sign.
endInd (int): last index to be changed in sign.
Returns:
a (numpy.ndarray): input array changed, returned in output
"""
for i in np.arange(len(a)):
if (i >= startInd) & (i <= endInd):
a[i] = -a[i]
return a
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
"""
# test array
arr = np.arange(21)
firstIndToChange = 9
lastIndToChange = 15
# change sign
arrChanged = changeSign(arr, firstIndToChange, lastIndToChange)
# Print section
print("original array = {}".format(arr))
print("array with changed sign = {}".format(arrChanged))
if __name__ == "__main__":
main()