diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-01-10 14:57:56 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-01-10 14:57:56 +0000 |
| commit | 612d4c4f6c947d4aa461b71402a8fc87855bab54 (patch) | |
| tree | f9255c46ac7a08712c398764321d3faa79a8b552 /challenge-042 | |
| parent | 4ea3daa6928c0a87d24f29bbc17fe9deb276694e (diff) | |
| download | perlweeklychallenge-club-612d4c4f6c947d4aa461b71402a8fc87855bab54.tar.gz perlweeklychallenge-club-612d4c4f6c947d4aa461b71402a8fc87855bab54.tar.bz2 perlweeklychallenge-club-612d4c4f6c947d4aa461b71402a8fc87855bab54.zip | |
- Added solutions by Kevin Colyer.
Diffstat (limited to 'challenge-042')
| -rw-r--r-- | challenge-042/kevin-colyer/raku/ch-1.p6 | 39 | ||||
| -rw-r--r-- | challenge-042/kevin-colyer/raku/ch-2.p6 | 59 |
2 files changed, 98 insertions, 0 deletions
diff --git a/challenge-042/kevin-colyer/raku/ch-1.p6 b/challenge-042/kevin-colyer/raku/ch-1.p6 new file mode 100644 index 0000000000..c7f2649e99 --- /dev/null +++ b/challenge-042/kevin-colyer/raku/ch-1.p6 @@ -0,0 +1,39 @@ +#!/usr/bin/perl6 +use v6; + +use Test; + + +=begin pod + +TASK #1 +Octal Number System +Write a script to print decimal number 0 to 50 in Octal Number System. + +For example: + +Decimal 0 = Octal 0 +Decimal 1 = Octal 1 +Decimal 2 = Octal 2 +Decimal 3 = Octal 3 +Decimal 4 = Octal 4 +Decimal 5 = Octal 5 +Decimal 6 = Octal 6 +Decimal 7 = Octal 7 +Decimal 8 = Octal 10 +and so on. + +=end pod + +# Roll my own version because? Why not! +sub dec-to-oct(Int $n is copy) returns Str { + return "0" if $n==0; + my Str $o = ""; + while $n>0 { + $o= $n +& 7 ~ $o; # add lower three bits + $n= $n +> 3 ; # shift off lower three bits + } + return $o; +} + +say "Decimal $_ = Octal {dec-to-oct($_)} is sprintf conversion {sprintf "%o", $_}" for ^51; diff --git a/challenge-042/kevin-colyer/raku/ch-2.p6 b/challenge-042/kevin-colyer/raku/ch-2.p6 new file mode 100644 index 0000000000..70f471113c --- /dev/null +++ b/challenge-042/kevin-colyer/raku/ch-2.p6 @@ -0,0 +1,59 @@ +#!/usr/bin/perl6 +use v6; + +use Test; + + +=begin pod + +TASK #2 + +Balanced Brackets +Write a script to generate a string with random number of ( and ) brackets. Then make the script validate the string if it has balanced brackets. + +For example: + +() - OK +(()) - OK +)( - NOT OK +())() - NOT OK + +=end pod + +sub match-brackets(Str $t) { + # can never match condition + return False if $t.chars < 2; + + # loop counting +1 for open -1 for close. + + # zero sum is matching + # positive sum is non matching + # negative is always non matching (and quick exit) + my $open=0; + for ^$t.chars -> $i { + $open++ if $t.substr($i,1) eq '('; + $open-- if $t.substr($i,1) eq ')'; + return False if $open < 0; + } + return $open == 0 ?? True !! False ; +} + +multi MAIN("test") { + is match-brackets("()") ,True ,"test matching"; + is match-brackets(")(") ,False,"test never matching"; + is match-brackets("()(") ,False,"test odd open not matching"; + is match-brackets("())") ,False,"test odd close not matching"; + is match-brackets("((())())"),True ,"test nested matching"; + is match-brackets("((())))") ,False,"test nested not matching"; + is match-brackets("(a(b(c)d)e(f)g)h"),True ,"test nested matching with alternate chars"; + done-testing; +} + +multi MAIN() { + for ^20 -> $i { + my Str $c; + my $j=1+(^5).roll; + $c~= ('(',')').pick for ^2*$j; + say (match-brackets($c) ?? " Matching: " !! "Not-Matching: ") ~ $c; + } +} |
