Raku / examples

Many examples of Raku code
https://examples.raku.org/
Artistic License 2.0
294 stars 89 forks source link

Add code for P46 of 99 problems #64

Open veb7vmehra opened 4 years ago

JJ commented 4 years ago

Can you please link the original problem?

Altai-man commented 4 years ago

The description of the task can be found at e.g. https://wiki.haskell.org/99_questions/46_to_50 ~But we have a lot of missing entries from 99 problems list, why 46 is special?~ Apparently, the code proposed at https://github.com/perl6/perl6-examples/pull/65

veb7vmehra commented 4 years ago

@JJ and @Altai-man I'll work on that.

Altai-man commented 4 years ago

Here's the quickly whipped up solution without redefining and, or etc operators:

table(-> $a, $b { $a and ($a or $b) });

sub table(&callable) {
    say "{$_.join("\t")}\t{&callable(|$_)}" for [X] [True, False] xx &callable.arity;
}
➜  ~ perl6 perl6.p6
True    True    True
True    False   True
False   True    False
False   False   False

What can I say, Raku is cool. This is a very golfed version to solve it in a single line, for an example variable names are necessary...

Altai-man commented 4 years ago

Naive solution for problem 49:

(**) Gray codes.

An n-bit Gray code is a sequence of n-bit strings constructed according to certain rules. For example,

n = 1: C(1) = ['0','1'].
n = 2: C(2) = ['00','01','11','10'].
n = 3: C(3) = ['000','001','011','010',´110´,´111´,´101´,´100´].

Find out the construction rules and write a predicate with the following specification: 
> sub gray($x) { say ([X] <0 1> xx $x).map(*.join) }
&gray
> gray(3)
(000 001 010 011 100 101 110 111)
veb7vmehra commented 4 years ago

@Altai-man I will try to solve P46 in the manner you have told(Thanks for your guidance). Should I have to do the same with P49?

Altai-man commented 4 years ago

@veb7vmehra I think you can use my solution as a base and make it easier to understand, splitting it over a couple of lines of code, with proper variables and so on.