Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

gina78 avatar gina78 4 Точки

08. Anonymous Threat

Здравейте, може ли някой да помогне стая задача

Anonymous Threat

The Anonymous have created a cyber hypervirus which steals data from the CIA. You, as the lead security developer in CIA, have been tasked to analyze the software of the virus and observe its actions on the data. The virus is known for his innovative and unbeleivably clever technique of merging and dividing data into partitions.

You will receive a single input line containing STRINGS separated by spaces.
The strings may contain any ASCII character except whitespace.

You will then begin receiving commands in one of the following formats:

  • merge {startIndex} {endIndex}
  • divide {index} {partitions}

Every time you receive the merge command, you must merge all elements from the startIndex, till the endIndex. In other words, you should concatenate them.
Example: {abc, def, ghi} -> merge 0 1 -> {abcdef, ghi}

If any of the given indexes is out of the array, you must take ONLY the range that is INSIDE the array and merge it.

Every time you receive the divide command, you must DIVIDE the element at the given index, into several small substrings with equal length. The count of the substrings should be equal to the given partitions.

Example: {abcdef, ghi, jkl} -> divide 0 3 -> {ab, cd, ef, ghi, jkl}

If the string CANNOT be exactly divided into the given partitions, make all partitions except the LAST with EQUAL LENGTHS, and make the LAST onethe LONGEST.

Example: {abcd, efgh, ijkl} -> divide 0 3 -> {a, b, cd, efgh, ijkl}

The input ends when you receive the command “3:1”. At that point you must print the resulting elements, joined by a space.

Input

  • The first input line will contain the array of data.
  • On the next several input lines you will receive commands in the format specified above.
  • The input ends when you receive the command “3:1”.

Output

  • As output you must print a single line containing the elements of the array, joined by a space.

Constrains

  • The strings in the array may contain any ASCII character except whitespace.
  • The startIndex and the endIndex will be in range [-1000, 1000].
  • The endIndex will ALWAYS be GREATER than the startIndex.
  • The index in the divide command will ALWAYS be INSIDE the array.
  • The partitions will be in range [0, 100].
  • Allowed working time/memory: 100ms / 16MB.

Examples

Input

Output

Ivo Johny Tony Bony Mony

merge 0 3

merge 3 4

merge 0 3

3:1

IvoJohnyTonyBonyMony

abcd efgh ijkl mnop qrst uvwx yz

merge 4 10

divide 4 5

3:1

abcd efgh ijkl mnop qr st uv wx yz

 

Моето  решение https://pastebin.com/HXMysVAb

незнам как да го довърша

 

Тагове:
0
PHP Fundamentals 26/06/2019 19:37:59
willystyle avatar willystyle 2472 Точки
<?php
$arr = explode(" ", readline());
while (true) {
    $comand = explode(" ", readline());
    if ($comand[0] == "3:1") {
        break;
    }
    switch ($comand[0]) {
        case "merge":
            $startIndex = $comand[1] < 0 ? 0 : $comand[1];            
            $endIndex = $comand[2] >= count($arr) ? count($arr) - 1 : $comand[2];            
            $elToMerge = '';
            for ($i = $startIndex; $i <= $endIndex; $i++) {
                $elToMerge .= $arr[$i];
            }
            array_splice($arr, $startIndex, $endIndex - $startIndex + 1, $elToMerge);
            break;
        case "divide":
            $index = $comand[1];
            $partitions = $comand[2];
            $element = $arr[$index];
            $partitionLen = floor(strlen($element) / $partitions);
            $lastPartitionLen = $partitionLen + (strlen($element) % $partitions);
            $elementsToAdd = [];
            for ($i = 0; $i < $partitions; $i++) {
                $len = $i == $partitions - 1 ? $lastPartitionLen : $partitionLen;
                $elementsToAdd[] = substr($element, $i * $partitionLen, $len);
            }
            array_splice($arr, $index, 1, $elementsToAdd);
            break;
    }
}
echo implode(" ", $arr);

 

0
gina78 avatar gina78 4 Точки

Благодаря ти много

1
DeyanAltanov avatar DeyanAltanov 6 Точки

Единствено не разбирам, какъв е "превода" на следните три реда:

$startIndex = $comand[1] < 0 ? 0 : $comand[1];            
$endIndex = $comand[2] >= count($arr) ? count($arr) - 1 : $comand[2]; 
$len = $i == $partitions - 1 ? $lastPartitionLen : $partitionLen;

Как се изчисляват тези стойности? Какво означават ? и : в случая?

Сравнение за true, или false?

0
willystyle avatar willystyle 2472 Точки

Прочети за тернарен (или условен) оператор: conditon ? expression1 : expression2;

Ako contion = true връща expresion1 иначе expresion2. Като цяло може да се мине без него, той е кратък запис на:

if (condition) {

return expression1

} else {

return expression2

}

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.