Blend面经+答案大放送

店面面的是fraction to Decimal, onsite面的是地里的generate matrix和lisp interpreter

def fractionToDecimal(self, numerator, denominator):
       if denominator*numerator == 0:
           return "0"
       res = ""
       if denominator*numerator < 0:
           res += "-"
       numerator, denominator = abs(numerator), abs(denominator)
       n, remainder = divmod(numerator, denominator)
       if remainder == 0:
           return res + str(n)
       res += str(n) + "."
       counter = {}
       while remainder:
           if remainder in counter:
               res = res[:counter[remainder]] + "(" + res[counter[remainder]:]+")"
               break
           counter[remainder] = len(res)
           remainder *= 10
           n, remainder = divmod(remainder, denominator)
           res += str(n)
       return res
import copy
def move(grid):
    def robotMove(grid, i, j):
        if i < 0 or i >= len(grid) or j < 0 or  j >= len(grid[0]):
            return 0
        if grid[i][j] == 1:
            return 0   
        if i == len(grid)-1 and j == len(grid[0]) - 1:
            return 1   
        arr = copy.deepcopy(grid)
        arr[i][j] = 1
        return   robotMove(arr,i-1,j) + robotMove(arr, i, j+1) + robotMove(arr, i,j-1) + robotMove(arr, i+1,j)
     
    return robotMove(grid, 0, 0)
import copy
def move(grid):
def robotMove(grid, i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]):
return 0
if grid[i][j] == 1:
return 0 
if i == len(grid)-1 and j == len(grid[0]) - 1:
return 1 
arr = copy.deepcopy(grid)
arr[i][j] = 1
return robotMove(arr,i-1,j) + robotMove(arr, i, j+1) + robotMove(arr, i,j-1) + robotMove(arr, i+1,j)
 
return robotMove(grid, 0, 0)
def letterCombinations(self, digits):
        if not digits:
            return []
        phoneMap = { '2' : "abc", '3': "def", '4':"ghi", 
           '5':"jkl", '6':"mno", '7':"pqrs", '8':"tuv",'9': "wxyz" }
 
        result = []
        self.dfs(digits, "", result,phoneMap)
        return result
 
    def dfs(self, digits, path, result,phoneMap):
        if not digits and path:
            result.append(path)
            return
        for letter in phoneMap[digits[0]]:
            self.dfs(digits[1:], path+letter, result,phoneMap)
import random
import copy
class MineSweeper():
    def __init__(self, row, col, mine_count):
        self.board = [["-" for _ in range(col)] for _ in range(row)]
        self.row = row
        self.col = col
        self.mines = self.place_mines(mine_count)
        self.gameOver = False
        self.visited = set()
        self.iteration = 0
        self.print_board()
        self.print_mine()
 
    def print_mine(self):
        print("Mines -------------")
        tmp = copy.deepcopy(self.board)
        for (row, col) in self.mines:
            tmp[row][col] = "X"
        for row in tmp:
            print(" ".join(row))        
 
    def print_board(self):
        print("Board at " + str(self.iteration)+ "th iteration: -----------------")
        for row in self.board:
            print(" ".join(row))
        self.iteration += 1
 
 
    def place_mines(self, mine_count):
        cnt = 0
        locations = set()
        while cnt < mine_count:
            row, col = self.generate_random()    
            if (row, col) not in locations:
                cnt += 1
                locations.add((row, col))
        return locations
 
 
    def click(self, row = None, col = None):
        if len(self.visited) == self.row*self.col:
            print("You win")
            self.gameOver = True
            return
 
        if row ==None and col ==None:
            row, col = self.generate_random()
            while (row, col) in self.visited:
                row, col = self.generate_random()
        print("Clicked at number {0} row, number {1} col".format(row, col))
        if (row, col) in self.mines:
            self.board[row][col] = "X"
            self.print_board()
            print("Game over!")
            self.gameOver = True
            return
        else:
            self.reveal(row, col)
            self.print_board()
 
    def reveal(self, row, col):
        mine_count = 0
        self.visited.add((row, col))
 
        for i in range(row-1, row+2):
            for j in range(col-1, col+2):
                if (i, j) in self.mines:
                    mine_count += 1
        if mine_count == 0:
            self.board[row][col] = "0"
        else:
            self.board[row][col] = str(mine_count)
            return
        for i in range(row-1, row+2):
            for j in range(col-1, col+2):
                if 0 <= i < self.row and 0 <= j < self.col and (i, j) not in self.visited:
                    self.reveal(i, j)
 
    def generate_random(self):
        return random.randint(0, self.row-1), random.randint(0, self.col-1)
 
 
if __name__ == "__main__":
    while True:
        config = input("Enter the number of rows, columns of the board size and mines with a space in between")
        config = config.strip().split()
        if len(config) != 3:
            print("Invalid input.")
            continue
        row, col, mines = int(config[0]), int(config[1]), int(config[2])
        minesweeper = MineSweeper(row, col, mines)
 
        while not minesweeper.gameOver:
            click = input("Enter row number and col number you want to click.").strip().split()
            row, col = int(click[0]) , int(click[1])
            minesweeper.click(row, col)    
def generateMatrix(self, n):
        if n == 0: return []
        if n == 1: return [[1]]
        result = [[0]*n for _ in range(n)]
        left, right, top, bottom = 0, n-1, 0, n-1
        num = 1
        while left <= right and top <= bottom:
            for j in range(left, right + 1):
                result[top][j] = num
                num += 1
            for j in range(top+1, bottom+1):
                result[j][right] = num
                num += 1
            for j in reversed(range(left, right)):
                if top < bottom:
                    result[bottom][j] = num
                    num += 1
            for j in reversed(range(top+1, bottom)):
                if left < right:
                    result[j][left] = num
                    num += 1
                . From 1point 3acres bbs
            left, right, top, bottom = left + 1, right - 1, top + 1, bottom - 1

        return result

# 11111 matrixm with 1 space between
def generateMatrix(width, height):
    if width == 0 or height == 0: 
        print("")
        return
    if height == 1:
        print("1"*width)
        return
    if width == 1:
        for _ in range(height):
            print("1")
        return
    result = [[" "]*width for _ in range(height)]
    left, right, top, bottom = 0, width-1, 0, height-1
    
    while left < right and top < bottom:
        for j in range(top, bottom+1):
            result[j][left] = "1"
            
        for j in range(left+1, right + 1):
            if top < bottom - 1:. check 1point3acres for more.
                result[bottom][j] = "1"   
                
        for j in reversed(range(top+2, bottom)):
            if left < right - 1:
                result[j][right] = "1"
                
        for j in reversed(range(left+2, right)):
            if top + 2 < bottom -1:
                result[top+2][j] = "1"        
        left, right, top, bottom = left + 2, right - 2, top + 2, bottom - 2
    for row in result:
        print("".join(row))
    return result