How to resolve the algorithm Combinations step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Combinations step by step in the Quackery programming language

Table of Contents

Problem Statement

Given non-negative integers   m   and   n,   generate all size   m   combinations   of the integers from   0   (zero)   to   n-1   in sorted order   (each combination is sorted and the entire table is sorted).

3   comb   5     is: If it is more "natural" in your language to start counting from   1   (unity) instead of   0   (zero), the combinations can be of the integers from   1   to   n.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Combinations step by step in the Quackery programming language

Source code in the quackery programming language

  [ 0 swap
    [ dup 0 != while
      dup 1 & if
        [ dip 1+ ]
      1 >> again ]
    drop ]                     is bits      (   n --> n )

  [ [] unrot
    bit times
      [ i bits
        over = if
          [ dip
              [ i join ] ] ]
    drop ]                     is combnums  ( n n --> [ )

  [ [] 0 rot
    [ dup 0 != while
      dup 1 & if
        [ dip
            [ dup dip join ] ]
       dip 1+
       1 >>
       again ]
    2drop ]                    is makecomb  (   n --> [ )

  [ over 0 = iff
      [ 2drop [] ] done
    combnums
    [] swap witheach
       [ makecomb
         nested join ] ]       is comb      ( n n --> [ )

  [ behead swap witheach max ] is largest   (   [ --> n )

  [ 0 rot witheach
      [ [ dip [ over * ] ] + ]
    nip ]                      is comborder ( [ n --> n )

  [ dup [] != while
    sortwith
      [ 2dup join
        largest 1+ dup dip
          [ comborder swap ]
        comborder < ] ]        is sortcombs (   [ --> [ )

  3 5 comb
  sortcombs
  witheach [ witheach [ echo sp ] cr ]

  [ stack ]                              is comb.stack
  [ stack ]                              is comb.items
  [ stack ]                              is comb.required
  [ stack ]                              is comb.result

  [ 1 - comb.items put
    1+ comb.required put
    0 comb.stack put
    [] comb.result put
    [ comb.required share
      comb.stack size = if
        [ comb.result take
          comb.stack behead 
          drop nested join
          comb.result put ]
      comb.stack take
      dup comb.items share
      = iff
          [ drop
            comb.stack size 1 > iff
              [ 1 comb.stack tally ] ]
            else
              [ dup comb.stack put
                1+ comb.stack put ]
             comb.stack size 1 = until ]
    comb.items release
    comb.required release
    comb.result take ]                   is comb ( n n --> )

  3 5 comb
  witheach [ witheach [ echo sp ] cr ]

  [ dup size dip
      [ witheach
          [ over swap peek swap ] ]
      nip pack ]                    is arrange ( [ [ --> [ )

  ' [ 10 20 30 40 50 ]
  3 5 comb
  witheach  
    [ dip dup arrange
      witheach [ echo sp ] 
      cr ]
  drop 
  cr
  $ "zero one two three four" nest$
  ' [ 4 3 1 0 1 4 3 ]  arrange 
  witheach [ echo$ sp ]

  

You may also check:How to resolve the algorithm Babbage problem step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the Wart programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the OCaml programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the OCaml programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the ALGOL 68 programming language