aboutsummaryrefslogtreecommitdiff
path: root/challenge-122/andinus/README
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-07-27 11:09:20 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-07-27 11:09:20 +0800
commit2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af (patch)
treea2b64a6a110d201ceed00119701e94e5dc7c965f /challenge-122/andinus/README
parentf4e18fcb93b12bfc825272d2800e63ad4e56b140 (diff)
parent4231c2f762b397e1cacd2cb7e3c2799254fcc1a4 (diff)
downloadperlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.tar.gz
perlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.tar.bz2
perlweeklychallenge-club-2c7cef286eb7a72c36c28f8fa4c5a2a88b76e4af.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-122/andinus/README')
-rw-r--r--challenge-122/andinus/README163
1 files changed, 142 insertions, 21 deletions
diff --git a/challenge-122/andinus/README b/challenge-122/andinus/README
index 369b209e92..796b1d2086 100644
--- a/challenge-122/andinus/README
+++ b/challenge-122/andinus/README
@@ -1,39 +1,122 @@
━━━━━━━━━━━━━━━
- CHALLENGE 115
+ CHALLENGE 122
Andinus
━━━━━━━━━━━━━━━
- 2021-06-02
+ 2021-07-21
Table of Contents
─────────────────
-Task 2 - Largest Multiple
+Task 1 - Average of Stream
+Task 2 - Basketball Points
-Task 2 - Largest Multiple
-═════════════════════════
+Task 1 - Average of Stream
+══════════════════════════
- You are given a list of positive integers (0-9), single digit.
+ You are given a stream of numbers, `@N'.
- Write a script to find the largest multiple of 2 that can be formed
- from the list.
+ Write a script to print the average of the stream at every point.
+ Example:
┌────
- │ Input: @N = (1, 0, 2, 6)
- │ Output: 6210
+ │ Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...)
+ │ Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ...
- │ Input: @N = (1, 4, 2, 8)
- │ Output: 8412
+ │ Average of first number is 10.
+ │ Average of first 2 numbers (10+20)/2 = 15
+ │ Average of first 3 numbers (10+20+30)/3 = 20
+ │ Average of first 4 numbers (10+20+30+40)/4 = 25 and so on.
+ └────
+
+
+Raku
+────
+
+ • Program: <file:raku/ch-1.raku>
+
+ The subroutine `avg' takes a list of numbers and returns their
+ average. We just loop over keys of `@nums' and print the average upto
+ each point.
+
+ ┌────
+ │ #| return average of lists.
+ │ sub avg(*@list) { (sum @list) / @list.elems; }
+ │
+ │ #| average of stream at every point.
+ │ sub MAIN(*@nums where {$_.all ~~ Int}) {
+ │ put @nums.keys.map({avg @nums[0..$_]});
+ │ }
+ └────
+
+
+C
+─
+
+ • Program: <file:c/ch-1.c>
+
+ `argv' holds the input & `argc' holds the number of inputs. We loop
+ over `argv' and convert each input to an integer and add it to `sum'
+ which holds the sum of inputs upto that point and print `sum / idx',
+ `idx' being the index of input.
+
+ ┌────
+ │ long sum = 0;
+ │ for (int idx = 1; idx < argc; idx++) {
+ │ int num;
+ │ const char *errstr;
+ │ num = strtonum(argv[idx], INT_MIN, INT_MAX, &errstr);
+ │ if (errstr != NULL)
+ │ errx(1, "number is %s: %s", errstr, argv[idx]);
+ │
+ │ sum += num;
+ │ printf("%ld ", sum / idx);
+ │ }
+ │ printf("\n");
+ └────
+
+
+Task 2 - Basketball Points
+══════════════════════════
+
+ You are given a score `$S'.
+
+ You can win basketball points e.g. 1 point, 2 points and 3 points.
+
+ Write a script to find out the different ways you can score `$S'.
+
+ Example:
+ ┌────
+ │ Input: $S = 4
+ │ Output: 1 1 1 1
+ │ 1 1 2
+ │ 1 2 1
+ │ 1 3
+ │ 2 1 1
+ │ 2 2
+ │ 3 1
- │ Input: @N = (4, 1, 7, 6)
- │ Output: 7614
+ │ Input: $S = 5
+ │ Output: 1 1 1 1 1
+ │ 1 1 1 2
+ │ 1 1 2 1
+ │ 1 1 3
+ │ 1 2 1 1
+ │ 1 2 2
+ │ 1 3 1
+ │ 2 1 1 1
+ │ 2 1 2
+ │ 2 2 1
+ │ 2 3
+ │ 3 1 1
+ │ 3 2
└────
@@ -42,14 +125,52 @@ Raku
• Program: <file:raku/ch-2.raku>
- `@nums' stores user entered numbers. The program terminates if there
- are no even numbers in `@nums'. We get the least even number & then
- just reverse the rest of the sorted list & join them.
+ `(0, 1, 2, 3) xx $score' creates the list `0..3', `$score' number of
+ times. And `[X]' creates cross product from those lists.
+
+ • Note: It's multipled `$score' number of times because `(1) xx
+ $score' is the maximum upto which we get `$score', after that the
+ sum will exceed `$score', we do have 0's there which means we'll get
+ more matches but we've already covered those cases.
+
+ Say the score is 3. We have 3 lists like these:
┌────
- │ @nums = @nums>>.Int.sort;
- │ die "No even number!" unless @nums.grep(* %% 2);
+ │ 0 0 0
+ │ 1 1 1
+ │ 2 2 2
+ │ 3 3 3
+ └────
+
+ And cross product will return:
+
+ ┌────
+ │ 0, 0, 0
+ │ 0, 0, 1
+ │ 0, 0, 2
+ │ 0, 0, 3
+ │ 0, 1, 0
+ │ ...
+ │ 3, 3, 3
+ └────
+
+ We loop over what the cross product returns and take the list if the
+ sum of all elements equals to the score.
+
+ ┌────
+ │ #| scoring basketball points
+ │ unit sub MAIN(Int $score);
- │ my Int $least-even = @nums.splice(@nums.first(* %% 2, :k), 1).first;
- │ say (|@nums.reverse, $least-even).join;
+ │ .put for gather for [X] ((0, 1, 2, 3) xx $score) -> @scores {
+ │ take @scores if ([+] @scores) == $score;
+ │ }.map(*.grep(* !== 0).join).unique.map(*.comb);
└────
+
+ After we gather the lists of scores, remove 0's from there and then we
+ remove duplicate entries. Duplicates entries are removed by converting
+ them to string, using `unique' method and converting them back to Int.
+
+ These entries occur because cross product includes them multiple
+ times. For example, for a score of 3: Cross product will return `0 1
+ 2' and `1 2 0', both of which will satisty the condition and we'll
+ gather them, after removing the 0's, they become duplicates.