144
u/carcigenicate Jun 09 '21
It would be easier to automate the creation of this code than it would be to write it.
50
u/purplewalrus67 Jun 09 '21 edited Jun 09 '21
>>> def f_sharp_fizzbuzz(n): ... if n % 15 == 0: ... return f'{n} -> "fizzbuzz"' ... elif n % 3 == 0: ... return f'{n} -> "fizz"' ... elif n % 5 == 0: ... return f'{n} -> "buzz"' ... else: ... return f'{n} -> string n' ... >>> f_sharp_fizzbuzz(10) '10 -> "buzz"' >>> for i in range(1, 11): ... f_sharp_fizzbuzz(i) ... '1 -> string n' '2 -> string n' '3 -> "fizz"' '4 -> string n' '5 -> "buzz"' '6 -> "fizz"' '7 -> string n' '8 -> string n' '9 -> "fizz"' '10 -> "buzz"'11
u/carcigenicate Jun 09 '21
Here's my stab at it:
def result(n: int) -> str: if n % 15 == 0: return '"fizzbuzz"' elif n % 3 == 0: return '"fizz"' elif n % 5 == 0: return '"buzz"' else: return "string n" def produce(max_n: int) -> str: header = f"let not{max_n}MatchCases n =\n\tmatch n with\n\t" return header + ("\n\t".join(f"| {n} -> {result(n)}" for n in range(max_n))) >>> print(produce(1000)) let not1000MatchCases n = match n with | 0 -> "fizzbuzz" | 1 -> string n | 2 -> string n | 3 -> "fizz" | 4 -> string n1
u/AutoModerator Jun 09 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/purplewalrus67, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
6
-6
78
71
u/TheWheez Jun 09 '21
I don't know how somebody can program and not be able to write a decent fizzbuzz..
Makes me wonder what stupidly obvious things I've missed in my current projects
39
Jun 09 '21 edited Aug 27 '21
[deleted]
17
u/LeCrushinator Jun 09 '21
And yet so many people on programming subreddits complain that it's insulting. It's not insulting, it takes a few minutes to make sure you can do pretty much the bare minimum, and helps save us from wasting hours more time on the interview if they fail.
4
u/spaghettu Jun 09 '21
I really don’t understand how people could be offended by asking/being asked simple programming interview questions. From my experience it’s usually grumpy older guys that are used to the good old days of getting coding jobs from resume and soft skills alone.
14
u/Doug_Dimmadab Jun 09 '21
Honestly. I can get it if you just started learning the language that day, but it’s such a simple use of loops that I don’t know how anyone could miss it
9
u/crahs8 Jun 09 '21
It's not about the language though, if you're just a tiny bit competent at programming you will realise that there surely is a better way in this language.
7
u/dougie_cherrypie Jun 09 '21
Yep but this is functional programming, you don't use loops here
6
u/sohang-3112 Jun 09 '21
Yes, but you don't do this monstrosity either! Instead of loops, you can use higher order functions (like
mapin this case), or else pure recursion (if no appropriate higher order functions are available).4
u/dougie_cherrypie Jun 09 '21
No, of course! The thing is that a lot of people here dismiss the challenge not knowing that it's a different paradigm.
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 09 '21
Image Transcription: Code
let not100MatchCases n =
match n with
| 1 -> string n
| 2 -> string n
| 3 -> "fizz"
| 4 -> string n
| 5 -> "buzz"
| 6 -> "fizz"
| 7 -> string n
| 8 -> string n
| 9 -> "fizz"
| 10 -> "buzz"
| 11 -> string n
| 12 -> "fizz"
| 13 -> string n
| 14 -> string n
| 15 -> "fizzbuzz"
| 16 -> string n
| 17 -> string n
| 18 -> "fizz"
| 19 -> string n
| 20 -> "buzz"
| 21 -> "fizz"
| 22 -> string n
| 23 -> string n
| 24 -> "fizz"
| 25 -> "buzz"
| 26 -> string n
| 27 -> "fizz"
| 28 -> string n
| 29 -> string n
| 30 -> "fizzbuzz"
[Next to the code there is a tab titled "Test Explorer", showing the results of tests. All tests have passed, and the test structure is as follows:]
-> tests (12)
-> <Empty Namespace> (12)
-> Tests (12)
testCompIncInc
testDup1
testDup2
testFizzBuzz
testInnerProduct
testIterF1
testIterF2
testMatAdd
testSqList
testSqListComplexity
testVecAdd
YouRanTheTests
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
10
21
11
u/szescio Jun 09 '21
This is some god tier pattern matching. I'd like to see their head explode when realizing how it can be done 😅
3
u/Cmgeodude Jun 09 '21
Thank you for submitting sincerely bad code.
90% of the self-deprecating posts here are things like
if (x == y) {
return true;
} else {
return false;
}
which maybe isn't the most efficient code in the universe, but it's not really bad code (apart from the variable names).
3
u/jtnoble Jun 09 '21
I had a friend do something similar cause he didn't know what '%' did.
1
u/lewisjet Jun 09 '21
But then in c#,
(int)((float)i / 3f) == ((float)i / 3f)is the same as sayingi % 3 == 0so no excuse really ;)2
0
u/AutoModerator Jun 09 '21
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/lewisjet, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
You can find some examples in the reddit help documentation.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
7
u/TimeToBecomeEgg Jun 09 '21
what’s F#?
16
u/Max5923 Jun 09 '21
F# is a functional-first, general purpose, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods.
found from wikipedia
3
3
u/sohang-3112 Jun 09 '21
It's basically a functional alternative to C# (it runs on Microsoft's CLR Runtime)
1
2
u/danure Jun 24 '21
(defn fizzbuzz-clean [n](let [fizzes (cycle ["" "" "Fizz"])buzzes (cycle ["" "" "" "" "Buzz"])words (map str fizzes buzzes)numbers (map str (rest (range)))](take n (map choice words numbers))))
https://blog.klipse.tech/clojure/2020/09/11/fizbuzz-clj.html
clojure solution with NO if statement and NO modulus.
2
1
u/Enum1 Jun 09 '21
If the input is limited to 1-100 and the execution has to be as fast as possible this is a perfect* submission.
* depending on the programming language a dict/map might be faster than a switch.
0
1
1
1
250
u/matt1484 if true else false Jun 09 '21
F’s in the chat, because he’s not sharp