Diagonals with same characters - Python - python-3.x

The program must accept a character matrix of size RxC as the input. The program must print the number of diagonals that parallel to the top-left to bottom-right diagonal and having the same characters in the matrix.
def lower_diagonals(row,matrix):
# a list to store the lower diagonals
# which are || to top left to bottom right
d=[]
# Iterating from the second row till the last row
for i in range(1,row):
nop,dummy = [],0
for j in range(i,row):
try:
nop.append(matrix[j][dummy])
except:
break
dummy+=1
d.append(nop)
return d
def upper_diagonals(col,matrix):
# a list to store the lower diagonals
# which are || to top left to bottom right
d=[]
# Iterating from 1st column till the last column
for i in range(1,col):
dum , nop = i,[]
# Iterating till the last before row
for j in range(row-1):
try:
nop.append(matrix[j][dum])
except:
break
dum+=1
d.append(nop)
return d
def diagonals(matrix,row,col):
return lower_diagonals(row,matrix) + upper_diagonals(col,matrix)
row,col = map(int,input().input().split())
matrix =[input().strip().split(' ') for i in range(row)]
new_matrix = diagonals(matrix,row,col)
t=0
for i in new_matrix:
if len(list(set(i))) == 1 : t+=1
print(t)
Example :
Input :
4 4
u m o a
h n a o
y h r w
b n h e
Output:
4
Input :
5 7
G a # z U p 3
e G b # n U p
a e G m # e U
L l e g k # t
j L a e G s #
Output:
6
My code works perfect for all the above mentioned cases but it fails for the below case
Input :
2 100
b h D k 2 D 9 I e Q # * B 5 H Z r q u n P C 4 a e K l 2 E p 6 R V v 0 d 8 x C F P M F C e m K H O y # 0 I T r P 8 P N 9 Z 7 S J J P c L g x X f 5 1 o i Y V Y G Y 9 A E O 2 r 2 # S 8 z D 6 a q r i k r
V o 4 T M m z p 6 G H D Y a 6 t O 7 # w y t 2 m A 1 a + 0 p t P D z 7 V N T x + I t 4 x x y 1 Q G M t M 0 v d G e u 4 b 8 m D # I v D i T 1 u L f e 1 Y E Y q Y c A 8 P 2 q 2 A 8 y b u E 3 c 1 s M n X
Expected Output:
9
My Output:
100
Can anyone help me in structuring the logic for this case Thanks in advance
Note :
2<=R,C<=100
Time limit : 500ms

I think i probably found a logic for my problem
r,c = map(int,input().strip().split())
mat = []
for i in range(r):
l = list(map(str,input().strip().split()))
mat.append(l[::-1])
count = 0
for i in range(r+c-1):
l = []
for row in range(r):
for col in range(c):
if row+col == i:
l.append(mat[row][col])
l = list(set(l))
if len(l) == 1:
count+=1
print(count)

Related

How to add line in code in python console

How to add new line in python. for example, I would like to print the rest of a sentence in a new line. but instead of putting "\n", I will automate it to type to a new line for every six words.
Morse code translator
sth like:
def wrapper(words, n):
to_print = ''
for i in range(0, len(words.split()), n):
to_print += ' '.join(words.split()[i:i+n]) + '\n'
return to_print
and result is:
print(wrapper('a b c d e f g h i j k l m n o p r s t u w x y z', 6))
a b c d e f
g h i j k l
m n o p r s
t u w x y z

How to Pass More then 10 argument in ShellScript

