aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-10 22:34:01 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-07-10 22:34:01 +0100
commit8158678b6a510fdcdcabe5e3e198335c74e49c32 (patch)
tree33ac318211d1087d8dde4c572e9fa715c4fdd212
parentb3f1536217ec7d4560567e0e9eddc276d4c45570 (diff)
downloadperlweeklychallenge-club-8158678b6a510fdcdcabe5e3e198335c74e49c32.tar.gz
perlweeklychallenge-club-8158678b6a510fdcdcabe5e3e198335c74e49c32.tar.bz2
perlweeklychallenge-club-8158678b6a510fdcdcabe5e3e198335c74e49c32.zip
- Added guest contributions by Laurent Rosenfeld.
-rw-r--r--challenge-172/laurent-rosenfeld/c/ch-2.c30
-rw-r--r--challenge-172/laurent-rosenfeld/ring/ch-2.ring18
-rw-r--r--challenge-172/laurent-rosenfeld/ruby/ch-2.rb20
3 files changed, 68 insertions, 0 deletions
diff --git a/challenge-172/laurent-rosenfeld/c/ch-2.c b/challenge-172/laurent-rosenfeld/c/ch-2.c
new file mode 100644
index 0000000000..e617c3daf3
--- /dev/null
+++ b/challenge-172/laurent-rosenfeld/c/ch-2.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int compare_int(const void *a,const void *b) {
+ int *x = (int *) a;
+ int *y = (int *) b;
+ return *x - *y;
+}
+
+float median (int count, int m[]) {
+ if (count % 2) {
+ return 1.0 * m[(count -1)/2];
+ } else {
+ return (m[count/2 -1] + m[count/2])/2.0;
+ }
+}
+
+int main() {
+ int moons[] = {0, 0, 1, 2, 63, 61, 27, 13};
+ int size = sizeof(moons)/sizeof(*moons);
+ qsort (moons, size, sizeof(*moons), compare_int);
+ float min = 1.0 * moons[0];
+ float med = median(size, moons);
+ int half = (int)size/2;
+ float first_q = median(half, moons);
+ float last_q = median(half, moons + 4);
+ float max = 1.0 * moons[size - 1];
+ printf ("%.2f %.2f %.2f %.2f %.2f", min, first_q, med, last_q, max);
+ return 0;
+}
diff --git a/challenge-172/laurent-rosenfeld/ring/ch-2.ring b/challenge-172/laurent-rosenfeld/ring/ch-2.ring
new file mode 100644
index 0000000000..48ce135550
--- /dev/null
+++ b/challenge-172/laurent-rosenfeld/ring/ch-2.ring
@@ -0,0 +1,18 @@
+moons = [0, 0, 1, 2, 63, 61, 27, 13]
+see summary(sort(moons))
+
+func summary(n)
+ min = n[1]
+ max = n[len(n)]
+ size = len(n)
+ med = median(1, size, n)
+ first_q = median(1, size/2, n)
+ last_q = median(size/2 +1, size, n)
+ return [min, first_q, med, last_q, max]
+
+func median(low, high, n)
+ if (high + low) % 2 = 0
+ return n[low + (high - low + 1)/2]
+ else
+ return (n[low + (high - low + 1)/2] + n[low + (high - low + 1)/2 - 1]) / 2.0
+ ok
diff --git a/challenge-172/laurent-rosenfeld/ruby/ch-2.rb b/challenge-172/laurent-rosenfeld/ruby/ch-2.rb
new file mode 100644
index 0000000000..b05e88740d
--- /dev/null
+++ b/challenge-172/laurent-rosenfeld/ruby/ch-2.rb
@@ -0,0 +1,20 @@
+def median(n)
+ size = n.length
+ if size %2 != 0
+ n[(size + 1) / 2]
+ else
+ (n[size/2] + n[size/2 + 1]) / 2.0
+ end
+end
+
+def summary(n)
+ min = n[0]
+ max = n[-1]
+ med = median(n)
+ first_q = median( n.select { |i| i < med })
+ last_q = median( n.select { |i| i > med })
+ return min, first_q, med, last_q, max
+end
+
+moons = [0, 0, 1, 2, 63, 61, 27, 13]
+print summary(moons.sort), "\n"