The Weekly Challenge 038

Date Finder and Scrabble Word Game

Solutions on GitHub

Task 1: Date Finder

Write a script to validate a 7-digit date representation format where the first digit represents the century (1 = 20, 2 = 19), followed by YYMMDD, and return the formatted date (YYYY-MM-DD).

Example Output

Input: "2230120" Output: "1923-01-20"

Logic

Unpack the 7-digit string into Century (1 char), Year (2 chars), Month (2 chars), and Day (2 chars). Prefix the year with "20" if century digit is 1, otherwise "19". Then validate that the month and day form a valid calendar date, accounting for leap years.

Perl Solution

ch-1.pl

sub date_finder { my ($input) = @_; die "Input must be 7 digits long\n" if length($input) != 7; my ($century_digit, $year_digits, $month_digits, $day_digits) = unpack "A1A2A2A2", $input; my $century = $century_digit == 1 ? "20" : "19"; my $year = "$century$year_digits"; die "Invalid month\n" if $month_digits < 1 || $month_digits > 12; 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; } die "Invalid day\n" if $day_digits < 1 || $day_digits > $days_in_month[$month_digits]; return sprintf "%04d-%02d-%02d", $year, $month_digits, $day_digits; }

Python Solution

ch-1.py

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}"

Task 2: Scrabble Word Game

Implement a Scrabble-like word game where a player draws 7 random tiles from a bag, enters a word constructed from those tiles, and calculates their score based on the values of the letters.

Example Output

Your hand: A E I R B L T Your word: TRIAL Score: 7

Logic

Define a tile bag with letter scores and frequencies. Simulating drawing 7 tiles at random (adjusting the available counts in the bag). Validate that the user-provided word can be formed entirely from the letters in their hand, and sum the point values of the letters in the word.

Perl Solution

ch-2.pl

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 is_valid_word { my ( $word, @hand ) = @_; my %hand_count; $hand_count{$_}++ for @hand; for my $char ( split //, $word ) { return 0 if !$hand_count{$char}--; } return length($word) > 0; }

Python Solution

ch-2.py

def is_valid_word(word, hand): hand_count = {} for tile in hand: hand_count[tile] = hand_count.get(tile, 0) + 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