diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-08-31 14:34:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-31 14:34:21 +0100 |
| commit | 08c36b5704b65b409ad9a2b3e579f554f08a6fa4 (patch) | |
| tree | 5bc42fb7027c9167e6ac35b2ebc9b768c086eba7 | |
| parent | 3852447ac29d6da57010ec4fca5d15642336a201 (diff) | |
| parent | ebfbfa3c78510205707fcb67f05ad53934ab6a86 (diff) | |
| download | perlweeklychallenge-club-08c36b5704b65b409ad9a2b3e579f554f08a6fa4.tar.gz perlweeklychallenge-club-08c36b5704b65b409ad9a2b3e579f554f08a6fa4.tar.bz2 perlweeklychallenge-club-08c36b5704b65b409ad9a2b3e579f554f08a6fa4.zip | |
Merge pull request #10735 from JTimothyKing/challenge-284
Solutions to challenge 284.
| -rw-r--r-- | challenge-284/jtimothyking/README | 1 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/csharp/InputParsingExtensions.cs | 23 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/csharp/LuckyInteger.cs | 21 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/csharp/RelativeSort.cs | 33 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/perl/1-lucky-integer.pl | 16 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/perl/2-relative-sort.pl | 19 | ||||
| -rw-r--r-- | challenge-284/jtimothyking/perl/InputParsing.pm | 21 |
7 files changed, 134 insertions, 0 deletions
diff --git a/challenge-284/jtimothyking/README b/challenge-284/jtimothyking/README new file mode 100644 index 0000000000..9bbfb44116 --- /dev/null +++ b/challenge-284/jtimothyking/README @@ -0,0 +1 @@ +Solution by Tim King. diff --git a/challenge-284/jtimothyking/csharp/InputParsingExtensions.cs b/challenge-284/jtimothyking/csharp/InputParsingExtensions.cs new file mode 100644 index 0000000000..d048e85f59 --- /dev/null +++ b/challenge-284/jtimothyking/csharp/InputParsingExtensions.cs @@ -0,0 +1,23 @@ +using System.Text.RegularExpressions; + +namespace PwcLib; + +public static class InputParsingExtensions +{ + /// <summary> + /// Parses a line of the form "@<symbol> = (1, 2, 3)" into an array of integers. + /// </summary> + public static int[] GetInts(this string line, string symbol = "ints") + { + var match = Regex.Match(line, $@" + \A \s* + \@{symbol} \s* = \s* \( + ( \d+ (?: \s* , \s* \d+ )* ) + \) + \s* \z + ", RegexOptions.IgnorePatternWhitespace); + if (!match.Success) + throw new ArgumentException($"Invalid input: {line}", nameof(line)); + return match.Groups[1].Value.Split(", ").Select(int.Parse).ToArray(); + } +}
\ No newline at end of file diff --git a/challenge-284/jtimothyking/csharp/LuckyInteger.cs b/challenge-284/jtimothyking/csharp/LuckyInteger.cs new file mode 100644 index 0000000000..809514c185 --- /dev/null +++ b/challenge-284/jtimothyking/csharp/LuckyInteger.cs @@ -0,0 +1,21 @@ +using PwcLib; + +namespace _1_lucky_integer; + +public static class LuckyInteger +{ + public static void Main(string[] args) + { + var input = Console.ReadLine() ?? ""; + var luckyInteger = input.GetInts().FindLargestLuckyInteger(); + Console.WriteLine(luckyInteger); + } + + private static int FindLargestLuckyInteger(this int[] ints) => + ints + .GroupBy(x => x) + .Where(grouping => grouping.Count() == grouping.Key) + .Select(grouping => grouping.Key) + .OrderByDescending(x => x) + .FirstOrDefault(defaultValue: -1); +}
\ No newline at end of file diff --git a/challenge-284/jtimothyking/csharp/RelativeSort.cs b/challenge-284/jtimothyking/csharp/RelativeSort.cs new file mode 100644 index 0000000000..418517ef78 --- /dev/null +++ b/challenge-284/jtimothyking/csharp/RelativeSort.cs @@ -0,0 +1,33 @@ +using PwcLib; + +namespace _2_relative_sort; + +public static class RelativeSort +{ + public static void Main(string[] args) + { + var line1 = Console.ReadLine() ?? ""; + var line2 = Console.ReadLine() ?? ""; + var sortedInts = line1.GetInts("list1") + .OrderRelativeTo(line2.GetInts("list2")); + Console.WriteLine($"({string.Join(", ", sortedInts)})"); + } + + /// <summary> + /// Orders the elements of the source list relative to the order of the reference list. + /// </summary> + private static int[] OrderRelativeTo(this IEnumerable<int> source, IEnumerable<int> reference) + { + var rank = reference.ToRankDictionary(); + return source + .OrderBy(x => rank.GetValueOrDefault(x, defaultValue: x + rank.Count)) + .ToArray(); + } + + /// <summary> + /// Returns a dictionary that maps each element of the source to its index in the source. + /// </summary> + private static Dictionary<TSource, int> ToRankDictionary<TSource>(this IEnumerable<TSource> source) + where TSource : notnull => + source.Select((x, i) => (x, i)).ToDictionary(xi => xi.x, xi => xi.i); +}
\ No newline at end of file diff --git a/challenge-284/jtimothyking/perl/1-lucky-integer.pl b/challenge-284/jtimothyking/perl/1-lucky-integer.pl new file mode 100644 index 0000000000..28260c9ea3 --- /dev/null +++ b/challenge-284/jtimothyking/perl/1-lucky-integer.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use v5.38; +use InputParsing; + +my $input = <STDIN>; +my $lucky_int = find_largest_lucky_int(InputParsing::get_ints($input)); +say $lucky_int; + +sub find_largest_lucky_int { + my %counts; + $counts{$_}++ for @_; + my @lucky_ints = grep { $counts{$_} == $_ } keys %counts; + return @lucky_ints ? (sort { $b <=> $a } @lucky_ints)[0] : -1; +} + +__END__ diff --git a/challenge-284/jtimothyking/perl/2-relative-sort.pl b/challenge-284/jtimothyking/perl/2-relative-sort.pl new file mode 100644 index 0000000000..f32ff6aa57 --- /dev/null +++ b/challenge-284/jtimothyking/perl/2-relative-sort.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use v5.38; +use InputParsing; + +my $input_line_1 = <STDIN>; +my $input_line_2 = <STDIN>; +my $sorter = relative_to(InputParsing::get_ints($input_line_2, 'list2')); +my @sorted_list = sort $sorter InputParsing::get_ints($input_line_1, 'list1'); +say '(' . join(', ', @sorted_list) . ')'; + +sub relative_to { + my $next_rank = 0; + my %rank = map { $_ => $next_rank++ } @_; + return sub { + return ($rank{$a} // $a + $next_rank) <=> ($rank{$b} // $b + $next_rank); + }; +} + +__END__ diff --git a/challenge-284/jtimothyking/perl/InputParsing.pm b/challenge-284/jtimothyking/perl/InputParsing.pm new file mode 100644 index 0000000000..4fdfe13a14 --- /dev/null +++ b/challenge-284/jtimothyking/perl/InputParsing.pm @@ -0,0 +1,21 @@ +package InputParsing; +use v5.38; + +sub get_ints { + my ($line, $symbol) = @_; + $symbol //= 'ints'; + chomp($line); + $line =~ m[ + \A \s* + \@$symbol \s* = \s* \( + ( \d+ (?: \s* , \s* \d+ )* ) + \) + \s* \z + ]x; + my @ints = split /\s*,\s*/, $1 // ''; + @ints or die "Invalid line for $symbol: $line\n"; + return @ints; +} + + +1;
\ No newline at end of file |
