I am also active at:
Monday, May 25, 2020
Thursday, May 14, 2020
Empty list of a given size in Python
l = [0]*n
creates a list of size n, with each element as 0
source: https://stackoverflow.com/questions/5205575/how-do-i-get-an-empty-array-of-any-size-in-python
creates a list of size n, with each element as 0
source: https://stackoverflow.com/questions/5205575/how-do-i-get-an-empty-array-of-any-size-in-python
Wednesday, May 13, 2020
Print Bracket Number using stack
Given an expression exp of length n consisting of some brackets. The task is to print the bracket numbers when the expression is being parsed.
Input:
The first line contains an integer T, the number of test cases. For each test case, there is a string exp containing the expression.
The first line contains an integer T, the number of test cases. For each test case, there is a string exp containing the expression.
Output:
For each test case, the output is the bracket numbers of the expression.
Example:For each test case, the output is the bracket numbers of the expression.
Input:
3
(a+(b*c))+(d/e)
((())(()))
((((()
Output:
1 2 2 1 3 3
1 2 3 3 2 4 5 5 4 1
1 2 3 4 5 5
#code
t= int(input())
while(t>0):
#n=int(input())
a= str(input())
from queue import LifoQueue
stack = LifoQueue(maxsize = len(a))
j = 0
k = 0
l = ""
for i in range(len(a)):
if a[i] == '(':
j = j + 1
stack.put(j)
l = l + str(j)+ " "
if a[i] == ')':
k = stack.get()
l = l + str(k) + " "
print(l)
t -= 1
Monday, May 11, 2020
Stack in Python: Implementation using Queue module
from
queue
import
LifoQueue
# Initializing a stack
stack
=
LifoQueue(maxsize
=
3
)
source: https://www.geeksforgeeks.org/stack-in-python/
Sunday, May 10, 2020
Find the smallest sum of the two numbers formed using a list of numbers
Q. Find the smallest sum of the two numbers formed using a list of numbers, such that each number of the list is used to form the two numbers.
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer N denoting the size of the array. Next line of each test contains N space seperated integers denoting the elements of the array.
Solution:
The answer is the sum of the two smallest numbers that can be formed using a list of numbers.
To construct the smallest two numbers, first we sort the given list in ascending order.
Then we take alternate digits from the list and form the two numbers. Finally we add the two to get the answer.
# Solution in Python
t= int(input())
while(t>0):
n=int(input())
a=list(map(int,input().split()))
a.sort()
q1=[]
q2=[]
n1= 0
n2= 0
for i in range(len(a)):
if(i%2==0):
q1.append(a[i])
else:
q2.append(a[i])
for j in reversed(range(len(q1))):
n1 = n1 + q1.pop(0)*10**(j)
for k in reversed(range(len(q2))):
n2 = n2 + q2.pop(0)*10**(k)
print(n1+n2)
t -= 1
Queue in Python https://www.geeksforgeeks.org/queue-in-python/
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. First line of each test case contains an integer N denoting the size of the array. Next line of each test contains N space seperated integers denoting the elements of the array.
Solution:
The answer is the sum of the two smallest numbers that can be formed using a list of numbers.
To construct the smallest two numbers, first we sort the given list in ascending order.
Then we take alternate digits from the list and form the two numbers. Finally we add the two to get the answer.
# Solution in Python
t= int(input())
while(t>0):
n=int(input())
a=list(map(int,input().split()))
a.sort()
q1=[]
q2=[]
n1= 0
n2= 0
for i in range(len(a)):
if(i%2==0):
q1.append(a[i])
else:
q2.append(a[i])
for j in reversed(range(len(q1))):
n1 = n1 + q1.pop(0)*10**(j)
for k in reversed(range(len(q2))):
n2 = n2 + q2.pop(0)*10**(k)
print(n1+n2)
t -= 1
Queue in Python https://www.geeksforgeeks.org/queue-in-python/
Subscribe to:
Posts (Atom)
इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर
इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...
-
Acronym Full Form AJAX Asynchronous JavaScript and XML API Application Programming Interface APK Android Application Package ASP Activ...
-
#include<stdio.h> int main() { int M,N,q; scanf("%i %i",&M, &N); if((M%N)!=0) {printf("0...
-
"A good company understands that problem solving ability and the ability to learn new things are far more important than knowledge o...