I want to write a program that can help me to calculate some values using python. The main idea is I want to make a table, which is only half of the cells are having value. (That means I want to make a triangle) So, the triangle I want to make is like this format.
12345
1234
123
12
1
But, things are not that easy. Please see the table below. (Also same as picture 1)
6.00 6.45 6.80 7.10 7.36 7.56 7.77
6.90 7.20 7.47 7.70 7.88 8.06
7.50 7.75 7.97 8.12 8.30
8.00 8.20 8.33 8.50
8.40 8.50 8.67
8.60 8.80
9.00
In the above triangle, each of the rows is make from the previous row by a certain formula. Please refer to the picture 2 and picture 3.
Also, please note that in the above triangle, each of the cells was multiplied by 100. For example, the 6.00 in the upper left corner is 6% (0.06) indeed. << Please do NOT multiplied the figures by 100 in my program, because it is just an accident here for demonstration.
Okay. So, I want to make a program that as long as I input the first row, then the program will calculate and show the remaining rows for me. (That means showing the whole triangle)
Below is my code. Please have a look. I have tried to write a frame, but there are two problems that I encounter.
- I don't know how to automatically fill my other arrays(the other rows) base on the first arrays (the first row) that I input in the program.
- I keep receiving the error of "IndexError: list index out of range" when I try to run my programme.
Here are my codes
array1 = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777] #This is the initial array that I input
array2 = [] #This should be the output after the first iteration, I hope that my programme can auto fill the remaining arrays (including this array) after I run my programme
array3 = [] #This is a blank array waiting for auto fill after I run my program
array4 = [] #This is a blank array waiting for auto fill after I run my program
array5 = [] #This is a blank array waiting for auto fill after I run my program
array6 = [] #This is a blank array waiting for auto fill after I run my program
array7 = [] #This is a blank array waiting for auto fill after I run my program
array8 = [] #This is a blank array waiting for auto fill after I run my program
r = 0 # Set the initial position for the table that I want to make, and this is similar to the x-asis. Please refer to picture 2.
s = 1 # Set the initial position for the table that I want to make, and this is similar to the y-asis. Please refer to picture 2.
def SpotRateForcast(i,j,k):
global r
global s
while (r <= len(k)): # I try to make the first loop here
s = 1 # Reset the value when the first row is completed such that the second loop can work in the second row
while (s <= len(k)): # this is the second loop to determine when to stop in the each row
x = ((((1 k[j - 1])**j) / ((1 k[i - 1])**i))**(1/(j-i))) - 1 #Calculate the value by the certain formula
print(("%.4f" % round(x,4)), end=' , ') #Print the value up to 4 decimal places
s = 1 #Record one value is calculated and done
SpotRateForcast(i,j 1,k) #Start to calculate and print the next value
r = 1 # Switch to the next row when the second loop is finished
SpotRateForcast(r,s,array1) #The code to run the program
Thank you for reading my question. I would like to ask someone to finish the programme for me. As you can see, my coding maybe bad because I am a newbie in programming. But I already tried my best to do everything I am able to do.
I do not mind your edit my codes. It is even better if you can write a new codes for me. And I can learn how to write better codes from you.
Lastly, I have one more request. Please add many many comments in the codes you wrote otherwise I may not able to understand what you wrote(I am a beginner). Thank you very much!
CodePudding user response:
You are overthinking it.
If you only need to print a triangle, just print each row in sequence, and for each row print its values in sequence:
S = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]
n = len(S)
print(('{:0.4f} ' * n).format(*S)) # print initial line
for i in range(1,n): # loop over rows
for j in range(i 1, n 1): # then over colums
num = (1 S[j -1])**j
den = (1 S[i - 1])**i
x = (num/den)**(1/(j - i)) -1 # ok we have f(i, j)
print('{:0.4f}'.format(x), end=' ') # print it followed with a space
print('') # add a newline after a full row
It gives:
0.0600 0.0645 0.0680 0.0710 0.0736 0.0756 0.0777
0.0690 0.0720 0.0747 0.0770 0.0787 0.0807
0.0750 0.0775 0.0797 0.0812 0.0830
0.0801 0.0821 0.0833 0.0850
0.0841 0.0849 0.0867
0.0857 0.0880
0.0904
Now if you want to first build arrays because you will later post-process them, just append each field value to a list for the row, and then append each of those lists to a global list of lists
S = [0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]
n = len(S)
result = [S] # add the initial row
for i in range(1,n): # loop over rows
row = [] # prepare a new list for the row
result.append(row) # and append it to result
for j in range(i 1, n 1): # then over colums
num = (1 S[j -1])**j
den = (1 S[i - 1])**i
x = (num/den)**(1/(j - i)) -1 # ok we have f(i, j)
row.append(x) # as a list is modifiable we can append to it
CodePudding user response:
array=[[0.06,0.0645,0.068,0.071,0.0736,0.0756,0.0777]]
n = len(array[0])
#outer loop
for i in range(1, n):
#append new row
array.append([])
for j in range(n-i):
#i dont understand your calculations but they'd stand
array[i][j] = #here