From 20ccf25c6944641c7db4b60fa15d520686cb7036 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Fri, 9 Feb 2024 15:28:55 +0100 Subject: feat(challenge-255/lubos-kolouch/perl,python,raku/): Challenge 255 LK Perl Python Raku --- challenge-255/lubos-kolouch/perl/ch-1.pl | 22 +++++++++++++++++++ challenge-255/lubos-kolouch/perl/ch-2.pl | 24 ++++++++++++++++++++ challenge-255/lubos-kolouch/python/ch-1.py | 24 ++++++++++++++++++++ challenge-255/lubos-kolouch/python/ch-2.py | 35 ++++++++++++++++++++++++++++++ challenge-255/lubos-kolouch/raku/ch-1.raku | 16 ++++++++++++++ challenge-255/lubos-kolouch/raku/ch-2.raku | 14 ++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 challenge-255/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-255/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-255/lubos-kolouch/python/ch-1.py create mode 100644 challenge-255/lubos-kolouch/python/ch-2.py create mode 100644 challenge-255/lubos-kolouch/raku/ch-1.raku create mode 100644 challenge-255/lubos-kolouch/raku/ch-2.raku diff --git a/challenge-255/lubos-kolouch/perl/ch-1.pl b/challenge-255/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..735422488f --- /dev/null +++ b/challenge-255/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; + +sub find_odd_character { + my ($s, $t) = @_; + + # Count characters in both strings + my %count; + $count{$_}++ for split //, $s; + $count{$_}-- for split //, $t; + + # Find the character with count -1 + foreach my $char (keys %count) { + return $char if $count{$char} == -1; + } +} + +# Tests +use Test::Simple tests => 3; +ok(find_odd_character("Perl", "Preel") eq "e", 'Test Example 1'); +ok(find_odd_character("Weekly", "Weeakly") eq "a", 'Test Example 2'); +ok(find_odd_character("Box", "Boxy") eq "y", 'Test Example 3'); diff --git a/challenge-255/lubos-kolouch/perl/ch-2.pl b/challenge-255/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..a1a73f376b --- /dev/null +++ b/challenge-255/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub most_frequent_word { + my ($p, $w) = @_; + my %word_count; + + # Clean and split the paragraph into words + $p =~ s/[^\w\s]//g; + my @words = split(/\s+/, lc $p); + + # Count the frequency of each word, excluding the banned word + $word_count{$_}++ for grep { $_ ne $w } @words; + + # Find the most frequent word + my $most_frequent = (sort { $word_count{$b} <=> $word_count{$a} } keys %word_count)[0]; + + return $most_frequent; +} + +# Test cases +print most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit"), "\n"; +print most_frequent_word("Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", "the"), "\n"; diff --git a/challenge-255/lubos-kolouch/python/ch-1.py b/challenge-255/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b09f8ab050 --- /dev/null +++ b/challenge-255/lubos-kolouch/python/ch-1.py @@ -0,0 +1,24 @@ +def find_odd_character(s: str, t: str) -> str: + """ + Finds the additional character in string t. + + :param s: Original string. + :param t: Modified string with one extra character. + :return: The additional character. + """ + from collections import Counter + + count_s = Counter(s) + count_t = Counter(t) + + for char in count_t: + if count_t[char] > count_s.get(char, 0): + return char + + return "" + + +# Tests +assert find_odd_character("Perl", "Preel") == "e" +assert find_odd_character("Weekly", "Weeakly") == "a" +assert find_odd_character("Box", "Boxy") == "y" diff --git a/challenge-255/lubos-kolouch/python/ch-2.py b/challenge-255/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..2c2f1d6807 --- /dev/null +++ b/challenge-255/lubos-kolouch/python/ch-2.py @@ -0,0 +1,35 @@ +def most_frequent_word(p: str, w: str) -> str: + """ + Find the most frequent word in a paragraph excluding a specific word. + + :param p: Paragraph as a string. + :param w: Banned word. + :return: Most frequent word. + """ + import re + from collections import Counter + + # Clean and split the paragraph into words + words = re.findall(r"\w+", p.lower()) + + # Count the frequency of each word, excluding the banned word + word_count = Counter(words) + if w in word_count: + del word_count[w] + + # Find the most frequent word + most_frequent = word_count.most_common(1)[0][0] + + return most_frequent + + +# Test cases +print( + most_frequent_word("Joe hit a ball, the hit ball flew far after it was hit.", "hit") +) +print( + most_frequent_word( + "Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", + "the", + ) +) diff --git a/challenge-255/lubos-kolouch/raku/ch-1.raku b/challenge-255/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..8cf738b730 --- /dev/null +++ b/challenge-255/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,16 @@ +sub find-odd-character(Str $s, Str $t --> Str) { + my %count; + %count{$_}++ for $s.comb; + %count{$_}-- for $t.comb; + + for %count.keys -> $char { + return $char if %count{$char} == -1; + } +} + +# Tests +use Test; +plan 3; +is find-odd-character("Perl", "Preel"), "e", 'Test Example 1'; +is find-odd-character("Weekly", "Weeakly"), "a", 'Test Example 2'; +is find-odd-character("Box", "Boxy"), "y", 'Test Example 3'; diff --git a/challenge-255/lubos-kolouch/raku/ch-2.raku b/challenge-255/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..223af36fe7 --- /dev/null +++ b/challenge-255/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,14 @@ +sub most-frequent-word(Str $p, Str $w) { + # Clean and split the paragraph into words + my @words = $p.subst(/<-[A..Za..z0..9\s]>/, '', :g).split(/\s+/).map(*.lc); + + # Count the frequency of each word, excluding the banned word + my %word-count = @words.grep(* ne $w).Bag; + + # Find the most frequent word + return %word-count.sort(*.value).reverse.head.key; +} + +# Test cases +say most-frequent-word("Joe hit a ball, the hit ball flew far after it was hit.", "hit"); +say most-frequent-word("Perl and Raku belong to the same family. Perl is the most popular language in the weekly challenge.", "the"); -- cgit