Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code for P46 of 99 problems #64

Open
veb7vmehra opened this issue Dec 21, 2019 · 7 comments
Open

Add code for P46 of 99 problems #64

veb7vmehra opened this issue Dec 21, 2019 · 7 comments

Comments

@veb7vmehra
Copy link

No description provided.

veb7vmehra added a commit to veb7vmehra/perl6-examples that referenced this issue Dec 21, 2019
@JJ
Copy link
Contributor

JJ commented Dec 21, 2019

Can you please link the original problem?

@Altai-man
Copy link
Member

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 #65

@veb7vmehra
Copy link
Author

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

@Altai-man
Copy link
Member

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
Copy link
Member

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
Copy link
Author

@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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants