Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dnaLength = readline();
- $bestDNA = [
- 'sample' => 0,
- 'index' => 'none',
- 'length' => 0,
- 'dna' => '',
- 'sum' => 0
- ];
- $sample = 0;
- while (true) {
- $input = readline();
- if ($input === 'Clone them!') {
- echo 'Best DNA sample ' . $bestDNA['sample'] . ' with sum: ' . $bestDNA['sum'] . '.' . PHP_EOL;
- echo $bestDNA['dna'] ;
- break;
- }
- $sample++;
- $dna = str_replace("!", "", $input);
- if (intval($dnaLength) === strlen($dna)) {
- $sum = array_sum(explode('!', $input));
- $dna = str_replace("0", " 0 ", $dna);
- $printDNA = trim(str_replace("!", " ", $input));
- $dna = trim(str_replace(" ", " ", $dna));
- $dna = explode(' ', $dna);
- $length = 0;
- $index = '';
- for ($i = 0; $i < count($dna); $i++) {
- if ($dna[$i] > 1) {
- if ($length < strlen($dna[$i])) {
- $length = strlen($dna[$i]);
- $index = strlen(implode("", array_slice($dna, 0, $i)));
- }
- }
- }
- $addToBestDNA = false;
- if ($bestDNA['index'] === 'none') {
- $addToBestDNA = true;
- } elseif ($bestDNA['length'] < $length) {
- $addToBestDNA = true;
- } elseif ($bestDNA['length'] === $length && $bestDNA['index'] > $index) {
- $addToBestDNA = true;
- } elseif ($bestDNA['length'] === $length && $bestDNA['index'] === $index && $bestDNA['sum'] < $sum) {
- $addToBestDNA = true;
- }
- if ($addToBestDNA) {
- $bestDNA['sample'] = $sample;
- $bestDNA['index'] = $index;
- $bestDNA['length'] = $length;
- $bestDNA['dna'] = $printDNA;
- $bestDNA['sum'] = $sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement