Diagonals in North East Direction - Time Limit Exceeded in Python 3.8.3 - python-3.x

The program must accept an integer matrix of size RxC as the input. The program must print the integers in the diagonals in the North-East directions of the matrix in the seprate line as output.
Boundary:
2<=R,C<=100
Time Limit : 500ms
Example 1:
Input:
3 3
73 77 76
71 17 87
37 73 98
Output:
73
71 77
37 17 76
73 87
98
Example 2:
Input:
4 6
97 78 7 39 92 45
68 100 49 95 97 100
59 41 81 22 26 100
46 37 81 12 93 10
Output:
97
68 78
59 100 7
46 41 49 39
37 81 95 92
81 22 97 45
12 26 100
93 100
10
My Code:
row,col = map(int,input().split())
matrix = [list(map(int,input().split())) for i in range(row)]
# Redundancy of row and col
rep = []
for i in range(row):
for j in range(col):
b = []
for k in range(i,row):
if (j,k) not in rep:
b.append(matrix[k][j])
rep.append((j,k))
j-=1
if j<0:break
if len(b):print(*(b[::-1]))
My code works well but when the matrix is of size (100,100) it exceeds the given time limit, is there a way to reduce it. Thanks in advance
Note : No External Libraries should be used!

The trick here is to realize that because each number only appears in the solution once, so we really only need to evaluate each value once.
We can also see that each matrix will result in row + col - 1 number of North-East direction diagonals, which will help us.
# Original code
row,col = map(int,input().split())
# I won't turn them into ints, strings actually make it easier for my work
matrix = [input().split() for i in range(row)]
diagonals = [""] * (row + col - 1)
for i in range(row):
for j in range(col):
# determine which diagonal the number belongs to, and prepend it
diagonals[i + j] = "%s %s" % (matrix[i][j], diagonals[i + j])
# print out diagonals one at a time
for diagonal in diagonals: print(diagonal)
I never got the chance to run it, but this should give the general idea!
(new to SO, plz be nice :D)

Related

When using min() - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() [duplicate]

How can I reference the minimum value of two dataframes as part of a pandas dataframe equation? I tried using the python min() function which did not work. I'm sorry if this is well-documented somewhere but I have not been able to find a working solution for this problem. I am looking for something along the lines of this:
data['eff'] = pd.DataFrame([data['flow_h'], data['flow_c']]).min() *Cp* (data[' Thi'] - data[' Tci'])
I also tried to use pandas min() function, which is also not working.
min_flow = pd.DataFrame([data['flow_h'], data['flow_c']]).min()
InvalidIndexError: Reindexing only valid with uniquely valued Index objects
I was confused by this error. The data columns are just numbers and a name, I wasn't sure where the index comes into play.
import pandas as pd
import numpy as np
np.random.seed(365)
rows = 10
flow = {'flow_c': [np.random.randint(100) for _ in range(rows)],
'flow_d': [np.random.randint(100) for _ in range(rows)],
'flow_h': [np.random.randint(100) for _ in range(rows)]}
data = pd.DataFrame(flow)
# display(data)
flow_c flow_d flow_h
0 82 36 43
1 52 48 12
2 33 28 77
3 91 99 11
4 44 95 27
5 5 94 64
6 98 3 88
7 73 39 92
8 26 39 62
9 56 74 50
If you are trying to get the row-wise mininum of two or more columns, use pandas.DataFrame.min. Note that by default axis=0; specifying axis=1 is necessary.
data['min_c_h'] = data[['flow_h','flow_c']].min(axis=1)
# display(data)
flow_c flow_d flow_h min_c_h
0 82 36 43 43
1 52 48 12 12
2 33 28 77 33
3 91 99 11 11
4 44 95 27 27
5 5 94 64 5
6 98 3 88 88
7 73 39 92 73
8 26 39 62 26
9 56 74 50 50
If you like to get a single minimum value of multiple columns:
data[['flow_h','flow_c']].min().min()
the first "min()" calculates the minimum per column and returns a pandas series. The second "min" returns the minimum of the minimums per column.

Plot individuals home-range with Adehabitat

I am trying to put the name from the individuals of my research in a polygons home-range plot, but after many attempts I still can not achieve it.
Here and example of my data: X and Y are coordinates and id are individuals
X Y id
29 29 4
44 28 7
57 57 5
60 81 11
32 41 4
43 29 7
57 57 5
46 83 11
32 41 4
43 29 7
57 56 5
60 82 11
35 40 4
43 28 7
62 55 5
54 73 11
27 40 4
43 28 7
61 54 5
First, i calculated the home-range of my data with MPC
cp <- mcp((data)[,1],percent=95, unin = c("m"), unout = c("m2"))
And the plot it
plot(cp, axes=TRUE, border = rainbow(12))
But i donĀ“t kown which polygons correspond to each individual, and if possible i need to include the id of my individuals inside each polygon
Any help would be appreciated!!
Thanks
Juan
Here is an example using the example data from the adehabitatHR package, since you not really providing a reproducible example.
library(adehabitatHR)
data("puechabonsp")
cp <- mcp(puechabonsp$relocs[, 1], percent=95, unin = c("m"), unout = c("m2"))
One way would be to use ggplot2 and sf:
library(sf)
library(tidyverse)
st_as_sf(cp) %>% ggplot(., aes(fill = id)) + geom_sf(alpha = 0.5) +
scale_fill_discrete(name = "Animal id")

A vectorized solution producing a new column in DataFrame that depends on conditions of existing columns and also the new column itself

