aboutsummaryrefslogtreecommitdiff
path: root/challenge-095
diff options
context:
space:
mode:
authormimosinnet <mimosinnet@gmail.com>2021-01-17 20:51:42 +0100
committermimosinnet <mimosinnet@gmail.com>2021-01-17 20:51:42 +0100
commit4dad7413508ff712a5d5ad2a9d4cdf50e6e4ad18 (patch)
tree646d3571e7c2c18a1215ee6efa13bc2526809aa4 /challenge-095
parent726a600906236574eef22e7dd2c27f92aa620c47 (diff)
downloadperlweeklychallenge-club-4dad7413508ff712a5d5ad2a9d4cdf50e6e4ad18.tar.gz
perlweeklychallenge-club-4dad7413508ff712a5d5ad2a9d4cdf50e6e4ad18.tar.bz2
perlweeklychallenge-club-4dad7413508ff712a5d5ad2a9d4cdf50e6e4ad18.zip
Solution for challenge-095
Diffstat (limited to 'challenge-095')
-rw-r--r--challenge-095/mimosinnet/raku/ch-1.raku60
-rw-r--r--challenge-095/mimosinnet/raku/ch-2.raku58
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-095/mimosinnet/raku/ch-1.raku b/challenge-095/mimosinnet/raku/ch-1.raku
new file mode 100644
index 0000000000..6e65e455f6
--- /dev/null
+++ b/challenge-095/mimosinnet/raku/ch-1.raku
@@ -0,0 +1,60 @@
+=begin comment
+Perl Weekly Challenge
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/
+
+095-1: Palindrome Number
+
+You are given a number $N.
+
+Write a script to figure out if the given number is Palindrome. Print 1 if true otherwise 0.
+
+Example 1:
+Input: 1221
+Output: 1
+
+Example 2:
+Input: -101
+Output: 0, since -101 and 101- are not the same.
+
+Example 3:
+Input: 90
+Output: 0
+
+=end comment
+
+class Palindrome {
+ has Int $.num;
+
+ method is_palindrome() {
+ return 0 unless $.num.chars %% 2;
+ return 0 if $.num < 0;
+ my @digits = $!num.Str.split('');
+
+ while @digits.elems {
+ return 0 if @digits.shift ne @digits.pop;
+ }
+ return 1;
+ }
+}
+
+multi sub MAIN( Int $num ) {
+ say "\nInput: " ~ $num;
+ say 'Output: ' ~ Palindrome.new( num => $num ).is_palindrome;
+}
+
+multi sub MAIN( 'challenge' ) {
+ <1221 -101 90>.map: { MAIN( .Int ) };
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my %test = 1221 => 1, -101 => 0, 90 => 0, 123321 => 1, 10100101 => 1 ;
+ for %test.keys {
+ is(
+ Palindrome.new( num => $_.Int ).is_palindrome,
+ %test{$_}
+ );
+ }
+
+}
diff --git a/challenge-095/mimosinnet/raku/ch-2.raku b/challenge-095/mimosinnet/raku/ch-2.raku
new file mode 100644
index 0000000000..0544da2424
--- /dev/null
+++ b/challenge-095/mimosinnet/raku/ch-2.raku
@@ -0,0 +1,58 @@
+=begin comment
+Perl Weekly Challenge
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/
+
+095-2 Demo Stack
+
+Write a script to demonstrate Stack operations like below:
+
+push($n) - add $n to the stack
+pop() - remove the top element
+top() - get the top element
+min() - return the minimum element
+
+Example:
+
+my $stack = Stack->new;
+$stack->push(2);
+$stack->push(-1);
+$stack->push(0);
+$stack->pop; # removes 0
+print $stack->top; # prints -1
+$stack->push(0);
+print $stack->min; # prints -1
+
+=end comment
+
+class Stack {
+ has @.stack;
+
+ method push( $n ) { @.stack.push: $n }
+ method pop() { @!stack.pop }
+ method top() { @!stack[@!stack.elems -1] }
+ method min() { @!stack.min }
+}
+
+multi sub MAIN( 'challenge' ) {
+ my $stack = Stack.new;
+ $stack.push(2);
+ $stack.push(-1);
+ $stack.push(0);
+ $stack.pop;
+ $stack.top.say;
+ $stack.push(0);
+ $stack.min.say;
+}
+
+multi sub MAIN( 'test' ) {
+ use Test;
+
+ my $stack = Stack.new;
+ $stack.push(2);
+ $stack.push(-1);
+ $stack.push(0);
+ $stack.pop;
+ is($stack.top, -1);
+ $stack.push(0);
+ is($stack.min, -1);
+}