diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-13 12:37:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-13 12:37:37 +0000 |
| commit | d18742a481f56afe01099d0a3252810bfbfef65c (patch) | |
| tree | 6403921b1f962095cb9cfd42e23228b855302606 | |
| parent | 32d3b1d2c7cc578c4347836715cbc8040cc20afb (diff) | |
| parent | 733b9428a3c65d35f20504ac9d0122379bad71a9 (diff) | |
| download | perlweeklychallenge-club-d18742a481f56afe01099d0a3252810bfbfef65c.tar.gz perlweeklychallenge-club-d18742a481f56afe01099d0a3252810bfbfef65c.tar.bz2 perlweeklychallenge-club-d18742a481f56afe01099d0a3252810bfbfef65c.zip | |
Merge branch 'manwar:master' into master
94 files changed, 4828 insertions, 2398 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 |
