All Questions
Tagged with combinatoricspython
157 questions
22votes
4answers
21kviews
Finding all k-subset partitions
The following code generates all \$k\$-subsets of a given array. A \$k\$-subset of set \$X\$ is a partition of all the elements in \$X\$ into \$k\$ non-empty subsets. Thus, for ...
19votes
4answers
6kviews
Printing all possible sums of two n-sided dice rolls
For my CS class, we were tasked with writing a program that calculates all the possible results from rolling two n-sided dice. For example, the input of 4 would ...
19votes
4answers
5kviews
Count number of ways to paint a fence with N posts using K colors
A friend sent me this question, and I'd love somebody to review this and give his opinion. Write an algorithm that counts the number of ways you can paint a fence with N posts using K colors such ...
14votes
4answers
3kviews
Exhaustive search for encryption keys
This does the job, but is not especially elegant. What is the preferred Pythonic idiom to my childish nests? ...
14votes
1answer
3kviews
Nonogram puzzle solution space
I've written some code to brute-force enumerate the solution space of nonogram puzzles up to a certain size. It does a good job up to the first ~4 billion puzzles, but I run out of disk space to store ...
13votes
3answers
20kviews
Counting permutations without repetitions for a number or a string
Can I make the following code faster with minor modifications? It times out on CodeWars website. ...
11votes
2answers
529views
Approximate (250 over 100) permutation best fitting certain criteria
Given a list of 250 words of 4 letters each, what is the fastest way, in Python, to find a subsample of 100 words (drawn without replacement) so that the distribution of letters across the whole ...
10votes
3answers
714views
Master Locksmith
Description Master Locksmith has just finished the work of his life: a combination lock so big and complex that no one will ever open it without knowing the right combination. He's done testing ...
10votes
1answer
1kviews
Programming Challenge from Kattis: Apparatus
I am trying to solve apparatus problem, paraphrased below. There is an apparatus with n on/off switches with a one-to-one correspondence to n lights (but with an unknown permutation). We have ...
10votes
3answers
9kviews
Finding number of integer partitions
I am trying to find number of integer partitions of given n - number. If I have n == 4, the answer should be 5 because: \$4 = 1+1+1+1\$ \$4 = 2+1+1\$ \$4 = 3+1\$ \$...
10votes
2answers
956views
Finding all contiguous sublists such that each member appears an even number of times
The program needs to find all contiguous sublists of numbers where the number of occurrences of every member in the contiguous sublist is divisible by 2. The first line of input is N (the number of ...
9votes
2answers
2kviews
Calculating 24 from 4 numbers (math Poker game)
This is a game of math in Poker. Two people each have a pile of cards. They then each place 2 cards on the table at the same time. Every card is treated as a number. The players need to find a way to ...
9votes
2answers
1kviews
Return all valid expressions with specific target
The problem is stated as: Given a string that contains only digits 0-9 and a target value, return all expressions that are created by adding some binary operators (+, -, or *) between the digits so ...
8votes
3answers
294views
Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"
I have come up with a sequence of steps to find the maximum product of y positive numbers which add up to x. But the program is ...
8votes
2answers
17kviews
List all possible permutations from a python dictionary of lists
The following code takes a dictionary of lists (indexed by keyword) and converts it into a list of list of dictionaries that contains all possible permutations of those lists. However, my solution ...