python3 - create list of strings elliptically - python-3.x

EDIT: I do not want your code! just help me think of a nice way to do it :)
I have a string with 25 characters:
ABCDEFGHIKLMNOPQRSTUVWXYZ
and I want to create a matrix5*5 given a position and a direction,
position can be one of the corners and direction can be clock or counter clock.
so if I gave this arguments:
create((0,0), clock)
I want to recive:
["ABCDE", "QRSTF", "PYZUG", "OXWVH", "NMLKI"]
and I could then print it and recieve:
A B C D E
Q R S T F
P Y Z U G
O X W V H
N M L K I

Related

pivot table help in excel to get a tree map

i need help in resolving a time consuming job
Input example
part number
where used
a
d
a
l
b
e
c
f
d
g
d
m
g
h
f
i
h
j
l
k
Result tree i need:
a
d
g
h
j
a
l
k
i am using pivot tabe to get this, but i cant arrive at that, kindly provide your guidance to make it

Diagonals with same characters - Python

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)

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

Sumproduct nested inside countifs

Am trying hard to crack this excel formula but need help. Basically I need count of all "p" & "f" on from different column ranges+rows (Status 1-4), if one the corresponding row value (Site) is "CH". Here is what the data looks like.
Status 1 Status 2 Status 3 Status 4 Site
P f n t CH
P f n t DL
P P P P BD
f f P P CR
f f f f CH
P P f f DL
P P P P BD
t t t t CR
t t t t CH
P f n t DL
P f P f BD
P f P f CR
P P P P CH
Any quick help will be much appreciated. I tried formula - =Countif(E2:E14,"CH",SUMPRODUCT(COUNTIF(A2:D14,{"p","f"}))), of course I knew that's wrong!
You need to combine several countifs that have OR conditions and sum them all up, for example like this:
=SUM(COUNTIFS($E$2:$E$14,"CH",A2:A14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",B2:B14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",C2:C14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",D2:D14,{"p","f"}))
You don't specify the expected result, but according to your written logic, it should be 10 and the formula returns just that.
Here is another approach. As much as I like to avoid INDIRECT, sometimes it is necessary.
=SUMPRODUCT(COUNTIFS(E2:E999, "CH", INDIRECT(ADDRESS(2, ROW(1:4), 4, 1)&":"&ADDRESS(999, ROW(1:4), 4, 1)), {"f","p"}))

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.

Resources