create account

RE: Introducing the Coding Challenge by philosophist

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

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

· @philosophist ·
$0.37
Here is function written in Matlab:
### Test Script:
``` 
for x = 1:100
    disp(fizzbuzz(x));
end
```

### Function:
```
function output = fizzbuzz(n)
% fizzbuzz(n) returns 'Fizz' if n is a multiple of 3, 
% 'Buzz' if n is a multiple of 5, 'FizzBuzz' if n is a 
% multiple of 5 and 3, and returns n if it is neither a 
% multiple of 3 or 5.
% Format of Call: fizzbuzz(n);
% Returns: output = 'Fizz', 'Buzz', 'FizzBuzz', or n 
% based on if n is a multiple of 3 or 5
    
    if ~isnumeric(n)
        error('Error: Input must be a number, not a %s.',class(n));
    else
        output = '';
        if rem(n,3) == 0
            output = 'Fizz';
        end
        if rem(n,5) == 0
            output = strcat(output,'Buzz');
        end
        if strcmp(output,'')
            output = num2str(n);
        end
    end
```
### Console Output
```
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz
```

The general design is to take the input value, check to be sure it's a number, then proceed to construct the output. Since 'FizzBuzz' is the combination of 'Fizz' and 'Buzz' it made sense to me to build the output string starting with an empty string and adding 'Fizz' if n is divisible by 3 followed by 'Buzz' if n is divisible by 5. Since they are concatinated into the empty string, 'FizzBuzz' logically follows if n is divisible by 3 and 5. Finally, if nothing was added to the output string then it must not have been divisible by either, so I assign a string form of n to the output.

I'm curious to see how far off I am from best practices for this challenge.
👍  ,
properties (23)
authorphilosophist
permlinkre-reggaemuffin-introducing-the-coding-challenge-20170817t191654393z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-17 19:16:54
last_update2017-08-17 19:16:54
depth1
children5
last_payout2017-08-24 19:16:54
cashout_time1969-12-31 23:59:59
total_payout_value0.313 HBD
curator_payout_value0.055 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,931
author_reputation1,642,759,591,030
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,112,691
net_rshares106,693,625,857
author_curate_reward""
vote details (2)
@reggaemuffin ·
Interesting choice :) I like your solution.

String compare will probably be a lot slower than doing the reminder again, but if it is an optimized strcmp it will still be pretty fast. 

Be sure to check a few other solutions and give feedback :)
properties (22)
authorreggaemuffin
permlinkre-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170817t193456828z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-17 19:35:00
last_update2017-08-17 19:35:00
depth2
children4
last_payout2017-08-24 19:35: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_length245
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,114,069
net_rshares0
@philosophist · (edited)
Good point. This should be more optimized. New variable 'neither' is a boolean set to true initially and set to false if n is either divisible by 3, 5, or both. Then that becomes the check for if neither is the case.
```
function output = fizzbuzz(n)
% fizzbuzz(n) returns 'Fizz' if n is a multiple of 3, 
% 'Buzz' if n is a multiple of 5, 'FizzBuzz' if n is a 
% multiple of 5 and 3, and returns n if it is neither a 
% multiple of 3 or 5.
% Format of Call: fizzbuzz(n);
% Returns: output = 'Fizz', 'Buzz', 'FizzBuzz', or n 
% based on if n is a multiple of 3 or 5
    
    if ~isnumeric(n)
        error('Error: Input must be a number, not a %s.',class(n));
    else
        neither = true;
        output = '';
        if rem(n,3) == 0
            output = 'Fizz';
            neither = false;
        end
        if rem(n,5) == 0
            output = strcat(output,'Buzz');
            neither = false;
        end
        if neither
            output = num2str(n);
        end
    end
```
properties (22)
authorphilosophist
permlinkre-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t013330149z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-18 01:33:30
last_update2017-08-18 01:36:27
depth3
children3
last_payout2017-08-25 01:33:30
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_length994
author_reputation1,642,759,591,030
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,134,774
net_rshares0
@reggaemuffin ·
That is a good idea! Nice improvement, this is worth a tip!
properties (22)
authorreggaemuffin
permlinkre-philosophist-re-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t052705282z
categorycoding-challenge
json_metadata{"tags":["coding-challenge"],"app":"steemit/0.1"}
created2017-08-18 05:27:06
last_update2017-08-18 05:27:06
depth4
children1
last_payout2017-08-25 05:27:06
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_length59
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,147,251
net_rshares0
@tipu ·
<table><tr><td>http://i.imgur.com/IFF4CSc.jpg</td><td><p>Hi @philosophist! You have just received a 0.5 SBD tip from @reggaemuffin!</p><p><strong>From @reggaemuffin : <a href=https://steemit.com/witness-category/@reggaemuffin/witness-reggaemuffin rel="noopener"> If You Like What I Do, Vote Me For Witness :)</a></strong></p></td></tr></table><hr><p><a href="https://steemit.com/steemit/@tipu/tipu-quick-guide" rel="noopener">@tipU - send tips by writing tip! in the comment and get share in service profit :)</a></p>
properties (22)
authortipu
permlinkre-re-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t013330149z-20170818t052721
categorycoding-challenge
json_metadata""
created2017-08-18 05:27:21
last_update2017-08-18 05:27:21
depth4
children0
last_payout2017-08-25 05:27:21
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_length517
author_reputation55,913,510,651,708
root_title"Introducing the Coding Challenge"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,147,271
net_rshares0