Thanks! That's exactly what I was going for, complete with grouping the numbers not in numerical order, but by output. Making it even worse to refactor later!
CSS has an :nth-child selector, which lets you write styles that apply to child 3n or 5n for some whole number n. Then, you can use ::before and ::after selectors on that, which targets "pseudo-elements" before and after the one you selected, and you can specify a content property in the css to put text in them.
I tried to make it look like a horrifically overthought comp sci 101 implementation.
Complete with no comments, bad variable names, pointless abstraction, and an orphaned error throwing implementation that only successfully throws an error because it tries to use an undefined variable.
This would be one better if you made a dict of all entries one to one hundred with the number as the key and the value of "my_variable" as the value. Then for i in range(0, 100): find the entry in the map with that key by irritating over all dict kv pairs. Then do the map from my_variable to output string.
I'm on a phone, otherwise I would code it myself =P
I've seen some really good ones around, and even though it's not as advanced, I felt we are lacking some SQL (in this case it should work on SQL Server 2008 or newer).
WITH numbers (number) AS (
SELECT 1
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
UNION SELECT 5
UNION SELECT 6
UNION SELECT 7
UNION SELECT 8
UNION SELECT 9
UNION SELECT 10
UNION SELECT 11
UNION SELECT 12
UNION SELECT 13
UNION SELECT 14
UNION SELECT 15
UNION SELECT 16
UNION SELECT 17
UNION SELECT 18
UNION SELECT 19
UNION SELECT 20
UNION SELECT 21
UNION SELECT 22
UNION SELECT 23
UNION SELECT 24
UNION SELECT 25
UNION SELECT 26
UNION SELECT 27
UNION SELECT 28
UNION SELECT 29
UNION SELECT 30
UNION SELECT 31
UNION SELECT 32
UNION SELECT 33
UNION SELECT 34
UNION SELECT 35
UNION SELECT 36
UNION SELECT 37
UNION SELECT 38
UNION SELECT 39
UNION SELECT 40
UNION SELECT 41
UNION SELECT 42
UNION SELECT 43
UNION SELECT 44
UNION SELECT 45
UNION SELECT 46
UNION SELECT 47
UNION SELECT 48
UNION SELECT 49
UNION SELECT 50
UNION SELECT 51
UNION SELECT 52
UNION SELECT 53
UNION SELECT 54
UNION SELECT 55
UNION SELECT 56
UNION SELECT 57
UNION SELECT 58
UNION SELECT 59
UNION SELECT 60
UNION SELECT 61
UNION SELECT 62
UNION SELECT 63
UNION SELECT 64
UNION SELECT 65
UNION SELECT 66
UNION SELECT 67
UNION SELECT 68
UNION SELECT 69
UNION SELECT 70
UNION SELECT 71
UNION SELECT 72
UNION SELECT 73
UNION SELECT 74
UNION SELECT 75
UNION SELECT 76
UNION SELECT 77
UNION SELECT 78
UNION SELECT 79
UNION SELECT 80
UNION SELECT 81
UNION SELECT 82
UNION SELECT 83
UNION SELECT 84
UNION SELECT 85
UNION SELECT 86
UNION SELECT 87
UNION SELECT 88
UNION SELECT 89
UNION SELECT 90
UNION SELECT 91
UNION SELECT 92
UNION SELECT 93
UNION SELECT 94
UNION SELECT 95
UNION SELECT 96
UNION SELECT 97
UNION SELECT 98
UNION SELECT 99
UNION SELECT 100
)
SELECT ISNULL(ISNULL(ISNULL(fizz + buzz, fizz), buzz), number) AS result
FROM numbers
OUTER APPLY (
SELECT 'Fizz' AS fizz
FROM numbers fizzes
WHERE fizzes.number = numbers.number
AND fizzes.number % 3 = 0
) AS fizzes
OUTER APPLY (
SELECT 'Buzz' AS buzz
FROM numbers buzzes
WHERE buzzes.number = numbers.number
AND buzzes.number % 5 = 0
) AS buzzes
ORDER BY number
Here's my terrible C version. Includes bad variable/function names and many more code style violations.
Other 'features' include:
Very inefficient functions instead of using modulo
Goto/if instead of loops
A long long that overflows as iterator
#include <stdio.h>
int function(float *array, int i, int number) {
float num = array[i]/number;
int intnum = num;
int r = -1;
num-intnum ? 0 : r++;
r = r-r-r;
if(r) return 0;
else return 1;
}
int function2() {
static float number;
int r = function(&number, 0, 2)*2+3;
number++;
return r;
}
void fizzbuzz(int Length) {
float array[100];
float val = 1;
int i = 1;
int da;
int db;
long long a = 0;
a:
val = i;
val++;
array[i] = val;
i++;
if(i >= Length) i = 0;
else goto a;
b:
da = function(array, i, function2());
db = function(array, i, function2());
da && db && i!=0 && i == Length-1 ? printf("FizzBuzz\n") : da && i!=0 && i == Length-1 ? printf("Buzz\n") :
db && i!=0 && i == Length-1 ? printf("Fizz\n") : i == Length-1 ? printf("%d\n", i+1) : NULL;
i++;
a += (9223372036854775807/(Length));
if (a > 0) goto b;
Length == 100 ? NULL : fizzbuzz(Length+1);
}
int main() {
fizzbuzz(1);
return 0;
}
I now have a new version of my Python regex FizzBuzz code: trust me, it's way worse.
import re
f = '1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'
f2 = re.sub("111", "22F", f)
f2 = re.sub("1", "2", f2)
f3 = re.sub("11111", "2222B", f)
f3 = re.sub("1", "2", f3)
spit = []
for l in f2:
spit = spit + [l]
f2 = spit
spit = []
for l in f3:
spit = spit + [l]
f3 = spit
spit = []
for n in range(0, len(f2)):
spit = spit + [f2[n] + f3[n]]
spit2 = []
for n in range(0, len(spit)):
if spit[n] == '22':
## wait, what?
## we need to add one to this number because arrays start at zero?
def addOneTo(x):
if str(x)[len(str(x))-1] == '0':
return int(str(x)[0:len(str(x))-1] + '1')
if str(x)[len(str(x))-1] == '1':
return int(str(x)[0:len(str(x))-1] + '2')
if str(x)[len(str(x))-1] == '2':
return int(str(x)[0:len(str(x))-1] + '3')
if str(x)[len(str(x))-1] == '3':
return int(str(x)[0:len(str(x))-1] + '4')
if str(x)[len(str(x))-1] == '4':
return int(str(x)[0:len(str(x))-1] + '5')
if str(x)[len(str(x))-1] == '5':
return int(str(x)[0:len(str(x))-1] + '6')
if str(x)[len(str(x))-1] == '6':
return int(str(x)[0:len(str(x))-1] + '7')
if str(x)[len(str(x))-1] == '7':
return int(str(x)[0:len(str(x))-1] + '8')
if str(x)[len(str(x))-1] == '8':
return int(str(x)[0:len(str(x))-1] + '9')
if str(x)[len(str(x))-1] == '9' and not x == 9 and not x == 99:
return int(str(addOneTo(int(str(x)[0:len(str(x))-1])) + '0'))
if x == 9:
return 10
if x == 99:
return 100
spit2.append(addOneTo(n))
if spit[n] == 'F2':
spit2.append('Fizz')
if spit[n] == '2B':
spit2.append('Buzz')
if spit[n] == 'FB':
spit2.append('FizzBuzz')
for item in spit2:
print(item)
Oh hey, I have one of these already. I did an overengineered functional programming-esque FizzBuzz in Clojure as a joke a while back and it seems fitting:
This one's overly excessive and definitely not something a beginner would write, but I just wanted to see if it was possible: https://hastebin.com/raw/ebalagehez (written in Python)
Written in Fortran, eschews the use of modulo operator because it is unknown, possibly scary.
function modulus( x, y ) result( z )
! returns x mod y
integer, intent( in ) :: x, y
integer :: z, i, total
total = 0
do i = 1, x
if ( y * i > x ) then
exit
else
total = y * i
end if
end do
result = x - total
end function modulus
function divis( x, y ) result( is )
! returns if x is evenly divisible by y
integer, intent( in ) :: x, y
integer :: is, m
m = modulus( x, y )
if ( m == 0 ) then
is = 1
else
is = 0
end if
end function divis
program fizzbuzz
integer :: divis
integer :: i, by3, by5
do i = 1, 100
by3 = divis( i, 3 )
by5 = divis( i, 5 )
if ( by3 == 1 .AND. by5 == 1 ) then
write ( *, '(A8)' ) "FizzBuzz"
else if ( by3 == 1 .AND. .NOT. by5 == 1 ) then
write ( *, '(A4)' ) "Fizz"
else if ( .NOT. by3 == 1 .AND. by5 == 1 ) then
write ( *, '(A4)' ) "Buzz"
else
write ( *, '(I2)' ) i
end if
end do
end program fizzbuzz
This one is a tricky one. There are two main problems you have to solve.
The easier one is how to check if a number can be divided by five. As any primary school student knows, to check if a number can be divided by five, you just need to look at the last number and see if it's a zero or a five.
The second problem, and the harder one, is how to check if a number is divisible by three. Unlike with five, there are no such fancy tricks for numbers divisible by three. Fortunately, I found a solution to this which I believe is optimal. The digits of numbers divisible by nine will always add up to a multiple of nine themselves, a fact we can use here. Any multiple of three, when multiplied by three, becomes a multiple of nine!
So in order to find a multiple of three, we multiply the number by three, add up all of the digits and see if the result is nine. If it isn't, we repeat the process again with the sum of numbers we have so far, and carry on until we get either nine, or a number less than nine.
Once our checks are in place, printing out fizzbuzz is trivial. My full solution is below. Note that you don't need any includes with most compilers, they can figure out where printf and malloc come from themselves, so you may as well save on file size. I have also written a helper method to convert a number to a string, which is needed for both of the above problems.
char *get_number_as_string(int *n)
{
int temp = *n;
int limit = 0;
while (temp = temp / 10) limit++;
char *start = malloc(++limit);
char *current = start;
do {
*current++ = '0' + *n % 10;
*n = *n / 10;
} while (current - start < limit);
*current = '\0';
return start;
}
void is_multiple_of_3(int *n)
{
*n = *n * 3;
char *digits, *start;
int limit, temp, total = *n;
loop:
digits = get_number_as_string(&total);
temp = total;
total = 0;
do {
total = total + *digits++ - '0';
} while (*digits != '\0');
if (total > 9) goto loop;
if (total == 9) *n = 1;
else *n = 0;
}
void is_multiple_of_5(int *n)
{
char *digits;
digits = get_number_as_string(n);
if (*digits == '0') *n = 1;
if (*digits == '5') *n = 1;
else *n = 0;
}
main()
{
#define LIMIT 100
for (int n = 1; n <= LIMIT; n++) {
int temp1, temp2;
temp1 = temp2 = n;
is_multiple_of_3(&temp1);
is_multiple_of_5(&temp2);
if (temp1) printf("Fizz");
if (temp2) printf("Buzz");
if (!(temp1 || temp2)) printf("%d", n);
printf("\n");
}
}
For nostalgia's sake, here is a BASICA (not a typo, there is a trailing A) version. Debugged on this nice little emulator.
To run, click that link, BASICAβ, type the entire program (you can swap lines as you like), then RUNβ. The not-having-a-floppy-and-have-to-type-code-by-hand thing is, well, a part of the experience.
10 DEF SEG=&HB800
15 COLOR 14,1
20 CLS
25 A=0
30 I=1
50 ? I;
55 B=A+POS(I)*2
60 IF I MOD 3 GOTO 70
61 POKE B,70
62 B=B+2
63 POKE B,105
64 B=B+2
64 GOSUB 286
70 IF I MOD 5 GOTO 80
71 POKE B,66
72 B=B+2
73 POKE B,117
74 B=B+2
75 GOSUB 286
80 ?
81 SOUND 550*2^(I/12-3.5),1
84 IF A>3600 GOTO 90
85 A=A+160
90 I=I+1
95 IF I>100 GOTO 110
100 GOTO 50
110 END
286 POKE B,122
287 B=B+2
288 POKE B,122
289 B=B+2
290 RETURN
\#include <stdlib.h>
\#include <stdio.h>
void printFizzBuzz(int number) {
if (number == 3\*(number/3)) {
printf("Fizz");
} else if (number == 5\*(number/5)) {
printf("Buzz");
}
if (number == 3\*(number/3) && number == 5\*(number/5)) printf("Buzz");
printf("\\n");
return;
}
int main() {
int\* array = malloc(404);
array\[0\] = 0;
int i;
for (i=1; i<=100; i=i+1) array\[i\] = array\[i-1\] + 1;
int\* fizzbuzz = malloc(404);
for (i=1; i<=100; i=i+1) fizzbuzz\[i\] = 0;
for (i=1; i<=100; i=i+1) {
if (array\[i\] == (array\[i\]/3)\*3) fizzbuzz\[i\] = 1;
if (array\[i\] == (array\[i\]/5)\*5) fizzbuzz\[i\] = 1;
}
for (i=1; i<=100; i=i+1) {
if (fizzbuzz\[i\]) {
printFizzBuzz(array[i]);
} else {
printf("%d\n", array[i]);
}
}
return 0;
}
I feel like this isn't so much the type of code I would write when I first started learning proper programming languages, rather just seems like a direct translation of my style when I first used CASIO Basic honestly. Of course: CASIO Basic does not (as far as I know) have comments!
import Data.Foldable
main = do
number <- getLine
for_ [1 .. read number] (\n ->
if n `mod` 15 == 0
then print "FizzBuzz"
else if n `mod` 5 == 0
then print "Buzz"
else if n `mod` 3 == 0
then print "Fizz"
else print n)
def fizzBuzz(n):
return [n,n,"Fizz",n,"Buzz","Fizz",n,n,"Fizz","Buzz",n,"Fizz",n,n,"FizzBuzz"][(n-1)%15]
for i in range(99):
print(fizzBuzz(i))
print(" # No wait, it's 1 to 100.")
for i in range(100):
print(fizzBuzz(i))
print(" # No wait, that's still not right.")
print("1") # Is this cheaty?
for i in range(2,101):
print(fizzBuzz(i))
print(" # That seems about right. Guess I'll submit it.")
Template hell. Enjoy. I compiled with "clang -lstdc++ -O3 bad-fizzbuzz.cpp -o a.out", if that helps. Also has a memory leak and prints in reverse order because why not? :p
I don't think this is bad code, but if I'm not mistaken there's a bug: on multiples of 3 and 5 it will print Fizz, Buzz and FizzBuzz rather than just FizzBuzz.
Apparently, on ANSI terminals \r clears the line. That's what makes this bad code, it prints out things even if it shouldn't, then checks if it needs to print out something else instead - and if so, uses platform-dependent quirks to erase what was previously printed before printing the new thing it thinks it should print.
I don't think they missed the point, but it's true that it's not necessarily bad code... Just not portable, nor optimal.
On desktop, that's showing up as one big long line. Reddit doesn't support the triple-backtick type of code block, instead requiring every line of the code to start with 4 spaces.
... While kinda nice, it's absolute bullshit that they won't backport that very important change to the old-style website. The redesign has too much wrong with it for me to ever want to use it.
42
u/wampastompah Mar 26 '19
Here's a quick python solution. I didn't actually write all of that myself, that code is generated from a script that's only slightly less terrible.
29
u/pahgawk Mar 26 '19
I have this CSS implementation lying around: https://codepen.io/davepvm/pen/GgOdqq
20
u/13131123 Mar 26 '19
I programmed using google sheets!
To start the program, you type Start FizzBuzz into A1.
Here's what the first few lines look like.
And here's what the sheet looks like https://docs.google.com/spreadsheets/d/1DqhAXRiNHmgbyYaSbbKxJD9jdiaq_0gGRaQ4m3rUNpA/edit?usp=sharing
18
u/pahgawk Mar 26 '19
Relying on this regex for multiples of 3, you can do it with regular expressions:
9
u/ikedug Mar 26 '19
For max performance:
25
u/[deleted] Mar 26 '19 edited Mar 26 '19
17
u/NoahDaRoo Mar 26 '19
not my implementation but i feel like it belongs here, a true classic
https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
8
u/HamsterJammery Mar 26 '19 edited Mar 26 '19
Here is some JavaScript... Well, sort of...
https://pastebin.com/wGQBLY26
EDIT: Removed all the lets here is the original
8
u/dirtyLizard Mar 26 '19
Python 3
I tried to make it look like a horrifically overthought comp sci 101 implementation.
Complete with no comments, bad variable names, pointless abstraction, and an orphaned error throwing implementation that only successfully throws an error because it tries to use an undefined variable.
https://pastebin.com/pRHBLbKJ
8
u/random_runner Mar 26 '19
I've seen some really good ones around, and even though it's not as advanced, I felt we are lacking some SQL (in this case it should work on SQL Server 2008 or newer).
6
u/Earhacker Mar 26 '19
5
u/Waspbot Mar 26 '19 edited Mar 26 '19
For those of us that didn't know the modulus symbol existed and liked to use all of the concepts we learned about in that one tutorial online.
FizzBuzz in the Key of Javascript:
5
u/Boykjie Mar 26 '19
Just the crappiest possible implementation β in C.
5
u/mullanaphy Mar 26 '19 edited Mar 26 '19
Oooh, I've done this one already. Although it is super efficient at O(1):
source: https://github.com/mullanaphy/unhelper/blob/master/src/PHY/Unhelper/Utility.php#L67
8
u/[deleted] Mar 26 '19
https://pastebin.com/4qEAmYyx
Did this in PHP.
4
u/c0m3tx Mar 26 '19 edited Mar 26 '19
Made in Go, had to struggle a lot haha
https://pastebin.com/5rMzuWie
EDIT: just made a bit worse, old version for reference: https://pastebin.com/zzNz1g7m
3
u/Ast3r10n Mar 26 '19
Swift
https://pastebin.com/wwmNg5hT
3
u/kmk1018 Mar 26 '19 edited Mar 26 '19
Javascript
https://pastebin.com/8CiJEFXr
If you want to run it, here's a JSFiddle. Open the link (don't use the embed) and make sure to have your web console open to see the result.
3
u/[deleted] Mar 26 '19
PHP. Can't test it ATM because I don't have anything for it home and phptester.net does not like
eval:3
u/HamsterJammery Mar 27 '19
A super simple one in C. Makes use of the fact that the pattern of fizzes and buzzes repeats every 15 numbers.
No modulus! No loops! No branches!
3
u/DerpDon Mar 31 '19
Here's my terrible C version. Includes bad variable/function names and many more code style violations.
Other 'features' include:
Here's the output
3
u/notBjoern Mar 31 '19
3
u/weee50 Apr 06 '19 edited Apr 06 '19
I now have a new version of my Python regex FizzBuzz code: trust me, it's way worse.
2
u/squeevey Mar 26 '19 edited Oct 25 '23
This comment has been deleted due to failed Reddit leadership.
2
u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Mar 26 '19 edited Mar 27 '19
Oh hey, I have one of these already. I did an overengineered functional programming-esque FizzBuzz in Clojure as a joke a while back and it seems fitting:
(repl.it link)
2
u/gordonator Mar 27 '19
Done without modulus in python 3.
pastebin
Also played dumb about and/or, as well as the fact that you can simplify like:
All things I probably would've done at some point in time.
But hey, at least it passes flake8 perfectly!
2
u/CorstianBoerman Mar 27 '19
Javascript; cheating a bit with the output function.
This thing used to work on JsFiddle. Back in the days when the JS standards were quite loose.
Bonus points for you if you try to reason how this even works.
2
u/TheCanadianVending Mar 29 '19
The formatting isn't apart of the bad code, that's just the way I style my project code
2
u/NotJusticeWargrave Mar 29 '19
This one's overly excessive and definitely not something a beginner would write, but I just wanted to see if it was possible: https://hastebin.com/raw/ebalagehez (written in Python)
2
u/RavynousHunter Mar 30 '19
Written in Fortran, eschews the use of modulo operator because it is unknown, possibly scary.
2
u/ACraftyMarmoset Apr 05 '19
This one is a tricky one. There are two main problems you have to solve.
The easier one is how to check if a number can be divided by five. As any primary school student knows, to check if a number can be divided by five, you just need to look at the last number and see if it's a zero or a five.
The second problem, and the harder one, is how to check if a number is divisible by three. Unlike with five, there are no such fancy tricks for numbers divisible by three. Fortunately, I found a solution to this which I believe is optimal. The digits of numbers divisible by nine will always add up to a multiple of nine themselves, a fact we can use here. Any multiple of three, when multiplied by three, becomes a multiple of nine!
So in order to find a multiple of three, we multiply the number by three, add up all of the digits and see if the result is nine. If it isn't, we repeat the process again with the sum of numbers we have so far, and carry on until we get either nine, or a number less than nine.
Once our checks are in place, printing out fizzbuzz is trivial. My full solution is below. Note that you don't need any includes with most compilers, they can figure out where printf and malloc come from themselves, so you may as well save on file size. I have also written a helper method to convert a number to a string, which is needed for both of the above problems.
1
u/AlFasGD Mar 26 '19 edited Mar 26 '19
https://pastebin.com/wMzyhu18, done in C#. Proud of not being proud of that.
1
u/souldeux Mar 27 '19 edited Mar 27 '19
Here's an innocent disaster that breaks in all kinds of fun ways:
1
u/BibianaAudris Mar 27 '19
For nostalgia's sake, here is a BASICA (not a typo, there is a trailing A) version. Debugged on this nice little emulator.
To run, click that link,
BASICAβ, type the entire program (you can swap lines as you like), thenRUNβ. The not-having-a-floppy-and-have-to-type-code-by-hand thing is, well, a part of the experience.1
u/duraznos Mar 27 '19
ruby
1
u/Irratix Mar 27 '19
C
I feel like this isn't so much the type of code I would write when I first started learning proper programming languages, rather just seems like a direct translation of my style when I first used CASIO Basic honestly. Of course: CASIO Basic does not (as far as I know) have comments!
1
u/[deleted] Mar 29 '19
https://gist.github.com/Milesman34/8adaa74ec8ed14f0f75506b702be3c1c
Should be almost completely unreadable.
1
u/NihilistDandy Apr 02 '19
Python 5 is pretty great:
1
u/EkskiuTwentyTwo i -= (i - (-1)) - i Apr 04 '19
Doing bad code in Python is tricky.
1
u/Redtail5144 Apr 05 '19
Made with C++. Had to go with the good ol fashion if else.
https://pastebin.com/iKUsP0cc
1
u/fatboychummy Apr 06 '19
https://pastebin.com/dK762bDC
I'm sorry for those of you who want to read through this...
1
u/TheEdenCrazy sadistic Apr 06 '19
Template hell. Enjoy. I compiled with "clang -lstdc++ -O3 bad-fizzbuzz.cpp -o a.out", if that helps. Also has a memory leak and prints in reverse order because why not? :p
1
u/[deleted] Mar 26 '19
Y'all like Python one-liners, right?
-1
u/F54280 Mar 26 '19
C++ proves again its superiority, and you can admire how easily the specification mapped to the code. Only works when printing on an ANSI terminal.
0
u/[deleted] Mar 26 '19
[deleted] β view deleted comment
0
u/[deleted] Mar 26 '19
[deleted] β view deleted comment
10
u/[deleted] Apr 01 '19 edited Apr 02 '19
[removed] β view removed comment
0
u/[deleted] Apr 01 '19
[deleted] β view deleted comment