diff options
| author | Ali <adeadmarshal@gmail.com> | 2023-03-12 15:34:03 +0330 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-12 15:34:03 +0330 |
| commit | f03f41fc92c57f5d8e548b4ee32a31b78082b332 (patch) | |
| tree | 20d0d64a5ba8fcfa0aced89f20a8cbbaea629340 | |
| parent | ec8a291e57738084a11dd4870e651e462eab41f5 (diff) | |
| parent | 33c228c577d6ca63cba6571081a8a43dec55471a (diff) | |
| download | perlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.tar.gz perlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.tar.bz2 perlweeklychallenge-club-f03f41fc92c57f5d8e548b4ee32a31b78082b332.zip | |
Merge branch 'manwar:master' into TWC207
66 files changed, 4428 insertions, 2335 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-017/paulo-custodio/perl/ch-2.pl b/challenge-017/paulo-custodio/perl/ch-2.pl index 18f0e2c89e..e4cc41a84d 100644 --- a/challenge-017/paulo-custodio/perl/ch-2.pl +++ b/challenge-017/paulo-custodio/perl/ch-2.pl @@ -20,7 +20,7 @@ use Modern::Perl; -my $url = shift; +my $url = <>; my $word = qr{ [a-z_] [a-z_0-9+.-]* }ix; my $path = qr{ (?: $word | / )+ }ix; $url =~ m{^ (?<scheme> $word ) [:] diff --git a/challenge-017/paulo-custodio/python/ch-2.py b/challenge-017/paulo-custodio/python/ch-2.py index 7493986e88..3fda361c71 100644 --- a/challenge-017/paulo-custodio/python/ch-2.py +++ b/challenge-017/paulo-custodio/python/ch-2.py @@ -21,7 +21,7 @@ import sys import re -url = sys.argv[1] +url = sys.stdin.read().rstrip() word = r"(?i:[a-z_][a-z_0-9+.-]*)" pathre = r"(?:"+word+r"|/)+" diff --git a/challenge-017/paulo-custodio/t/test-2.yaml b/challenge-017/paulo-custodio/t/test-2.yaml index a1b82bbc9b..5225b60a3c 100644 --- a/challenge-017/paulo-custodio/t/test-2.yaml +++ b/challenge-017/paulo-custodio/t/test-2.yaml @@ -1,7 +1,7 @@ - setup: cleanup: - args: jdbc://user:password@localhost:3306/pwc?profile=true#h1 - input: + args: + input: jdbc://user:password@localhost:3306/pwc?profile=true#h1 output: | scheme: jdbc userinfo: user:password @@ -12,8 +12,8 @@ fragment: h1 - setup: cleanup: - args: jdbc://localhost:3306/pwc?profile=true#h1 - input: + args: + input: jdbc://localhost:3306/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -24,8 +24,8 @@ fragment: h1 - setup: cleanup: - args: jdbc://localhost/pwc?profile=true#h1 - input: + args: + input: jdbc://localhost/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -36,8 +36,8 @@ fragment: h1 - setup: cleanup: - args: jdbc:/pwc?profile=true#h1 - input: + args: + input: jdbc:/pwc?profile=true#h1 output: | scheme: jdbc userinfo: @@ -48,8 +48,8 @@ fragment: h1 - setup: cleanup: - args: jdbc:/pwc?profile=true - input: + args: + input: jdbc:/pwc?profile=true output: | scheme: jdbc userinfo: @@ -60,8 +60,8 @@ fragment: - setup: cleanup: - args: jdbc:/pwc - input: + args: + input: jdbc:/pwc output: | scheme: jdbc userinfo: diff --git a/challenge-093/paulo-custodio/Makefile b/challenge-093/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-093/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-093/paulo-custodio/c/ch-1.c b/challenge-093/paulo-custodio/c/ch-1.c index b796bf97eb..0145df5b10 100644 --- a/challenge-093/paulo-custodio/c/ch-1.c +++ b/challenge-093/paulo-custodio/c/ch-1.c @@ -32,6 +32,14 @@ Output: 3 #include <stdio.h> #include <stdlib.h> +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + typedef struct Point { int x, y; } Point; @@ -85,7 +93,7 @@ int main(int argc, char* argv[]) { // allocate memory for the point array and read it from argv[] points_size = (argc-1)/2; - points = calloc(points_size, sizeof(Point)); + points = check_mem(calloc(points_size, sizeof(Point))); assert(points); for (size_t i = 0; i < points_size; i++) { points[i].x = atoi(argv[1+2*i]); diff --git a/challenge-093/paulo-custodio/c/ch-2.c b/challenge-093/paulo-custodio/c/ch-2.c index 29f56f4f22..578ab7c199 100644 --- a/challenge-093/paulo-custodio/c/ch-2.c +++ b/challenge-093/paulo-custodio/c/ch-2.c @@ -47,9 +47,17 @@ typedef struct Tree { char** lines; size_t num_lines; +void* check_mem(void* p) { + if (!p) { + fputs("Out of memory", stderr); + exit(EXIT_FAILURE); + } + return p; +} + // create and delete a tree Tree* tree_new() { - Tree* node = calloc(1, sizeof(Tree)); + Tree* node = check_mem(calloc(1, sizeof(Tree))); assert(node); return node; } @@ -64,7 +72,7 @@ void tree_delete(Tree* node) { void lines_read() { char line[MAXLINE]; while (fgets(line, sizeof(line), stdin)) { - lines = realloc(lines, (num_lines + 1) * sizeof(char*)); + lines = check_mem(realloc(lines, (num_lines + 1) * sizeof(char*))); assert(lines); lines[num_lines] = strdup(line); assert(lines[num_lines]); diff --git a/challenge-098/paulo-custodio/Makefile b/challenge-098/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-098/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-100/paulo-custodio/Makefile b/challenge-100/paulo-custodio/Makefile new file m |
