diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-13 08:07:11 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-13 08:07:11 +0000 |
| commit | 33806e13a5128e2247736254bb3add02ebe29ef8 (patch) | |
| tree | 0f463831c4a02e402532aa927cf799c7a3d4703a | |
| parent | f11428be4c3cb93df2041f02b4590b5a8265d664 (diff) | |
| parent | 993f42239dd76abf920c8f43994eeae1f9b84948 (diff) | |
| download | perlweeklychallenge-club-33806e13a5128e2247736254bb3add02ebe29ef8.tar.gz perlweeklychallenge-club-33806e13a5128e2247736254bb3add02ebe29ef8.tar.bz2 perlweeklychallenge-club-33806e13a5128e2247736254bb3add02ebe29ef8.zip | |
Merge pull request #7715 from LubosKolouch/master
Challenge 007 008 LK Perl Python
| -rw-r--r-- | challenge-007/lubos-kolouch/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-007/lubos-kolouch/perl/ch-2.pl | 42 | ||||
| -rw-r--r-- | challenge-007/lubos-kolouch/python/ch-1.py | 22 | ||||
| -rw-r--r-- | challenge-007/lubos-kolouch/python/ch-2.py | 48 | ||||
| -rw-r--r-- | challenge-008/lubos-kolouch/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-008/lubos-kolouch/perl/ch-2.pl | 24 | ||||
| -rw-r--r-- | challenge-008/lubos-kolouch/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-008/lubos-kolouch/python/ch-2.py | 24 |
8 files changed, 243 insertions, 0 deletions
diff --git a/challenge-007/lubos-kolouch/perl/ch-1.pl b/challenge-007/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..9c583cee75 --- /dev/null +++ b/challenge-007/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,21 @@ +use strict; +use warnings; + +# Function to calculate the sum of digits +sub sum_of_digits { + my $number = shift; + my $sum = 0; + while ( $number > 0 ) { + $sum += $number % 10; + $number = int( $number / 10 ); + } + return $sum; +} + +# Main program +for my $num ( 0 .. 50 ) { + my $sum = sum_of_digits($num); + if ( $sum > 0 && $num % $sum == 0 ) { + print "$num\n"; + } +} diff --git a/challenge-007/lubos-kolouch/perl/ch-2.pl b/challenge-007/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..84b3903e8d --- /dev/null +++ b/challenge-007/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use strict; +use warnings; + +sub find_shortest_ladder { + my ( $word1, $word2, @wordlist ) = @_; + + # Check if input words are valid + my $wordlen = length($word1); + my %wordset = map { $_ => 1 } @wordlist; + unless ( $wordset{$word1} && $wordset{$word2} && length($word2) == $wordlen ) { + return (); + } + + # Use breadth-first search to find shortest ladder + my @queue = [ $word1, [$word1] ]; + my %visited = ( $word1 => 1 ); + while (@queue) { + my ( $word, $path ) = @{ shift(@queue) }; + for my $i ( 0 .. $wordlen - 1 ) { + for my $char ( 'a' .. 'z' ) { + my $newword = substr( $word, 0, $i ) . $char . substr( $word, $i + 1 ); + if ( $newword eq $word2 ) { + return [ @$path, $word2 ]; + } + if ( $wordset{$newword} && !$visited{$newword} ) { + push @queue, [ $newword, [ @$path, $newword ] ]; + $visited{$newword} = 1; + } + } + } + } + + # If no ladder is found, return empty list + return (); +} + +# Test cases +my @wordlist = ( 'cold', 'cord', 'core', 'care', 'card', 'ward', 'warm', 'worm' ); +my $ladder = find_shortest_ladder( 'cold', 'warm', @wordlist ); +my $expected = [ 'cold', 'cord', 'card', 'ward', 'warm' ]; +die "Test case 1 failed" unless ( join( ',', @$ladder ) eq join( ',', @$expected ) ); diff --git a/challenge-007/lubos-kolouch/python/ch-1.py b/challenge-007/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..02a91e9edf --- /dev/null +++ b/challenge-007/lubos-kolouch/python/ch-1.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +def sum_of_digits(number: int) -> int: + """Return the sum of digits of a given number.""" + total = 0 + while number > 0: + total += number % 10 + number //= 10 + return total + + +def print_niven_numbers() -> None: + """Print all the niven numbers from 0 to 50 inclusive.""" + for num in range(51): + digit_sum = sum_of_digits(num) + if digit_sum != 0 and num % digit_sum == 0: + print(num) + + +if __name__ == '__main__': + print_niven_numbers() diff --git a/challenge-007/lubos-kolouch/python/ch-2.py b/challenge-007/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..29aaf2ccce --- /dev/null +++ b/challenge-007/lubos-kolouch/python/ch-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List, Tuple + + +def find_shortest_ladder(word1: str, word2: str, wordlist: List[str]) -> List[str]: + """ + Find the shortest word ladder between two input words. + + Args: + word1: The first input word. + word2: The second input word. + wordlist: A list of words to search for the word ladder. + + Returns: + A list of words in the ladder if it exists, otherwise an empty list. + """ + # Check if input words are valid + wordlen = len(word1) + wordset = set(wordlist) + if word1 not in wordset or word2 not in wordset or len(word2) != wordlen: + return [] + + # Use breadth-first search to find shortest ladder + queue = [(word1, [word1])] + visited = set([word1]) + while queue: + word, path = queue.pop(0) + for i in range(wordlen): + for char in "abcdefghijklmnopqrstuvwxyz": + newword = word[:i] + char + word[i + 1 :] + if newword == word2: + return path + [word2] + if newword in wordset and newword not in visited: + queue.append((newword, path + [newword])) + visited.add(newword) + + # If no ladder is found, return empty list + return [] + + +# Test cases +wordlist = ["cold", "cord", "core", "care", "card", "ward", "warm", "worm"] + +# Test case 1: Find the shortest ladder from "cold" to "warm" +ladder = find_shortest_ladder("cold", "warm", wordlist) +assert ladder == ["cold", "cord", "card", "ward", "warm"], "Test case 1 failed" diff --git a/challenge-008/lubos-kolouch/perl/ch-1.pl b/challenge-008/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..e653bb41f7 --- /dev/null +++ b/challenge-008/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,32 @@ +use strict; +use warnings; + +sub is_prime { + my $num = shift; + return 0 if ( $num < 2 ); + for ( my $i = 2 ; $i <= sqrt($num) ; $i++ ) { + return 0 if ( $num % $i == 0 ); + } + return 1; +} + +sub get_next_prime { + my $num = shift; + $num++; + while ( !is_prime($num) ) { + $num++; + } + return $num; +} + +my @perfect_numbers = (); +my $p = 2; +my $num = 2**( $p - 1 ) * ( 2**$p - 1 ); +while ( scalar(@perfect_numbers) < 5 ) { + push @perfect_numbers, $num; + $p = get_next_prime($p); + $num = 2**( $p - 1 ) * ( 2**$p - 1 ); +} + +print "The first five perfect numbers are: "; +print join( ", ", @perfect_numbers ); diff --git a/challenge-008/lubos-kolouch/perl/ch-2.pl b/challenge-008/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..1d622194a6 --- /dev/null +++ b/challenge-008/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,24 @@ +sub center { + my @lines = @_; + + # Find the maximum length of the lines + my $max_length = 0; + foreach my $line (@lines) { + my $length = length($line); + if ( $length > $max_length ) { + $max_length = $length; + } + } + + # Center each line by adding spaces at the beginning + my @centered_lines; + foreach my $line (@lines) { + my $padding = ( $max_length - length($line) ) / 2; + my $centered_line = ' ' x int($padding) . $line; + push @centered_lines, $centered_line; + } + + return @centered_lines; +} + +print( center( "This", "is", "a test of the", "center function" ) ); diff --git a/challenge-008/lubos-kolouch/python/ch-1.py b/challenge-008/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..510cecef94 --- /dev/null +++ b/challenge-008/lubos-kolouch/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +def is_prime(num: int) -> bool: + if num < 2: + return False + for i in range(2, int(num**0.5) + 1): + if num % i == 0: + return False + return True + + +def get_next_prime(num: int) -> int: + num += 1 + while not is_prime(num): + num += 1 + return num + + +perfect_numbers = [] +p = 2 +num = 2 ** (p - 1) * (2**p - 1) + +while len(perfect_numbers) < 5: + perfect_numbers.append(num) + p = get_next_prime(p) + num = 2 ** (p - 1) * (2**p - 1) + +print("The first five perfect numbers are:", perfect_numbers) diff --git a/challenge-008/lubos-kolouch/python/ch-2.py b/challenge-008/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..0b0c06baa3 --- /dev/null +++ b/challenge-008/lubos-kolouch/python/ch-2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def center(lines: List[str]) -> List[str]: + # Find the maximum length of the lines + max_length = max(len(line) for line in lines) + + # Center each line by adding spaces at the beginning + centered_lines = [] + for line in lines: + padding = (max_length - len(line)) // 2 + centered_line = " " * padding + line + centered_lines.append(centered_line) + + return centered_lines + + +# Test cases +lines1 = ["This", "is", "a test of the", "center function"] +expected1 = [" This", " is", " a test of the", "center function"] +assert center(lines1) == expected1 |
