diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-05 21:30:39 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-05 21:30:39 +0100 |
| commit | 01bbda5685a45abbd5ed652e67f95d206783d0c9 (patch) | |
| tree | 3b2824e04c4a6f50a1dd102a7621237571a99290 | |
| parent | 7573054b72d6484f0b3b8083fa2fbe0232439e35 (diff) | |
| parent | 10ef70c467c2ee1c898e038edc4b8e8643a3d5e7 (diff) | |
| download | perlweeklychallenge-club-01bbda5685a45abbd5ed652e67f95d206783d0c9.tar.gz perlweeklychallenge-club-01bbda5685a45abbd5ed652e67f95d206783d0c9.tar.bz2 perlweeklychallenge-club-01bbda5685a45abbd5ed652e67f95d206783d0c9.zip | |
Merge pull request #10039 from 2colours/branch-for-challenges-266-267
PHP solutions for weeks 266 and 267
| -rw-r--r-- | challenge-266/2colours/php/ch-1.php | 14 | ||||
| -rw-r--r-- | challenge-266/2colours/php/ch-2.php | 22 | ||||
| -rw-r--r-- | challenge-267/2colours/php/ch-1.php | 8 | ||||
| -rw-r--r-- | challenge-267/2colours/php/ch-2.php | 26 |
4 files changed, 70 insertions, 0 deletions
diff --git a/challenge-266/2colours/php/ch-1.php b/challenge-266/2colours/php/ch-1.php new file mode 100644 index 0000000000..3079fd5dff --- /dev/null +++ b/challenge-266/2colours/php/ch-1.php @@ -0,0 +1,14 @@ +<?php + +echo '$line1 = '; +$line1 = preg_split('/\s+/', rtrim(fgets(STDIN))); +echo '$line2 = '; +$line2 = preg_split('/\s+/', rtrim(fgets(STDIN))); + +$word_frequencies = array_count_values($line1); +foreach (array_count_values($line2) as $key => $value) { + @$word_frequencies[$key] += $value; +} + +$solution = array_keys(array_filter($word_frequencies, fn($freq) => $freq === 1)) ?: ['']; +var_dump($solution); diff --git a/challenge-266/2colours/php/ch-2.php b/challenge-266/2colours/php/ch-2.php new file mode 100644 index 0000000000..c8f7064f37 --- /dev/null +++ b/challenge-266/2colours/php/ch-2.php @@ -0,0 +1,22 @@ +<?php + +$matrix = json_decode(file_get_contents('php://stdin')); +$size = count($matrix[0]); # let's hope it's actually a square matrix... + +// all 0's except for those 2 - which may be one if it's the middle +function is_row_compliant($row, $diagonal_position, $size) +{ + $is_middle = $diagonal_position === $size - 1 - $diagonal_position; + return @array_count_values($row)[0] === $size - ($is_middle ? 1 : 2) && $row[$diagonal_position] !== 0 && $row[$size - 1 - $diagonal_position] !== 0; +} + +//[$diagonal_position, $solution] = array_reduce($matrix, fn ($state, $row) => [$state[0] + 1, $state[1] && is_row_compliant($row, $state[0], $size)], [0, true]); # is this really worth it? :D +$solution = true; +foreach ($matrix as $row_index => $row) { + if (!is_row_compliant($row, $row_index, $size)) { + $solution = false; + break; + } +} + +var_dump($solution);
\ No newline at end of file diff --git a/challenge-267/2colours/php/ch-1.php b/challenge-267/2colours/php/ch-1.php new file mode 100644 index 0000000000..8b6b877a8a --- /dev/null +++ b/challenge-267/2colours/php/ch-1.php @@ -0,0 +1,8 @@ +<?php + +function sign_of_product(array $ints) +{ + return array_reduce($ints, fn($acc, $current) => $acc * gmp_sign($current), 1); +} + +echo 'The sign of the product is ' . sign_of_product(array_slice($argv, 1)); diff --git a/challenge-267/2colours/php/ch-2.php b/challenge-267/2colours/php/ch-2.php new file mode 100644 index 0000000000..c824e95eea --- /dev/null +++ b/challenge-267/2colours/php/ch-2.php @@ -0,0 +1,26 @@ +<?php + +const SCREEN_WIDTH = 100; +echo '$str = '; +$str = rtrim(fgets(STDIN)); +echo '@width = '; +$width = array_combine(range('a', 'z'), explode(' ', rtrim(fgets(STDIN)))); + +$current_line_number = 1; +$current_line_content = ''; +$current_column = 0; +$characters = str_split($str); + +foreach ($characters as $current_char) { + $current_width = $width[$current_char]; + if ($current_column + $current_width > SCREEN_WIDTH) { + echo "Line $current_line_number: $current_line_content ($current_column pixels)\n"; + $current_line_content = "$current_char"; + $current_column = $current_width; + $current_line_number++; + continue; + } + $current_column += $current_width; + $current_line_content .= $current_char; +} +echo "Line $current_line_number: $current_line_content ($current_column pixels)";
\ No newline at end of file |
