The task is: introduced 2 numbers N(0-10000) - number of components, Q - number of requests. Then inputs N numbers of tests results. And then input Q numbers. Need to summarize an amount of all numbers in N which are less then Q
Sample Input:
10 3
2 6 8 4 5 1 9 0 3 7
3
8
0
Sample Output:
6
36
0
Made this task. If i m doing it local, with other random numbers it works. When i m trying to upload it on the stepik task I`ve got timelimit exceeded.
N, Q = input().split()
N = int(N)
Q = int(Q)
problems = input().split()
numMis = [0]*Q
MB = [0]*Q
if N > 0 or Q > 0:
for i in range(Q):
numMis[i] = int(input())
for i in range(Q):
for j in range(N):
if numMis[i] >= int(problems[j]):
MB[i] += int(problems[j])
for i in range(Q):
print(MB[i])
else:
print('0')
Related
Let's put int 5 as a sample input; in my code its expressed as this:
n = int(input())
for i in range(0, n):
n = n - 1
print(n**2)
I get an output as:
16
9
4
1
0
Instead I want to reverse the result to:
0
1
4
9
16
How do you go about solving this problem?
The following will reverse the output:
n = int(input())
for i in range(0,n):
print(i**2)
It will loop from 0 to your inputted n value, printing it's square at each iteration. This also negates the need for the n = n - 1 line.
Output:
0 ** 2
1 ** 2
.
.
.
n ** 2
Why the program count one more value? For example, I give him N = 50. It gives out:
1
4
9
16
25
36
49
64
Code:
N = int(input())
n = 1
k = 1
while n < N:
n = k ** 2
print(n)
k = k + 1
As explained, you're checking n then changing n, you want to change n then check before continuing.
You can use the walrus operator to assign n and check it's value all in the while statement. (requires Python 3.8+)
N = int(input())
n = 1
k = 1
while (n := k**2) < N:
print(n)
k += 1
This essentially assigns n to k**2 then checks if that result is <N before continuing.
1
4
9
16
25
36
49
Your program outputs 1 4 9 16 25 36 49 64 if your input is 50 because the `while`` loop is checking the value before you increase it. Once in the loop, you increase it, calculate the square and then print.
If you want it to terminate, try setting calculating n as the last step in the loop:
N = int(input())
n = 1
k = 1
while n < N:
print(n)
k = k + 1
n = k ** 2
You're checking whether you reached the limit before you calculate the square and print it. So you're checking the previous value of n, not the one that's about to be printed.
Move the check inside the loop.
while True:
n = k ** 2
if n >= N:
break
print(n)
k += 1
The n < N is evaluated after you've changed (and printed) n.
n = 1
k = 1
N=50
while 1:
n = k ** 2
if n > N:
break
print(n)
k = k + 1
To fix this, break before you print, moving the evaluation inside the loop rather than after the last update of n
1
4
9
16
25
36
49
With the condition of your code, for example, when n = 49, The condition is fulfilled because 49 < 50 therefore it will continue to process the value and print the new one. But once n = 64 which is > 50, it stops. This is a possible solution:
N = int(input())
n = 1
k = 1
while True:
if n >= N:
break
n = k ** 2
print(n)
k = k + 1
This will continuously execute the code but once the condition is met that n >= N, it will stop executing.
Thanks for the help in advance.
I compare 2 variables which hold numbers with the operation !=
the program takes input like this
4
1 2 3 4
Then the program should return the highest product exluding a square product. In this case the result should be 12(4x3) not 16(4x4) but it is incorrectly returning 16.
The code is here
def max_pairwise_product():
n = int(input())
a = input().split()
maxno = 0
for integer1 in a:
product = int(integer1) * n
if n != integer1:
if product > maxno:
maxno = product
return maxno
print(max_pairwise_product())
This is because your comparing an int to a str. I have edited your code minimally to fix this issue, but you have better to edit your list input before hand.
def max_pairwise_product():
n = int(input())
a = input().split() #If I were you I would do: a = [int(x) for x in input().split()]
maxno = 0
for integer1 in a:
product = int(integer1) * n
if n != int(integer1): #This is were I have changed
if product > maxno:
maxno = product
return maxno
print(max_pairwise_product())
You compare string vs. integer which is always False:
for integer1 in a: # integer1 is a string
product = int(integer1) * n
if n != integer1: # n is a number
Fix:
def max_pairwise_product():
_ = input() # not used
a = "1 2 3 4".split() # input() removed and fixed input given
a = sorted(set(map(int,a))) # convert all to intergers and sort unique numbers
maxno = 0
for idx,number in enumerate(a):
for number2 in a[idx+1:]:
maxno = max( maxno, number*number2 )
return maxno
print(max_pairwise_product())
Output:
12
The loops are optimized leveraging the fact that they numbers are sorted and unique - thats why the second loop can be shorter - no need to recompute already computed results.
Its good if you get inputs like '1 1 1 1 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6' because it will only compute for '1 2 3 4 5 6' and never compute any paring twice.
Can you help me in how to print this on the right way? I've tried many ways but none work'd
while True:
m = int(input())
mlen = m
sm = 1
aux = 1
matriz = []
if m == 0:
print()
break
for i in range(m):
linha = []
for j in range(m):
linha.append(sm)
matriz.append(linha)
while m - 2 > 0:
for i in range(aux, m - 1):
for j in range(aux, m - 1):
matriz[i][j] = sm + 1
sm += 1
aux += 1
m -= 1
for i in matriz:
for j in i:
print('{:4}'.format(j), end='')
print('')
I have to print the matrix as the example below. It's an URI Online Judge exercise (num 1435). The dots below are spaces and I can't have any after the last element of the matrix.
Accepted Output Your Output
1 ··1···1···1···1 1 ···1···1···1···1
2 ··1···2···2···1 2 ···1···2···2···1
3 ··1···2···2···1 3 ···1···2···2···1
4 ··1···1···1···1 4 ···1···1···1···1
6 ··1···1···1···1···1 6 ···1···1···1···1···1
7 ··1···2···2···2···1 7 ···1···2···2···2···1
8 ··1···2···3···2···1 8 ···1···2···3···2···1
9 ··1···2···2···2···1 9 ···1···2···2···2···1
10 ··1···1···1···1···1 10 ···1···1···1···1···1
Thanks in advance.
Try using str.rjust(width[, fillchar]), you can then use the fillchar to get the dots
https://docs.python.org/3.6/library/stdtypes.html?highlight=rjust#str.rjust
Memory limit: 256 MB
Time limit: 1 s
Hello.
We have the following code:
N, M = list(map(int, input().split()))
stones = list(map(int, input().split()))
for __ in range(M):
command, index, num = input().split()
index, num = int(index), int(num)
if command == "S":
print(sum(stones[index:num + 1]))
elif command == "M":
stones[index] = num
Where:
N is the length of list stones
M is number of commands
1 ≤ N, M ≤ 10**5
commands in form {type} {index} {index2 or value} of two types:
'S' to print sum of items in range [index; index2]
'M' to change value of item on index to new 0 ≤ value ≤ 10
This code exceeds the time limit. So, how to optimize it?
Sample Input:
9 10
1 1 2 3 5 0 4 9 4
S 2 4
S 8 8
S 0 8
S 4 5
M 5 9
S 0 8
S 4 5
M 0 7
S 1 8
S 0 5
Sample Output:
10
4
29
5
38
14
37
27