Skip to main content

All Questions

Tagged with
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. ...
Caitlin Quintero Weaver's user avatar
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 ...
Gareth Rees's user avatar
  • 49.9k
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'...
Martin Burch's user avatar
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:...
user138440's user avatar
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 ...
rookie's user avatar
  • 1,233
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)....
overexchange's user avatar
  • 3,401
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 ...
alecxe's user avatar
  • 17.4k
12 votes
6 answers
4k views

Python 3 - Fibonacci Implementation

I wrote a function returning the n-th Fibonacci number in Python 3: ...
user7802048's user avatar
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 ...
Oliver H's user avatar
  • 123
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 ...
200_success's user avatar
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 ...
Nikolay Derkach's user avatar
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 ...
rishai's user avatar
  • 219
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 ...
ngmh's user avatar
  • 113
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 ...
Ian's user avatar
  • 233
10 votes
2 answers
3k views

My recursive attempt at Collatz Sequence in Python

...
flamethrower10's user avatar

15 30 50 per page
1
2 3 4 5
17