Related
So I am trying to insert the word array into the 20x20 matrix. I am not sure how to write a loop that inserts each character of the list into the grid. The final outcome of this code should be a crossword puzzle.
def crossword(L):
L1 = L.sort(key = len)
L1 = L.copy()
L1.reverse()
matrix = [[0 for i in range(20)] for j in range(20)]
word = [list(L1[i]) for i in range(len(L1))]
#for i in range(len(L1)):
# word = list(L1[i])
for i in range(len(L1[0])):
print(word[0][i])
word[0][i] = matrix[i][0]
for i in matrix:
print(i)
words = ["help","hi", "interest", "total", "lame"]
crossword(words)
My plan is creating a descending list of the words, highest to lowest amount of characters and insert each one in the grid, both horizontally and vertically.
The outcome of the above code is:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
This is just the empty 20x20 grid. Furthermore I don't want to use numpy, just plain vanilla python.
I want to implement a function that receives a list of words and returns a tensor
with the dimensions (#word,length of longest word,26)
the idea is to create a (length of longest word,26) tensor per each word where each row is filled with zeros and single one that represents the letter in that position. for example the word "abc"
will be represented by the following tensor:
tensor([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]], dtype=torch.int32)
each "row" (that represents a word) in the resulting tensor should be of the same size.
so I use padding of zero rows for each word.
for example, if we have the input list of word ["cd", "abc"]. the resulting tensor should be:
tensor([[[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]],
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]]] dtype=torch.int32)
we assume that word are consisted only of lower case letters.
import numpy as np
import torch
def hot_one(words):
max_l = max([len(i) for i in words]) #get length of longest word
result = torch.empty((1,max_l, 26)).int() #create the resulting tensor
for word in words:
ints = (np.fromstring(word,dtype=np.uint8)-ord('a')) #create an array of latters value
addition = np.zeros((max_l - ints.shape[0],)) -1 #padding for words that are shorter
tr = torch.Tensor(np.expand_dims(np.hstack((ints,addition)),-1)) #create a tensor with the right dims
tr = (tr[:] == torch.arange(26)).int() #this line converts to values of 1,0
#result = torch.cat((result, tr)) #!!doesn't work!!#
print(result)
the tricky part is that its not allowed to loop over the resulting tensor.
any ideas how this can be done?
edit: only allowed to use numpy and Torch functions
I have a 3d array x [k][n][m] with shape (10, 10, 100) and i need to verify if element m in range(0, 100) does not exist in other arrays x[k-1][n-1] then it can access the loop otherwise it needs to pass to m+1
This is what I did
def main(self):
self.xam = [[[0 for m in range(0, self.Vt)] for n in range(0, self.N)] for x in range(0, self.rn)]
for k in range(0, self.rn):
for n in range(0, self.N):
for m in range(0, self.Vt):
self.iteration = 0
if (self.xam[k-1][n-1][m] != 1):
while self.iteration < self.Iter_max:
if self.NU[k][n][m] == self.AU[k][n][m] == 1:
self.xam[k][n][m] = 1
print("station", k, "channel", n, "user", m, self.xam[k][n][m])
self.iteration += 1
the problem that i had is that it does not take the condition into account:
station 7 channel 9 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
station 8 channel 0 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I want to make a grid-like structure and willing to check that the geographical point lie in the cell or not?
I have Tuple of coordinates as a tuple shown below, I have other coordinates as a list of the list which also contains some coordinate, a different number of coordinate in each sublist. I want the check each coordinate of the sublist inside coordinates with the geographical coordinate in the Tuple. As a result, I want a list of size of Tuple for each sublist of coordinate with value 1 or 0(based on the given condition). If coordinates of the sublist of a coordinate lie in the range of coordinate of data1 write 1 else 0.
If I pick a geographical point (6.8, 51.35) from sublist of coordinate, I will check it each geographical coordinates of Tuple, like a=(x,y) modify it to (z=x+0.49, p=y+0.49), 0.49 because the cell gap is 0.05, then compare the value (6.8, 51.35) with a range of (a,b) & (z,p), replace the value (x,y) with 1 else 0.
Tuple = ((6.8, 51.35), (6.85, 51.4), (6.9, 51.45), (6.95, 51.5), (7.0, 51.55), (7.05, 51.6), (7.1, 51.65), (7.15, 51.7), (7.2, 51.75), (7.25, 51.8), (7.3, 51.85), (7.35, 51.9), (7.4, 51.95), (7.45, 52.0), (7.5, 52.05), (7.55, 52.1), (7.6, 52.15), (7.65, 52.2), (7.7, 52.25), (7.75, 52.3), (7.8, 52.35), (7.85, 52.4), (7.9, 52.45), (7.95, 52.5), (8.0, 52.55), (8.05, 52.6), (8.1, 52.65), (8.15, 52.7), (8.2, 52.75), (8.25, 52.8), (8.3, 52.85), (8.35, 52.9), (8.4, 52.95), (8.45, 53.0), (8.5, 53.05), (8.55, 53.1), (8.6, 53.15), (8.65, 53.2), (8.7, 53.25), (8.75, 53.3), (8.8, 53.35), (8.85, 53.4), (8.9, 53.45), (8.95, 53.5), (9.0, 53.55), (9.05, 53.6), (9.1, 53.65), (9.15, 53.7), (9.2, 53.75), (9.25, 53.8), (9.3, 53.85), (9.35, 53.9), (9.4, 53.95), (9.45, 54.0), (9.5, 54.05), (9.55, 54.1), (9.6, 54.15), (9.65, 54.2), (9.7, 54.25), (9.75, 54.3), (9.8, 54.35), (9.85, 54.4), (9.9, 54.45))
coordinate = [[('9.72', '52.33071'), ('9.72005', '52.3306')],[('9.7909166', '52.353843'), ('9.7907', '52.35399'), ('9.7904', '52.35417'), ('9.7897', '52.35454'), ('9.7891696', '52.354815')]]
The code I wrote is
for i in range(0,2):
list=[]
point=cordinate[i]
y=len(cordinate[i])
print(y)
for j in range(0,y):
l=float(point[j][0])
m=float(point[j][1])
#list=[]
for k in range(0,len(Tuple)):
x=Tuple[k][0]
y=Tuple[k][1]
gap = 0.049
if (x < l < x + gap) and (y < m < y + gap):
value = 1
else:
value = 0
list.append(value)
print(list)
Current Output:
2
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Expected output There should list of size 63, which contains 0 or 1:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
You have three problems. The first two are related: Vic pointed out that your "else" clause is executed whether or not the inner if is True. To simplify that check, upgrade your gap-checking code with
gap = 0.049
if (x < l < x + gap) and \
(y < m < y + gap):
value = 1
else:
value = 0
list.append(value)
This also repairs your second problem: in "found" cases, you would append value twice: once inside the if, and once after.
Also, note that you can reduce that check to one long item:
list.append(int((x < l < x + gap) and (y < m < y + gap)))
Finally, you have a problem-problem in that your data will not produce any 1 values by design. You have defined a diagonal series of cells. The first has diagonal corners of (6.8, 51.35) and (6.849, 51.399); the next picks up at the second corner and continues along the diagonal of your grid.
The points in coordinate do not fall into any of those cells. For instance, the first point is
(9.72, 52.33071)
The grid (Tuple) points with the containing x coordinates are
(9.7, 54.25), (9.75, 54.3)
But the required y coordinates are nearly two units (40 steps) above that point. Thus, there is no matching cell for that point. The other coordinate points have the same problem. You simply do not have any data point that fits the criteria, so your output list will be all zeroes.
It appears that either your data or your problem description is faulty.
Gautam I agree with the comments above about an MCVE submission. One place you may want to look at based on the code you posted is your 4-parameter if statement:
if l>x and l<z:
if m>y and m<p:
value=1
else : value = 0
You are probably getting the undesired output from this code because your else statement executes every iteration.
I have the following annual cash flows:
w=np.array([ -56501, -14918073, -1745198, -20887403, -9960686, -31076934,
0, 0, 11367846, 26736802, -2341940, 20853917,
22166416, 19214094, 23056582, -11227178, 18867100, 24947517,
28733869, 24707603, -17030396, 7753089, 27526723, 31534327,
26726270, -24607953, 11532035, 29444013, 24350595, 30140678,
-33262793, 5640172, 32846900, 38165710, 31655489, -74343373,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, -8727068])
I calculate IRR using np.irr
np.irr(w)
Out[141]: -0.05393588064654964
When I use IRR function in Excel for the same cash flows, I get 12%.
These two functions usually produce the same result. Does anyone know why in this case the results are so different? Thanks!
For the given cash flows, the IRR is not unique; see Multiple IRRs. Both the numpy and Excel values for r satisfy NPV(r) = 0, where NPV is the net present value.
Here's a plot of NPV(r) for the data in w. The red stars mark the IRR values (where NPV(r) is zero).
Here's the script that generates the plot:
import numpy as np
import matplotlib.pyplot as plt
w = np.array([ -56501, -14918073, -1745198, -20887403, -9960686, -31076934,
0, 0, 11367846, 26736802, -2341940, 20853917,
22166416, 19214094, 23056582, -11227178, 18867100, 24947517,
28733869, 24707603, -17030396, 7753089, 27526723, 31534327,
26726270, -24607953, 11532035, 29444013, 24350595, 30140678,
-33262793, 5640172, 32846900, 38165710, 31655489, -74343373,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, -8727068])
r_excel = 0.1200963665
r_numpy = np.irr(w)
rr = np.linspace(-0.055, 0.16, 500)
npvals = np.array([np.npv(r, w) for r in rr])
plt.plot(rr, npvals/1e6, alpha=0.8)
plt.plot(r_numpy, 0, 'r*')
plt.plot(r_excel, 0, 'r*')
plt.grid(True)
plt.xlabel('r')
plt.ylabel('NPV(r) [millions]')
plt.show()