Rebuild string using each x-letter position with PHP

I am trying to figure out how to reconfigure a line using each x position, so that an example input line of "ABCDEFGHI" and x equal to 4 will give DHCIFEGBA. Here is how I understood it:

The first letter is simple: symbol 4. [A, B, C, D ]
The second letter is also simple: it is symbol 8. [E, F, G, H ]
The third letter is basically simple: it is symbol 3. This is because I obsessed as I had hoped, so I used the I of, A, Bed and, the C .
Fourth letter - this is where things get complicated: it is a symbol 9. As the D and H were gone, they are not used in the analysis, resulting in E, the F, the G, I of .
Letter # 5 follows the same pattern, skipping C and D: A, B, E, F
Letter # 6 has gaps and wrapper: G, A, B, E .
№7 letter again wraps: the G, A, Bed and, the G .
Letter No. 8 also wraps up (technically twice, since the "cursor" was beyond G to: A, B, A, B
Letter # 9 is our remainder: A, A, A, A


Obviously, it will be necessary to loop until the length of the output line matches the length of the input line - this is all a mess in the middle (mainly gaps and loops) that I cannot figure out for my life.

Any help or guidance appreciated.

+3
source share
3 answers

Should work with anyone $sand$n

  <?php
    $s = "ABCDEFGHI";
    $n = 4;

    for ($i=0,$l=strlen($s) ; $l ; $l--)
    {
       $i = ($i+$n-1) % $l;
       echo $s[$i];
       $s = ($i ? substr($s, 0, $i) : '') . ($i < $l-1 ? substr($s, $i+1) : '');
    }
    echo "\n";
  ?>
0
source

works great with a unique range but won't work with something like ABCDEAFGHAI

$s = 'ABCDEFGHI';
$_len = strlen($s);
$out = '';
$c = 3;
echo '<pre>';
while ( $_len > 0 ) {
    echo strlen($s) ,  ' :: '.  $s . "\n";
    $term = $s[$c-1];
    list($a,$b) = explode($term, $s);
    $s = $b.$a;
    $x = strlen($s);
    if($x <= $c) {
        for($i=0; $i<($c-$x);$i++)
            $s .= $s;
    }

    $out .= $term;
    $_len--;
}

echo "\n\n\n" , $out;

output:

9 :: ABCDEFGHI
8 :: EFGHIABC
7 :: IABCEFG
6 :: EFGIAB
5 :: ABEFG
4 :: GABE
6 :: GABGAB
4 :: ABAB
4 :: AAAA

DHCIFEGBA

reliable solution

function rearrangeString($string, $step = 4) {
    $output = '';
    $lenght = strlen($string);

    if($step >= $lenght) {
        return strrev($string);
    } elseif($step < 1) {
        return $string;
    } else {
        for($i=0; $i < $lenght; $i++) {
            $_tempLen = strlen($string);

            $output .= $string[$step-1];

            list($a,$b) = explode($string[$step-1], $string);

            $string = $b . $a;

            $_tempLen = strlen($string);
            if($step > $_tempLen) {
                $string = str_repeat($string, $step-$_tempLen+1);
            }
            unset($_tempLen, $a, $b);
        }

        return $output;
    }
}

$rearranged = rearrangeString('ABCDEFGHI');
var_dump($rearranged == 'DHCIFEGBA');

$rearranged = rearrangeString('ABCDEFGHI', 9);
var_dump($rearranged == 'IHGFEDCBA');

$rearranged = rearrangeString('ABCDEFGHI', 1);
var_dump($rearranged == 'ABCDEFGHI');

$rearranged = rearrangeString('ABCDEFGHI', 3);
var_dump($rearranged == 'CFIDHEBGA');

$rearranged = rearrangeString('ABCDEFGHI', 6);
var_dump($rearranged == 'FCAIBEHD');

$rearranged = rearrangeString('FARBE', 2);
var_dump($rearranged == 'ABFER');
+2
source

I would try to do this, although I have not tested the code yet:

$s = 'ABCDEFGHI';
$out = '';

While strlen($s) > 0 {
    $pos = 4 % strlen($s);
    $out += $s[$pos];
    if (strlen($s) > 3) $end = 3;
    else $end = strlen($s) - 1;
    $s = substr($s, 4, strlen($s)) + substr($s, 0, $end);
}

echo $out;
0
source

Source: https://habr.com/ru/post/1770328/


All Articles