aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-06-07 20:10:04 +0200
committerAbigail <abigail@abigail.be>2021-06-08 15:47:36 +0200
commit5f7b533e8cf34449a3a92a91dadc67856a883660 (patch)
tree2623082bdb2d849d7a06388c00c9d02441d6a985
parentf08a13f3afd794045d93196c78b2c75f4959bd10 (diff)
downloadperlweeklychallenge-club-5f7b533e8cf34449a3a92a91dadc67856a883660.tar.gz
perlweeklychallenge-club-5f7b533e8cf34449a3a92a91dadc67856a883660.tar.bz2
perlweeklychallenge-club-5f7b533e8cf34449a3a92a91dadc67856a883660.zip
AWK, Bash, C, Lua, Node.js, Perl, Python, Ruby solutions for week 116, part 2.
-rw-r--r--challenge-116/abigail/README.md8
-rw-r--r--challenge-116/abigail/awk/ch-2.awk19
-rw-r--r--challenge-116/abigail/bash/ch-2.sh53
-rw-r--r--challenge-116/abigail/c/ch-2.c35
-rw-r--r--challenge-116/abigail/lua/ch-2.lua22
-rw-r--r--challenge-116/abigail/node/ch-2.js26
-rw-r--r--challenge-116/abigail/perl/ch-2.pl29
-rw-r--r--challenge-116/abigail/python/ch-2.py23
-rw-r--r--challenge-116/abigail/ruby/ch-2.rb21
9 files changed, 236 insertions, 0 deletions
diff --git a/challenge-116/abigail/README.md b/challenge-116/abigail/README.md
index 629117cbfb..a68ef6430c 100644
--- a/challenge-116/abigail/README.md
+++ b/challenge-116/abigail/README.md
@@ -47,6 +47,14 @@ Output: 0 as 5^2 + 2^2 => 25 + 4 => 29
~~~~
### Solutions
+* [AWK](awk/ch-2.awk)
+* [Bash](bash/ch-2.sh)
+* [C](c/ch-2.c)
+* [Lua](lua/ch-2.lua)
+* [Node.js](node/ch-2.js)
+* [Perl](perl/ch-2.pl)
+* [Python](python/ch-2.py)
+* [Ruby](ruby/ch-2.rb)
### Blog
[Sum of Squares](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-116-2.html)
diff --git a/challenge-116/abigail/awk/ch-2.awk b/challenge-116/abigail/awk/ch-2.awk
new file mode 100644
index 0000000000..6353b61eb8
--- /dev/null
+++ b/challenge-116/abigail/awk/ch-2.awk
@@ -0,0 +1,19 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-2.awk < input-file
+#
+
+{
+ sum_of_squares = 0
+ for (i = 1; i <= length ($1); i ++) {
+ # Sum of squares
+ sum_of_squares += substr ($1, i, 1) * substr ($1, i, 1)
+ }
+ root = int (.5 + int (sqrt (sum_of_squares)))
+ print (sum_of_squares == root * root ? 1 : 0)
+}
diff --git a/challenge-116/abigail/bash/ch-2.sh b/challenge-116/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..8d0f9fe95d
--- /dev/null
+++ b/challenge-116/abigail/bash/ch-2.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+set -f
+
+while read line
+do #
+ # Calculate the sum of the squares of the digits. We'll ignore
+ # any character which isn't a digit, and we ignore 0 as well
+ # (as it won't contribute anything to the sum)
+ #
+ ((sum_of_squares = 0))
+ for ((i = 0; i < ${#line}; i ++))
+ do char=${line:$i:1}
+ if test "0" "<" $char -a $char "<" ":" # 1 - 9
+ then ((sum_of_squares += char * char))
+ fi
+ done
+
+ #
+ # Find a smallest power of two whose square is larger than sum-of-squares
+ #
+ root_upper=1
+ while ((root_upper * root_upper < sum_of_squares))
+ do ((root_upper = root_upper * 2))
+ done
+
+ #
+ # Use binary search to zoom into the square of sum-of-squares.
+ #
+ ((root_lower = root_upper / 2))
+ ((out = 0))
+ while ((root_lower < root_upper))
+ do ((root_mid = (root_upper + root_lower) / 2))
+ ((square_mid = root_mid * root_mid))
+ if ((square_mid == sum_of_squares))
+ then ((out = 1))
+ ((root_lower = root_upper))
+ else if ((square_mid < sum_of_squares))
+ then ((root_lower = root_mid + 1))
+ else ((root_upper = root_mid))
+ fi
+ fi
+ done
+ echo $out
+done
diff --git a/challenge-116/abigail/c/ch-2.c b/challenge-116/abigail/c/ch-2.c
new file mode 100644
index 0000000000..ef0b8bd20a
--- /dev/null
+++ b/challenge-116/abigail/c/ch-2.c
@@ -0,0 +1,35 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <ctype.h>
+# include <math.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ char * line_ptr = line;
+ long sum_of_squares = 0;
+ while (* line_ptr) {
+ if (isdigit (* line_ptr)) {
+ sum_of_squares += (* line_ptr - '0') * (* line_ptr - '0');
+ }
+ line_ptr ++;
+ }
+ long root = (long) floor (.5 + sqrt (sum_of_squares));
+ printf ("%d\n", sum_of_squares == root * root ? 1 : 0);
+ }
+ free (line);
+
+ return (0);
+}
diff --git a/challenge-116/abigail/lua/ch-2.lua b/challenge-116/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..f53ea72076
--- /dev/null
+++ b/challenge-116/abigail/lua/ch-2.lua
@@ -0,0 +1,22 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+for line in io . lines () do
+ local sum_of_squares = 0
+ for d in line : gmatch ("%d") do
+ local number = tonumber (d)
+ sum_of_squares = sum_of_squares + number * number
+ end
+ root = math . floor (.5 + math . sqrt (sum_of_squares))
+ if sum_of_squares == root * root
+ then print (1)
+ else print (0)
+ end
+end
diff --git a/challenge-116/abigail/node/ch-2.js b/challenge-116/abigail/node/ch-2.js
new file mode 100644
index 0000000000..c11aa92ad4
--- /dev/null
+++ b/challenge-116/abigail/node/ch-2.js
@@ -0,0 +1,26 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-2.js < input-file
+//
+
+require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => console . log (squares_sum_to_square (_)))
+;
+
+
+function squares_sum_to_square (line) {
+ let sum_of_squares = 0
+ line . split ('') . forEach (letter => {
+ if ("0" <= letter && letter <= "9") {
+ sum_of_squares += + letter * + letter
+ }
+ })
+ let root = Math . floor (.5 + Math . sqrt (sum_of_squares))
+ return (sum_of_squares == root * root ? 1 : 0)
+}
diff --git a/challenge-116/abigail/perl/ch-2.pl b/challenge-116/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..6e3f826663
--- /dev/null
+++ b/challenge-116/abigail/perl/ch-2.pl
@@ -0,0 +1,29 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-2.pl < input-file
+#
+use List::Util qw [sum];
+
+#
+# Print 1 if the squares of the digits sum to a perfect square, 0 otherwise.
+#
+while (<>) {
+ # Calculate the sum of the squares of the digits.
+ my $sum_of_squares = sum map {$_ * $_} /[1-9]/g; # Ignore 0s
+ # Is it a perfect square? (Take care of rounding errors).
+ say $sum_of_squares == int (.5 + sqrt $sum_of_squares) ** 2 ? 1 : 0
+}
diff --git a/challenge-116/abigail/python/ch-2.py b/challenge-116/abigail/python/ch-2.py
new file mode 100644
index 0000000000..0876b16b64
--- /dev/null
+++ b/challenge-116/abigail/python/ch-2.py
@@ -0,0 +1,23 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as: python ch-2.py < input-file
+#
+
+import fileinput
+from math import sqrt
+
+for line in fileinput . input ():
+ sum_of_squares = 0
+ for char in line:
+ if "1" <= char and char <= "9":
+ sum_of_squares = sum_of_squares + int (char) * int (char)
+ root = int (.5 + sqrt (sum_of_squares))
+ if sum_of_squares == root * root:
+ print (1)
+ else:
+ print (0)
diff --git a/challenge-116/abigail/ruby/ch-2.rb b/challenge-116/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..5dce7242cc
--- /dev/null
+++ b/challenge-116/abigail/ruby/ch-2.rb
@@ -0,0 +1,21 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+ARGF . each_line do
+ |line|
+ sum_of_squares = 0
+ line . split('') . each do
+ |char|
+ if "1" <= char && char <= "9"
+ then sum_of_squares += (char . to_i) ** 2
+ end
+ end
+ puts (sum_of_squares == (Math . sqrt (sum_of_squares) . round) ** 2 ? 1 : 0)
+end