1

I am trying to solve the FizzBuzz question in PHP (in this case using 'ApaBole' instead).

Counting from 1 to 100, numbers divisible by 3 should output 'Apa', numbers divisible by 5 should output 'Bole', and numbers divisible by both should output 'ApaBole'. All other numbers should output themselves.

Desired Result

1, 2, Apa, 4, Bole, Apa, 7, 8, Apa, Bole, 11, Apa, 13, 14, ApaBole, 16, 17, Apa, 19, Bole, Apa, 22, 23, Apa, Bole, 26, Apa, 28, 29, ApaBole, 31, 32, Apa, 34, Bole, Apa, 37, 38, Apa, Bole, 41, Apa, 43, 44, ApaBole, 46, 47, Apa, 49, Bole, Apa, 52, 53, Apa, Bole, 56, Apa, 58, 59, ApaBole, 61, 62, Apa, 64, Bole, Apa, 67, 68, Apa, Bole, 71, Apa, 73, 74, ApaBole, 76, 77, Apa, 79, Bole, Apa, 82, 83, Apa, Bole, 86, Apa, 88, 89, ApaBole, 91, 92, Apa, 94, Bole, Apa, 97, 98, Apa, Bole

My Attempt

for ($i= 1; $i <= 100; $i++)
{
    if($i % 15 == 0){
        echo ", ApaBole";
    } elseif($i % 3 == 0){
        echo ", Apa";
    } elseif($i % 5 == 0){
        echo ", Bole";
    } else{
        // rtrim($i, ", ");
        echo ', '.$i;
    }
}

Output

, 1, 2, Apa, 4, Bole, Apa, 7, 8, Apa, Bole, 11, Apa, 13, 14, ApaBole, 16, 17, Apa, 19, Bole, Apa, 22, 23, Apa, Bole, 26, Apa, 28, 29, ApaBole, 31, 32, Apa, 34, Bole, Apa, 37, 38, Apa, Bole, 41, Apa, 43, 44, ApaBole, 46, 47, Apa, 49, Bole, Apa, 52, 53, Apa, Bole, 56, Apa, 58, 59, ApaBole, 61, 62, Apa, 64, Bole, Apa, 67, 68, Apa, Bole, 71, Apa, 73, 74, ApaBole, 76, 77, Apa, 79, Bole, Apa, 82, 83, Apa, Bole, 86, Apa, 88, 89, ApaBole, 91, 92, Apa, 94, Bole, Apa, 97, 98, Apa, Bole%

