aboutsummaryrefslogtreecommitdiff
path: root/challenge-289/aplgr
diff options
context:
space:
mode:
authorAndré Plöger <andre.ploeger@gmx.de>2024-10-04 22:04:44 -0400
committerAndré Plöger <andre.ploeger@gmx.de>2024-10-04 22:04:44 -0400
commit89e60c3925ceb4966f6f2cd095b19eff7c87a876 (patch)
tree6bee33cc3daee280c58f208b38e5b01a20f7cd12 /challenge-289/aplgr
parent5bfb306153546664c3549d2aafab5a2bed4b1c88 (diff)
downloadperlweeklychallenge-club-89e60c3925ceb4966f6f2cd095b19eff7c87a876.tar.gz
perlweeklychallenge-club-89e60c3925ceb4966f6f2cd095b19eff7c87a876.tar.bz2
perlweeklychallenge-club-89e60c3925ceb4966f6f2cd095b19eff7c87a876.zip
Solution for PWC #289
Diffstat (limited to 'challenge-289/aplgr')
-rw-r--r--challenge-289/aplgr/README2
-rw-r--r--challenge-289/aplgr/blog.txt1
-rw-r--r--challenge-289/aplgr/go/ch-1.go31
-rw-r--r--challenge-289/aplgr/go/ch-1_test.go28
-rw-r--r--challenge-289/aplgr/go/ch-2.go48
-rw-r--r--challenge-289/aplgr/go/ch-2_test.go66
-rw-r--r--challenge-289/aplgr/perl/ch-1.pl13
-rw-r--r--challenge-289/aplgr/perl/ch-1.t20
-rw-r--r--challenge-289/aplgr/perl/ch-2.pl38
-rw-r--r--challenge-289/aplgr/perl/ch-2.t36
10 files changed, 282 insertions, 1 deletions
diff --git a/challenge-289/aplgr/README b/challenge-289/aplgr/README
index 5bd6a6b0cb..38ae16a447 100644
--- a/challenge-289/aplgr/README
+++ b/challenge-289/aplgr/README
@@ -1 +1 @@
-Solution by André Plöger.
+Solution by Andre Ploeger. \ No newline at end of file
diff --git a/challenge-289/aplgr/blog.txt b/challenge-289/aplgr/blog.txt
new file mode 100644
index 0000000000..870ac30f9c
--- /dev/null
+++ b/challenge-289/aplgr/blog.txt
@@ -0,0 +1 @@
+https://dev.to/aplgr/data-transformations-third-maximum-and-jumbled-letters-np3 \ No newline at end of file
diff --git a/challenge-289/aplgr/go/ch-1.go b/challenge-289/aplgr/go/ch-1.go
new file mode 100644
index 0000000000..db0bcc75a1
--- /dev/null
+++ b/challenge-289/aplgr/go/ch-1.go
@@ -0,0 +1,31 @@
+package main
+
+import (
+ "errors"
+ "sort"
+)
+
+func thirdMax(ints []int) (int, error) {
+ if len(ints) == 0 {
+ return 0, errors.New("input slice is empty")
+ }
+
+ unique := make(map[int]struct{})
+ for _, num := range ints {
+ unique[num] = struct{}{}
+ }
+
+ numsSorted := make([]int, 0, len(unique))
+ for num := range unique {
+ numsSorted = append(numsSorted, num)
+ }
+
+ sort.Slice(numsSorted, func(i, j int) bool {
+ return numsSorted[i] > numsSorted[j]
+ })
+
+ if len(numsSorted) >= 3 {
+ return numsSorted[2], nil
+ }
+ return numsSorted[0], nil
+}
diff --git a/challenge-289/aplgr/go/ch-1_test.go b/challenge-289/aplgr/go/ch-1_test.go
new file mode 100644
index 0000000000..72a634b3d0
--- /dev/null
+++ b/challenge-289/aplgr/go/ch-1_test.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+ "errors"
+ "testing"
+)
+
+func TestThirdMax(t *testing.T) {
+ tests := []struct {
+ args []int
+ exp int
+ err error
+ desc string
+ }{
+ {args: []int{5, 6, 4, 1}, exp: 4, err: nil, desc: "Third element"},
+ {args: []int{4, 5}, exp: 5, err: nil, desc: "Last element"},
+ {args: []int{1, 2, 2, 3}, exp: 1, err: nil, desc: "Third element with duplicates"},
+ {args: []int{3, 3, 3}, exp: 3, err: nil, desc: "Only one value"},
+ {args: []int{}, exp: 0, err: errors.New("input slice is empty"), desc: "Empty array"},
+ }
+
+ for _, test := range tests {
+ got, err := thirdMax(test.args)
+ if got != test.exp || (err != nil && err.Error() != test.err.Error()) {
+ t.Errorf("%s: got %d, want %d, error: %v", test.desc, got, test.exp, err)
+ }
+ }
+} \ No newline at end of file
diff --git a/challenge-289/aplgr/go/ch-2.go b/challenge-289/aplgr/go/ch-2.go
new file mode 100644
index 0000000000..3db1cd9012
--- /dev/null
+++ b/challenge-289/aplgr/go/ch-2.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "math/rand"
+ "regexp"
+ "strings"
+ "time"
+)
+
+func jumbleWord(word string) string {
+ if len(word) <= 3 {
+ return word
+ }
+
+ middle := word[1 : len(word)-1]
+ chars := []rune(middle)
+
+ rand.Seed(time.Now().UnixNano())
+ rand.Shuffle(len(chars), func(i, j int) {
+ chars[i], chars[j] = chars[j], chars[i]
+ })
+
+ return string(word[0]) + string(chars) + string(word[len(word)-1])
+}
+
+func jumbleText(text string) string {
+ re := regexp.MustCompile(`(\W+|_)`)
+ tokens := re.Split(text, -1)
+ nonWordTokens := re.FindAllString(text, -1)
+
+ var result []string
+
+ for i, token := range tokens {
+ if isAlpha(token) {
+ result = append(result, jumbleWord(token))
+ }
+ if i < len(nonWordTokens) {
+ result = append(result, nonWordTokens[i])
+ }
+ }
+
+ return strings.Join(result, "")
+}
+
+func isAlpha(s string) bool {
+ re := regexp.MustCompile(`^[A-Za-z]+$`)
+ return re.MatchString(s)
+}
diff --git a/challenge-289/aplgr/go/ch-2_test.go b/challenge-289/aplgr/go/ch-2_test.go
new file mode 100644
index 0000000000..0626981421
--- /dev/null
+++ b/challenge-289/aplgr/go/ch-2_test.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "regexp"
+ "sort"
+ "strings"
+ "testing"
+)
+
+func TestJumbleWord(t *testing.T) {
+ tests := []struct {
+ input string
+ desc string
+ }{
+ {"abc", "Test case 1: Short word remains unchanged"},
+ {"word", "Test case 2: First and last letter remain the same"},
+ {"jumbled", "Test case 3: First and last letter remain the same"},
+ }
+
+ for _, test := range tests {
+ jumbled := jumbleWord(test.input)
+
+ if jumbled[0] != test.input[0] {
+ t.Errorf("%s - First letter remains the same", test.desc)
+ }
+ if jumbled[len(jumbled)-1] != test.input[len(test.input)-1] {
+ t.Errorf("%s - Last letter remains the same", test.desc)
+ }
+ if !lettersPreserved(test.input, jumbled) {
+ t.Errorf("%s - All letters are preserved", test.desc)
+ }
+ }
+}
+
+func lettersPreserved(original, jumbled string) bool {
+ originalLetters := strings.Split(original, "")
+ jumbledLetters := strings.Split(jumbled, "")
+
+ sort.Strings(originalLetters)
+ sort.Strings(jumbledLetters)
+
+ return strings.Join(originalLetters, "") == strings.Join(jumbledLetters, "")
+}
+
+func TestJumbleText(t *testing.T) {
+ inputText := "According to a research at Cambridge University, it doesn't matter in what order the letters in a word are."
+ jumbledText := jumbleText(inputText)
+
+ inputWords := regexp.MustCompile(`(\W+|_)`).Split(inputText, -1)
+ jumbledWords := regexp.MustCompile(`(\W+|_)`).Split(jumbledText, -1)
+
+ for i, word := range inputWords {
+ if word == "" {
+ continue
+ }
+ if jumbledWords[i][0] != word[0] {
+ t.Errorf("First letter remains the same for '%s'", word)
+ }
+ if jumbledWords[i][len(jumbledWords[i])-1] != word[len(word)-1] {
+ t.Errorf("Last letter remains the same for '%s'", word)
+ }
+ if !lettersPreserved(word, jumbledWords[i]) {
+ t.Errorf("All letters are preserved for '%s'", word)
+ }
+ }
+}
diff --git a/challenge-289/aplgr/perl/ch-1.pl b/challenge-289/aplgr/perl/ch-1.pl
new file mode 100644
index 0000000000..1b42465adb
--- /dev/null
+++ b/challenge-289/aplgr/perl/ch-1.pl
@@ -0,0 +1,13 @@
+use strict;
+use warnings;
+
+sub third_maximum {
+ my @ints = @_;
+
+ my %unique = map { $_ => 1 } @ints;
+ my @distinct = sort { $b <=> $a } keys %unique;
+
+ return @distinct >= 3 ? $distinct[2] : $distinct[0];
+}
+
+1; \ No newline at end of file
diff --git a/challenge-289/aplgr/perl/ch-1.t b/challenge-289/aplgr/perl/ch-1.t
new file mode 100644
index 0000000000..80e3b6f0c3
--- /dev/null
+++ b/challenge-289/aplgr/perl/ch-1.t
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use Test::More;
+
+require './ch-1.pl';
+
+my @tests = (
+ { args => [5, 6, 4, 1], exp => 4, desc => 'Third element' },
+ { args => [4, 5], exp => 5, desc => 'Last element' },
+ { args => [1, 2, 2, 3], exp => 1, desc => 'Third element with duplicates' },
+ { args => [3, 3, 3], exp => 3, desc => 'Only one value' },
+ { args => [], exp => undef, desc => 'Empty array' },
+);
+
+# Tests ausführen
+foreach my $test (@tests) {
+ is(third_maximum(@{$test->{args}}), $test->{exp}, $test->{desc});
+}
+
+done_testing(); \ No newline at end of file
diff --git a/challenge-289/aplgr/perl/ch-2.pl b/challenge-289/aplgr/perl/ch-2.pl
new file mode 100644
index 0000000000..fa36adaa57
--- /dev/null
+++ b/challenge-289/aplgr/perl/ch-2.pl
@@ -0,0 +1,38 @@
+use strict;
+use warnings;
+use List::Util 'shuffle';
+
+sub jumble_word {
+ my ($word) = @_;
+
+ return $word if length($word) <= 3;
+
+ my $middle = substr($word, 1, -1);
+ my @m_chars = split('', $middle);
+ @m_chars = shuffle(@m_chars);
+
+ my $subst = join('', @m_chars);
+ substr($word, 1, -1, $subst);
+
+ return $word;
+}
+
+sub jumble_text {
+ my ($text) = @_;
+
+ my @tokens = split(/(\W+|_)/, $text);
+
+ for my $token (@tokens) {
+ if ($token =~ /^[A-Za-z]+$/) {
+ $token = jumble_word($token);
+ }
+ }
+
+ return join('', @tokens);
+}
+
+my $input_text = "According to a research at Cambridge University, it doesn't matter in what order the letters in a word are.";
+my $jumbled_text = jumble_text($input_text);
+print "$jumbled_text\n";
+
+1; \ No newline at end of file
diff --git a/challenge-289/aplgr/perl/ch-2.t b/challenge-289/aplgr/perl/ch-2.t
new file mode 100644
index 0000000000..cb7374981a
--- /dev/null
+++ b/challenge-289/aplgr/perl/ch-2.t
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use Test::More;
+require './ch-2.pl';
+
+# Test cases for jumble_word
+my @word_tests = (
+ { input => 'abc', desc => 'Test case 1: Short word remains unchanged' },
+ { input => 'word', desc => 'Test case 2: First and last letter remain the same' },
+ { input => 'jumbled', desc => 'Test case 3: First and last letter remain the same' },
+);
+
+# Run tests for jumble_word
+foreach my $test (@word_tests) {
+ my $jumbled = jumble_word($test->{input});
+
+ # Check if the first and last letter remain the same
+ is(substr($jumbled, 0, 1), substr($test->{input}, 0, 1), "$test->{desc} - First letter remains the same");
+ is(substr($jumbled, -1), substr($test->{input}, -1), "$test->{desc} - Last letter remains the same");
+ cmp_ok([sort split('', $test->{input})], 'cmp', [sort split('', $jumbled)], "$test->{desc} - All letters are preserved");
+}
+
+# Test cases for jumble_text
+my $input_text = "According to a research at Cambridge University, it doesn't matter in what order the letters in a word are.";
+my $jumbled_text = jumble_text($input_text);
+
+my @input_words = split(/\W+|_/, $input_text);
+my @jumbled_words = split(/\W+|_/, $jumbled_text);
+
+for (my $i = 0; $i < scalar @input_words; $i++) {
+ is(substr($jumbled_words[$i], 0, 1), substr($input_words[$i], 0, 1), "First letter remains the same for '$input_words[$i]'");
+ is(substr($jumbled_words[$i], -1), substr($input_words[$i], -1), "Last letter remains the same for '$input_words[$i]'");
+ cmp_ok([sort split('', $input_words[$i])], 'cmp', [sort split('', $jumbled_words[$i])], "All letters are preserved for '$input_words[$i]'");
+}
+
+done_testing();