All Questions
14 questions
7
votes
2
answers
6k
views
Permutations with replacement in Python
Many a times, I've had the need to use a permutations with replacement function.
So, I've written a function to do just that:
...
3
votes
2
answers
291
views
Fixed Content Necklace Generator
I wrote a class that generates combinatorial necklaces with fixed content as per Sawada: A fast algorithm to generate necklaces with fixed content. The class is instantiated with an input list that ...
4
votes
3
answers
553
views
Generate parentheses solution
I have coded a solution to build all valid permutations of parentheses.
My code is below.
I have a question on my code based on a comment by my PEP8 checker. It said that there was no need to ...
4
votes
1
answer
4k
views
Recursive implementation of power set in Python
I was doing a recursion problem called power set, and would like to get code review. Here's the problem.
...
3
votes
1
answer
254
views
python magic square finder for arbitrary numbers
I wrote this code to get all possible magic squares that can be made with a list you put in. The only contraints are that the count of numbers is a square and that each number is unique. It even works ...
2
votes
1
answer
9k
views
Computing the powerset of a list
I've written the following code for computing the powerset of a list (we consider the list has all distinct elements, although it doesn't really matter).
Can this function be further optimized?
<...
4
votes
1
answer
3k
views
K-combination recursive algorithm implementation
Tried to implement this solution for finding n choose k.
I have used recursion and this is how it goes for k as 4 and n as 6. Zero based
The idea is to have array of size k keeping sequence of ...
6
votes
2
answers
7k
views
Making the same amount from different combinations of coins (top-down approach)
I was reading a typical interview question found here. Given an amount and a list of denominations, count the number of possible ...
4
votes
1
answer
427
views
Counting ways to fit bottles in crates
I'm calculating how many possibilities there are to fit \$n\$ identical bottles inside \$k\$ crates of various capacities.
n = Number of bottles
k = Number of crates
K = List of the number of bottles ...
8
votes
1
answer
983
views
Recursive search for combinations of words that have a specified MD5 hash
This code solves a challenge problem that a local company is hosting to attract/screen applicants. The link to the challenge is here.
In brief, we are given a wordlist containing approximately 100,...
4
votes
4
answers
9k
views
Staircase problem solved using recursion
Problem
Question taken from here.
I want to go up a flight of stairs that has n steps. I can either take 1 or 2 steps each time. How many different ways can I go up this flight of stairs? Write a ...
2
votes
1
answer
3k
views
Insect combinatorics challenge
Below is the problem taken from Berkeley's Cs61A page here
Question 9: Insect Combinatorics*
Consider an insect in an M by N grid. The insect starts at the bottom left corner, (0, 0), and wants ...
2
votes
2
answers
1k
views
Count the number of combinations with gap-restriction for every pair of values
A lottery draw consists of choosing 6 different values from 1 to 50.
I want to calculate the number of combinations with gaps of at least 3 between every pair of values.
I have used the following ...
2
votes
1
answer
6k
views
Nsum problem: find all n elements in an array whose sum equals a given value
I am pretty new to Python, so I want to listen to any advice that improve my coding style in a "pythonic" way, even about naming style of variables. The following code reflects a very general paradigm ...