examples icon indicating copy to clipboard operation
examples copied to clipboard

Add code for P46 of 99 problems

Open veb7vmehra opened this issue 5 years ago • 7 comments

veb7vmehra avatar Dec 21 '19 08:12 veb7vmehra

Can you please link the original problem?

JJ avatar Dec 21 '19 08:12 JJ

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

Altai-man avatar Dec 21 '19 10:12 Altai-man

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

veb7vmehra avatar Dec 21 '19 13:12 veb7vmehra

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 avatar Dec 21 '19 16:12 Altai-man

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)

Altai-man avatar Dec 22 '19 18:12 Altai-man

@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?

veb7vmehra avatar Dec 24 '19 04:12 veb7vmehra

@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.

Altai-man avatar Dec 24 '19 13:12 Altai-man