4
19/10/2024 9:42 am
Topic starter
You are given a sequence of strings, each on a new line. Every odd line on the console is representing the type of the gold (e.g. Yellow, Rose, White, and so on), and its karats. Your task is to collect the resources while you receive "stop" and print each on a new line.
Print the resources and their quantities in format:
"{type} –> {karats}K"
The karats inputs will be in the range [1 … 24 000]
Examples:
1 Answer
3
19/10/2024 9:43 am
Here is my solution:
<?php $input = readline(); $goldArr = []; while ($input !== "stop") { $type = $input; $carats = intval(readline()); if (!key_exists($type, $goldArr)) { $goldArr[$type] = $carats; } else { $goldArr[$type] += $carats; } $input = readline(); } foreach ($goldArr as $type => $carats) { echo "$type -> $carats" . "K" . PHP_EOL; }