aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-02 12:44:35 +0000
committerGitHub <noreply@github.com>2021-02-02 12:44:35 +0000
commit9ab5090416bbf115a05528619dacc713ff079f40 (patch)
treed465177a7b14c7fd4d3a72ed9a1f2babacb186b1
parent75d1f0c28e61764946a008a925742e605e3d25cd (diff)
parent7bfd5dce6a6efb34f5d8b632c34a68a7deb7f7eb (diff)
downloadperlweeklychallenge-club-9ab5090416bbf115a05528619dacc713ff079f40.tar.gz
perlweeklychallenge-club-9ab5090416bbf115a05528619dacc713ff079f40.tar.bz2
perlweeklychallenge-club-9ab5090416bbf115a05528619dacc713ff079f40.zip
Merge pull request #3448 from andinus/master
Add Raku solution for challenge-098, challenge-090: fix file path
-rw-r--r--challenge-090/andinus/README2
-rw-r--r--challenge-090/andinus/README.org2
-rw-r--r--challenge-098/andinus/README83
-rw-r--r--challenge-098/andinus/README.org78
-rw-r--r--challenge-098/andinus/blog-1.txt1
-rwxr-xr-xchallenge-098/andinus/raku/ch-1.raku19
6 files changed, 66 insertions, 119 deletions
diff --git a/challenge-090/andinus/README b/challenge-090/andinus/README
index b662f1cb50..3b627c71f9 100644
--- a/challenge-090/andinus/README
+++ b/challenge-090/andinus/README
@@ -33,7 +33,7 @@ Table of Contents
1.1 Raku
────────
- • Program: <file:perl/ch-2.raku>
+ • Program: <file:raku/ch-2.raku>
Start by taking `$A' & `$B' which are defined to be `Int' & positive.
┌────
diff --git a/challenge-090/andinus/README.org b/challenge-090/andinus/README.org
index c4f5da3378..8ebbde0e84 100644
--- a/challenge-090/andinus/README.org
+++ b/challenge-090/andinus/README.org
@@ -11,7 +11,7 @@ You are given two positive numbers $A and $B.
Write a script to demonstrate [[https://threesixty360.wordpress.com/2009/06/09/ethiopian-multiplication/][Ethiopian Multiplication]] using the given
numbers.
** Raku
-- Program: [[file:perl/ch-2.raku]]
+- Program: [[file:raku/ch-2.raku]]
Start by taking =$A= & =$B= which are defined to be =Int= & positive.
#+BEGIN_SRC raku
diff --git a/challenge-098/andinus/README b/challenge-098/andinus/README
index b662f1cb50..98fe099f22 100644
--- a/challenge-098/andinus/README
+++ b/challenge-098/andinus/README
@@ -1,95 +1,58 @@
━━━━━━━━━━━━━━━
- CHALLENGE 090
+ CHALLENGE 098
Andinus
━━━━━━━━━━━━━━━
- 2020-12-08
+ 2021-02-02
Table of Contents
─────────────────
-1. Task 2 - Ethiopian Multiplication
+1. Task 1 - Read N-characters
.. 1. Raku
-1 Task 2 - Ethiopian Multiplication
-═══════════════════════════════════
+1 Task 1 - Read N-characters
+════════════════════════════
- You are given two positive numbers $A and $B.
+ You are given file `$FILE'.
- Write a script to demonstrate [Ethiopian Multiplication] using the
- given numbers.
+ Create subroutine `readN($FILE, $number)' returns the first
+ n-characters and moves the pointer to the `(n+1)th' character.
-[Ethiopian Multiplication]
-<https://threesixty360.wordpress.com/2009/06/09/ethiopian-multiplication/>
-
1.1 Raku
────────
- • Program: <file:perl/ch-2.raku>
+ • Program: <file:raku/ch-1.raku>
- Start by taking `$A' & `$B' which are defined to be `Int' & positive.
+ `readN' is defined as such:
┌────
- │ sub MAIN (
- │ #= positive numbers
- │ Int $A is copy where * > 0,
- │ Int $B is copy where * > 0
+ │ sub readN (
+ │ IO $file, Int $chars --> Str
│ ) {
│ ...
│ }
└────
- Here's relevant part from the link that was given above:
- Start with the two numbers on top. Halve one, ignoring any
- remainders or fractions, and double the other, stopping
- when you get to 1.
-
- 14 & 12 7 & 24 3 & 48 [See how I ignored the fact that
- halving 7 leaves 1 left over?] 1 & 96 <— Stop here.
-
- Now look at the numbers on the right. Some are across from
- an even number: in this case, 12 is across from the
- original 14. Ignore those, and add the rest. So we’ll add
- 24, 48, and 96, which were across from odd numbers, and
- get 168. And that’s the product! Isn’t that cool?
+ The pointer index is stored in a state array (`%pointers'). It's
+ stores the pointer separately for each file. It's initialized with 0.
+ ┌────
+ │ # %pointers stores the pointer index.
+ │ state Int %pointers;
+ │ %pointers{$file} = 0 without %pointers{$file};
+ └────
- We do the same thing & also print the instructions.
+ The pointer is updated & required string is returned.
┌────
- │ my %sets;
- │
- │ say "Ethopian Multiplication.\n";
- │ say "Start with $A, $B.";
- │ say "Divide $A by 2 & multiple $B by 2 at each step.";
- │ say "Continue until $A equals 1. Drop the remainder, both should be Integer.\n";
- │
- │ say "$A, $B";
- │ while True {
- │ %sets{$A} = $B.Int;
- │ $A = ($A / 2).Int;
- │ $B = ($B * 2).Int;
- │ last if $A < 1;
- │ say "$A, $B";
- │ }
- │
- │ say "\nNow to find the product, simply add all the numbers on right side of ','.";
- │ say "But skip those numbers which have an even number on the left side.\n";
- │
- │ my Int $product = 0;
- │ for %sets.sort({.key.Int}).reverse -> $pair {
- │ if $pair.key % 2 != 0 {
- │ $product += $pair.value;
- │ say "- Adding ", $pair.value, " to product.";
- │ } else {
- │ say "- Skipping ", $pair.value, " because ", $pair.key, " is even.";
- │ }
+ │ with %pointers{$file} -> $idx {
+ │ %pointers{$file} += $chars;
+ │ return $file.slurp.substr($idx, $chars);
│ }
- │
- │ say "\nProduct: $product";
└────
diff --git a/challenge-098/andinus/README.org b/challenge-098/andinus/README.org
index c4f5da3378..353698db4f 100644
--- a/challenge-098/andinus/README.org
+++ b/challenge-098/andinus/README.org
@@ -2,74 +2,38 @@
#+HTML_LINK_UP: ../index.html
#+OPTIONS: toc:2
#+EXPORT_FILE_NAME: index
-#+DATE: 2020-12-08
-#+TITLE: Challenge 090
+#+DATE: 2021-02-02
+#+TITLE: Challenge 098
-* Task 2 - Ethiopian Multiplication
-You are given two positive numbers $A and $B.
+* Task 1 - Read N-characters
+You are given file =$FILE=.
-Write a script to demonstrate [[https://threesixty360.wordpress.com/2009/06/09/ethiopian-multiplication/][Ethiopian Multiplication]] using the given
-numbers.
+Create subroutine =readN($FILE, $number)= returns the first n-characters
+and moves the pointer to the =(n+1)th= character.
** Raku
-- Program: [[file:perl/ch-2.raku]]
+- Program: [[file:raku/ch-1.raku]]
-Start by taking =$A= & =$B= which are defined to be =Int= & positive.
+=readN= is defined as such:
#+BEGIN_SRC raku
-sub MAIN (
- #= positive numbers
- Int $A is copy where * > 0,
- Int $B is copy where * > 0
+sub readN (
+ IO $file, Int $chars --> Str
) {
...
}
#+END_SRC
-Here's relevant part from the link that was given above:
-#+BEGIN_QUOTE
-Start with the two numbers on top. Halve one, ignoring any remainders or
-fractions, and double the other, stopping when you get to 1.
-
-14 & 12
-7 & 24
-3 & 48 [See how I ignored the fact that halving 7 leaves 1 left over?]
-1 & 96 <— Stop here.
-
-Now look at the numbers on the right. Some are across from an even
-number: in this case, 12 is across from the original 14. Ignore those,
-and add the rest. So we’ll add 24, 48, and 96, which were across from
-odd numbers, and get 168. And that’s the product! Isn’t that cool?
-#+END_QUOTE
-
-We do the same thing & also print the instructions.
+The pointer index is stored in a state array (=%pointers=). It's stores
+the pointer separately for each file. It's initialized with 0.
#+BEGIN_SRC raku
-my %sets;
-
-say "Ethopian Multiplication.\n";
-say "Start with $A, $B.";
-say "Divide $A by 2 & multiple $B by 2 at each step.";
-say "Continue until $A equals 1. Drop the remainder, both should be Integer.\n";
-
-say "$A, $B";
-while True {
- %sets{$A} = $B.Int;
- $A = ($A / 2).Int;
- $B = ($B * 2).Int;
- last if $A < 1;
- say "$A, $B";
-}
-
-say "\nNow to find the product, simply add all the numbers on right side of ','.";
-say "But skip those numbers which have an even number on the left side.\n";
+# %pointers stores the pointer index.
+state Int %pointers;
+%pointers{$file} = 0 without %pointers{$file};
+#+END_SRC
-my Int $product = 0;
-for %sets.sort({.key.Int}).reverse -> $pair {
- if $pair.key % 2 != 0 {
- $product += $pair.value;
- say "- Adding ", $pair.value, " to product.";
- } else {
- say "- Skipping ", $pair.value, " because ", $pair.key, " is even.";
- }
+The pointer is updated & required string is returned.
+#+BEGIN_SRC raku
+with %pointers{$file} -> $idx {
+ %pointers{$file} += $chars;
+ return $file.slurp.substr($idx, $chars);
}
-
-say "\nProduct: $product";
#+END_SRC
diff --git a/challenge-098/andinus/blog-1.txt b/challenge-098/andinus/blog-1.txt
new file mode 100644
index 0000000000..699ade364d
--- /dev/null
+++ b/challenge-098/andinus/blog-1.txt
@@ -0,0 +1 @@
+https://andinus.tilde.institute/pwc/challenge-098/
diff --git a/challenge-098/andinus/raku/ch-1.raku b/challenge-098/andinus/raku/ch-1.raku
new file mode 100755
index 0000000000..3ebcf32c9e
--- /dev/null
+++ b/challenge-098/andinus/raku/ch-1.raku
@@ -0,0 +1,19 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+# readN takes a file & returns the first `$chars' characters if called
+# the first time. It stores the pointer index & returns characters
+# from `$idx' to `$idx + $chars'.
+sub readN (
+ IO $file, Int $chars --> Str
+) {
+ # %pointers stores the pointer index.
+ state Int %pointers;
+ %pointers{$file} = 0 without %pointers{$file};
+
+ with %pointers{$file} -> $idx {
+ %pointers{$file} += $chars;
+ return $file.slurp.substr($idx, $chars);
+ }
+}