diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-19 11:50:11 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-19 11:50:11 +0100 |
| commit | 04628691d7a57167fd7c2d32efc7fb8cde58d9eb (patch) | |
| tree | 598337fd46e4f781a3ddd5dee4edb13b546b6927 | |
| parent | 12933b400c4041aed0ae285aeefce79a752adb6d (diff) | |
| parent | a1ee13894a0da22ca73d325e3a1ff8be6cd47ecd (diff) | |
| download | perlweeklychallenge-club-04628691d7a57167fd7c2d32efc7fb8cde58d9eb.tar.gz perlweeklychallenge-club-04628691d7a57167fd7c2d32efc7fb8cde58d9eb.tar.bz2 perlweeklychallenge-club-04628691d7a57167fd7c2d32efc7fb8cde58d9eb.zip | |
Merge pull request #12540 from jeanluc2020/jeanluc2020-335
Add solution 335.
| -rw-r--r-- | challenge-335/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-335/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-335/jeanluc2020/perl/ch-1.pl | 93 | ||||
| -rwxr-xr-x | challenge-335/jeanluc2020/perl/ch-2.pl | 132 |
4 files changed, 227 insertions, 0 deletions
diff --git a/challenge-335/jeanluc2020/blog-1.txt b/challenge-335/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..a142a12391 --- /dev/null +++ b/challenge-335/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-335-1.html diff --git a/challenge-335/jeanluc2020/blog-2.txt b/challenge-335/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..fb43ade5b1 --- /dev/null +++ b/challenge-335/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-335-2.html diff --git a/challenge-335/jeanluc2020/perl/ch-1.pl b/challenge-335/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..4c3316a1f4 --- /dev/null +++ b/challenge-335/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,93 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/#TASK1 +# +# Task 1: Common Characters +# ========================= +# +# You are given an array of words. +# +# Write a script to return all characters that is in every word in the given array including duplicates. +# +## Example 1 +## +## Input: @words = ("bella", "label", "roller") +## Output: ("e", "l", "l") +# +# +## Example 2 +## +## Input: @words = ("cool", "lock", "cook") +## Output: ("c", "o") +# +# +## Example 3 +## +## Input: @words = ("hello", "world", "pole") +## Output: ("l", "o") +# +# +## Example 4 +## +## Input: @words = ("abc", "def", "ghi") +## Output: () +# +# +## Example 5 +## +## Input: @words = ("aab", "aac", "aaa") +## Output: ("a", "a") +# +############################################################ +## +## discussion +## +############################################################ +# +# We do this one in multiple passes: +# 1. split each word into its characters and count how often each character +# appears in it +# 2. initialize an output data hash with the first hash from the resulting +# array +# 3. for each each character in the keys of that hash and for each hash in the +# array, check whether the character appears in the hash. If it doesn't, +# remove it from the output data hash. If it does, set the corresponding +# value in the data output hash to the minimum of the two current values +# 4. collect the remaining characters and the number of their occurrences to +# create the output array + +use v5.36; + +common_characters("bella", "label", "roller"); +common_characters("cool", "lock", "cook"); +common_characters("hello", "world", "pole"); +common_characters("abc", "def", "ghi"); +common_characters("aab", "aac", "aaa"); + +sub common_characters( @words ) { + say "Input: (\"" . join("\", \"", @words) . "\")"; + my @data = (); + foreach my $word (@words) { + my $tmp = {}; + foreach my $char (split //, $word) { + $tmp->{$char}++; + } + push @data, $tmp; + } + my $output = { %{$data[0]} }; + foreach my $hash (@data) { + foreach my $char (keys %$output) { + if($hash->{$char}) { + $output->{$char} = $hash->{$char} if $hash->{$char} < $output->{$char}; + } else { + delete $output->{$char}; + } + } + } + my @out = (); + foreach my $char (keys %$output) { + foreach my $count (1..$output->{$char}) { + push @out, $char; + } + } + say "Output: (" . join(", ", map {"\"$_\""} @out) . ")"; +} diff --git a/challenge-335/jeanluc2020/perl/ch-2.pl b/challenge-335/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..d1f1835e49 --- /dev/null +++ b/challenge-335/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,132 @@ +#!/usr/bin/env perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-335/#TASK2 +# +# Task 2: Find Winner +# =================== +# +# You are given an array of all moves by the two players. +# +# Write a script to find the winner of the TicTacToe game if found based on the +# moves provided in the given array. +# +# UPDATE: Order move is in the order - A, B, A, B, A, …. +# +## Example 1 +## +## Input: @moves = ([0,0],[2,0],[1,1],[2,1],[2,2]) +## Output: A +## +## Game Board: +## [ A _ _ ] +## [ B A B ] +## [ _ _ A ] +# +# Note: Example 1 has a typo either in the list of moves (second move +# should be [1,0]) or in the output Game Board (B in line 1 column 0 +# should move to line 2, column 0). I just use both alternatives below +# +## Example 2 +## +## Input: @moves = ([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]) +## Output: B +## +## Game Board: +## [ A A B ] +## [ A B _ ] +## [ B _ _ ] +# +# +## Example 3 +## +## Input: @moves = ([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]) +## Output: Draw +## +## Game Board: +## [ A A B ] +## [ B B A ] +## [ A B A ] +# +# +## Example 4 +## +## Input: @moves = ([0,0],[1,1]) +## Output: Pending +## +## Game Board: +## [ A _ _ ] +## [ _ B _ ] +## [ _ _ _ ] +# +# +## Example 5 +## +## Input: @moves = ([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]) +## Output: B +## +## Game Board: +## [ B B B ] +## [ A A _ ] +## [ _ _ A ] +# +############################################################ +## +## discussion +## +############################################################ +# +# Let's first initialize an empty board. Then we replace the fields +# corresponding to a move with the corresponding character. +# Now we have to check the board - all rows, columns and diagonals. +# If we find a winning position for any of the players, return the +# result. If we don't, then we return "Draw" if all fields in the +# board have been filled, otherwise we return "Pending". + +use v5.36; + +find_winner([0,0],[2,0],[1,1],[2,1],[2,2]); +find_winner([0,0],[1,0],[1,1],[2,1],[2,2]); +find_winner([0,0],[1,1],[0,1],[0,2],[1,0],[2,0]); +find_winner([0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]); +find_winner([0,0],[1,1]); +find_winner([1,1],[0,0],[2,2],[0,1],[1,0],[0,2]); + + +sub find_winner( @moves ) { + say "Input: (" . join(", ", map {"[$_->[0],$_->[1]]"} @moves) . ")"; + my $board = [ ["_", "_", "_"], ["_", "_", "_"], ["_", "_", "_"] ]; + my $next = "A"; + foreach my $move (@moves) { + $board->[$move->[0]]->[$move->[1]] = $next; + $next = $next eq "A" ? "B" : "A"; + } + my $winner = "Pending"; + my $is_filled = 1; + foreach my $x (0..2) { + foreach my $y (0..2) { + $is_filled = 0 if $board->[$x]->[$y] eq "_"; + } + } + # let's check the rows + foreach my $x (0..2) { + if($board->[$x]->[0] eq $board->[$x]->[1] && $board->[$x]->[1] eq $board->[$x]->[2] && $board->[$x]->[0] ne "_") { + return say "Output: $board->[$x]->[0]"; + } + } + # let's check the columns + foreach my $y (0..2) { + if($board->[0]->[$y] eq $board->[1]->[$y] && $board->[1]->[$y] eq $board->[2]->[$y] && $board->[0]->[$y] ne "_") { + return say "Output: $board->[0]->[$y]"; + } + } + # let's check the diagonals + if($board->[0]->[0] eq $board->[1]->[1] && $board->[1]->[1] eq $board->[2]->[2] && $board->[1]->[1] ne "_") { + return say "Output: $board->[0]->[0]"; + } + if($board->[2]->[0] eq $board->[1]->[1] && $board->[1]->[1] eq $board->[0]->[2] && $board->[1]->[1] ne "_") { + return say "Output: $board->[2]->[0]"; + } + if($is_filled) { + $winner = "Draw"; + } + say "Output: $winner"; +} |
