All Questions
245 questions
28
votes
4
answers
17k
views
Repeatedly multiplying digits until a single digit is obtained
I've written code that solves a problem from codewars.com. My code passes all the tests on the codewars site. It's a simple enough problem to solve naïvely, but I would love to improve the structure.
...
27
votes
2
answers
4k
views
Sudoku using 'exact cover' solver
1. Introduction
This is a solution to Weekend Challenge #3: a Sudoku solver in Python. It works by translating a Sudoku puzzle into an exact cover problem, and then solving the exact cover problem ...
19
votes
5
answers
65k
views
Get value from dictionary given a list of nested keys
I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
18
votes
2
answers
3k
views
A KenKen puzzle/solver in Python
I've written a simple KenKen puzzle/solver in Python. I'd love some feedback on the design of the puzzle, as well as the architecture for the solver.
To model the puzzle, I have the following classes:...
17
votes
3
answers
5k
views
Knapsack branch and bound: forward filter
I've coded branch and bound to solve the knapsack problem, and use a greedy linear relaxation to find an upper bound on a node I'm currently exploring. What I mean by this is that I first sort the ...
13
votes
3
answers
41k
views
GCD using Euclid algorithm
Below is the problem taken from here.
Question 10: GCD*
The greatest common divisor of two positive integers a and b is the largest integer which evenly divides both numbers (with no remainder)....
13
votes
2
answers
903
views
N-Queens, bitwise approach
I've recently solved the famous N-Queens problem:
Can you place \$N\$ chess queens on a \$N × N\$ board in such a way that no one
attacks another queen?
The idea here is based on the ideas from ...
12
votes
6
answers
4k
views
Python 3 - Fibonacci Implementation
I wrote a function returning the n-th Fibonacci number in Python 3:
...
12
votes
2
answers
2k
views
Tower of Hanoi Recursive Implementation using Python with OOP
I have just finished a small program for Tower of Hanoi using recursion. I did it to practice OOP which I recently learnt. It took me about 2 hours to research about the recursive algorithm and write ...
12
votes
2
answers
3k
views
Sudoku solver using simple deductions and brute-force guessing
Here's my attempt at Weekend Challenge #3.
Key characteristics of this Python entry are:
The strategy is to alternate between "auto-complete" (making simple deductions such as naked singles and ...
12
votes
1
answer
2k
views
Flatten an array of integers in Python
The goal is to flatten an array of nested arrays of integers.
For example [1, [2], [3, [4]]] should return [1, 2, 3, 4]
I'm ...
11
votes
5
answers
3k
views
Python: pass "mutable integer" in recursion
I'm working on some code to find the max depth/height in a tree. I came up with a recursive solution, and I find myself needing to pass an integer "by reference". Currently, I'm using a mutable ...
11
votes
2
answers
5k
views
Lovely Lucky LAMBs
I am trying to solve this question from google's foobar. However, my code exceeds the time limit, even though when I tested it I got correct answers. How can I make my code more efficient?
Lovely ...
11
votes
2
answers
462
views
Finding acronyms with >=1 letters at the start of each consecutive word
Problem
I'm seeking a clean and efficient way to determine whether one string is an initialism/acronym of another.
Example: IBM => International Business Machines
However, a key condition for my ...
10
votes
2
answers
3k
views