My current dataframe data is as follows:
df=pd.DataFrame([[1.4,3.5,4.6],[2.8,5.4,6.4],[7.8,6.5,5.8]],columns=['t','i','m'])
t i m
0 14 35 46
1 28 54 64
2 28 34 64
3 78 65 58
My goal is to apply a vectorized operations on a df with a conditions as follows (pseudo code):
New column of answer starts with value of 1.
For row in df.itertuples():
if (m > i) & (answer in row-1 is an odd number):
answer in row = answer in row-1 + m
elif (m > i):
answer in row = answer in row-1 - m
else:
answer in row = answer in row-1
The desired output is as follows:
t i m answer
0 14 35 46 1
1 28 54 59 60
2 78 12 58 2
3 78 91 48 2
Any elegant solution would be appreciated.

How many non diagonal moves from origin point?

87 76 67 58 49 40 31 22 13 4
77 68 59 50 41 32 23 14 5 -4
69 60 51 42 33 24 15 6 -3-12
61 52 43 34 25 16 7 -2-11-20
53 44 35 26 17 8 -1-10-19-28
45 36 27 18 9 0 -9-18-27-36
37 28 19 10 1 -8-17-26-35-44
29 20 11 2 -7-16-25-34-43-52
21 12 3 -6-15-24-33-42-51-60
I am using this grid of numbers to define levels in a pygame tile based dictionary of maps. Each number is a dictionary key defining a maps location in reference to each other map with the value being a 2d list. I need a function that returns the number of non diagonal moves away from 0 each number in the grid is. I am going to use this number to calculate difficulty level. So the farther the player gets from the origin point of 0 the more difficult the game gets. To better understand how these numbers are generated... A move North increments by 8, a move south increments by -8, a move east increments by -9, and a move west increments by 9. So for example maps 17,-1,1,and -17 would equal 2 while maps 8,9,-9,-8 would equal 1 and so on. I am making some headway learning to write decent code, but sometimes the math required is beyond my capabilities.
Okay so I figured out a much less complicated way to solve my problem. I used a list of 2 values. I incremented or decremented position 0 by 1 for north,south moves and incremented or decremented position 1 by 1 for west,east moves respectively. I then added the absolute value of position 0 and the absolute value of position 1 and returned the answer. This seems to be giving me an accurate distance from 0. YAY!
dif_list =[0,0]
if move == 'north':
dif_list[0] += 1
if move == 'south':
dif_list[0] -= 1
if move == 'west':
dif_list[1] += 1
if move == 'east':
dif_list[1] -= 1
def difficulty():
num = abs(dif_list[0])+abs(dif_list[1])
return num

Excel formula to auto-increment after X amount of rows

I imported a few thousand rows of data into Excel and whereas one item represented one row, I've had to modify each item so that 11 rows represent the same item id.
For example:-
Original
63 --->data
64 --->data
65 --->data
Current
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
63 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
64 --->data
(you get the idea)...
However, due to the formula I have used to populate the additional 10 rows per item, I am left with the same ID in Column A as all the rows the formula was based on.
I'm looking for a formula that will auto-increment the cell values based but only every 11 rows, so that I can click and drag down column A and it will fill the same id for 11 rows and then auto-increment (+1) and fill the next 11 rows like this.
I've tried a number of variants all to no avail. Thanks.
EDIT
Here is an example of what I currently have and wish to simplify:-
A B C D E F
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
79 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
80 <--already correct id
58 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
57 <-- needs to be changes to 81
58 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
57 <-- needs to be changes to 82
There are thousands of rows like this...
Here's another approach if you're interested:
Enter 1 into A1
Then enter this formula into A2:
=IF(MOD(ROWS($A$1:A1),11)=0,A1+1,A1)
Then just drag the formula from A2 down
You can also use this formula, it will also usefull for even and odd numbering
=INT(((ROW(a1)-1)/11))*1+1
use *1 for 1 increment, *2 for 2 increment,
+1 is starting number, if you want to start from 79 use +79 at the end
If you put one column containing a straight sequence from 1 to the number of lines you've got. (1, 2, 3, 4, 5, ...)
You can use that column to make a division by 11, taking only the integer part of the result.
Supposing the column with straight sequence is A:
= int(A1/11)
= int(A2/11)
See:
A B Result
0 =int(A1/11) 0
1 =int(A2/11) 0
2 =int(A3/11) 0
3 =int(A4/11) 0
4 =int(A5/11) 0
5 =int(A6/11) 0
6 =int(A7/11) 0
7 =int(A8/11) 0
8 =int(A9/11) 0
9 =int(A10/11) 0
10 =int(A11/11) 0
11 =int(A12/11) 1
12 =int(A13/11) 1
13 =int(A14/11) 1
14 =int(A15/11) 1
15 =int(A16/11) 1
16 =int(A17/11) 1
17 =int(A18/11) 1
18 =int(A19/11) 1
19 =int(A20/11) 1
20 =int(A21/11) 1
21 =int(A22/11) 1
22 =int(A23/11) 2
23 =int(A24/11) 2
.......keep on until the last line
If Im understanding the issue correctly there is no need for a complex formula.
try this in a column to test for your self to see if this is what you need.
Start in A1 and put the num 1 in each of 3 cells (a1,a2,a3)
in A4 put A4 = A1+1
then drag down. YOu will see the sequence you need...
1
1
1
2
2
2
3
3
3
if the sequence you need is indeed sequential then you can apply this as needed.

Resources