aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-11 23:14:00 +0000
committerGitHub <noreply@github.com>2023-03-11 23:14:00 +0000
commitb851618685a2fbdfbcc76a9922ff7fc30be55b3b (patch)
tree89de4c30abc8eb58de539d9394835b2bd159144d
parent1f9e919800ebc28992c10467f2e9a2d1afe90169 (diff)
parente1775c1d9e4d74de0a21e4e971a0eb701f21c8a5 (diff)
downloadperlweeklychallenge-club-b851618685a2fbdfbcc76a9922ff7fc30be55b3b.tar.gz
perlweeklychallenge-club-b851618685a2fbdfbcc76a9922ff7fc30be55b3b.tar.bz2
perlweeklychallenge-club-b851618685a2fbdfbcc76a9922ff7fc30be55b3b.zip
Merge pull request #7706 from LubosKolouch/master
Blog 207
-rw-r--r--challenge-005/lubos-kolouch/perl/ch-1.pl40
-rw-r--r--challenge-005/lubos-kolouch/perl/ch-2.pl57
-rw-r--r--challenge-005/lubos-kolouch/python/ch-1.py43
-rw-r--r--challenge-005/lubos-kolouch/python/ch-2.py81
-rw-r--r--challenge-006/lubos-kolouch/perl/ch-1.pl72
-rw-r--r--challenge-006/lubos-kolouch/python/ch-1.py50
-rw-r--r--challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md121
7 files changed, 464 insertions, 0 deletions
diff --git a/challenge-005/lubos-kolouch/perl/ch-1.pl b/challenge-005/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..01c8817f8c
--- /dev/null
+++ b/challenge-005/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+sub print_anagrams {
+ my $word = shift;
+ my %freq;
+
+ # Count the frequency of each letter in the word
+ $freq{$_}++ for split //, $word;
+
+ # Recursively generate all anagrams
+ sub generate_anagrams {
+ my ( $prefix, $freq_ref ) = @_;
+
+ # Base case: no more letters left to add
+ if ( length $prefix == length $word ) {
+ print "$prefix\n";
+ return;
+ }
+
+ # Recursive case: add one more letter to the prefix
+ for my $letter ( keys %$freq_ref ) {
+ if ( $freq_ref->{$letter} > 0 ) {
+ $freq_ref->{$letter}--;
+ generate_anagrams( $prefix . $letter, $freq_ref );
+ $freq_ref->{$letter}++;
+ }
+ }
+ }
+
+ # Start generating anagrams with an empty prefix and the frequency hash
+ generate_anagrams( "", \%freq );
+}
+
+# Test the function with some example words
+print_anagrams("hello");
+print_anagrams("pear");
+print_anagrams("racecar");
diff --git a/challenge-005/lubos-kolouch/perl/ch-2.pl b/challenge-005/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..918be4654e
--- /dev/null
+++ b/challenge-005/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# This program finds the sequence of characters that has the most anagrams.
+
+=head1 SYNOPSIS
+
+This program finds the sequence of characters that has the most anagrams.
+
+=cut
+
+# Function to find the sequence of characters that has the most anagrams
+sub find_most_anagrams {
+ my ($str) = @_;
+
+ # Create a hash to store all the anagrams
+ my %anagrams;
+
+ # Iterate over each substring of the given string
+ for my $substring ( substrings($str) ) {
+
+ # Sort the characters of the substring
+ my $sorted_substring = join '', sort split //, $substring;
+
+ # Add the substring to the anagrams hash
+ push @{ $anagrams{$sorted_substring} }, $substring;
+ }
+
+ # Find the sequence with the most anagrams
+ my $max_anagrams = 0;
+ my $max_sequence = "";
+ for my $sequence ( keys %anagrams ) {
+ my $count = scalar @{ $anagrams{$sequence} };
+ if ( $count > $max_anagrams ) {
+ $max_anagrams = $count;
+ $max_sequence = $sequence;
+ }
+ }
+
+ return $max_sequence;
+}
+
+# Function to generate all substrings of a given string
+sub substrings {
+ my ($str) = @_;
+
+ my @substrings;
+ for my $i ( 0 .. length($str) - 1 ) {
+ for my $j ( $i .. length($str) ) {
+ push @substrings, substr( $str, $i, $j - $i );
+ }
+ }
+
+ return @substrings;
+}
diff --git a/challenge-005/lubos-kolouch/python/ch-1.py b/challenge-005/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..131d7f5333
--- /dev/null
+++ b/challenge-005/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+This program prints all anagrams of a given word.
+
+Input:
+word - a string representing the word to find anagrams of
+
+Output:
+A list of all anagrams of the given word
+
+"""
+
+import itertools
+
+
+def get_anagrams(word: str) -> set:
+ """
+ This function takes a word and finds all anagrams of it
+
+ Input:
+ word - a string representing the word to find anagrams of
+
+ Output:
+ A list of all anagrams of the given word
+
+ """
+
+ # generate all permutations of the given word
+ permutations = itertools.permutations(word)
+
+ # convert the permutations to strings
+ anagrams = ["".join(p) for p in permutations]
+
+ # remove duplicates
+ anagrams = list(set(anagrams))
+
+ return set(anagrams)
+
+
+# tests
+assert get_anagrams("cat") == {"tca", "tac", "act", "atc", "cta", "cat"}
diff --git a/challenge-005/lubos-kolouch/python/ch-2.py b/challenge-005/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..c5d59cd975
--- /dev/null
+++ b/challenge-005/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+This program finds the sequence of characters that has the most anagrams.
+
+Input:
+A string of any length
+
+Output:
+The sequence of characters that has the most anagrams
+
+"""
+
+import itertools
+
+
+def find_anagrams(string):
+ """
+ This function takes a string and returns a list of all its anagrams.
+
+ Parameters:
+ string (str): The string to be used
+
+ Returns:
+ anagrams (list): A list of all the anagrams of the input string
+ """
+
+ all_permutations = list(itertools.permutations(string))
+ anagrams = []
+
+ for permutation in all_permutations:
+ anagram = "".join(permutation)
+ anagrams.append(anagram)
+
+ return anagrams
+
+
+def most_anagrams(string):
+ """
+ This function takes a string and returns the sequence of characters that has the most anagrams.
+
+ Parameters:
+ string (str): The string to be used
+
+ Returns:
+ most_anagrams (str): The sequence of characters that has the most anagrams
+ """
+
+ all_anagrams = []
+
+ for i in range(1, len(string) + 1):
+ all_anagrams.append(find_anagrams(string[:i]))
+
+ max_length = 0
+ most_anagrams = ""
+
+ for anagrams in all_anagrams:
+ if len(anagrams) > max_length:
+ max_length = len(anagrams)
+ most_anagrams = anagrams[0]
+
+ return most_anagrams
+
+
+# Tests
+
+
+def test_find_anagrams():
+ assert find_anagrams("cat") == ["cat", "cta", "act", "atc", "tca", "tac"]
+
+
+def test_most_anagrams():
+ assert most_anagrams("cat") == "ca"
+
+
+if __name__ == "__main__":
+ string = input("Please enter a string: ")
+ print(
+ f"The sequence of characters that has the most anagrams is: {most_anagrams(string)}"
+ )
diff --git a/challenge-006/lubos-kolouch/perl/ch-1.pl b/challenge-006/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..03722cf548
--- /dev/null
+++ b/challenge-006/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+=head1 DESCRIPTION
+
+This script takes a list of numbers from command line and print the same in the compact form.
+
+For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.
+
+=cut
+
+# Get the list of numbers from command line
+my @numbers = @ARGV;
+
+# Sort the list of numbers in ascending order
+@numbers = sort { $a <=> $b } @numbers;
+
+# Initializing variables
+my @compact_list;
+my $first_num = $numbers[0];
+my $last_num = $numbers[0];
+
+# Generate the compact list
+foreach my $num (@numbers) {
+ if ( $num == $last_num + 1 ) {
+
+ # If the current number is 1 more than the last number,
+ # update the last number
+ $last_num = $num;
+ }
+ else {
+ # If the current number is not 1 more than the last number,
+ # add the range of numbers to the compact list
+ if ( $first_num == $last_num ) {
+ push @compact_list, $first_num;
+ }
+ else {
+ push @compact_list, "$first_num-$last_num";
+ }
+
+ # Reset the variables
+ $first_num = $num;
+ $last_num = $num;
+ }
+}
+
+# Add the last range of numbers to the compact list
+if ( $first_num == $last_num ) {
+ push @compact_list, $first_num;
+}
+else {
+ push @compact_list, "$first_num-$last_num";
+}
+
+# Print the compact list
+print join( ',', @compact_list );
+
+=head1 TESTING
+
+=over 4
+
+=item *
+
+Input: 1,2,3,4,9,10,14,15,16
+
+Expected Output: 1-4,9,10,14-16
+
+=back
+
+=cut
diff --git a/challenge-006/lubos-kolouch/python/ch-1.py b/challenge-006/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..24f6e3d0e0
--- /dev/null
+++ b/challenge-006/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""
+This script takes a list of numbers from command line and prints them in the compact form.
+
+Example:
+ Input: 1,2,3,4,9,10,14,15,16
+ Output: 1-4,9,10,14-16
+"""
+
+import sys
+
+
+def CompactList(numbers):
+ """This function takes a list of numbers as input and returns the list in compact form.
+
+ Args:
+ numbers (list): A list of numbers
+
+ Returns:
+ str: The list in compact form
+
+ Examples:
+ >>> CompactList([1,2,3,4,9,10,14,15,16])
+ '1-4,9,10,14-16'
+ """
+ compact_list = []
+ start = numbers[0]
+ end = numbers[0]
+ for i in range(1, len(numbers)):
+ if numbers[i] - numbers[i - 1] == 1:
+ end = numbers[i]
+ else:
+ if start == end:
+ compact_list.append(str(start))
+ else:
+ compact_list.append(str(start) + "-" + str(end))
+ start = numbers[i]
+ end = numbers[i]
+ if start == end:
+ compact_list.append(str(start))
+ else:
+ compact_list.append(str(start) + "-" + str(end))
+ return ",".join(compact_list)
+
+
+if __name__ == "__main__":
+ numbers = [int(i) for i in sys.argv[1].split(",")]
+ print(CompactList(numbers))
diff --git a/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md b/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md
new file mode 100644
index 0000000000..cf5b28321d
--- /dev/null
+++ b/challenge-207/lubos-kolouch/20230311_Weekly_challenge_207.md
@@ -0,0 +1,121 @@
+# THE WEEKLY CHALLENGE - 207
+
+###
+
+https://theweeklychallenge.org/blog/perl-weekly-challenge-207/
+
+**March 11th, 2023**
+
+## Task 1 - How to Check if a Word is Typable with a Single Keyboard Row
+
+We all know the feeling of typing out a long word and suddenly realizing that you had to switch keyboard rows to do it. Well, now there's a way to check if a word can be typed using only one row of a standard QWERTY keyboard!
+
+This Perl code snippet uses a hash map to store the keyboard layout, then checks it against a given word to see if it can be typed using only one row:
+
+```perl
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+
+my @keyboard_rows = qw(qwertyuiop asdfghjkl zxcvbnm);
+my %keyboard_map;
+
+# Create a hash map to store the keyboard layout
+for my $i ( 0 .. 2 ) {
+ my $row = $keyboard_rows[$i];
+ $keyboard_map{$_} = $i + 1 for split( '', $row );
+}
+
+# Function to check if a word can be typed using only one row of the keyboard
+sub is_single_row_word {
+ my ($word) = @_;
+ my $row = $keyboard_map{ lc( substr( $word, 0, 1 ) ) };
+ for my $c ( split( '', lc($word) ) ) {
+ return 0 if $keyboard_map{$c} != $row;
+ }
+ return 1;
+}
+
+# Test example 1
+my @words1 = ( "Hello", "Alaska", "Dad", "Peace" );
+my @single_row_words1 = grep { is_single_row_word($_) } @words1;
+is_deeply( \@single_row_words1, [ "Alaska", "Dad" ], "Example 1" );
+
+# Test example 2
+my @words2 = ( "OMG", "Bye" );
+my @single_row_words2 = grep { is_single_row_word($_) } @words2;
+is_deeply( \@single_row_words2, [], "Example 2" );
+
+done_testing();
+```
+
+This code uses the `grep` function to filter a list of words and return the ones that can be typed using only one row of the keyboard. For example, the list `"Hello", "Alaska", "Dad", "Peace"` would return `"Alaska", "Dad"` as these are the only two words that can be typed with a single row.
+
+Now you can be sure that you're not typing words that require two different keyboard rows!
+
+## Task 2 - Unlocking the Mysteries of the H-Index with Perl
+
+Have you ever wanted to know your H-Index? It's not a mystery anymore! With the help of Perl, you can figure out your own H-Index in no time.
+
+An H-Index is the largest number h such that h articles have at least h citations each. This is used to measure the impact of a researcher's work.
+
+Here is how to calculate your H-Index using Perl.
+
+First, you need to use strict and warnings in your code.
+
+```perl
+use strict;
+use warnings;
+```
+
+Then, you should use feature 'say' to make your code more readable.
+
+```perl
+use feature 'say';
+```
+
+Next, you will need to include the Test::More module. This will allow you to run tests on your code.
+
+```perl
+use Test::More;
+```
+
+Now, you can create a subroutine to calculate your H-Index.
+
+```perl
+sub h_index {
+ my @citations = @_;
+ my $n = scalar @citations;
+ my $h = 0;
+ my @sorted_citations = sort { $b <=> $a } @citations;
+
+ for ( my $i = 0 ; $i < $n ; $i++ ) {
+ if ( $sorted_citations[$i] >= $i + 1 ) {
+ $h = $i + 1;
+ }
+ else {
+ last;
+ }
+ }
+ return $h;
+}
+```
+
+Finally, you can run tests on your code to make sure it is working correctly.
+
+```perl
+# Run the tests
+my @citations_1 = ( 10, 8, 5, 4, 3 );
+my $h_index_1 = h_index(@citations_1);
+is( $h_index_1, 4, "Test Example 1" );
+
+my @citations_2 = ( 25, 8, 5, 3, 3 );
+my $h_index_2 = h_index(@citations_2);
+is( $h_index_2, 3, "Test Example 2" );
+
+done_testing();
+```
+
+And there you have it! You can now calculate your own H-Index using Perl.