Package: ccc-calculator
cccCommand line Combinatorial Calculator for Counting Constrained Collections.Introductionccc is a calculator that can:tell you theprobabilityof possible ways a certain collection of items can meet one or more constraintscountthe number of possible ways a certain collection of items can meet one or more constraintsFor example, consider the following problem (asked onstats.stackexchange.com):There are232tickets for an event. 363 people apply for a ticket,12of whom are from a particular group (so351are not from the group). Each ticket is allocated to one person at random and each person can recieve at most one ticket. What is the probability thatat most 2tickets are given to people in the group?ccc makes it easy to translate the problem into a single command, handling the tedious computational details from you:$ ccc probability draw --from 'group=12; rest=351' \
                       --number 232 \
                       --constraints 'town <= 2' \
                       --float
0.00093That's roughly 1 in 10000.Or suppose we're designing aMagic: The Gatheringdeck:A deck of 60 cards contains13mountain cards and12swamp cards. What is the probability that we draw7cards and getbetween 1 and 3 mountainsandexactly 2 swamps?Using ccc, we can write these constraints very easily:$ ccc prob draw --from 'mountain=13; swamp=12; rest=35' \
                --number 7 \
                --constraints '1 <= mountain <= 3, swamp == 2' \
                --float
0.23264About 23%.These types of problem are common in everyday life but, depending on how familiar you are with statistics or combinatorics, they can be hard to calculate or even estimate accurately.See below for numerous other examples and explanations.InstallInstallation requires Python 3.6 or newer.pip install ccc-calculatorDevelopment installClone the repo, create a virtual environment, install the development dependencies, install the module in editable mode, install pre-commit hooks:git clone https://github.com/ajcr/ccc
cd ccc
python -m venv venv
. venv/bin/activate
python -m pip requirements-dev.txt
python -m pip install -e .
pre-commit installUnit tests can be run withpytest. Pre-commit hooks will run on each git commit. The hooks can also be invoked withpre-commit run -a.More examplesThe easiest way to introduce ccc is to show various example calculations from the command-line.ccc is always invoked in the following way:ccc [computation-type] [collection-type] [--args]In all of the examples below, notice how easy it is to express constraints that would otherwise necessitate writing complicated code, or performing repetitive arithmetic on paper.Drawsccc can tell you the probability of selecting particular counts of items from a collection (either with or without replacement).Here is an example:A bag contains:3 red marbles (:red_circle::red_circle::red_circle:)5 black marbles (:black_circle::black_circle::black_circle::black_circle::black_circle:)7 blue marbles (:large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle::large_blue_circle:)You must draw at random (and without replacement) 4 of these marbles. You lose if you draw 1 or more of the blue marbles (:blue_circle:).What is your probability of winning?To solve this with ccc we can easily specify thecollectionwe draw from, thesizeof the draw we make, and anyconstraintson the draw:ccc probability draw --from "red=3; black=5; blue=5" \
                     --number 4 \
                     --constraints "blue == 0"This puts the probability of winning (not drawing a blue marble) at2/39, so perhaps we'd win once every 19 or so attempts.Notice how easy it is to specify the constraints. Just the item's name (blue) and its desired count (0). Any comparison operators can be used (==,!=,<,<=,>,>=).If we wanted to allow a marble to be replaced after each draw, we would add the--replaceflag.We can specify more complicated constraints on what we want to draw from the bag:This time, draw5marbles without replacement from the same collection, but with 2 white marbles (:white_circle::white_circle:)  added.You win a toy if you draw a collection which comprisesboth white marbles (:white_circle::white_circle:),andat least 1 black marble (:black_circle:+)What is you chance of winning the toy?ccc probability draw --from "red=3; black=5; blue=7; white=2" \
                     --number 5 \
                     --constraints "white == 2, black >= 1"Our chance of winning is3/119according to ccc, so we would expect to win around once every 40 attempts.You can see that to express multiple constraints on our draw, we simply used commas,to separate them.Lastly, it is possible to useor(any number of times) in constraints:Draw3marbles such that you get:3 red marbles (:red_circle::red_circle::red_circle:)or1 white, 1 black and 1 blue marble (:white_circle::black_circle::large_blue_circle:)What is the probability of succeeding now?ccc probability draw --from "red=3; black=5; blue=7; white=2" \
                     --number 3 \
                     --constraints "red == 3 or (white == 1, black == 1, blue == 1)"The probability is0.1.MultisetsMultisets are unordered collections (like sets) in which an item may appear multiple times.How many ways can we gather up 20 pieces of fruit such that we have:fewer than 10 apples (:green_apple:),andat least 5 bananas (:banana:),andthe number of grapes is not 13 (:grapes:),andthere are are even number of strawberries (:strawberry:)?ccc count multisets --size 20 \
                    --constraints 'apples < 10, bananas >= 5, grapes != 13, strawberries % 2 == 0'The answer is406.The modulo (%) operator used above provides the facility to solve coin change problems, for example:The UK currency has the following coins:1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p)How many different ways can £5 be made using any number of coins?cf.Project Euler problem 31ccc count multisets --constraints 'a%1==0, b%2==0, c%5==0, d%10==0, e%20==0, f%50==0, g%100==0, h%200==0' \
                    --size 500The answer is computed within a couple of seconds as6,295,434different ways.We could also put additional contraints on the coins, such as restricting the number of times a coin may be used.PermutationsPermutations of words can be counted as follows:ccc count permutations MISSISSIPPIThere are34650unique permutations of this famous river/state.What about if we only count permutations where instances of the same letter are not adjacent to each other?ccc count permutations MISSISSIPPI --constraints no_adjacentUsing the 'no_adjacent' constraint, the answer comes back immediately as2016. The speed of this calculation is thanks to the use ofGeneralised Larguerre Polynomials.We can also treat the letters as distinguishable (the--same-distinctflag) to solve a related type of problem:Five couples are to seated in a row. How many ways can they be seated such no couples are seated together?ccc count permutations AABBCCDDEE --constraints no_adjacent --same-distinct1,263,360ways.ccc allows derangements (permutations where no item occupies its original place) to be counted.Five couples draw names from a hat. What is the probability that nobody draws either their own name, or the name of their partner?ccc probability permutation AABBCCDDEE --constraints derangement --floatIt turns out that there is only a0.121probability of this occurring.SequencesSequences are ordered collections of items.How many 30 letter sequences can you make using no more than 20 each ofA,BandC?ccc count sequences --size 30 --constraints 'A <= 20, B <= 20, C <= 20'The answer is a lot: there are205,863,750,414,990such sequences.