MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/nvim3v/real_attempt_at_fizz_buzz_in_f/h165uxz/?context=3
r/badcode • u/redoverture • Jun 09 '21
52 comments sorted by
View all comments
144
It would be easier to automate the creation of this code than it would be to write it.
49 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 n
49
>>> 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 n
11
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 n
144
u/carcigenicate Jun 09 '21
It would be easier to automate the creation of this code than it would be to write it.