From d82f0dd189cf91d086b3aa9dc64fea3e6b05d423 Mon Sep 17 00:00:00 2001 From: Tim King Date: Wed, 2 Oct 2024 09:12:16 -0400 Subject: Solutions to challenge 289 by Tim King --- challenge-289/jtimothyking/blog.txt | 1 + challenge-289/jtimothyking/csharp/ch-1.cs | 32 +++++++++++++++++++++++++++++++ challenge-289/jtimothyking/csharp/ch-2.cs | 25 ++++++++++++++++++++++++ challenge-289/jtimothyking/perl/ch-1.pl | 10 ++++++++++ challenge-289/jtimothyking/perl/ch-2.pl | 19 ++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 challenge-289/jtimothyking/blog.txt create mode 100644 challenge-289/jtimothyking/csharp/ch-1.cs create mode 100644 challenge-289/jtimothyking/csharp/ch-2.cs create mode 100644 challenge-289/jtimothyking/perl/ch-1.pl create mode 100644 challenge-289/jtimothyking/perl/ch-2.pl diff --git a/challenge-289/jtimothyking/blog.txt b/challenge-289/jtimothyking/blog.txt new file mode 100644 index 0000000000..fe2195f4ef --- /dev/null +++ b/challenge-289/jtimothyking/blog.txt @@ -0,0 +1 @@ +https://github.com/JTimothyKing/PWC/blob/main/challenge-289/blog.md diff --git a/challenge-289/jtimothyking/csharp/ch-1.cs b/challenge-289/jtimothyking/csharp/ch-1.cs new file mode 100644 index 0000000000..24e79631a2 --- /dev/null +++ b/challenge-289/jtimothyking/csharp/ch-1.cs @@ -0,0 +1,32 @@ +namespace ch_1; + +public static class Ch1 +{ + public static void Main(string[] args) + { + var firstThree = args.Select(int.Parse) + .OrderDescending().Distinct() + .Take(3).ToArray(); + Console.WriteLine( + firstThree.Length >= 3 ? firstThree[2] : + firstThree.Length > 0 ? firstThree[0] : + "" + ); + } + + private static IEnumerable Distinct(this IOrderedEnumerable source) + { + using var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) yield break; + var comparer = EqualityComparer.Default; + var current = enumerator.Current; + yield return current; + while (enumerator.MoveNext()) + { + var next = enumerator.Current; + if (comparer.Equals(current, next)) continue; + current = next; + yield return current; + } + } +} \ No newline at end of file diff --git a/challenge-289/jtimothyking/csharp/ch-2.cs b/challenge-289/jtimothyking/csharp/ch-2.cs new file mode 100644 index 0000000000..4d78fcb891 --- /dev/null +++ b/challenge-289/jtimothyking/csharp/ch-2.cs @@ -0,0 +1,25 @@ +using System.Text.RegularExpressions; + +namespace ch_2; + +public static class Ch2 +{ + public static void Main() + { + while (Console.ReadLine() is { } line) + foreach (var outputWord in Regex.Split($"{line}\n", @"\b").Select(Jumble)) + Console.Write(outputWord); + } + + private static string Jumble(string word) => + word.Length < 4 || !Regex.IsMatch(word, @"\w") + ? word + : word[0] + word[1..^1].Shuffle() + word[^1]; + + private static string Shuffle(this string str) + { + var chars = str.ToCharArray(); + Random.Shared.Shuffle(chars); + return new string(chars); + } +} \ No newline at end of file diff --git a/challenge-289/jtimothyking/perl/ch-1.pl b/challenge-289/jtimothyking/perl/ch-1.pl new file mode 100644 index 0000000000..1ea9bf70f7 --- /dev/null +++ b/challenge-289/jtimothyking/perl/ch-1.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use v5.38; +use warnings; + +use List::Util qw(uniqint); + +my @unique_ints = uniqint sort { $b <=> $a } @ARGV; +say $unique_ints[2] // $unique_ints[0]; + +__END__ diff --git a/challenge-289/jtimothyking/perl/ch-2.pl b/challenge-289/jtimothyking/perl/ch-2.pl new file mode 100644 index 0000000000..9fa7b5c2eb --- /dev/null +++ b/challenge-289/jtimothyking/perl/ch-2.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use v5.38; +use warnings; + +use List::Util qw(shuffle); + +while (<>) { + print for map { jumble($_) } split /\b/; +} + +sub jumble { + my $word = shift; + return $word if length $word < 4; + return $word if $word !~ /\w/; + my @letters = split //, $word; + return $letters[0] . (join '', shuffle @letters[1 .. $#letters - 1]) . $letters[-1]; +} + +__END__ -- cgit From 3f5b268c8d4dd9a0049bf319f884e6e5d297bd21 Mon Sep 17 00:00:00 2001 From: Tim King Date: Wed, 2 Oct 2024 15:18:06 -0400 Subject: Additional solution for challenge 289 task 1, by Tim King --- challenge-289/jtimothyking/csharp/ch-1.cs | 38 +++++++++++++++++++++++++++++++ challenge-289/jtimothyking/perl/ch-1.pl | 34 +++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/challenge-289/jtimothyking/csharp/ch-1.cs b/challenge-289/jtimothyking/csharp/ch-1.cs index 24e79631a2..8ecd2e058c 100644 --- a/challenge-289/jtimothyking/csharp/ch-1.cs +++ b/challenge-289/jtimothyking/csharp/ch-1.cs @@ -3,6 +3,12 @@ public static class Ch1 { public static void Main(string[] args) + { + // Call whichever implementation you'd like to use. + PrintThirdMax_impl2(args); + } + + private static void PrintThirdMax_impl1(string[] args) { var firstThree = args.Select(int.Parse) .OrderDescending().Distinct() @@ -14,6 +20,16 @@ public static class Ch1 ); } + private static void PrintThirdMax_impl2(string[] args) + { + var firstThree = args.Select(int.Parse).Max3().ToArray(); + Console.WriteLine( + firstThree.Length >= 3 ? firstThree[2] : + firstThree.Length > 0 ? firstThree[0] : + "" + ); + } + private static IEnumerable Distinct(this IOrderedEnumerable source) { using var enumerator = source.GetEnumerator(); @@ -29,4 +45,26 @@ public static class Ch1 yield return current; } } + + /// Return the top 3 unique maximum integers in O(n) time. + private static IEnumerable Max3(this IEnumerable source) + { + const int numMaxValues = 3; + var comparer = Comparer.Default; + var maxValues = new List(); + foreach (var value in source) InsertIfNewMax(value); + return maxValues; + + void InsertIfNewMax(T value) + { + for (var i = 0; i < numMaxValues; i++) + { + if (i < maxValues.Count && comparer.Compare(value, maxValues[i]) == 0) break; + if (i < maxValues.Count && comparer.Compare(value, maxValues[i]) <= 0) continue; + maxValues.Insert(i, value); + if (maxValues.Count > numMaxValues) maxValues.RemoveAt(numMaxValues); + break; + } + } + } } \ No newline at end of file diff --git a/challenge-289/jtimothyking/perl/ch-1.pl b/challenge-289/jtimothyking/perl/ch-1.pl index 1ea9bf70f7..9c808466fc 100644 --- a/challenge-289/jtimothyking/perl/ch-1.pl +++ b/challenge-289/jtimothyking/perl/ch-1.pl @@ -4,7 +4,37 @@ use warnings; use List::Util qw(uniqint); -my @unique_ints = uniqint sort { $b <=> $a } @ARGV; -say $unique_ints[2] // $unique_ints[0]; +# Call whichever implementation you'd like to use. +implementation_2(); + +sub implementation_1 { + my @unique_ints = uniqint sort { $b <=> $a } @ARGV; + say $unique_ints[2] // $unique_ints[0]; +} + +sub implementation_2 { + my @max_ints = max3(@ARGV); + say $max_ints[2] // $max_ints[0]; +} + +# Return the top 3 unique maximum integers in O(n) time. +sub max3 { + my $num_max_ints = 3; + my @max_ints; + + my sub insert_if_new_max { + my $int = shift; + for my $i (0 .. $num_max_ints - 1) { + last if $int == $max_ints[$i]; + next if (defined $max_ints[$i] && $int < $max_ints[$i]); + splice @max_ints, $i, 0, $int; + pop @max_ints if @max_ints > $num_max_ints; + last; + } + } + + insert_if_new_max($_) for (@_); + return @max_ints; +} __END__ -- cgit