P

@praveen11

check whether given is a power of 2 or not

Python
4 years ago
def checkpowerof2(n): if(n==0): return False; while(n != 1): if(n%2 != 0): return False n=n//2 return True n=int(input())

gcd of two numbers

Python
4 years ago
def gcd(a,b): if(b==0): return a else: return gcd(b,a%b) t=int(input()) for i in range(t): a,b=map(int,input().split()) ans=gcd(a,b)

precision handling

Python
4 years ago
a=int(input()) print(a) print('%.4f'%a)

count equal pairs in a list

Python
4 years ago
l=[] l=[int(i) for i in input().split()] n=len(l) count=0 for i in range(n): for j in range(i,n): if(i!=j and l[i]==l[j]): count+=1 print(count)

lower and upper index of an element occurences in an array

C++
4 years ago
#include<bits/stdc++.h> #define ll long long int using namespace std; ll lowerindex(ll a[],ll n,ll key){ ll low=0; ll high=n-1; ll res=-1; while(low<=high){ ll mid=(low+high)/2;

pattern model

C++
4 years ago
//we have to print the following pattern for a given value "n" /* for n=3 *** * * * ** ** * * * *** for n=5

total count number of occurances of char of one string in another

Python
4 years ago
jewels=input() #string of unique characters stones=input() ans=0 for i in jewels: ans+=stones.count(i) print(ans)

searching an element in given list

Python
4 years ago
n=int(input()) #size of list l=[] l=[int(i) for i in input().split()] #input of list elements key=int(input()) #element need to search if(key in l): print("Yes") else: print("No")

getting input from user of list

Python
4 years ago
n=int(input()) l=[] l=[int(i) for i in input().split()] for i in l: print(i)

sieve of eratosthenes

Python
4 years ago
def sieve(n): prime=[True for i in range(n+1)] p=2 while(p*p<=n): if(prime[p]==True): for i in range(p*p,n+1,p): prime[i]=False p+=1 for i in range(2,n+1):

multiplication of two matrices

Python
4 years ago
r,c = map(int,input().split()) a=[] for i in range(r): a.append([int(j) for j in input().split()]) m,n = map(int,input().split()) b=[] for i in range(m): b.append([int(j) for j in input().split()])

addition of matrices

Python
4 years ago
r,c=map(int,input().split()) a=[] b=[] for i in range(r): a.append([int(j) for j in input().split()]) for i in range(r): b.append([int(j) for j in input().split()])

converting string to list

Python
4 years ago
s="23 3 45 6 7 8" x="" l=[] for i in s: if(i!=" "): x=x+i else: l.append(x) x="" l.append(x)

taking user input without size in deque

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ deque<int> d; int input; while(cin>>input){ d.push_back(input); } for(int i=0;i<d.size();i++){ cout<<d[i]<<" ";

Deque initailisation

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ deque<int> d; d.push_back(3); d.push_back(4); d.push_front(2); d.push_front(1); for(int i=0;i<d.size();i++){ cout<<d[i]<<" ";

perfect cube or not

Python
4 years ago
t=int(input()) for i in range(t): n=int(input()) n=abs(n) cube=round(n**(1/3)) if(cube**3 == n): print("YES") else: print("NO")

printing vector using iterator

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> v = {1,2,3,4,5}; for(auto it=v.begin();it!=v.end();++it){ cout<<*it<<" "; } cout<<"\n"; for(auto it=v.rbegin();it!=v.rend();++it){ cout

vector operations

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> v; int input; while(cin>>input){ v.push_back(input); } for(int i=0;i<v.size();i++){ cout<<v[i]<<"\n";

taking user input of vector without size

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> v; int input; while(cin>>input){ v.push_back(input); } for(int i=0;i<v.size();i++){ cout<<v[i]<<"\n";

vector initialising

C++
4 years ago
#include<bits/stdc++.h> using namespace std; int main(){ vector<int> v; for(int i=0;i<=16;i++){ v.push_back(i); } cout<<"Size: "<<v.size()<<"\n"; cout<<"Capacity: "<<v.capacity()<<"\n"; cout<<"Max_size: "<<v.