create account

RE: Introducing the Coding Challenge by mkt

View this thread on: hive.blogpeakd.comecency.com

Viewing a response to: @reggaemuffin/introducing-the-coding-challenge

· @mkt · (edited)
$0.47
PHP Version:
(lazy and dirty)

https://pastebin.com/q0GqCaG2

```
<?php

function fizzbuzz($n) {
  if (!($n % 3)) {
    $fizzbuzz .= 'Fizz';
  }
  if (!($n % 5)) {
    $fizzbuzz .= 'Buzz';
  }
  return $fizzbuzz;
}

for ($n = 0; $n <= 100; $n++) {
  echo $n . ': ';
  echo fizzbuzz($n);
  echo "\n";
}

function fizzbuzzTest() {
  echo (fizzbuzz(0) == 'FizzBuzz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(3) == 'Fizz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(6) == 'Fizz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(5) == 'Buzz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(10) == 'Buzz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(15) == 'FizzBuzz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(30) == 'FizzBuzz' ? 'passed' : 'failed') . "\n";
  echo (fizzbuzz(31) == '' ? 'passed' : 'failed') . "\n";
}

fizzbuzzTest();
```
#

I think most people will do 3 checks:

```
if %3 AND %5 ...
else if %3 ...
else if %5 ...
```

But

```
if %3 ...
if %5 ...
```

will have the same result when concatenating the strings.

You don't even have to handle n = 0 as a special case because strictly speaking 0 is a multiple of 3 and 5 because 3 * 0 is 0 and 5 * 0 is 0. Otherwise there's just another `if ($n)` missing.
👍  , ,
properties (23)
authormkt
permlinkre-reggaemuffin-introducing-the-coding-challenge-20170817t154821852z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"links":["https://pastebin.com/q0GqCaG2"],"app":"steemit/0.1"}
created2017-08-17 15:48:09
last_update2017-08-17 15:53:57
depth1
children3
last_payout2017-08-24 15:48:09
cashout_time1969-12-31 23:59:59
total_payout_value0.353 HBD
curator_payout_value0.113 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,234
author_reputation45,513,283,519,678
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,096,667
net_rshares134,106,493,332
author_curate_reward""
vote details (3)
@reggaemuffin ·
I think you made a few errors in it ;) You only print the number if it is no fizz and no buzz :)

the implicit integer to bool conversion is scary `!($n % 5)` I probably would ask you to switch it if it is code i have to maintain, `($n % 5 === 0)` is a bit more readable in my opinion. But you said it was quick and dirty so I won't complain :)

For your tests I would suggest you throw an exception if they fail, as no one will read test logs :)
properties (22)
authorreggaemuffin
permlinkre-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170817t171542809z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-17 17:15:45
last_update2017-08-17 17:15:45
depth2
children2
last_payout2017-08-24 17:15:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length446
author_reputation37,964,839,695,531
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,103,479
net_rshares0
@mkt · (edited)
Oh I really just forgot the "in all other cases" part... :D Actually I never print the number, only in the loop but that was just to check the result.
But I have to say that it is hard to write "good" code without a real use case. In my opinion a one-liner (like @benniebanana suggested) is often a perfect copy/paste solution for simple and unshakable logic. I like code that is confident enough to say "Don't f***ing touch me. I just work! Please do the same!"
Concerning the if-statements I find my version more readable but that's maybe only because I am so used to do it that way. But I don't see any problems with that anyway. Also the function could return null.... maybe that's an issue. But we're in the PHP world here... :P It's like asking your 8 yo child to look after your 4 yo one while you're at work. You get prettily painted walls but the cake doesn't taste that well. :D
Anyway... your competition could become very funny and educational. Keep it on!
properties (22)
authormkt
permlinkre-reggaemuffin-re-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170817t203611617z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"users":["benniebanana"],"app":"steemit/0.1"}
created2017-08-17 20:36:00
last_update2017-08-17 20:40:42
depth3
children1
last_payout2017-08-24 20:36:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length968
author_reputation45,513,283,519,678
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,118,156
net_rshares0
@ratticus · (edited)
$0.07
PHP certainly is like an 8 year old looking after a 4 year old!

I'm less a fan of such confident code when it's just there without comment. If it's a one line code like the one above, I'd hope it has 2 lines of comments explaining it so you know not to mess with it.
👍  
properties (23)
authorratticus
permlinkre-mkt-re-reggaemuffin-re-mkt-re-reggaemuffin-introducing-the-coding-challenge-20170818t072901425z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-18 07:29:00
last_update2017-08-18 07:30:00
depth4
children0
last_payout2017-08-25 07:29:00
cashout_time1969-12-31 23:59:59
total_payout_value0.050 HBD
curator_payout_value0.016 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length267
author_reputation5,793,669,654,286
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,153,295
net_rshares19,163,037,586
author_curate_reward""
vote details (1)