Skip to main content

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']       

String to Integer

String to Integer 



Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which is ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ' ' is considered a whitespace character.
  • Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, 231 − 1 or −231 is returned.

 

Example 1:

Input: str = "42"
Output: 42

Example 2:

Input: str = "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign. Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: str = "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: str = "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: str = "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer. Thefore INT_MIN (−231) is returned.

 

Constraints:

  • 0 <= s.length <= 200
  • s consists of English letters (lower-case and upper-case), digits, ' ''+''-' and '.'.


Solution:

str="+12"
a=str.lstrip()
#print(a)
l=len(a)
#print(l)
c=""
i=0
if (ord(a[0])>=48 and ord(a[0])<=57) or a[0]=="+" or a[0]== "-":
    if a[0]=="+" or a[0]=="-":
        c+=a[0]
        i=1
        
    
    while(ord(a[i])>=48 and ord(a[i])<=57 and i<l):
        c+=a[i]
        i+=1
        #print(c)
        if(i>=l):
            break
    k=int(c)
    #print(k)
    print(min(max(k,-2**31),(2**31)-1))
else:
    print(0)

Comments

Post a Comment

Popular posts from this blog

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

Perfect Peak of Array

Problem Description Given an integer array  A  of size  N . You need to check that whether there exists an element which is  strictly greater than all the elements on the left  of it and  strictly smaller than all the elements  on the right of it. If it exists return  1  else return  0 . NOTE: Do not consider the corner elements i.e  A[0] and A[N-1]  as the answer. Problem Constraints 3 <= N <= 10 5 1 <= A[i] <= 10 9 Input Format First and only argument is an integer array  A  containing  N  integers. Output Format Return  1  if there exist a element that is  strictly greater than all the elements on left  of it and  strictly smaller than all the elements  on right of it else return  0. Example Input Input 1: A = [5, 1, 4, 3, 6, 8, 10, 7, 9] Input 2: A = [5, 1, 4, 4] Example Output Output 1: 1 Output 2: 0 Example Explanation Explanation 1: A[4] = 6 is the element we are looking for. All elements on left of A[4] are smaller than it and all elements on right are greater