Ad

Create a simple sudoku solver.
The input is a 81 digit string. Blank squares are zero's
Output is an 81 digit string with the solved puzzle

The test cases are simple enough that back tracking should be possible within the given time.
Good luck

#import <vector>
#import <unordered_set>

std::vector<std::vector<int>> puzzle;

std::vector<int> get_row(int r)
{
    return puzzle[r];
}

std::vector<int> get_col(int c)
{
    std::vector<int> col;
    for (int r = 0; r < 9; r++)
    {
        col.push_back(puzzle[r][c]);
    }
    return col;
}

std::vector<int> get_block(int r, int c)
{
    std::vector<int> block;
    r = r / 3 * 3; //this gives the top row for the block of the given r. by abusing that int floors numbers ex 5/3 = 1 *3 = 3;
    c = c / 3 * 3;
    for (int y = r; y < r + 3; y++)
    {
        for (int x = c; x < c + 3; x++)
        {
            block.push_back(puzzle[y][x]);
        }
    }
    return block;
}

std::unordered_set<int> get_options(int r, int c)
{
    if (puzzle[r][c] != 0)
        return {puzzle[r][c]};
    std::unordered_set<int> options = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::vector<int> row = get_row(r);
    std::vector<int> col = get_col(c);
    std::vector<int> block = get_block(r, c);
    for (int i = 0; i < 9; i++)
    {
        options.erase(row[i]);
        options.erase(col[i]);
        options.erase(block[i]);
    }
    return options;
}

bool backtrack(int r = 0, int c = 0)
{
    if (r == 9)
        return true; //returns true if you've finished the last row
    int r2, c2;

    while (puzzle[r][c] != 0) //if the square is already set, try next ones until you find a 0
    {
        if (c == 8) //calculate the next piece
        {
            c = 0;
            r++;
        }
        else
            c++;
        if (r > 8)
            return true; //if you find the end of the puzzle before a zero, return true.
    }
    if (c == 8) //calculate the next piece
    {
        c2 = 0;
        r2 = r + 1;
    }
    else
    {
        r2 = r;
        c2 = c + 1;
    }
    std::unordered_set<int> options = get_options(r, c);
    if (options.size() == 0)
        return false; //if there are no options something must have gone wrong before. return false
    for (int o : options)
    {
        puzzle[r][c] = o; //set the square to one of the options
        if (backtrack(r2, c2) == true)
            return true;
        puzzle[r][c] = 0; //return the square to 0;
    }
    return false;
}

std::string toString()
{
    std::string puzzle_string;
    for (int r = 0; r < 9; r++)
    {
        for (int c = 0; c < 9; c++)
        {
            puzzle_string += std::to_string(puzzle[r][c]);
        }
    }
    return puzzle_string;
}

void set_puzzle(std::string puzzleString)
{
    std::vector<std::vector<int>> newPuzzle;
    for (int r = 0; r < 9; r++)
    {
        std::vector<int> row;
        for (int c = 0; c < 9; c++)
        {
            row.push_back((int)puzzleString[r * 9 + c] - '0'); //converts char to int and puts it in the row
        }
        newPuzzle.push_back(row);
    }
    puzzle = newPuzzle;
}
  
std::string solve_sudoku(std::string puzzleString)
  {
  set_puzzle(puzzleString);
  backtrack();
  return toString();
}

For some unknown reason (there is a reason, but "for some unknown reason" sounds more fun) there is no product function in python
Let's add it!

note: The product of an empty list is 0.

def prod(numbers):
    if numbers == []: return 0
    product = 1
    for number in numbers:
        product *= number
    return product

2048 is a single-player sliding block puzzle game designed by Italian web developer Gabriele Cirulli. The objective of the game is to slide numbered tiles on a grid to combine them to create a tile with the number 2048; however, one can continue to play the game after reaching the goal, creating tiles with larger numbers.
The question is how high you can go.

Every turn, a new tile will randomly appear in an empty spot on the board with a value of either 2 or 4. If two tiles of the same number collide, they will merge into a tile with the total value of the two tiles that collided.

For a given board size, return the highest numbers, and sum of all numbers.

For example, for a normal 4x4 board and you are really lucky, the best possible board is:
131072 65536 32768 16384
1024 2048 4096 8192
512 256 128 64
4 8 16 32

The best possible block is 131072, the sum of all blocks is 262140

def calculate_2048(size):
    sq = 4
    for _ in range(0,size*size-1): #calculating the highest square by simply multiplying it by 2
        sq *= 2
    highest = sq
    sumtotal = sq
    for _ in range(0,size*size-1): #calculating the highest score by adding up every number back down to 4
        sq = int(sq/2)
        sumtotal = int(sumtotal+sq)
    return highest, int(sumtotal)

Your input is a list containing digits, strings, and iterables (lists, sets, tuples) containing those.
Unpack the list into a string with all values seperated by spaces

Code
Diff
  • def isIterable(l):
        try:
            l[0]
            return True
        except:
            return False
        
    def unpackIterable(l):
        output = []
        for item in l:
            if isIterable(item) and not isinstance(item, str):
                output += unpackIterable(item)
            else:
                output.append(item)
        return output
    
    def unpackList(l):
        outputList = []
        for item in l:
            if isIterable(item) and not isinstance(item, str):
                outputList += unpackIterable(item)
            else:
                outputList.append(item)
        return " ".join(map(str, outputList))
        
    
    
    
    • l = [1,2,3,4,[1,2,3],(1,2),'a']
    • def isIterable(l):
    • try:
    • l[0]
    • return True
    • except:
    • return False
    • def unpackIterable(l):
    • output = []
    • for item in l:
    • if isIterable(item) and not isinstance(item, str):
    • output += unpackIterable(item)
    • else:
    • output.append(item)
    • return output
    • def unpackList(l):
    • outputList = []
    • for item in l:
    • if isIterable(item) and not isinstance(item, str):
    • outputList += unpackIterable(item)
    • else:
    • outputList.append(item)
    • return " ".join(map(str, outputList))
    • print(*l)