Comp sci student here,
Very lost on how to add those X's on a multiplication table like the added photo. https://i.stack.imgur.com/cdHoZ.png
How on earth would I add those X's while also using functions? Here's my code if this helps:
for i in range(1,11):
for j in range(1,11):
print(i * j, end='\t')
print('')
The rule for the X is i>3 and j>2 and i*j != 81
for i in range(1, 10):
for j in range(1, 10):
if i > 3 and j > 2 and i * j != 81:
print('X', end='\t')
else:
print(i * j, end='\t')
print()
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 X X X X X X X
5 10 X X X X X X X
6 12 X X X X X X X
7 14 X X X X X X X
8 16 X X X X X X X
9 18 X X X X X X 81
Related
Hey guys I hope you're doing well.
The problem I have is that my loop is not well defined, therefore the condition that I give it is not met. The print of the DataFrame that I implemented inside the while loop is not performed when the condition is not met.
This is the code I have so far. By implementing the while loop it stopped returning me the modified dataframe. As I said before, the loop is poorly constructed.
Dataframe content:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 X X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
Code:
import pandas as pd
def modifyDF():
T = pd.read_fwf('file', header= None, names=['1','2','3','4','5','6','7','8','9'])
T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})
df = pd.DataFrame(T)
print(T,'\n')
x= input('row: ')
y= input('column: ')
v= input('value: ')
while 'X' in df:
f = df.loc[x,y]= v
print(f)
while 'X' not in df:
break
modifyDF()
Expected OUTPUT:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 X X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
row: D #For example
column: 2 #For example
value: 1 #For example
#The modified dataframe:
1 2 3 4 5 6 7 8 9
A 5 3 X X 7 X X X X
B 6 X X 1 9 5 X X X
C X 9 8 X X X X 6 X
D 8 1 X X 6 X X X 3
E 4 X X 8 X 3 X X 1
F 7 X X X 2 X X X 6
G X 6 X X X X 2 8 X
H X X X 4 1 9 X X 5
I X X X X 8 X X 7 9
#The goal would be for this to run like a loop until there are no 'X' left in the dataframe.
I really appreciate your help :)
Generally speaking, you'd better not loop through a pandas DataFrame, but use more pythonic methods. In this case, you need to move your while loop a bit higher in your code, before the input statements, so your function would become:
def modifyDF():
T = pd.read_fwf('file', header=None, names=['1','2','3','4','5','6','7','8','9'])
T = T.rename(index={0:'A',1:'B',2:'C',3:'D',4:'E',5:'F',6:'G',7:'H',8:'I'})
df = pd.DataFrame(T)
print(T,'\n')
while df.isin(['X']).any().any():
x = input('row: ')
y = input('column: ')
v = input('value: ')
df.loc[x,y] = v
f = v
print(f)
Also remember that f = df.loc[x,y]= v is wrong in Python.
The program must accept an integer matrix of size R*C and four integers X, Y, P, Q as the input. The program must divide the matrix into nine submatrices based on the following condition. The program must divide the matrix horizontally after the Xth row and Yth row. Then the program must divide the matrix vertically after the Pth column and Qth column. Finally, the program must print the sum of integers in each submatrix as the output.
Input:
6 5
6 9 2 9 2
7 1 9 3 2
9 9 1 2 6
6 5 7 1 9
6 6 6 2 3
1 6 7 9 7
3 5 2 4
Output:
41 26 10 23 16 12 7 16 7
Explanation:
Here X = 3, Y=5, P = 2 and Q = 4
The nine submatrices and their sums are given below.
1st submatrix sum= 6+9+7+1+9+9 =41
6 9
7 1
9 9
2nd submatrix sum= 2+9+9+3+1+2 =26
2 9
9 3
1 2
3rd submatrix sum= 2+2+6 = 10
2
2
6
4th submatrix sum= 6+5+6+6 = 23
6 5
6 6
5th submatrix sum = 7+1+6+2 = 16
7 1
6 2
6th submatrix sum = 9 + 3 = 12
9
3
7th submatrix sum = 1 + 6 = 7
1 6
8th submatrix sum = 7 + 9 = 16
7 9
9th submatrix sum = 7
7
My program:
r,c=map(int,input().split())
m=[list(map(int,input().split())) for i in range(r)]
x,y,p,q=list(map(int,input().split()))
for i in range(x):
for j in range(p):
print(m[i][j])
print()
How to iterate from the given row and column and find the submatrix and print the sum?
Here are five solutions...
After reading all input like you did, you could go through the three boundary pairs for columns and the three boundary pairs for columns:
print(*(sum(sum(row[j:J]) for row in m[i:I])
for i, I in [(0, x), (x, y), (y, r)]
for j, J in [(0, p), (p, q), (q, c)]))
Same idea, slicing earlier / less often:
print(*(sum(sum(row[j:J]) for row in rows)
for rows in [m[:x], m[x:y], m[y:]]
for j, J in [(0, p), (p, q), (q, c)]))
Or without slicing:
print(*(sum(m[i][j]
for i in range(*irange)
for j in range(*jrange))
for irange in [(0, x), (x, y), (y, r)]
for jrange in [(0, p), (p, q), (q, c)]))
Or go through the matrix and update the right one of the nine sums:
sums = [0] * 9
for i in range(r):
for j in range(c):
sums[((i>=x)+(i>=y)) * 3 + (j>=p)+(j>=q)] += m[i][j]
print(*sums)
Again a variation:
sums = [0] * 9
for i in range(r):
for j in range(c):
sums[(0 if i<x else 3 if i<y else 6) +
(0 if j<p else 1 if j<q else 2)] += m[i][j]
print(*sums)
Try it online!
check the pattern of image, I want to make it.
I just can do bellow and not more:
n = 5
d = n
for x in range(1, n + 1):
for y in range(1, n * 2 + 1):
if (y >= d) != 0 and y <= n + x - 1:
print(x * y, end=" ")
elif y <= n:
print(" ", end=" ")
else:
pass
d -= 1
print()
this is my output:
5
8 10 12
9 12 15 18 21
8 12 16 20 24 28 32
5 10 15 20 25 30 35 40 45
what should i do?
how should complete this?
One approach is to continue your work with a second for loop. Also, note that there is no need for the else: pass clause.
n = 5
d = n
for x in range(1, n + 1):
for y in range(1, n * 2 + 1):
if (y >= d) != 0 and y <= n + x - 1:
print(x * y, end=" ")
elif y <= n:
print(" ", end=" ")
d -= 1
print()
d+=2
for x in range(n+1, 2*n):
for y in range(1, n * 2 + 1):
if (y >= d) != 0 and y <= 3*n - x-1:
print(x * y, end=" ")
elif y <= n:
print(" ", end=" ")
d += 1
print()
Which results in this output:
5
8 10 12
9 12 15 18 21
8 12 16 20 24 28 32
5 10 15 20 25 30 35 40 45
12 18 24 30 36 42 48
21 28 35 42 49
32 40 48
45
On the other hand, you might appreciate this script, which takes advantage of the str.format method.
n = 5
d = n
form = '{0:<4}'
rows = [[' '*4 for _ in range(2*n)] for _ in range(2*n)]
for x in range(1,n):
for y in range(d,n+x):
rows[x][y] = form.format(x*y)
d-=1
for x in range(n, 2*n):
for y in range(d, 3*n - x):
rows[x][y] = form.format(x*y)
d+=1
for row in rows:
print(''.join(row))
Here's the resulting output:
5
8 10 12
9 12 15 18 21
8 12 16 20 24 28 32
5 10 15 20 25 30 35 40 45
12 18 24 30 36 42 48
21 28 35 42 49
32 40 48
45
You might find it interesting to see how the outputs change as n is varied.
We could also get the same result as the above without the d parameter:
n = 5
form = '{0:<4}'
rows = [[' '*4 for _ in range(2*n+1)] for _ in range(2*n+1)]
for x in range(1,n):
for y in range(n-x+1,n+x):
rows[x][y] = form.format(x*y)
for x in range(n, 2*n):
for y in range(x-n+1, 3*n - x):
rows[x][y] = form.format(x*y)
for row in rows:
print(''.join(row))
I have the following data frames:
A.
k m n
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
5 x x x
6 x x x
7 x x x
8 x x x
9 x x x
B1.
l i j
1 x 46 x
2 x 64 x
3 x 83 x
9 x 70 x
B2.
l i j
0 x 23 x
4 x 34 x
6 x 54 x
8 x 32 x
B3.
l i j
0 x 11 x
5 x 98 x
7 x 94 x
9 x 80 x
How can I add the column "i" (from data frames B1, B2, and B3) to the data frame A?
Regarding the duplicate values (e.g. index 9 in B1 and B3 & index 0 in B2 and B3), I want to keep the leftmost value from [B1, B2, B3] (e.g. 23 for index 0 & 70 for index 9).
A desired output would be:
k m n i
0 x x x 23
1 x x x 46
2 x x x 64
3 x x x 83
4 x x x 34
5 x x x 98
6 x x x 54
7 x x x 94
8 x x x 32
9 x x x 70
you can concat the Bx dataframes, and use duplicated on the index to remove the duplicated index and keep the first.
A['i'] = (pd.concat([B1, B2, B3])
.loc[lambda x: ~x.index.duplicated(keep='first'), 'i'])
print(A)
k m n i
0 x x x 23
1 x x x 46
2 x x x 64
3 x x x 83
4 x x x 34
5 x x x 98
6 x x x 54
7 x x x 94
8 x x x 32
9 x x x 70
I have a dataframe that looks like this:
bucket type v
0 0 X 14
1 1 X 10
2 1 Y 11
3 1 X 15
4 2 X 16
5 2 Y 9
6 2 Y 10
7 3 Y 20
8 3 X 18
9 3 Y 15
10 3 X 14
The desired output looks like this:
bucket type v v_paired
0 1 X 14 nan (no Y coming before it)
1 1 X 10 nan (no Y coming before it)
2 1 Y 11 14 (highest X in bucket 1 before this row)
3 1 X 15 11 (lowest Y in bucket 1 before this row)
4 2 X 16 nan (no Y coming before it in the same bucket)
5 2 Y 9 16 (highest X in same bucket coming before)
6 2 Y 10 16 (highest X in same bucket coming before)
7 3 Y 20 nan (no X coming before it in the same bucket)
8 3 X 18 20 (single Y coming before it in same bucket)
9 3 Y 15 18 (single Y coming before it in same bucket)
10 3 X 14 15 (smallest Y coming before it in same bucket)
The goal is to construct the v_paired column, and the rules are as follows:
Look for rows in the same bucket, coming before this one, that have opposite type(X vs Y), call these 'pair candidates'
If the current row is X, choose the min. v out of the pair candidates to become v_paired for the current row, if the current row is Y, choose the max. v out of the pair candidates to be the v_paired for the current row
Thanks in advance.
I believe this should be done in a sequential manner...
first group by bucket
groups = df.groupby('bucket', group_keys=False)
this function will be applied to each bucket group
def func(group):
y_value = None
x_value = None
result = []
for _, (_, value_type, value) in group.iterrows():
if value_type == 'X':
x_value = max(filter(None,(x_value, value)))
result.append(y_value)
elif value_type == 'Y':
y_value = min(filter(None,(y_value, value)))
result.append(x_value)
return pd.DataFrame(result)
df['v_paired'] = groups.apply(func)
hopefuly this will do the job