aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-03-09 11:59:48 +0100
committerLubos Kolouch <lubos@kolouch.net>2024-03-09 11:59:48 +0100
commit4b3e48c6126c7b0f150ffdac8df0f8002f26790e (patch)
tree4ab0ef2098bf6a4cefb93523c99c8799afba5281
parent3c2e1dfb49d9c363c01952126c3cb6b89c2cb479 (diff)
downloadperlweeklychallenge-club-4b3e48c6126c7b0f150ffdac8df0f8002f26790e.tar.gz
perlweeklychallenge-club-4b3e48c6126c7b0f150ffdac8df0f8002f26790e.tar.bz2
perlweeklychallenge-club-4b3e48c6126c7b0f150ffdac8df0f8002f26790e.zip
feat(challenge-259/lubos-kolouch/perl,python,raku/): Challenge 259 LK Perl Python Raku
-rw-r--r--challenge-259/lubos-kolouch/perl/ch-1.pl29
-rw-r--r--challenge-259/lubos-kolouch/perl/ch-2.pl59
-rw-r--r--challenge-259/lubos-kolouch/python/ch-1.py40
-rw-r--r--challenge-259/lubos-kolouch/python/ch-2.py58
-rw-r--r--challenge-259/lubos-kolouch/raku/ch-1.raku24
-rw-r--r--challenge-259/lubos-kolouch/raku/ch-2.raku44
6 files changed, 254 insertions, 0 deletions
diff --git a/challenge-259/lubos-kolouch/perl/ch-1.pl b/challenge-259/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..d5df455ea9
--- /dev/null
+++ b/challenge-259/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub is_prime {
+ my ($n) = @_;
+ return 0 if $n <= 1;
+ return 1 if $n <= 3;
+ return 0 if $n % 2 == 0 || $n % 3 == 0;
+ my $i = 5;
+ while ( $i * $i <= $n ) {
+ return 0 if $n % $i == 0 || $n % ( $i + 2 ) == 0;
+ $i += 6;
+ }
+ return 1;
+}
+
+sub prime_sum {
+ my ($N) = @_;
+ return 0 if $N < 2;
+ return 1 if is_prime($N);
+ return 2 if $N % 2 == 0;
+ return 2 if is_prime( $N - 2 );
+ return 3;
+}
+
+# Testing with example
+my $N = 9;
+say prime_sum($N);
diff --git a/challenge-259/lubos-kolouch/perl/ch-2.pl b/challenge-259/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..4add019984
--- /dev/null
+++ b/challenge-259/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,59 @@
+use strict;
+use warnings;
+
+sub load_grid {
+ my ($file_path) = @_;
+ open my $fh, '<', $file_path or die "Could not open file: $!";
+ my @grid = map { [ split ' ', $_ ] } <$fh>;
+ close $fh;
+ return \@grid;
+}
+
+sub load_words {
+ my ($file_path) = @_;
+ open my $fh, '<', $file_path or die "Could not open file: $!";
+ my %words = map { chomp; $_ => 1 } <$fh>;
+ close $fh;
+ return \%words;
+}
+
+sub find_words_in_grid {
+ my ( $grid, $words ) = @_;
+ my @found_words;
+ my @directions = (
+ [ -1, -1 ], [ -1, 0 ], [ -1, 1 ], [ 0, -1 ],
+ [ 0, 1 ], [ 1, -1 ], [ 1, 0 ], [ 1, 1 ]
+ );
+
+ my $rows = @$grid;
+ my $cols = @{ $grid->[0] };
+
+ for my $row ( 0 .. $rows - 1 ) {
+ for my $col ( 0 .. $cols - 1 ) {
+ foreach my $dir (@directions) {
+ my ( $dx, $dy ) = @$dir;
+ my ( $x, $y ) = ( $row, $col );
+ my $word = '';
+ while ( $x >= 0 && $x < $rows && $y >= 0 && $y < $cols ) {
+ $word .= $grid->[$x][$y];
+ push @found_words, $word if exists $words->{$word};
+ $x += $dx;
+ $y += $dy;
+ }
+ }
+ }
+ }
+ return \@found_words;
+}
+
+# Example usage
+# Replace the file paths with the actual paths to your grid and words files
+my $grid_file = 'grid.txt';
+my $words_file = 'words.txt';
+
+my $grid = load_grid($grid_file);
+my $words = load_words($words_file);
+my $found_words = find_words_in_grid( $grid, $words );
+
+print "Found words:\n";
+print "$_\n" foreach @$found_words;
diff --git a/challenge-259/lubos-kolouch/python/ch-1.py b/challenge-259/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..0591b00811
--- /dev/null
+++ b/challenge-259/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List
+
+
+def is_prime(n: int) -> bool:
+ """Check if a number is prime."""
+ if n <= 1:
+ return False
+ if n <= 3:
+ return True
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ i = 5
+ while i * i <= n:
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ i += 6
+ return True
+
+
+def prime_sum(N: int) -> int:
+ """Find the minimum number of prime numbers required whose summation gives N."""
+ if N < 2:
+ return 0
+ if is_prime(N):
+ return 1
+ if N % 2 == 0:
+ return 2 # Since N is even, it can be represented as the sum of two primes (Goldbach's conjecture)
+ # For an odd N, check if N-2 is prime. If it is, then N can be expressed as the sum of 2 and N-2 (both primes)
+ if is_prime(N - 2):
+ return 2
+ return 3 # If neither of the above conditions hold, N can be expressed as the sum of three primes
+
+
+# Test the function with the example
+assert prime_sum(9) == 2 # 2 + 7 = 9
+
+prime_sum(9)
diff --git a/challenge-259/lubos-kolouch/python/ch-2.py b/challenge-259/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..dda831f7c0
--- /dev/null
+++ b/challenge-259/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from typing import List, Set, Tuple
+import re
+
+
+def load_grid(file_path: str) -> List[List[str]]:
+ """Load the word search grid from a file."""
+ with open(file_path, "r") as file:
+ grid = [list(line.strip().upper()) for line in file]
+ return grid
+
+
+def load_words(file_path: str) -> Set[str]:
+ """Load the list of words from a file."""
+ with open(file_path, "r") as file:
+ words = {line.strip().upper() for line in file}
+ return words
+
+
+def find_words_in_grid(grid: List[List[str]], words: Set[str]) -> Set[str]:
+ """Find all words in the grid."""
+ found_words = set()
+ rows, cols = len(grid), len(grid[0])
+ directions = [
+ (dx, dy)
+ for dx in range(-1, 2)
+ for dy in range(-1, 2)
+ if not (dx == 0 and dy == 0)
+ ]
+
+ def word_at_position(x: int, y: int, dx: int, dy: int) -> str:
+ """Generate a word starting at (x, y) in direction (dx, dy)."""
+ word = ""
+ while 0 <= x < rows and 0 <= y < cols:
+ word += grid[x][y]
+ if word in words:
+ found_words.add(word)
+ x, y = x + dx, y + dy
+
+ for row in range(rows):
+ for col in range(cols):
+ for dx, dy in directions:
+ word_at_position(row, col, dx, dy)
+
+ return found_words
+
+
+# Example usage
+grid_file = "grid.txt" # Replace with actual file path
+words_file = "words.txt" # Replace with actual file path
+
+grid = load_grid(grid_file)
+words = load_words(words_file)
+found_words = find_words_in_grid(grid, words)
+
+print(f"Found {len(found_words)} words:", found_words)
diff --git a/challenge-259/lubos-kolouch/raku/ch-1.raku b/challenge-259/lubos-kolouch/raku/ch-1.raku
new file mode 100644
index 0000000000..1d4f1621cc
--- /dev/null
+++ b/challenge-259/lubos-kolouch/raku/ch-1.raku
@@ -0,0 +1,24 @@
+sub is-prime(Int $n --> Bool) {
+ return False if $n <= 1;
+ return True if $n <= 3;
+ return False if $n %% 2 || $n %% 3;
+ my $i = 5;
+ while ($i * $i <= $n) {
+ return False if $n %% $i || $n %% ($i + 2);
+ $i += 6;
+ }
+ return True;
+}
+
+sub prime-sum(Int $N --> Int) {
+ return 0 if $N < 2;
+ return 1 if is-prime($N);
+ return 2 if $N %% 2;
+ return 2 if is-prime($N - 2);
+ return 3;
+}
+
+# Testing with example
+my $N = 9;
+say prime-sum($N);
+
diff --git a/challenge-259/lubos-kolouch/raku/ch-2.raku b/challenge-259/lubos-kolouch/raku/ch-2.raku
new file mode 100644
index 0000000000..ec1324c895
--- /dev/null
+++ b/challenge-259/lubos-kolouch/raku/ch-2.raku
@@ -0,0 +1,44 @@
+sub load-grid(Str $file-path) {
+ my @grid = $file-path.IO.lines.map(*.comb);
+ return @grid;
+}
+
+sub load-words(Str $file-path) {
+ my %words = $file-path.IO.lines.map({ $_ => True }).hash;
+ return %words;
+}
+
+sub find-words-in-grid(@grid, %words) {
+ my @found-words;
+ my @directions = (-1, 0, 1) X (-1, 0, 1) ->> grep { $_.head != 0 || $_.tail != 0 };
+ my $rows = @grid.elems;
+ my $cols = @grid[0].elems;
+
+ for 0..^$rows -> $row {
+ for 0..^$cols -> $col {
+ for @directions -> ($dx, $dy) {
+ my ($x, $y) = ($row, $col);
+ my $word = '';
+ while $x >= 0 && $x < $rows && $y >= 0 && $y < $cols {
+ $word ~= @grid[$x][$y];
+ @found-words.push($word) if %words{$word}:exists;
+ ($x, $y) = ($x + $dx, $y + $dy);
+ }
+ }
+ }
+ }
+ return @found-words;
+}
+
+# Example usage
+# Replace the file paths with the actual paths to your grid and words files
+my $grid-file = 'grid.txt';
+my $words-file = 'words.txt';
+
+my @grid = load-grid($grid-file);
+my %words = load-words($words-file);
+my @found-words = find-words-in-grid(@grid, %words);
+
+say "Found words:";
+say $_ for @found-words;
+