diff options
| author | Tim King <timk@theperlshop.com> | 2024-10-02 09:12:16 -0400 |
|---|---|---|
| committer | Tim King <timk@theperlshop.com> | 2024-10-02 09:12:16 -0400 |
| commit | d82f0dd189cf91d086b3aa9dc64fea3e6b05d423 (patch) | |
| tree | 9d0ca5ea617c580a00c3c27b7d8f90deb58e3e26 | |
| parent | 3260fe0e07221d6637b73740228a1b29678cea51 (diff) | |
| download | perlweeklychallenge-club-d82f0dd189cf91d086b3aa9dc64fea3e6b05d423.tar.gz perlweeklychallenge-club-d82f0dd189cf91d086b3aa9dc64fea3e6b05d423.tar.bz2 perlweeklychallenge-club-d82f0dd189cf91d086b3aa9dc64fea3e6b05d423.zip | |
Solutions to challenge 289 by Tim King
| -rw-r--r-- | challenge-289/jtimothyking/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/csharp/ch-1.cs | 32 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/csharp/ch-2.cs | 25 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-289/jtimothyking/perl/ch-2.pl | 19 |
5 files changed, 87 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..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<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; + } + } +}
\ 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__ |
