Combinatorics is a branch of mathematics concerning the study of finite or countable discrete structures.

learn more… | top users | synonyms

5
votes
1answer
69 views

Project Euler #15 in haskell

I've solved Project Euler #15 in Haskell, but I feel like there might be a better way to express my solution that I'm not seeing. I start out defining my types: import qualified Data.Map.Strict as ...
3
votes
2answers
78 views

Permutation of a string eliminating duplicates

This code lists the permutations of the string, and eliminates duplicates if any. I'm looking for code review, best practices, optimizations etc. Also verifying complexity: O(n! * n) as time ...
1
vote
2answers
63 views

Calculate all possible combinations of given characters

I was asked in my textbook Lectures on Discrete Mathematics for Computer Science to construct a program that would take an alphabet ({a,b,c} or any combination of characters {1,4,s,a}) as well as a ...
5
votes
2answers
118 views

Print routes on the stairway when only 1 or 2 steps are possible

Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible ways you go from bottom to top. I'm looking for code review, best practices, optimizations etc. ...
3
votes
1answer
96 views

Find all permutations of a string in C++

I need a review of the following code for finding all permutations of a string in C++: List Permute(const String& string) { if (string.length == 0) return EmptyStrings(1); List ...
8
votes
3answers
399 views

Brute Force Algorithm in C

This is a simple brute force algorithm I have in C. All the program does it print out every possible combination of the given alphabet for the given length. I would prefer suggestions on how to ...
0
votes
1answer
131 views

Fastest way for working with itertools.combinations

I need to speed up the function below: import numpy as np import itertools def combcol(myarr): ndims = myarr.shape[0] solutions = [] for idx1, idx2, idx3, idx4, idx5, idx6 in ...
2
votes
1answer
33 views

Produce product of lists

I want to combine elements in different lists. For example, here are 3 lists. list_a : [ 1,2,3 ] list_b : [ 4,5,6 ] list_c : [ 7,8 ] Selecting elements from list_b and list_c is mutually exclusive. ...
2
votes
0answers
56 views

Determine if a word can be constructed from list of subsets - follow-up

I have reworked my code as suggested from my previous question: Determine if a word can be constructed from list of subsets. Please instruct me on the complexity along with a review feedback, as ...
2
votes
2answers
116 views

Determine if a word can be constructed from list of subsets [closed]

The question is explained well by examples in the comments below. Also I request verifying complexity: O( n * n!), where n is the number of words in the subsets. Review my code for optimizations, ...
4
votes
2answers
92 views

Integer Partition using an iterator

I tried implementing Integer Partition (enumerating all possible sets of whole numbers whose sum is a given integer). Looking at @rolfl's answer, I reasoned that with a stack data structure, we ...
3
votes
3answers
122 views

Permutation algorithm: What is so bad about this code?

I was asked to write a permutation algorithm to find the permutations of {a,b,c}. Since this is a famous question to which an answer is readily available online, I wanted to do it a little ...
6
votes
2answers
96 views

Integer Partition

The aim is to enumerate all possible sets of whole numbers whose sum is a given integer. class partition { public static void main(String[] ar) { part(66); } public static ...
3
votes
1answer
86 views

Better way to create samples

I've done this piece of code for creating all possible samples without repetitions, but I think that it is a so heavy algorithm because every time I call this recursive function I create a new vector ...
18
votes
6answers
1k views

How can I make this mess of Java for-loops faster?

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words called sortedDictionary. I have ...
6
votes
1answer
863 views

Find all subsets of an int array whose sums equal a given target

I am trying to implement a function below: Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array. For example: Target sum is 15. An int array is { ...
1
vote
1answer
55 views

Increase efficiency of card-ordering program

As mentioned in @rolfl comment. What this program does is that it "resequences the cards using a fixed system and count the number of times the resequencing happens before the output is the same as ...
2
votes
0answers
39 views

TLE for SPOJ Upper Right King

I am solving this problem on SPOJ Upper Right King (Hard). I've written a solution in Python with a time complexity of O(n(logm)2). But it's showing TLE. Can anyone help me out with this? For more ...
2
votes
1answer
231 views

Ugly numbers problem

Taken from one of codeeval.com challenges (https://www.codeeval.com/open_challenges/42/): Challenge Description: Credits: This challenge has appeared in a google competition before. ...
3
votes
2answers
153 views

Improving run-time and max-digits of digit summing code

As a challenge I decided to do the following problem: How many different numbers with n digits are there whose digits add up to k As this is an embarassingly parallel problem I decided to use a ...
3
votes
1answer
102 views

How to improve this permutations program?

I wrote a small program which shows all possible permutations of choosing m integers from the set {1, 2, ..., n}, as follows: def perm(nlist, m): """ Find all possible permutations of ...
2
votes
1answer
132 views

Python infinite product generator

I have the code below to get an infinite generator of the products of an iterable (e.g. for the iterable "ABC" it should return A, B, C, AA, AB, AC, BA, BB, BC, CA, CB, CC, AAA, AAB, AAC etc. for ...
6
votes
2answers
741 views

Permutation of given string

I am reading a book on DSA, and the author explains how to generate the permutation of strings using recursion. I want to know if there are better ways of doing the same, unless this is the best ...
1
vote
0answers
65 views

How to make python knapsack program faster

I have knapsack problem: I have data: List of product prices = MENU Most popluar product count = MOST_POPULAR_COCKTAIL Knapsack price = CASH Knapsack length = CHECK_LEN I need get all knapsack ...
1
vote
1answer
134 views

How to make this algorithm faster?

It works fine, but it is slow. Could anybody help make this faster? import itertools from decorators import timer from settings import MENU, CASH class Cocktail(object): def __init__(self, ...
3
votes
2answers
240 views

Computing the number of permutations of n consecutive numbers

I'm just starting to learn C++ and have been experimenting with various functions. Here is one that computes the number of permutations of n consecutive numbers as well as all possible permutations ...
2
votes
1answer
228 views

Generating permutations with repetitions of string

I have written the below code which generates all possible permutations with repetitions of a string. I want to make this program shorter and much faster, but I don't know how to do that. Imports ...
1
vote
1answer
74 views

Any way to optimize or improve this python combinations/subsets functions?

I believe this is essentially the same method as the itertools.combinations function, but is there any way to make this code more more perfect in terms of speed, code size and readability : def ...
2
votes
2answers
170 views

multi-recursive replacement function

I wrote a function for creating all possible translations of a source string, based on a multiple translation map. It works, but generates a lot of intermediate maps (see line marked with *). Is there ...
1
vote
1answer
165 views

Discovering words from letters in Clojure (Letterpress Solver)

I created a Letterpress solver that will take a string of letters and return a list of valid words that can be constructer with the provided letters. Not all letters need to be used in each word. ...
9
votes
2answers
982 views

Print all permutations with repetition of characters

Given a string of length n, print all permutation of the given string. Repetition of characters is allowed. Print these permutations in lexicographically sorted order Examples: Input: AB ...
3
votes
1answer
242 views

Generating all subsets of a given set

I am learning Java Collections Framework. Can someone look at this code for generating all subsets of a given set and tell me any issues with it. import java.util.*; public class AllSubsets { ...
4
votes
1answer
210 views

compression condition permutation

I have 16 permutations each with 104 variables using the standard alphabet. The 104 variable permutation is selected based off of compression complexity. This is one example of the pattern I am able ...
5
votes
1answer
414 views

Pairwise Combinations Generator

Below is an algorithm to generate all pairwise combinations for a set of random variables. See here for an explanation of what constitutes a minimal all-pairs set. The algorithm below works but I feel ...
5
votes
2answers
10k views

Permutations of a list of numbers

I have written a program to find all the possible permutations of a given list of items. This precisely means that my program prints all possible P(n,r) values for r=0 to n. package com.algorithm; ...
5
votes
1answer
357 views

Enumerate k-combinations in Clojure (P26 from 99 problems)

I've been playing with Clojure for the last few evenings, going through the well known 99 problems (I'm using a set adapted for Scala). Problem 26 calls for a function that, given a set S and a no. ...
2
votes
3answers
469 views

Permutations Finder Help Number 2

I am that same eighth grader with the Java Application For Finding Permutations Efficiently question. I have changed quite a bit of code, and need some more help... class Permutations { static ...
16
votes
5answers
6k views

Better way to generate all combinations?

I'm generating all combinations of an array, so for instance, ["a", "b", "c", "d"] will generate: [ "a", "b", "ab", "c", "ac", "bc", "abc", "d", "ad", "bd", "abd", "cd", ...
8
votes
1answer
343 views

Java application for finding permutations efficiently

I am an eighth grader with a school project of creating and application in Java that returns the total permutations of two given numbers. It needs to be light and efficient, for later deployment on a ...
2
votes
1answer
252 views

A combinatorial algorithm

How could I improve this algorithm? The goal is to organize matches between sport teams such that: Each team affront each other A minimum number of team should start a match after just finishing one ...
1
vote
2answers
2k views

Python generator function that yields combinations of elements in a sequence sorted by subset order

In Python, itertools.combinations yields combinations of elements in a sequence sorted by lexicographical order. In the course of solving certain math problems, I found it useful to write a function, ...