The first input of following code is a single value like a=3, and second input is an array consisting of pairs of values like: B = [[1000, 1], [1000, 3], [1, 2]]. If the first value of each pair is even then I want the corresponding output value to be based on some specific calculation as shown in the code; if the first value is odd, there is another calculation as shown in code. The second value in the pair is the number of times I'd like to repeat the calculation for the pair. Calculations in the example should be repeated 1, 3 and 2 times for pairs 1, 2 and 3, respectively. Also, if the answer of calculation is negative there is another calculation base on the code.
example for positive cases: calculation for pair 2 should be like this: first repetition: (1000-99)*3= 2703 since 2703 is odd then second repetition is (2703-15)*2=5376 and since 5736 is even third repetition is (5736-99)*3=15831.
example for negative cases: calculation for pair 3 should be like this: first repetition: since 1 is odd it should deducted from 15 ---> (1-15)= -14 and the answer should be deducted from 1000000 ---> (1000000-14)=99986 and then doubled ---> (999986x2)=1999972 and finally loop back by deducting from 1000000 ---> (1999972-1000000)=99972 and then for second repetition since 999972 is even so we subtract 99 (getting 999873) and triple it (getting 2999619 and then looping back to 999619).
The final output should be:
2703
15831
999619
I am not sure how to set if statement for negative part.
import numpy as np
a = np.array(input(), dtype=int)
B=[]
for i in range(a):
b = np.array(input().split(), dtype=int)
B.append(b)
B = np.array(B)
C=[]
for i in range(a):
for x in range(B[i][1]):
if B[i][0] % 2 == 0:
B[i][0]=(B[i][0]-99)
if B[i][0]>0:
(B[i][0])*3
elif B[i][0]<0:
B[i][0]=(1000000+B[i][0])*3
else:
B[i][0]=(B[i][0]-15)
if B[i][0]>0:
(B[i][0])*2
elif B[i][0]<0:
B[i][0]=(1000000+B[i][0])*2
C.append(B[i][0])
for i in range (len(B)):
print(B[i,0])
Aucun commentaire:
Enregistrer un commentaire