diff options
| -rw-r--r-- | challenge-289/jtimothyking/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/csharp/ch-1.cs | 70 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/csharp/ch-2.cs | 25 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/perl/ch-2.pl | 19 |
5 files changed, 155 insertions, 0 deletions
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..8ecd2e058c --- /dev/null +++ b/challenge-289/jtimothyking/csharp/ch-1.cs @@ -0,0 +1,70 @@ +namespace ch_1; + +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() + .Take(3).ToArray(); + Console.WriteLine( + firstThree.Length >= 3 ? firstThree[2] : + firstThree.Length > 0 ? firstThree[0] : + "" + ); + } + + 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<T> Distinct<T>(this IOrderedEnumerable<T> source) + { + using var enumerator = source.GetEnumerator(); + if (!enumerator.MoveNext()) yield break; + var comparer = EqualityComparer<T>.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; + } + } + + /// <summary>Return the top 3 unique maximum integers in O(n) time.</summary> + private static IEnumerable<T> Max3<T>(this IEnumerable<T> source) + { + const int numMaxValues = 3; + var comparer = Comparer<T>.Default; + var maxValues = new List<T>(); + 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/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..9c808466fc --- /dev/null +++ b/challenge-289/jtimothyking/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use v5.38; +use warnings; + +use List::Util qw(uniqint); + +# 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__ 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__ |
