Skip to main content

Posts

Showing posts from January, 2021

Adjacency List in a Graph

   from collections import defaultdict graph=defaultdict(list) v,e=map(int,input().split()) for i in range(e):     u,v=map(str,input().split())     graph[u].append(v)     graph[v].append(u)      for i in graph:     print(i,"->",graph[i]) Input: 7 9 A B A C A F C E C F C D D E D G G F Output: F -> ['A', 'C', 'G'] C -> ['A', 'E', 'F', 'D'] G -> ['D', 'F'] D -> ['C', 'E', 'G'] A -> ['B', 'C', 'F'] E -> ['C', 'D'] B -> ['A']       

Exclusive Or

Find the XOR of two numbers and print it. Hint: Input the numbers as strings. Input Format First-line contains the first number X and the second line contains second number Y. The numbers will be given to you in binary form. Constraints 0 <= X <= 2^1000 0 <= Y <= 2^1000 Output Format Output one number in binary format, the XOR of two numbers. Sample Input 0 11011 10101 Sample Output 0 01110     CODE: a=input() b=input() c=""   if len(a)<=len(b):     k=len(b)-len(a)     l="0"*k     a=l+a else:     k=len(a)-len(b)     l="0"*k     b=l+b     for i in range(len(a)):     c+=str(int(a[i])^int(b[i]))   print(c)

Square in a Circle

  A square is kept inside a circle. It keeps expanding until all four of its vertices touch the circumference of the circle. Another smaller circle is kept inside the square now and it keeps expanding until its circumference touches all four sides of the square. The outer and the inner circle form a ring. Find the area of this ring. Input Input consists of multiple test cases. Each test case contains one integer denoting the side-length of the square between the two circles. Output Print the area of the ring. Sample Input 0 3 3 4 5 Sample Output 0 7.068583 12.566371 19.634954   Code: import math t=int(input()) while(t!=0):     t-=1     q=int(input())     a_o=q/math.sqrt(2)     a_i=q/2     print("%.6f"%(math.acos(-1)*(a_o*a_o - a_i*a_i)))    

Trouble with the Number System

  Barua has developed his own Operating System known as "Barua OS" ( BOS ). One day while booting up his system he runs into a bug. You want to impress Barua so you jump in and offer to solve the bug yourself. Barua doesn't like binary numbers very much and his operating system uses a new number system called BNS ( Barua Number System ). The following are the properties of a number represented in BNS form : 1.       The number can only be made up of 2 distinct digits, one or zero. 2.       The number cannot start with zero. 3.       The number can have any number of zeroes, but only one instance of the digit one. For example 100, 1000, 10000 are Barua Numbers whereas 101, 502, 625 are not Barua Numbers. Unfortunately one decimal number has crept into a list of Barua Numbers and Barua OS cannot find its product. Can you? Input Format: First line contains an integer N, total number of elements in the list. Next N lines contains a number a[i] <= 10^18. N

Residue Arithmetic

  For operators with large integers, residue arithmetic is used. A residue of a number n modulo a prime p is defined as the remainder obtained when we divide a by p. For example, if the residue of 100 modulo 3 is 1. Two large primes are chosen and all numbers are expressed as modulo those primes. For example, if 7 and 3 were chosen as the primes, 25 would be represented as 4,1 in this representation, 3 would be represented as 3,3 and 30 would be represented as 2,0. Now if we do 25 * 3 - 30 in this model, we do the operation and compute the residue of the result with each of the primes in turn. SO with respect to prime 7, the result is 4 * 3 - 2 or 10 which has a residue of 3 modulo 7. With respect to 13, the operation is 12 * 3 - 4 = 32 whose residue is 6 modulo 13. Hence the result is 3,6 in the residue notation. There is only one number less than 91 which has this representation, and that is 45, which is what we expect. In this problem, we will be given 3 numbers a,b and c in residue

Consecutive Prime Sum

Some prime numbers can be expressed as sums of other consecutive prime numbers. For example 5 = 2 + 3 17 = 2 + 3 + 5 + 7 41 = 2 + 3 + 5 + 7 + 11 + 13 Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to the constraint that summation should always start with number 2. Write code to find out a number of prime numbers that satisfy the above-mentioned property in a given range. Input Format: Each test case contains a number N <= 1000000000 Output Format: Print the total number of all such prime numbers which are less than or equal to N.   Code: import math def isprime(n):     if (n==2 or n==3):         return 1     if (n==1 or n%2==0 or n%3==0 ):         return 0     for i in range(5,int(math.sqrt(n)+1),6):         if(n%i==0 or n%(i+2)==0 ):             return 0     return 1 n=int(input()) sum=2 i=3 c=0 while(sum<=n):     if(isprime(i)==1):         sum+=i      

Strings Methods

  s="Hello HOw you are Doing Today" s1="goa" a="123nk" m=" " k=",,,..mks..orange" t="r\ti\tg\th\tt" print(s.capitalize()) print(s.casefold()) print(s.center(50,"0")) print(s.count("o")) print(s.endswith("Today")) print(t.expandtabs(3)) print(s.find("o"))#returns -1 if not present print(s.index("o"))#returns exception if not present print(a.isalnum())# returns true for [a-z] [0-9] print(a.isalpha()) print(a.isdigit())#return true for decimals,superscripts and false for fractions print(a.isdecimal())#return true for decimals and false for fractions ,superscripts  print(a.isidentifier()) #should start with [a-z] [0-9] or _ , false for space ("my room") print(s.islower()) print(s.isnumeric())#return true for decimals,superscripts fractions print(m.isspace()) print(s.istitle())#Hi Ram What Happened print(s.isupper()) print(" ".join(["i","Love"

Isolation Centers

  Problem Link: Codechef As a health expert, Vinay is keeping a close watch on the ongoing pandemic of coronavirus disease (COVID-19). He thought of a different situation where there are  26 26  types of viruses, named " a orona", " b orona", " c orona", …, " z orona". You are given a string  S S  with length  N N . There are  N N  people (numbered  1 1  through  N N ) and for each valid  i i , the  i i -th person is infected by exactly one type of virus named  S i Si orona (i.e. "corona" with the first letter replaced by the  i i -th character of  S S ). You should answer  Q Q  queries. In each query: ·        You are given an integer,  C C  denoting the number of available  isolation centers . ·        Each isolation center has an infinite capacity, but with the restriction that two people infected with the same type of virus cannot stay in the same isolation center. ·        There is also a  pending queue  with an infinite