aboutsummaryrefslogtreecommitdiff
path: root/challenge-267
diff options
context:
space:
mode:
author2colours <polgar.marton@windowslive.com>2024-05-05 03:27:47 +0200
committer2colours <polgar.marton@windowslive.com>2024-05-05 03:27:47 +0200
commit10ef70c467c2ee1c898e038edc4b8e8643a3d5e7 (patch)
tree3b2824e04c4a6f50a1dd102a7621237571a99290 /challenge-267
parent7573054b72d6484f0b3b8083fa2fbe0232439e35 (diff)
downloadperlweeklychallenge-club-10ef70c467c2ee1c898e038edc4b8e8643a3d5e7.tar.gz
perlweeklychallenge-club-10ef70c467c2ee1c898e038edc4b8e8643a3d5e7.tar.bz2
perlweeklychallenge-club-10ef70c467c2ee1c898e038edc4b8e8643a3d5e7.zip
PHP solutions for weeks 266 and 267
Diffstat (limited to 'challenge-267')
-rw-r--r--challenge-267/2colours/php/ch-1.php8
-rw-r--r--challenge-267/2colours/php/ch-2.php26
2 files changed, 34 insertions, 0 deletions
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