diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-05 07:28:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-05 07:28:51 +0000 |
| commit | 6b9a71d1298e2a3fa332ab7494f8caf4fd476e5e (patch) | |
| tree | dde1cd725a682e08356849f053dc9eb0d51105c9 | |
| parent | 3d9e194aa64089a27cc11dcb5d63fb2c3ceb3442 (diff) | |
| parent | 0c6900967ae431384adaa0c31b64f459d47dbfb8 (diff) | |
| download | perlweeklychallenge-club-6b9a71d1298e2a3fa332ab7494f8caf4fd476e5e.tar.gz perlweeklychallenge-club-6b9a71d1298e2a3fa332ab7494f8caf4fd476e5e.tar.bz2 perlweeklychallenge-club-6b9a71d1298e2a3fa332ab7494f8caf4fd476e5e.zip | |
Merge pull request #3455 from pkmnx/branch-for-challenge-98
challenge 98, no.'s 1 & 2, in raku
| -rwxr-xr-x | challenge-098/pkmnx/raku/ch-1.raku | 35 | ||||
| -rwxr-xr-x | challenge-098/pkmnx/raku/ch-2.raku | 34 |
2 files changed, 69 insertions, 0 deletions
diff --git a/challenge-098/pkmnx/raku/ch-1.raku b/challenge-098/pkmnx/raku/ch-1.raku new file mode 100755 index 0000000000..78b18663ab --- /dev/null +++ b/challenge-098/pkmnx/raku/ch-1.raku @@ -0,0 +1,35 @@ +#!/usr/bin/env raku + + +sub MAIN() { + + my $fi = "input.txt"; + readN( $fi, 4).say; + readN( $fi, 4).say; + readN( $fi, 4).say; + readN( $fi, 0); + readN( $fi, 4).say; + readN( $fi, 4).say; + +} + +sub readN($FILE, $number) { + if ( $number == 0 ) { + GLOBAL::<$FILE> = 0 if defined GLOBAL::<$FILE>; + } else { + GLOBAL::<$FILE> = 0 if ! defined GLOBAL::<$FILE>; + my $ch = ""; + if $FILE.IO.e { + given $FILE.IO.open { + .seek: GLOBAL::<$FILE>, SeekFromBeginning; + $ch = .readchars: $number; + .close; + } + } else { + die ("$FILE does not exist.") + } + $ch.chomp; + GLOBAL::<$FILE> += $ch.chars; + $ch; + } +} diff --git a/challenge-098/pkmnx/raku/ch-2.raku b/challenge-098/pkmnx/raku/ch-2.raku new file mode 100755 index 0000000000..33cbc8f441 --- /dev/null +++ b/challenge-098/pkmnx/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku + +# usage: change @N & $N accordingly. + +sub MAIN() { + + my @N = (11, 13, 15, 17); + my $N = 19; + + my $indx = binsrch( @N, $N ); + my $vl = @N[$indx]; + + printf( "Input: \@N = (%s) and \$N = %d\n", @N.join(", "), $N ); + + if ( $N == $vl ) { + say "Output: $indx since the target $N is in the array at the index $indx." + } else { + $indx++ if $N > $vl; + say "Output: $indx since the target $N is missing and should be placed at the index $indx." + } + +} + +sub binsrch( @N, $N ) { + my ( $l, $r, $m ) = ( 0, @N.elems -1, -1 ); + while ( $l <= $r ) { + $m = Int( ($l +$r) / 2); + my $vl = @N[$m]; + if ( $vl < $N ) { $l = $m +1 } + elsif ( $vl > $N ) { $r = $m -1 } + else { return $m } + } + $m +} |
