Viewing a response to: @reggaemuffin/introducing-the-coding-challenge
I'm from a Java background but here is my amateur Python submission. Started learning Python recently and fell in love with it so I thought I'd give this a try. ```` def fizzbuzz(n): string = "" try: if n%3 == 0: string += "Fizz" if n%5 == 0: string += "Buzz" else: string = n return string except TypeError: return "Error: Not a Number!" def main(): for n in range(101): print(fizzbuzz(n)) if __name__ == '__main__': main() ```` **Explanation:** The modulo operator(%) returns the remainder of a division. For example 8%5 will give us 3. When a number is fully dividable by another number it will return 0 thus checking if the remainder of a division is 0 we know that the number is fully dividable by the other which makes it a multiple of that number. **EDIT** After posting my solution which is pretty much my level of Python I decided to do some research to see how the solution could be optimized and this is when I came across this gloriously ugly Python one-liner <a href="http://michaeljgilliland.blogspot.co.za/2013/01/one-line-fizz-buzz-test-in-python.html">here</a> that does exactly the same thing as my script of 23 lines so I thought I'd share it: ```` print('\n'.join("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(0,101))) ```` This made me a little depressed.
author | benniebanana |
---|---|
permlink | re-reggaemuffin-introducing-the-coding-challenge-20170817t151818433z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1","links":["http://michaeljgilliland.blogspot.co.za/2013/01/one-line-fizz-buzz-test-in-python.html"]} |
created | 2017-08-17 15:18:18 |
last_update | 2017-08-17 16:57:42 |
depth | 1 |
children | 5 |
last_payout | 2017-08-24 15:18:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.436 HBD |
curator_payout_value | 0.137 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,406 |
author_reputation | 875,530,017,816 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,094,236 |
net_rshares | 164,600,576,083 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
reggaemuffin | 0 | 105,617,815,737 | 1% | ||
dewallenband | 0 | 6,331,332,274 | 100% | ||
benniebanana | 0 | 7,013,378,391 | 100% | ||
gokulnk | 0 | 29,888,261,369 | 30% | ||
dirkodie | 0 | 817,070,647 | 100% | ||
maretha94 | 0 | 643,383,496 | 100% | ||
dse | 0 | 13,601,977,269 | 1% | ||
pieterb3 | 0 | 687,356,900 | 100% |
One suggestion I would have is keep the return type of fizzbuzz consistent. So I would probably do a `string = str(n)` to ensure it is always a string. I also think you have a bug in there, as the else only counts for the second if, not the first one, so for n=3 is will most likely print `3` and not `Fizz` ;) On coding style: I would rename the `string` variable to something that conveys meaning and not type, maybe `return_text` The one-liner is pretty ugly in my opinion, your version is way more maintainable.
author | reggaemuffin |
---|---|
permlink | re-benniebanana-re-reggaemuffin-introducing-the-coding-challenge-20170817t172427039z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 17:24:30 |
last_update | 2017-08-17 17:24:30 |
depth | 2 |
children | 4 |
last_payout | 2017-08-24 17:24:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.023 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 518 |
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,104,149 |
net_rshares | 6,931,827,479 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
benniebanana | 0 | 6,931,827,479 | 100% |
Ah man I didn't even pick up on that. Thanks for the feedback though, appreciated. As for the str(n) I actually had that in there and decided to take it out haha, don't know why though. Will keep my original answer as is so other people might learn from my mistakes :) . Here's an updated one after following your advice, not sure if it's very efficient but it doesn't have any bugs anymore lol ```` def fizzbuzz(n): try: if n%3 == 0 and n%5 == 0: text = "FizzBuzz" elif n%3 == 0: text = "Fizz" elif n%5 == 0: text = "Buzz" else: text = str(n) return text except TypeError: return "Error: Not a Number!" def main(): for n in range(101): print(fizzbuzz(n)) if __name__ == '__main__': main() ````
author | benniebanana |
---|---|
permlink | re-reggaemuffin-re-benniebanana-re-reggaemuffin-introducing-the-coding-challenge-20170817t173128127z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 17:31:27 |
last_update | 2017-08-17 17:44:48 |
depth | 3 |
children | 3 |
last_payout | 2017-08-24 17:31:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.303 HBD |
curator_payout_value | 0.063 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 822 |
author_reputation | 875,530,017,816 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,104,637 |
net_rshares | 105,617,815,737 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
reggaemuffin | 0 | 105,617,815,737 | 1% |
That looks pretty efficient to me :) Well done! I like your try except btw, that is exactly the python way of asking for forgiveness, not for permission. Here is a tip! for getting back on it and improving it :) If you want you can look at my solution, I made a few things that make it more maintainable (but it get's longer with it) so I would love to hear your feedback on that. And I would be glad if you would make a small post about it explaining how you tackeled the task, what your thinking was etc. :)
author | reggaemuffin |
---|---|
permlink | re-benniebanana-re-reggaemuffin-re-benniebanana-re-reggaemuffin-introducing-the-coding-challenge-20170817t175240016z |
category | coding-challenge |
json_metadata | {"tags":["coding-challenge"],"app":"steemit/0.1"} |
created | 2017-08-17 17:52:42 |
last_update | 2017-08-17 17:52:42 |
depth | 4 |
children | 1 |
last_payout | 2017-08-24 17:52:42 |
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 | 515 |
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,106,337 |
net_rshares | 0 |
<table><tr><td>http://i.imgur.com/IFF4CSc.jpg</td><td><p><strong>Hi @benniebanana! You have just received a 0.5 SBD tip from @reggaemuffin!</strong></p><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></td></tr></table>
author | tipu |
---|---|
permlink | re-re-reggaemuffin-re-benniebanana-re-reggaemuffin-introducing-the-coding-challenge-20170817t173128127z-20170817t175257 |
category | coding-challenge |
json_metadata | "" |
created | 2017-08-17 17:52:57 |
last_update | 2017-08-17 17:52:57 |
depth | 4 |
children | 0 |
last_payout | 2017-08-24 17:52:57 |
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 | 344 |
author_reputation | 55,916,319,164,488 |
root_title | "Introducing the Coding Challenge" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 12,106,356 |
net_rshares | 0 |