diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-03-26 13:11:43 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-03-26 13:11:43 +0200 |
| commit | 2a6bb0ae68aad6f29c807dc4f0423768cc845d9a (patch) | |
| tree | 443f0decd33a748576a4e759ac2ea121319ebcc6 /challenge-038 | |
| parent | e654b9a8e0b8cb51c472c474d80c9b71affb892a (diff) | |
| download | perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.gz perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.tar.bz2 perlweeklychallenge-club-2a6bb0ae68aad6f29c807dc4f0423768cc845d9a.zip | |
Challenges 035 038 042 LK Perl Python
Diffstat (limited to 'challenge-038')
| -rw-r--r-- | challenge-038/lubos-kolouch/perl/ch-1.pl | 39 | ||||
| -rw-r--r-- | challenge-038/lubos-kolouch/perl/ch-2.pl | 120 | ||||
| -rw-r--r-- | challenge-038/lubos-kolouch/python/ch-1.py | 30 | ||||
| -rw-r--r-- | challenge-038/lubos-kolouch/python/ch-2.py | 95 |
4 files changed, 284 insertions, 0 deletions
diff --git a/challenge-038/lubos-kolouch/perl/ch-1.pl b/challenge-038/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..91a9f3d71d --- /dev/null +++ b/challenge-038/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More tests => 6; + +sub date_finder { + my ($input) = @_; + if ( length($input) != 7 ) { + die "Input must be 7 digits long\n"; + } + + my ( $century_digit, $year_digits, $month_digits, $day_digits ) = + unpack "A1A2A2A2", $input; + + my $century = $century_digit == 1 ? "20" : "19"; + my $year = "$century$year_digits"; + + if ( $month_digits < 1 || $month_digits > 12 ) { + die "Invalid month\n"; + } + + my @days_in_month = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ); + if ( $year % 4 == 0 && ( $year % 100 != 0 || $year % 400 == 0 ) ) { + $days_in_month[2] = 29; + } + + if ( $day_digits < 1 || $day_digits > $days_in_month[$month_digits] ) { + die "Invalid day\n"; + } + + return sprintf "%04d-%02d-%02d", $year, $month_digits, $day_digits; +} + +is( date_finder('2230120'), '1923-01-20', 'Test case: valid date' ); +is( date_finder('2230230'), '1923-02-30', 'Test case: invalid day' ); +is( date_finder('2230015'), '1923-00-15', 'Test case: invalid month' ); +is( date_finder('1230229'), '2023-02-29', 'Test case: leap year' ); +is( date_finder('1230228'), '2023-02-28', 'Test case: non-leap year' ); +is( date_finder('1230429'), '2023-04-29', 'Test case: valid date' ); diff --git a/challenge-038/lubos-kolouch/perl/ch-2.pl b/challenge-038/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..474afa59b4 --- /dev/null +++ b/challenge-038/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,120 @@ +use strict; +use warnings; +use Test::More; + +my %tiles = ( + 'A' => [ 1, 8 ], + 'G' => [ 1, 3 ], + 'I' => [ 1, 5 ], + 'S' => [ 1, 7 ], + 'U' => [ 1, 5 ], + 'X' => [ 1, 2 ], + 'Z' => [ 1, 5 ], + 'E' => [ 2, 9 ], + 'J' => [ 2, 3 ], + 'L' => [ 2, 3 ], + 'R' => [ 2, 3 ], + 'V' => [ 2, 3 ], + 'Y' => [ 2, 5 ], + 'F' => [ 3, 3 ], + 'D' => [ 3, 3 ], + 'P' => [ 3, 5 ], + 'W' => [ 3, 5 ], + 'B' => [ 4, 5 ], + 'N' => [ 4, 4 ], + 'T' => [ 5, 5 ], + 'O' => [ 5, 3 ], + 'H' => [ 5, 3 ], + 'M' => [ 5, 4 ], + 'C' => [ 5, 4 ], + 'K' => [ 10, 2 ], + 'Q' => [ 10, 2 ] +); + +my @hand = draw_tiles(7); +print "Your hand: @hand\n"; +my $word = get_word(@hand); +my $score = calculate_score($word); +print "Your word: $word\n"; +print "Score: $score\n"; + +sub draw_tiles { + my ($num_tiles) = @_; + my @tiles; + for my $i ( 1 .. $num_tiles ) { + my $tile = get_random_tile(); + push @tiles, $tile; + } + return @tiles; +} + +sub get_random_tile { + my $total_tiles = 0; + for my $letter ( keys %tiles ) { + $total_tiles += $tiles{$letter}[1]; + } + my $rand_num = int( rand($total_tiles) ) + 1; + for my $letter ( keys %tiles ) { + if ( $rand_num <= $tiles{$letter}[1] ) { + $tiles{$letter}[1]--; + return $letter; + } + else { + $rand_num -= $tiles{$letter}[1]; + } + } +} + +sub get_word { + my (@hand) = @_; + print "Enter a word using the tiles in your hand: "; + chomp( my $word = <STDIN> ); + while ( !is_valid_word( $word, @hand ) ) { + print "Invalid word. Try again: "; + chomp( $word = <STDIN> ); + } + return uc($word); +} + +sub is_valid_word { + my ( $word, @hand ) = @_; + my %hand_count; + for my $tile (@hand) { + $hand_count{$tile}++; + } + + for my $char ( split //, $word ) { + if ( !$hand_count{$char}-- ) { + return 0; + } + } + + return length($word) > 0; +} + +sub calculate_score { + my ($word) = @_; + my $score = 0; + + for my $char ( split //, $word ) { + if ( exists( $tiles{$char} ) ) { + $score += $tiles{$char}[0]; + } + else { + die "Invalid character in word: $char\n"; + } + } + + return $score; +} + +# Tests +is( is_valid_word( 'CAT', ( 'C', 'A', 'T' ) ), 1 ); +is( is_valid_word( 'DOG', ( 'D', 'O', 'G' ) ), 1 ); +is( is_valid_word( 'CAT', ( 'C', 'A' ) ), 0 ); +is( is_valid_word( 'DOG', ( 'D', 'O' ) ), 0 ); + +is( calculate_score('CAT'), 8 ); +is( calculate_score('DOG'), 6 ); + +done_testing(); diff --git a/challenge-038/lubos-kolouch/python/ch-1.py b/challenge-038/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..b42d45790f --- /dev/null +++ b/challenge-038/lubos-kolouch/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from datetime import datetime + + +def date_finder(input_str): + if len(input_str) != 7: + raise ValueError("Input must be 7 digits long") + + century_digit = input_str[0] + year_digits = input_str[1:3] + month_digits = input_str[3:5] + day_digits = input_str[5:7] + + century = "20" if century_digit == "1" else "19" + year = f"{century}{year_digits}" + + if int(month_digits) < 1 or int(month_digits) > 12: + raise ValueError("Invalid month") + + try: + datetime.strptime(f"{year}-{month_digits}-{day_digits}", "%Y-%m-%d") + except ValueError: + raise ValueError("Invalid day") + + return f"{year}-{month_digits}-{day_digits}" + + +print(date_finder("2230120")) # 1923-01-20 diff --git a/challenge-038/lubos-kolouch/python/ch-2.py b/challenge-038/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..6d330ed840 --- /dev/null +++ b/challenge-038/lubos-kolouch/python/ch-2.py @@ -0,0 +1,95 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import random + +tiles = { + "A": [1, 8], + "G": [1, 3], + "I": [1, 5], + "S": [1, 7], + "U": [1, 5], + "X": [1, 2], + "Z": [1, 5], + "E": [2, 9], + "J": [2, 3], + "L": [2, 3], + "R": [2, 3], + "V": [2, 3], + "Y": [2, 5], + "F": [3, 3], + "D": [3, 3], + "P": [3, 5], + "W": [3, 5], + "B": [4, 5], + "N": [4, 4], + "T": [5, 5], + "O": [5, 3], + "H": [5, 3], + "M": [5, 4], + "C": [5, 4], + "K": [10, 2], + "Q": [10, 2], +} + + +def draw_tiles(num_tiles): + tiles_drawn = [] + for i in range(num_tiles): + tile = get_random_tile() + tiles_drawn.append(tile) + return tiles_drawn + + +def get_random_tile(): + total_tiles = sum([tiles[letter][1] for letter in tiles]) + rand_num = random.randint(1, total_tiles) + for letter in tiles: + if rand_num <= tiles[letter][1]: + tiles[letter][1] -= 1 + return letter + else: + rand_num -= tiles[letter][1] + + +def get_word(hand): + word = input("Enter a word using the tiles in your hand: ").upper() + while not is_valid_word(word, hand): + word = input("Invalid word. Try again: ").upper() + return word + + +def is_valid_word(word, hand): + hand_count = {} + for tile in hand: + if tile in hand_count: + hand_count[tile] += 1 + else: + hand_count[tile] = 1 + + for char in word: + if char in hand_count and hand_count[char] > 0: + hand_count[char] -= 1 + else: + return False + + return len(word) > 0 + + +def calculate_score(word): + score = 0 + for char in word: + if char in tiles: + score += tiles[char][0] + else: + raise ValueError(f"Invalid character in word: {char}") + + return score + + +hand = draw_tiles(7) +print(f"Your hand: {' '.join(hand)}") +word = get_word(hand) +score = calculate_score(word) +print(f"Your word: {word}") +print(f"Score: {score}") |
