Problem 21 ?

Back to Problem Solutions forum

sergey144010     2015-11-04 08:54:57

It's about Array Counters

$str = '4 13 3 16 1 4 12 16 1 7 9 14 1 1 2 2 6 13 6 10 13 5 10 1 13 7 2 5 13 6 1 16 3 3 15 3 6 11 3 7 1 11 5 2 12 6 4 1 3 10 11 15 14 4 16 11 10 2 15 7 8 15 6 10 2 5 13 8 16 15 14 1 10 2 2 5 8 6 6 11 15 16 10 12 4 9 6 14 10 5 4 2 4 10 12 5 15 8 12 14 7 10 14 16 12 16 5 4 5 10 14 3 9 7 15 13 16 5 10 10 9 14 11 13 7 6 1 6 14 13 3 4 6 1 3 2 1 7 5 5 1 3 8 10 9 7 6 9 11 15 2 4 13 13 16 4 2 1 9 16 13 12 3 3 12 6 4 13 13 9 2 13 11 9 6 4 16 12 12 11 11 13 15 7 10 14 10 11 15 3 10 12 14 13 14 10 2 2 6 15 10 7 11 4 16 1 7 15 12 2 9 7 15 7 13 9 5 7 3 4 9 13 15 7 10 12 16 12 13 6 10 6 13 5 10 12 6 16 11 2 2 4 8 1 11 4 9 16 11 12 3 3 9 2 10 2 14 9 14 10 15 7 16 11 12 10 7 1 9 2 2 11 5 9 12 16 13 4 16 8 16 2 10 8 4 4 10';

17 23 16 20 15 19 18 8 17 27 17 18 22 14 16 20 - Your answer

20 22 16 20 17 18 18 17 14 23 19 27 15 16 17 8 - My answer

Considered in manual 4, their 20, not 17.

Considered in manual 13, their 22, not 23.

Considered in manual 8, their 8, not 20.

Where is the error?

<?php
$array = explode(' ', $str);
print_r(array_count_values($array));
?>
Quandray     2015-11-04 10:19:05
User avatar

Hi,

Sorry I can't tell you where the error is, but the data contains 17 ones, so the answer should start with 17.

Rodion (admin)     2015-11-04 11:37:18
User avatar

Hi! I'm not sure whether you use exactly print_r for printing the result, but it seems you forget to sort it by keys. So answers are correct, but not in order. 20 is the answer for 4-s, 22 for 13-s etc.

By the way, you could input $str from virtual or real console by using something like

$str = fgets(STDIN);
sergey144010     2015-11-04 12:17:29

Really... But here, for my

Answer should contain exactly N values, separated by spaces. First should give amount of 1-s, second - amount of 2-s and so on.

It's not obvious. Because in your example

data input:
10 3
1 2 3 2 3 1 1 1 1 3

answer:
5 2 3

All 3 values (1 2 3) and they are consecutive. One gets the impression that the sorting is not needed.

Or change the description or change the first digits for example 1 3 2 or 3 2 1 or 2 1 3.

Thank you, it worked.

Rodion (admin)     2015-11-04 13:19:07
User avatar

Thanks for hint! Let us change the example to

3 2 1 2 3 1 1 1 1 3
Please login and solve 5 problems to be able to post at forum