Google_imp_easy Flashcards

https://www.linkedin.com/in/tejaswini-yella/

1
Q

Flipping an image(reverse a row and invert 0’s to 1’s)

A
class Solution(object):
    def flipAndInvertImage(self, A):
        """
        :type A: List[List[int]]
        :rtype: List[List[int]]
        """
        return [[1-i for i in row[::-1]] for row in A]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Island Perimeter

A
# p+=4 in finding island and in for loop of neighbiurs per-=1
class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        per=0
    r=len(grid)
    c=len(grid[0])
    x=[0,0,-1,1]
    y=[-1,1,0,0]

    for i in range(r):
        for j in range(c):
            if grid[i][j]==1:
                per+=4

                for k in range(4):
                    nx=i+x[k]
                    ny=j+y[k]

                    if nx>=0 and nx=0 and ny
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Disappeared in array

A

return [ i for i in range(1,n+1) if i not in set(k) ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Power of 2

Power of 3

A
# if n==0 return False
while n if n==1 return true,elif n%2!=0 return false else n=n/2

same instead of 2 use 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reverse vowels of a string

(make a list and append string which are vowels with indexes in tuples and then loop through this list to half and interchange values while inserting to list )

A

s=list(s)
vo=[]
for i,j in enumerate(s):
if j.lower() in [‘a’,’e’,’i’,’o’,’u’]:
vo.append((i,j))
for i in range(len(vo)//2):
s[vo[i][0]],s[vo[~i][0]]=vo[~i][1],vo[i][1]

    return ''.join(s)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Plus one

A
for i in range(len(digits)-1,-1,-1):
            if digits[i]<9:
                digits[i]+=1
                break
            else:
                digits[i]=0
    if sum(digits)==0:

        return [1]+digits

    return digits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Valid Parenthesis (if multiple parentheses uses stack)

A
stack=[]
        for i in s:
            if len(stack)==0:
                stack.append(i)
            else:
                if (stack[-1]=='(' and i==')') or (stack[-1]=='[' and i==']') or (stack[-1]=='{' and i=='}'):
                    stack.pop()
                else:
                    stack.append(i)
        if len(stack)==0:
            return True
        else:
            return False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly