Land of Logic

Arcade Universe | Intro

longest Word

Define a word as a sequence of consecutive English letters. Find the longest word from the given string.

def longestWord(text):
    return max(re.split('[^a-zA-Z]', text), key=len)
def longestWord(t):
    return max("".join([i if i in string.ascii_letters else " " for i in t]).split(),key=len)

Valid Time

Check if the given string is a correct time representation of the 24-hour clock.

def validTime(time):
    return 0<=h<24 and 0<=m<60

Sum Up Number

Calculate the total number in the string. Like: “There are 12 Mangos, and 24 Bananas”, Answer is 36.

def sumUpNumbers(inputString):
    l = re.findall(r"\d+",inputString)
    return sum([int(i) for i in l])
def sumUpNumbers(s):
    return sum(map(int,"".join([i if i.isdigit() else " " for i in s]).split()))
def sumUpNumbers(i):
    return sum(map(int,re.findall('\d+',i))) 
def sumUpNumbers(inputString):

    return sum(map(int, re.findall(r'\d+', inputString, re.ASCII)))

Different Squares

File Naming

Message From Binary Code

Spiral Numbers


Sudoku is a number-placement puzzle. Check that if the Sudoku is correct or not. Return ‘True’, or ‘False’.

def sudoku(grid):

    def r(i):
        return sorted(grid[i]) != list(range(1,10))
    def c(i):
        return sorted([grid[x][i] for x in range(9)]) != list(range(1,10))
    def g(x,y):
        return sorted([grid[i][j] for i in range(x,x+3) for j in range(y,y+3)]) != list(range(1,10))

    for i in range(9):
        if r(i) or c(i):
            return False
    for i in range(0,9,3):
        for j in range(0,9,3):
            if g(i,j):
                return False
    return True
def sudoku(grid):
    columnum = 0
    row = all([len(set(x))==9 for x in grid])
    column = all([len(set([grid[y][x] for y in range(9)]))==9 for x in range(9)])
    sub_grid = all([len(set([grid[y+columnum*3][x+rownum*3] for y in range(3) for x in range(3)]))==9 for rownum in range(3) for columnum in range(3)])
    return all((row,column,sub_grid))
def sudoku(grid):
    subgrids = [grid[i][j:j+3] + grid[i+1][j:j+3] + grid[i+2][j:j+3] for i in (0, 3, 6) for j in (0, 3, 6)]
    for number_group_type in grid, zip(*grid), subgrids:
        for number_group in number_group_type:
            if len(frozenset(number_group)) != 9:
                return False
    return True

Leave a Reply

Your email address will not be published. Required fields are marked *