aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-115/abigail/README.md8
-rw-r--r--challenge-115/abigail/awk/ch-2.awk56
-rw-r--r--challenge-115/abigail/bash/ch-2.sh52
-rw-r--r--challenge-115/abigail/c/ch-2.c71
-rw-r--r--challenge-115/abigail/lua/ch-2.lua59
-rw-r--r--challenge-115/abigail/node/ch-2.js53
-rw-r--r--challenge-115/abigail/perl/ch-2.pl44
-rw-r--r--challenge-115/abigail/python/ch-2.py49
-rw-r--r--challenge-115/abigail/ruby/ch-2.rb57
9 files changed, 449 insertions, 0 deletions
diff --git a/challenge-115/abigail/README.md b/challenge-115/abigail/README.md
index 75cec1ed8e..0d966f083c 100644
--- a/challenge-115/abigail/README.md
+++ b/challenge-115/abigail/README.md
@@ -43,6 +43,14 @@ Output: 7614
~~~~
### 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
[String Chain](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-115-2.html)
diff --git a/challenge-115/abigail/awk/ch-2.awk b/challenge-115/abigail/awk/ch-2.awk
new file mode 100644
index 0000000000..2b2162d5f0
--- /dev/null
+++ b/challenge-115/abigail/awk/ch-2.awk
@@ -0,0 +1,56 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-2.awk < input-file
+#
+
+{
+ #
+ # Initialize digits array
+ #
+ for (i = 0; i < 10; i ++) {
+ digits [i] = 0
+ }
+
+ #
+ # Read in the data; count digits
+ #
+ for (i = 1; i <= NF; i ++) {
+ digits [$i] ++
+ }
+
+ #
+ # Find the smallest even number; this one goes last
+ #
+ last = -1
+ for (i = 0; i < 10 && last < 0; i += 2) {
+ if (digits [i] > 0) {
+ last = i
+ digits [i] --
+ }
+ }
+
+ #
+ # Skip if the input does not contain an even digit
+ #
+ if (last < 0) {
+ next
+ }
+
+ #
+ # Create the output: rest of digits are from highest to lowest
+ #
+ out = ""
+ for (i = 9; i >= 0; i --) {
+ while (digits [i] -- > 0) {
+ out = out i
+ }
+ }
+ print out last
+}
+
+
diff --git a/challenge-115/abigail/bash/ch-2.sh b/challenge-115/abigail/bash/ch-2.sh
new file mode 100644
index 0000000000..3da1e61c2b
--- /dev/null
+++ b/challenge-115/abigail/bash/ch-2.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+#
+# See ../README.md
+#
+
+#
+# Run as: bash ch-2.sh < input-file
+#
+
+declare -a digits
+
+while read -a input
+do #
+ # Count the digits of the input
+ #
+ unset digits
+ for ((i = 0; i < ${#input[@]}; i ++))
+ do ((digits[input[i]] ++))
+ done
+
+ #
+ # Find smallest even number; this will be last number.
+ #
+ last=-1
+ for ((i = 8; i >= 0; i -= 2))
+ do if ((digits[i] > 0))
+ then ((last = i))
+ fi
+ done
+
+ #
+ # Skip if the input doesn't contain an even digit
+ #
+ if ((last < 0))
+ then continue
+ fi
+
+ ((digits[last] --))
+
+ #
+ # Create the output
+ #
+ out=""
+ for ((i = 9; i >= 0; i --))
+ do for ((j = 0; j < digits[i]; j ++))
+ do out=$out$i
+ done
+ done
+
+ echo $out$last
+done
diff --git a/challenge-115/abigail/c/ch-2.c b/challenge-115/abigail/c/ch-2.c
new file mode 100644
index 0000000000..975aace0ef
--- /dev/null
+++ b/challenge-115/abigail/c/ch-2.c
@@ -0,0 +1,71 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <ctype.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+
+# define NR_OF_DIGITS 10
+# define ZERO '0'
+
+int main (void) {
+ char * line = NULL;
+ size_t len = 0;
+ size_t str_len;
+
+ while ((str_len = getline (&line, &len, stdin)) != -1) {
+ /*
+ * Read in a line of data. Count the number of digits,
+ * and put them in an array 'digits'.
+ */
+ char * line_ptr = line;
+ int digits [NR_OF_DIGITS];
+ for (size_t i = 0; i < NR_OF_DIGITS; i ++) {
+ digits [i] = 0;
+ }
+ while (* line_ptr) {
+ if (isdigit (* line_ptr)) {
+ digits [* line_ptr - ZERO] ++;
+ }
+ line_ptr ++;
+ }
+
+ /*
+ * Find the lowest even digit.
+ */
+ int last = -1;
+ for (ssize_t i = NR_OF_DIGITS - 2; i >= 0; i -= 2) {
+ if (digits [i]) {
+ last = i;
+ }
+ }
+
+ /*
+ * Skip if the input does not contain an even digit.
+ */
+ if (last < 0) {
+ continue;
+ }
+ digits [last] --;
+
+ /*
+ * Print the output
+ */
+ for (ssize_t i = NR_OF_DIGITS - 1; i >= 0; i --) {
+ for (int j = 0; j < digits [i]; j ++) {
+ printf ("%zu", i);
+ }
+ }
+ printf ("%d\n", last);
+
+ }
+ free (line);
+
+ return (0);
+}
diff --git a/challenge-115/abigail/lua/ch-2.lua b/challenge-115/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..4b92535d53
--- /dev/null
+++ b/challenge-115/abigail/lua/ch-2.lua
@@ -0,0 +1,59 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+local NR_OF_DIGITS = 10
+
+for line in io . lines () do
+
+ --
+ -- Process the input, count digits
+ --
+ local digits = {}
+ for i = 0, NR_OF_DIGITS - 1
+ do digits [i] = 0
+ end
+ for d in line : gmatch ("%d")
+ do d = tonumber (d)
+ digits [d] = digits [d] + 1
+ end
+
+ --
+ -- Find the lowest even digit
+ --
+ local last = -1
+ for i = NR_OF_DIGITS - 2, 0, -2
+ do if digits [i] > 0
+ then last = i
+ end
+ end
+
+ --
+ -- Skip if there is no even digit in the input
+ --
+ if last < 0
+ then goto end_loop
+ end
+
+ digits [last] = digits [last] - 1
+
+ --
+ -- Create output: digits from high to low
+ --
+ local out = ""
+ for i = NR_OF_DIGITS - 1, 0, -1
+ do for j = 1, digits [i]
+ do out = out .. tostring (i)
+ end
+ end
+
+ print (out .. tostring (last))
+
+ ::end_loop::
+end
diff --git a/challenge-115/abigail/node/ch-2.js b/challenge-115/abigail/node/ch-2.js
new file mode 100644
index 0000000000..3e76bf958c
--- /dev/null
+++ b/challenge-115/abigail/node/ch-2.js
@@ -0,0 +1,53 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-2.js < input-file
+//
+
+let NR_OF_DIGITS = 10
+
+ require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => {
+ //
+ // Parse the input: count the digits
+ //
+ let digits = []
+ for (let i = 0; i < NR_OF_DIGITS; i ++) {
+ digits [i] = 0
+ }
+ _ . split (/\s+/) . map (_ => {digits [+_] ++})
+
+ //
+ // Find the smallest even number
+ //
+ let last = -1;
+ for (let i = 0; i < NR_OF_DIGITS && last < 0; i += 2) {
+ if (digits [i] > 0) {
+ last = i
+ digits [i] --
+ }
+ }
+
+ //
+ // Skip if there are no even digits in the input
+ //
+ if (last < 0) {
+ return
+ }
+
+ //
+ // Create the output: print the remaining numbers from high to low
+ //
+ let out = ""
+ for (let i = NR_OF_DIGITS - 1; i >= 0; i --) {
+ for (let j = 0; j < digits [i]; j ++) {
+ out = out + i . toString ()
+ }
+ }
+ console . log (out + last . toString ())
+})
diff --git a/challenge-115/abigail/perl/ch-2.pl b/challenge-115/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..c0adf3617f
--- /dev/null
+++ b/challenge-115/abigail/perl/ch-2.pl
@@ -0,0 +1,44 @@
+#!/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
+#
+
+my @DIGITS = (0 .. 9);
+my @EVENS = grep {$_ % 2 == 0} @DIGITS;
+
+while (<>) {
+ #
+ # Read in data, store counts of each digit.
+ #
+ my @digits = (0) x @DIGITS;
+ $digits [$_] ++ for do {local $" = ""; /[@DIGITS]/g};
+
+ #
+ # The last number of the output should be the smallest
+ # even number in the input. If there is no even number
+ # in the input, skip it.
+ #
+ my ($last) = grep {$digits [$_]} @EVENS;
+ next unless defined $last;
+ $digits [$last] --;
+
+ #
+ # Print the result, with the highest numbers first.
+ #
+ print join "" => map {$_ x $digits [$_]} reverse @DIGITS;
+ say $last;
+}
diff --git a/challenge-115/abigail/python/ch-2.py b/challenge-115/abigail/python/ch-2.py
new file mode 100644
index 0000000000..a3b040b832
--- /dev/null
+++ b/challenge-115/abigail/python/ch-2.py
@@ -0,0 +1,49 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as: python ch-2.py < input-file
+#
+
+import fileinput
+
+NR_OF_DIGITS = 10
+
+for line in fileinput . input ():
+ #
+ # Parse the input, count digits
+ #
+ digits = []
+ for d in range (NR_OF_DIGITS):
+ digits . append (0)
+ for d in line . split ():
+ d = int (d)
+ digits [d] = digits [d] + 1
+
+ #
+ # Find the smallest even number
+ #
+ last = -1
+
+ for d in range (NR_OF_DIGITS - 2, -1, -2):
+ if digits [d] > 0:
+ last = d
+
+ #
+ # If we don't have an even number, skip
+ #
+ if last < 0:
+ continue
+ digits [last] = digits [last] - 1
+
+ #
+ # Print the rest of the digits, highest to lowest
+ #
+ for d in range (NR_OF_DIGITS - 1, 0, -1):
+ for i in range (digits [d]):
+ print (d, end = '')
+
+ print (last)
diff --git a/challenge-115/abigail/ruby/ch-2.rb b/challenge-115/abigail/ruby/ch-2.rb
new file mode 100644
index 0000000000..e1e41a91fd
--- /dev/null
+++ b/challenge-115/abigail/ruby/ch-2.rb
@@ -0,0 +1,57 @@
+#!/usr/bin/ruby
+
+#
+# See ../README.md
+#
+
+#
+# Run as: ruby ch-2.rb < input-file
+#
+
+nr_of_digits = 10
+
+ARGF . each_line do
+ |line|
+ #
+ # Read the input and count the digits
+ #
+ digits = []
+ for d in 0 .. nr_of_digits - 1 do
+ digits [d] = 0
+ end
+ line . split() . each do
+ |d|
+ digits [d . to_i] += 1
+ end
+
+ #
+ # Find the lowest even number
+ #
+ last = -1
+ for i in 1 .. nr_of_digits do
+ d = nr_of_digits - i
+ if d % 2 == 0 && digits [d] > 0
+ then last = d
+ end
+ end
+
+ #
+ # Skip if the input does not contain an even number
+ #
+ if last < 0
+ then next
+ end
+
+ digits [last] -= 1
+
+ #
+ # Print the digits, highest to lowest
+ #
+ for i in 1 .. nr_of_digits do
+ d = nr_of_digits - i
+ for j in 1 .. digits [d] do
+ print (d)
+ end
+ end
+ puts (last)
+end