Here I have this code:
N = int(input())
Tmp=n
While tmp>0 :
Print(n)
Tmp-=1
But for ex: when I have:
3
2
1
0
As entered nums, it just prints:
3
3
3
But I need to print:
3
3
3
2
2
1
Here I have this code:
N = int(input())
Tmp=n
While tmp>0 :
Print(n)
Tmp-=1
But there is a problem!bc it just prints
3
3
3
Instead of:
3
3
3
2
2
1
you need to print(tmp) instead of print(n)
this will print 3 2 1.
To get 3 3 3 2 2 1 you need to change your code more:
n = int(input())
tmp=n
while tmp > 0:
for _ in range(tmp)
print(tmp)
tmp -= 1
You need to add if statement when tmp equals 0 then subtract n by 1 and assign tmp back to n:
n = int(input('input : '))
tmp = n
while tmp > 0:
print(n)
tmp -= 1
if tmp == 0:
n -= 1
tmp = n
Output:
# input : 3
# 3
# 3
# 3
# 2
# 2
# 1
My code :
R = int(input("Enter the Size of Square Matrix : "))
matrix = []
print("\nEnter the entries row-wise : ")
for i in range(R):
a = []
for j in range(R):
a.append(int(input()))
matrix.append(a)
print("\nMatrix : \n")
for i in range(R):
for j in range(R):
print(matrix[i][j], end=" ")
print()
print("\n")
print("\nBoundary Matrix\n")
for i in range(R):
for j in range(R):
if (i == 0):
print(matrix[i][j])
elif (i == R - 1):
print(matrix[i][j])
elif (j == 0):
print(matrix[i][j])
elif (j == R - 1):
print(matrix[i][j])
else:
print(" "),
print()
output :
Boundary
Matrix
1
2
3
4
6
7
8
9
I'm not able to print the boundary elements in form of a matrix. the output is in form of a straight line.
Please help me to do so.
Note: I have tried changing the position of print() but that didn't help much.
print("\nBoundary Matrix\n")
for i in range(R):
print(
"{}{}{}".format(
"\t".join(map(str, matrix[i])) if i in (0, R - 1) else matrix[i][0],
"" if i in (0, R - 1) else (" \t" * (R - 1)),
"" if i in (0, R - 1) else matrix[i][R - 1],
)
)
Matrix :
1 2 3 4
5 6 7 8
9 10 11 12
13 1 4 15
Boundary Matrix
1 2 3 4
5 8
9 12
13 1 4 15
I have a programm which draws a picture from numbers in certain way
n = int(input(" Введіть ваше число "))
m = n * 2 - 1
pp = " "
i = 0
while m != 0:
l = []
while m > n:
while i < n:
i += 1
j = n - i
k = i
while j != 0:
l.append(pp)
j -= 1
while k != 0:
l.append(str(k))
k -= 1
m -= 1
a = " "
print(a.join(l))
l = []
i = 0
OUTPUT:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
But now I get a task to draw this picture
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Is there any hint how to reflect it without overwriting the whole code?
For the output you are expecting, a very simple way to do it is with this code :
n = 5
for i in range(n):
print(' '.join([str(x+1) for x in range(i+1)]))
Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
You can try this. Its simple.
for i in range(1,n+1):
for j in range(1, i+1):
print(j, end="")
print()
Please help me print the pattern below as it is, if the input entered is 7:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
I figured out to find the middle element of the pattern with any input:
rows=int(input("Enter the number of rows:"))
l=[]
for x in range(1,rows+1):
if x%2!=0:
l.append(x)
mid_value=len(l)
Please help me complete the above pattern......
Thanks in advance!
If you use a list-of-lists to store the values, the value for any specific cell can be determined by doing some basic math involving the cell indexes and the number of rows.
An illustration:
def cell_value(i, j, n_rows):
# The value of any cell is the minimum distance
# from its own coordinates (i, j) to the "outside" (ie,
# an i or j less than 0 or equal to n_rows). Imagine an
# ant in the grid. How many steps would it have to take
# to escape the grid, using the shortest route?
return min(
abs(i - -1),
abs(i - n_rows),
abs(j - -1),
abs(j - n_rows),
)
N_ROWS = 7
rows = [
[
cell_value(i, j, N_ROWS)
for j in range(N_ROWS)
]
for i in range(N_ROWS)
]
for r in rows:
print(*r)
Output:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
This looks like a homework question, so I'm going to try and explain how to approach it rather than just provide code.
A few things worth noting to start:
- The pattern's symmetrical in both directions, so we can save some effort and logic by only solving the top-left quarter, and copying it to the rest.
- Each row is similar the the one before, with one added at the point where the row and column indices (i and j) are equal - rather than recalculate every row from scratch, we can take the one before as a base.
So, for the first row, make a list of 1s the length of your input (7, in this case).
Copy this for the seventh row (note: row6 = row0 won't create a copy; you'll need row6 = list(row0) )
For the second and sixth rows, take a copy of the first row. If i is equal to or greater than j and is in the first half of the row, add 1 to it. You'll need to copy that in reverse for the back half of the row. (Alternative - set the value to j+1 rather than just adding 1)
Repeat until the fourth row, and you should be done.
EDIT: code included, because it was an interesting problem
numberOfRows = int(input("Enter the number of rows:"))
listOut = [[1]*numberOfRows] * numberOfRows #grid of 1s of appropriate size
for j in range(int((numberOfRows+1)/2)): #symmetrical, so only look to the middle
if j > 0:
listOut[j] = list(listOut[j-1]) #copy previous row
for i in range(int((numberOfRows+1)/2)):
if i>=j:
listOut[j][i] = j+1
listOut[j][numberOfRows-(i+1)] = j+1
#copy current row to appropriate distance from the end
listOut[numberOfRows-(j+1)] = list(listOut[j])
for row in listOut:
# * for sequence unpacking, printing lists as strings w/o commas
print(*row)
It might not be the most elegant solution but something like this should work:
n = int(input('Enter the number of rows:'))
table = [[1 for _ in range(n)] for _ in range(n)]
start = 0
end = n
while start < end:
start += 1
end -= 1
for i in range(start, end):
for j in range(start, end):
table[i][j] += 1
for row in table:
print(' '.join(str(ele) for ele in row))
Simple implementation without using any list
n = int(input())
x = n
for i in range((n // 2 + 1) if n % 2 and n > 1 else n //2):
for l in range(1, i + 1):
print(l, end=' ')
print((str(i + 1) + ' ') * x, end='')
for r in range(i, 0, -1):
print(r, end=' ')
print()
x -= 2
y = 1
for j in range(n // 2, 0, -1):
for l in range(1, j):
print(l, end=' ')
print((str(j) + ' ') * (2 * y + 1 if n % 2 else 2 * y), end='')
for r in range(j-1, 0, -1):
print(r, end=' ')
print()
y += 1
I need to delete all first occurrence in list.
time limit = 2 s.
memory limit = 256 mb.
Given list a. Some elements in a repeated. If len(a) = 1 print 0.
Input: 1 1 5 2 4 3 3 4 2 5.
Output: 1 3 4 2 5
My solution: For 48 test 22 - good. Else - limited time. How it solve without .count
n = int(input())
A = list(map(int, input().split()))
C = []
if len(A) == 1:
print(0)
else:
for j in range(n):
if A[:j].count(A[j]):
C.append(A[j])
print(len(C))
print(*C)
Pls help
Another way than using sets
First
n = input().split(" ")
u = []
if(len(n) == 1): print(0, end="")
else:
d = {}
for a in n:
if a in d: u.append(int(a))
else: d[a] = 1
print(*u, end="")
# input: 1 1 5 2 4 3 3 4 2 5
# output: 1 3 4 2 5
# input: 5
# output: 0
# input: 1 1 1 1
# output: 1 1 1
Last
n = input().split(" ")
u = []
if(len(n) == 1): print(0, end="")
else:
n.reverse()
d = {}
for a in n:
if a in d: u.append(int(a))
else: d[a] = 1
u.reverse()
print(*u, end="")
# # input: 1 1 5 2 4 3 3 4 2 5
# # output: 1 5 2 4 3
# # input: 5
# # output: 0
# # input: 1 1 1 1
# # output: 1 1 1
To remove duplicates use set()
_ = input()
A = set(map(int, input().split()))
print(*list(A))