sed -e "s/${MyToken}/${arg}/g" file
Value Of 'arg' working fine till 9th argument.after 10th arguments its failing
Here is an example :
#!/bin/ksh
echo There is $# parameters
while [ $# -ne 0 ]; do
echo $1
shift
done
The output :
Will:/home> ./script a b c d e f g h i j k l m n o p q r s t u v w x y z
There is 26 parameters
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
$#count the number of parameters passed to your script.
Shift is moving the param to the left so after one loop $1 become b and so on.

Split a single row of data (dat file) into multiple columns

I want to split a row of data into multiple columns like
a.dat
A B C D E F G H I J K L M N O P Q R S T U
into
b.dat
A B C D E F G
H I J K L M N
O P Q R S T U
I have tried using the pr function
pr -ts" " --columns 7 --across a.dat > b.dat
But it doesn't work, b.dat is the same as a.dat
I like fold for these thingies:
$ fold -w 14 file
A B C D E F G
H I J K L M N
O P Q R S T U
With -w you set the width you desire to have.
Although xargs is more useful if you want to split based on number of fields instead of characters:
$ xargs -n 7 < file
A B C D E F G
H I J K L M N
O P Q R S T U
Regarding your attempt in pr: I don't really know why it is not working, although from some examples I see it doesn't look like the tool for such job.

Filter pandas dataframe based on values in multiple columns

I have a dataframe which has 472 columns. Of those 99 columns are dxpoa1, dxpoa2,...,dxpoa99. I want to filter out row(s) of dataframe in which dxpoa columns' values are either 7 or N or BLANK only. dxpoa's can have many values like Y, W,E,1, 7, N etc. Or they remain BLANK. Only those rows in which dxpoa's have either only 7 or N should be filtered out from data frame.
Dataset is huge having many hundred thousands rows. Therefore an efficient method will be appreciated.
a b c dxpoa1 dxpoa2 dxpoa3 dxpoa4
0 0 A X W N X
1 Z W 2 7 7
2 7 W N W W 1 Z
3 1 7 E N N N N
4 Y 0 W N X 1
5 N X 1 E 1 Z 7
6 1 X 7 0 A W A
7 X X Z X N A 1
8 7 1 A N X Z N
9 N A Z N N N
10 A N Z 7 0 A E
11 E N A Z N N 1
12 E A 1 Z E E W
13 N W Z E X A 0
14 Y 1 A W A E X
I want row number 1, 3, 9 removed from dataframe.
I have tried many ways like:
df_col = [list of dxpoa column names]
df1 = df[df_col].isin(["Y", "W", "1", "E"]).values
It does not filter out.
UPDATE:
you can replace empty strings with NaN, 7 or N and then use isin:
In [196]: df[~df[cols].replace('',np.nan).isin(['7','N', np.nan]).all(axis=1)]
Out[196]:
a b c dxpoa1 dxpoa2 dxpoa3 dxpoa4
0 0 A X W N X
2 7 W N W W 1 Z
4 Y 0 W N X 1
5 N X 1 E 1 Z 7
6 1 X 7 0 A W A
7 X X Z X N A 1
8 7 1 A N X Z N
10 A N Z 7 0 A E
11 E N A Z N N 1
12 E A 1 Z E E W
13 N W Z E X A 0
14 Y 1 A W A E X
OLD answer:
show those containing 7 or N
In [197]: df.loc[df[cols].isin(['7','N']).any(axis=1)]
Out[197]:
a b c dxpoa1 dxpoa2 dxpoa3 dxpoa4
0 0 A X W N X
1 Z W 2 7 7
3 1 7 E N N N N
4 Y 0 W N X 1
5 N X 1 E 1 Z 7
7 X X Z X N A 1
8 7 1 A N X Z N
9 N A Z N N N
10 A N Z 7 0 A E
11 E N A Z N N 1
remove those containing 7 or N
In [198]: df.loc[~df[cols].isin(['7','N']).any(axis=1)]
Out[198]:
a b c dxpoa1 dxpoa2 dxpoa3 dxpoa4
2 7 W N W W 1 Z
6 1 X 7 0 A W A
12 E A 1 Z E E W
13 N W Z E X A 0
14 Y 1 A W A E X
replace any to all if you want to have/exclude those where all columns should contain either 7 or N
setup:
rows = 15
s = [''] + list('YWE17N0AZX')
df = pd.DataFrame(np.random.choice(s, size=(rows, 7)), columns=list('abc') + ['dxpoa1', 'dxpoa2', 'dxpoa3', 'dxpoa4'])
cols = df.filter(like='dxpoa').columns
You could use df.filter(regex=r'^dxpoa') to select columns whose name starts with 'dxpoa', and
use .isin(['7','N','']).all(axis=1) to create a boolean mask (for the rows) which is True when all the values in the row are either '7', 'N', or the empty string, '':
For example,
import pandas as pd
df = pd.DataFrame(
{'a': ['0','Z','7','1','Y','N','1','X','7','N','A','E','E','N','Y'],
'b': ['A','W','W','7','','X','X','X','1','A','N','N','A','W','1'],
'c': ['X','2','N','E','0','1','7','Z','A','Z','Z','A','1','Z','A'],
'dxpoa1': ['W','7','W','N','W','E','0','X','N','N','7','Z','Z','E','W'],
'dxpoa2': ['N','7','W','N','N','1','A','N','X','N','0','N','E','X','A'],
'dxpoa3': ['X','','1','N','X','Z','W','A','Z','N','A','N','E','A','E'],
'dxpoa4': ['','','Z','N','1','7','A','1','N','','E','1','W','0','X']})
mask = df.filter(regex=r'^dxpoa').isin(['7','N','']).all(axis=1)
print(df.loc[~mask])
yields
a b c dxpoa1 dxpoa2 dxpoa3 dxpoa4
0 0 A X W N X
2 7 W N W W 1 Z
4 Y 0 W N X 1
5 N X 1 E 1 Z 7
6 1 X 7 0 A W A
7 X X Z X N A 1
8 7 1 A N X Z N
10 A N Z 7 0 A E
11 E N A Z N N 1
12 E A 1 Z E E W
13 N W Z E X A 0
14 Y 1 A W A E X
Use apply. If applied function returns boolean it can be used to filter rows
like in example below. Note that I didn't try to reproduce your filtering logic.
def analyze_row(r):
# do whatever you want with column values here
# return boolean: True - row stays, False - row gone
ret = False
if r['dpxoa1'] == 'W':
ret = True
return ret
filtered_df = df.ix[df.apply(analyze_row, axis = 1), :]

Is it possible to use a rgY color space?

Would it be possible to take a rgY color space:
r = R / (R + G + B)
g = G / (R + G + B)
Y = R * 0.299 + G * 0.587 + B * 0.114
(unnecessary) b = 1 - r - g
and convert it back to RGB? If so, how would it be done?
(No factorials are in this answer. The syntax is similar to Wolfram-Alpha plain text output standards, exception being spaces are placed before and after the != [not equals] sign and the use of 'if [conditional] then' statements.)
if r = 0 and 473 g+114 != 0 and Y != 0 then
R = 0 and G = (1000 g Y)/(473 g+114) and B = -(1000 (g-1) Y)/(473 g+114)
if Y = 0 and g = 1/473 (-185 r-114) and r != 0 then
G = -((185 r+114) R)/(473 r) and B = ((587-288 r) R)/(473 r) and R!=0
if Y = 0 and r = 0 and g = -114/473 then
R = 0 and B = -(587 G)/114 and G!=0
if 473 g+185 r+114 != 0 and r != 0 and r Y != 0
R = (1000 r Y)/(473 g+185 r+114) and G = (1000 g Y)/(473 g+185 r+114) and B = -(1000 Y (g+r-1))/(473 g+185 r+114)

Resources