Viewing a response to: @reggaemuffin/introducing-the-coding-challenge
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.
author | philosophist |
---|---|
permlink | re-reggaemuffin-introducing-the-coding-challenge-20170817t191654393z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 19:16:54 |
last_update | 2017-08-17 19:16:54 |
depth | 1 |
children | 5 |
last_payout | 2017-08-24 19:16:54 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.313 HBD |
curator_payout_value | 0.055 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,931 |
author_reputation | 1,642,759,591,030 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,112,691 |
net_rshares | 106,693,625,857 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
reggaemuffin | 0 | 105,637,451,265 | 1% | ||
quarterlifehuman | 0 | 1,056,174,592 | 100% |
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 :)
author | reggaemuffin |
---|---|
permlink | re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170817t193456828z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 19:35:00 |
last_update | 2017-08-17 19:35:00 |
depth | 2 |
children | 4 |
last_payout | 2017-08-24 19:35:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 245 |
author_reputation | 37,964,839,695,531 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,114,069 |
net_rshares | 0 |
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 ```
author | philosophist |
---|---|
permlink | re-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t013330149z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-18 01:33:30 |
last_update | 2017-08-18 01:36:27 |
depth | 3 |
children | 3 |
last_payout | 2017-08-25 01:33:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 994 |
author_reputation | 1,642,759,591,030 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,134,774 |
net_rshares | 0 |
That is a good idea! Nice improvement, this is worth a tip!
author | reggaemuffin |
---|---|
permlink | re-philosophist-re-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t052705282z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-18 05:27:06 |
last_update | 2017-08-18 05:27:06 |
depth | 4 |
children | 1 |
last_payout | 2017-08-25 05:27:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 59 |
author_reputation | 37,964,839,695,531 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,147,251 |
net_rshares | 0 |
<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>
author | tipu |
---|---|
permlink | re-re-reggaemuffin-re-philosophist-re-reggaemuffin-introducing-the-coding-challenge-20170818t013330149z-20170818t052721 |
category | coding-challenge |
json_metadata | "" |
created | 2017-08-18 05:27:21 |
last_update | 2017-08-18 05:27:21 |
depth | 4 |
children | 0 |
last_payout | 2017-08-25 05:27:21 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 517 |
author_reputation | 55,913,510,651,708 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,147,271 |
net_rshares | 0 |