diff options
| author | Abigail <abigail@abigail.be> | 2021-09-20 21:21:20 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-09-20 21:22:50 +0200 |
| commit | b0bcfe611b46d74e07822751a7c76e6d4ab7c879 (patch) | |
| tree | 5df6cd3649b922d78ba33fddffe0cc151d713f1a | |
| parent | 344d8d42e675e131ad896d69a13221d36a982abd (diff) | |
| download | perlweeklychallenge-club-b0bcfe611b46d74e07822751a7c76e6d4ab7c879.tar.gz perlweeklychallenge-club-b0bcfe611b46d74e07822751a7c76e6d4ab7c879.tar.bz2 perlweeklychallenge-club-b0bcfe611b46d74e07822751a7c76e6d4ab7c879.zip | |
First set of solutions for week 131.
Part 1: AWK, C, Perl.
Part 2: AWK, Perl.
| -rw-r--r-- | challenge-131/abigail/README.md | 11 | ||||
| -rw-r--r-- | challenge-131/abigail/awk/ch-1.awk | 18 | ||||
| -rw-r--r-- | challenge-131/abigail/awk/ch-2.awk | 35 | ||||
| -rw-r--r-- | challenge-131/abigail/c/ch-1.c | 35 | ||||
| -rw-r--r-- | challenge-131/abigail/perl/ch-1.pl | 24 | ||||
| -rw-r--r-- | challenge-131/abigail/perl/ch-2.pl | 48 |
6 files changed, 171 insertions, 0 deletions
diff --git a/challenge-131/abigail/README.md b/challenge-131/abigail/README.md index 38e51d0b9c..4c27485f4c 100644 --- a/challenge-131/abigail/README.md +++ b/challenge-131/abigail/README.md @@ -1 +1,12 @@ # Solutions by Abigail + +## Part 1 + +* [AWK](awk/ch-1.awk) +* [C](c/ch-1.c) +* [Perl](perl/ch-1.pl) + +## Part 2 + +* [AWK](awk/ch-2.awk) +* [Perl](perl/ch-2.pl) diff --git a/challenge-131/abigail/awk/ch-1.awk b/challenge-131/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..bdf93e5a1a --- /dev/null +++ b/challenge-131/abigail/awk/ch-1.awk @@ -0,0 +1,18 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + prev = 0 + for (i = 1; i <= NF; i ++) { + printf ("%s%d", prev == 0 ? "" : (prev + 1 == $i) ? " " : "\n", + (prev = $i)) + } + printf ("\n") +} diff --git a/challenge-131/abigail/awk/ch-2.awk b/challenge-131/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..5215d94acf --- /dev/null +++ b/challenge-131/abigail/awk/ch-2.awk @@ -0,0 +1,35 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +NR == 1 { + n = split ($0, delims, "") +} + +NR == 2 { + m = split ($0, chars, "") + out1 = "" + out2 = "" + for (i = 1; i <= m; i ++) { + for (j = 1; j <= n; j += 2) { + if (delims [j] == chars [i]) { + out1 = out1 delims [j] + break + } + } + for (j = 2; j <= n; j += 2) { + if (delims [j] == chars [i]) { + out2 = out2 delims [j] + break + } + } + } + print out1 + print out2 +} diff --git a/challenge-131/abigail/c/ch-1.c b/challenge-131/abigail/c/ch-1.c new file mode 100644 index 0000000000..783d0f19d3 --- /dev/null +++ b/challenge-131/abigail/c/ch-1.c @@ -0,0 +1,35 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.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; + int num; + int offset; + int prev = 0; + while (sscanf (line_ptr, "%d%n", &num, &offset) == 1) { + printf ("%s%d", prev == 0 ? "" + : prev + 1 == num ? " " + : "\n", num); + prev = num; + line_ptr += offset; + } + printf ("\n"); + } + free (line); + + return (0); +} diff --git a/challenge-131/abigail/perl/ch-1.pl b/challenge-131/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..413c037a86 --- /dev/null +++ b/challenge-131/abigail/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/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-1.pl < input-file +# + +while (<>) { + my $prev = 0; + print $prev ? $prev + 1 == $_ ? " " : "\n" : "", $prev = $_ for /[0-9]+/g; + print "\n"; +} diff --git a/challenge-131/abigail/perl/ch-2.pl b/challenge-131/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..03fba700d3 --- /dev/null +++ b/challenge-131/abigail/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/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 +# +# First line of input: delimiters +# Second line of input: string to search +# + +# +# What a strange way of phrasing the exercise. We're given a string +# of "delimiter pairs", but we actually have to do nothing at all with +# the "delimiter" part. +# +# Basically, we have two sets of characters which we need to extract +# from the string, but for some reason, the two sets of characters +# are interleaved. Weird. +# + +chomp (my $chars = <>); +$_ = <>; # String to search + +# +# Extract the characters on the odd and even positions; +# replace / with \/. +# +my $odds = $chars =~ s/(.)./$1/gr =~ s!/!\\/!r; +my $even = $chars =~ s/.(.)/$1/gr =~ s!/!\\/!r; + +# +# Use y///dc to get rid of the characters which don't match, and +# print the remainder. +# +say eval "y/$odds//dcr"; +say eval "y/$even//dcr"; |