How do I remove the first , ?

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • you should not right a wrong, change your logic in `else` with `echo ($i !== 1) ? ', ' . $i : $i;` – jibsteroos Sep 09 '20 at 08:57
  • 1
    Does this answer your question? [How to remove the first character of string in PHP?](https://stackoverflow.com/questions/4638569/how-to-remove-the-first-character-of-string-in-php) – executable Sep 09 '20 at 08:59

5 Answers5

2

There's a few different ways you can achieve this. One being removing the first element in the string as suggested by @BadHorsie, but you can also use a check in the part that generates the first string, if you are at the first iteration or not.

<?php

for ($i= 1; $i <= 100; $i++) {
    if ($i % 15 == 0) {
        echo ", ApaBole";
    } elseif ($i % 3 == 0) {
        echo ", Apa";
    } elseif ($i % 5 == 0) {
        echo ", Bole";
    } else {
        echo ($i == 1 ? '' : ', ').$i;
    }
}

Another way could be to store all the values in an array, and once you have all the values, implode() it with your desired delimiter.

$values = [];
for ($i= 1; $i <= 100; $i++) {
    if ($i % 15 == 0) {
        $values[] = "ApaBole";
    } elseif ($i % 3 == 0) {
        $values[] = "Apa";
    } elseif ($i % 5 == 0) {
        $values[] = "Bole";
    } else {
        $values[] = $i;
    }
}
echo implode(", ", $values);

Another could be to store all your values in a string-variable, then trim the first comma before you output it all.

$output = '';
for ($i= 1; $i <= 100; $i++) {
    if ($i % 15 == 0) {
        $output .= ", ApaBole";
    } elseif ($i % 3 == 0) {
        $output .= ", Apa";
    } elseif ($i % 5 == 0) {
        $output .= ", Bole";
    } else {
        $output .= ', '.$i;
    }
}
echo ltrim($output, ', ');
Qirel
  • 25,449
  • 7
  • 45
  • 62
2

Another way to achieve your result is to store the content in an array and then implode() it :

$result = [];
for ($i= 1; $i <= 100; $i++)
{
    $temp = "";
    
    if($i % 3 == 0){
        $temp = "Apa";
    }
    
    if($i % 5 == 0){
        $temp .= "Bole"; // concate with previous value,
    }                    // if $i % 3 == 0 && $i % 5 == 0 then $i % 15 == 0
    
    if ($temp == "")
    {
        $temp = $i;
    }
    
    $result[] = $temp;
}

echo implode(", ", $result);

Try it yourself

Cid
  • 14,968
  • 4
  • 30
  • 45
1

Solution to your question

I see you're actually trying to solve the common FizzBuzz interview question, except yours is called 'ApaBole' as I'm guessing this is not English.

You could solve it neatly like so:

$output = [];
for ($i = 1; $i <= 100; $i++) {
    $output[] = ($i % 3 ? '' : 'Apa') . ($i % 5 ? '' : 'Bole') ?: $i;
}
echo implode(', ', $output);

By adding all the elements to an array, you can use implode() to join them all together with commas, without any stray leading or trailing commas added to the string.


Additional notes about removing characters

To answer your questions about general string manipulation in PHP, here are some pointers which will hopefully be helpful for your learning.

To get rid of the first character of a string, you just need to get a substring starting at the second character.

$output = substr($input, 1);

However, it looks like you have a comma-separated list where you want to ensure there's no leading comma or 'empty' first item (you also have a space there). In which case, you might in fact want to trim off all commas/spaces at the start of the string, using ltrim().

$output = ltrim($input, ' ,');

If you want to ensure there's no commas or spaces at the start or end of your string, you can just use trim() instead.

$output = trim($input, ' ,');

In any case, it's better to improve your original code so that you don't have additional leading/trailing commas in the first place. This is exactly what the point of the interview question is - to see if you can properly craft a solution which doesn't leave mess like that.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
1

I think the easiest way is to remove , at the creation of the string like :

<?php
for ($i= 1; $i <= 100; $i++)
{
    if($i % 15 == 0){
        echo ", ApaBole";
    } elseif($i % 3 == 0){
        echo ", Apa";
    } elseif($i % 5 == 0){
        echo ", Bole";
    } else{
        // rtrim($i, ", ");
        if($i == 1){
            echo $i;
        }else{
            echo ', '.$i;
        }
    }
}

Demo: http://sandbox.onlinephpfunctions.com/code/de34ba2ec723ac96a08c1a81c1a82d8463cb1e9d

executable
  • 3,365
  • 6
  • 24
  • 52
0

One more approach using arrays:

<?php
$res = []; // create empty array
for ($i= 1; $i <= 100; $i++)
{
    if($i % 15 == 0){
        array_push($res, "ApaBole"); // push value into array
    } elseif($i % 3 == 0){
        array_push($res, "Apa"); // push value into array
    } elseif($i % 5 == 0){
        array_push($res, "Bole"); // push value into array
    } else {
        array_push($res, $i); // push value into array
    }
}

echo implode(',', $res); // implode array with commas
?>

https://phpize.online/

another compact code without if:

<?php
$res = [];
for ($i= 1; $i <= 100; $i++)
{
    array_push($res, (
        $i % 15 == 0 ? "ApaBole" : (
            $i % 3 == 0 ? "Apa" : (
                $i % 5 == 0 ? "Bole" : $i
            )
        )
    ));
}

echo implode(',', $res);
?>
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39