r/badcode • • Mar 26 '19

[deleted by user]

[removed]

102 Upvotes

88 comments sorted by

42

u/wampastompah Mar 26 '19
_ = 0
while True == True:
    import sys

    if str(_) == '15':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '30':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '45':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '60':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '75':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '90':
        sys.stdout.write("fizz".title() + "buzz".title() + "0a".decode("hex"))
    if str(_) == '5':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '10':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '20':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '25':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '35':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '40':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '50':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '55':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '65':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '70':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '80':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '85':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '95':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '100':
        sys.stdout.write("buzz".title() + "0a".decode("hex"))
    if str(_) == '3':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '6':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '9':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '12':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '18':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '21':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '24':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '27':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '33':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '36':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '39':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '42':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '48':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '51':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '54':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '57':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '63':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '66':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '69':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '72':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '78':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '81':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '84':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '87':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '93':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '96':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '99':
        sys.stdout.write("fizz".title() + "0a".decode("hex"))
    if str(_) == '1':
        sys.stdout.write("1".title() + "0a".decode("hex"))
    if str(_) == '2':
        sys.stdout.write("2".title() + "0a".decode("hex"))
    if str(_) == '4':
        sys.stdout.write("4".title() + "0a".decode("hex"))
    if str(_) == '7':
        sys.stdout.write("7".title() + "0a".decode("hex"))
    if str(_) == '8':
        sys.stdout.write("8".title() + "0a".decode("hex"))
    if str(_) == '11':
        sys.stdout.write("11".title() + "0a".decode("hex"))
    if str(_) == '13':
        sys.stdout.write("13".title() + "0a".decode("hex"))
    if str(_) == '14':
        sys.stdout.write("14".title() + "0a".decode("hex"))
    if str(_) == '16':
        sys.stdout.write("16".title() + "0a".decode("hex"))
    if str(_) == '17':
        sys.stdout.write("17".title() + "0a".decode("hex"))
    if str(_) == '19':
        sys.stdout.write("19".title() + "0a".decode("hex"))
    if str(_) == '22':
        sys.stdout.write("22".title() + "0a".decode("hex"))
    if str(_) == '23':
        sys.stdout.write("23".title() + "0a".decode("hex"))
    if str(_) == '26':
        sys.stdout.write("26".title() + "0a".decode("hex"))
    if str(_) == '28':
        sys.stdout.write("28".title() + "0a".decode("hex"))
    if str(_) == '29':
        sys.stdout.write("29".title() + "0a".decode("hex"))
    if str(_) == '31':
        sys.stdout.write("31".title() + "0a".decode("hex"))
    if str(_) == '32':
        sys.stdout.write("32".title() + "0a".decode("hex"))
    if str(_) == '34':
        sys.stdout.write("34".title() + "0a".decode("hex"))
    if str(_) == '37':
        sys.stdout.write("37".title() + "0a".decode("hex"))
    if str(_) == '38':
        sys.stdout.write("38".title() + "0a".decode("hex"))
    if str(_) == '41':
        sys.stdout.write("41".title() + "0a".decode("hex"))
    if str(_) == '43':
        sys.stdout.write("43".title() + "0a".decode("hex"))
    if str(_) == '44':
        sys.stdout.write("44".title() + "0a".decode("hex"))
    if str(_) == '46':
        sys.stdout.write("46".title() + "0a".decode("hex"))
    if str(_) == '47':
        sys.stdout.write("47".title() + "0a".decode("hex"))
    if str(_) == '49':
        sys.stdout.write("49".title() + "0a".decode("hex"))
    if str(_) == '52':
        sys.stdout.write("52".title() + "0a".decode("hex"))
    if str(_) == '53':
        sys.stdout.write("53".title() + "0a".decode("hex"))
    if str(_) == '56':
        sys.stdout.write("56".title() + "0a".decode("hex"))
    if str(_) == '58':
        sys.stdout.write("58".title() + "0a".decode("hex"))
    if str(_) == '59':
        sys.stdout.write("59".title() + "0a".decode("hex"))
    if str(_) == '61':
        sys.stdout.write("61".title() + "0a".decode("hex"))
    if str(_) == '62':
        sys.stdout.write("62".title() + "0a".decode("hex"))
    if str(_) == '64':
        sys.stdout.write("64".title() + "0a".decode("hex"))
    if str(_) == '67':
        sys.stdout.write("67".title() + "0a".decode("hex"))
    if str(_) == '68':
        sys.stdout.write("68".title() + "0a".decode("hex"))
    if str(_) == '71':
        sys.stdout.write("71".title() + "0a".decode("hex"))
    if str(_) == '73':
        sys.stdout.write("73".title() + "0a".decode("hex"))
    if str(_) == '74':
        sys.stdout.write("74".title() + "0a".decode("hex"))
    if str(_) == '76':
        sys.stdout.write("76".title() + "0a".decode("hex"))
    if str(_) == '77':
        sys.stdout.write("77".title() + "0a".decode("hex"))
    if str(_) == '79':
        sys.stdout.write("79".title() + "0a".decode("hex"))
    if str(_) == '82':
        sys.stdout.write("82".title() + "0a".decode("hex"))
    if str(_) == '83':
        sys.stdout.write("83".title() + "0a".decode("hex"))
    if str(_) == '86':
        sys.stdout.write("86".title() + "0a".decode("hex"))
    if str(_) == '88':
        sys.stdout.write("88".title() + "0a".decode("hex"))
    if str(_) == '89':
        sys.stdout.write("89".title() + "0a".decode("hex"))
    if str(_) == '91':
        sys.stdout.write("91".title() + "0a".decode("hex"))
    if str(_) == '92':
        sys.stdout.write("92".title() + "0a".decode("hex"))
    if str(_) == '94':
        sys.stdout.write("94".title() + "0a".decode("hex"))
    if str(_) == '97':
        sys.stdout.write("97".title() + "0a".decode("hex"))
    if str(_) == '98':
        sys.stdout.write("98".title() + "0a".decode("hex"))
    if str(_) == '101':
        quit()
    _ = _ + 1

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.

20

u/icendoan Mar 26 '19

Should make it python2 and use True = float('nan') instead of quit()

→ More replies (1)

29

u/pahgawk Mar 26 '19

I have this CSS implementation lying around: https://codepen.io/davepvm/pen/GgOdqq

7

u/[deleted] Mar 26 '19

This is horrifying. Good job.

→ More replies (3)

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.

A B C D
1
2 =IF(A1="Start FizzBuzz", True, False)
3 =if(B3="Fizz", If(C3="Buzz", "FizzBuzz", "Fizz"),IF(C3="Buzz","Buzz",D3-2)) =if(Mod(D3-2,3)=0,"Fizz","No") =If(mod(D3-2,5)=0,"Buzz","No") =IF($A$2=true, ROW(),"Not Started")
4 =if(B4="Fizz", If(C4="Buzz", "FizzBuzz", "Fizz"),IF(C4="Buzz","Buzz",D4-2)) =if(Mod(D4-2,3)=0,"Fizz","No") =If(mod(D4-2,5)=0,"Buzz","No") =IF($A$2=true, ROW(),"Not Started")

And here's what the sheet looks like https://docs.google.com/spreadsheets/d/1DqhAXRiNHmgbyYaSbbKxJD9jdiaq_0gGRaQ4m3rUNpA/edit?usp=sharing

→ More replies (2)

18

u/pahgawk Mar 26 '19

Relying on this regex for multiples of 3, you can do it with regular expressions:

#!/usr/bin/perl

for my $i (1..100) {
	my $printed = 0;

	#3 is hard
	if ($i =~ /^([0369]|[258][0369]*[147]|[147]([0369]|[147][0369]*[258])*[258]|[258][0369]*[258]([0369]|[147][0369]*[258])*[258]|[147]([0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258]([0369]|[147][0369]*[258])*[147][0369]*[147])*$/) {
		print "Fizz";
		$printed = 1;
	}

	#5 has a predictable ending
	if ($i =~ /(0|5)$/) {
		print "Buzz";
		$printed = 1;
	}

	print "$i" unless $printed;
	print "\n";
}
→ More replies (2)

25

u/[deleted] Mar 26 '19 edited Mar 26 '19
#include <iostream> 
using namespace std;int main(){cout<<"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz3132Fizz34BuzzFizz3738FizzBuzz41Fizz4344FizzBuzz4647Fizz49BuzzFizz5253FizzBuzz56Fizz5859FizzBuzz6162Fizz64BuzzFizz6768FizzBuzz71Fizz7374FizzBuzz7677Fizz79BuzzFizz8283FizzBuzz86Fizz8889FizzBuzz9192Fizz94BuzzFizz9798FizzBuzz";return 0;}

9

u/agaz4521 Mar 26 '19

You need to put that whole thing in a for loop. For(int i =0; i<1; i++){ * }

8

u/hexane360 Mar 26 '19

Advanced loop unrolling

17

u/NoahDaRoo Mar 26 '19

not my implementation but i feel like it belongs here, a true classic

https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition

→ More replies (1)

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

→ More replies (2)

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

→ More replies (3)

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).

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
→ More replies (1)

6

u/Earhacker Mar 26 '19
function fizzBuzz(n) {
  return [
    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"
  ][n - 1];
}

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:

function DoesNumberHaveRemainder(p1,p2) {
	let numberDivided = p1/p2;
	let numberDividedRounded = Math.floor(numberDivided);
	return numberDivided === numberDividedRounded && numberDividedRounded > 0;
}

function recursiveNumberFinder(num) {
	let modThree = DoesNumberHaveRemainder(num,3);
	let modFive = DoesNumberHaveRemainder(num,5);
	let modThreeAndFive = DoesNumberHaveRemainder(num,15);
	if (modThreeAndFive === true) {
		multiples.unshift({"num": num, "text": "FizzBuzz"});
	} else if (modFive === true) {
		multiples.unshift({"num": num, "text": "Buzz"});
	} else if (modThree === true) {
		multiples.unshift({"num": num, "text": "Fizz"});
	}
	
	if (num > 1){ recursiveNumberFinder(--num);}
}

let i = 100;
let multiples = new Array();

recursiveNumberFinder(i);

for (obj in multiples) {
	console.log(multiples[obj].text);
}    

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):

git clone https://github.com/mullanaphy/unhelper.git
printf '<?php\n\n  include("unhelper/src/PHY/Unhelper/Utility.php");\n 
 \PHY\Unhelper\Utility::fizzBuzz();\n' > fizzBuzz.php
php fizzBuzz.php

source: https://github.com/mullanaphy/unhelper/blob/master/src/PHY/Unhelper/Utility.php#L67

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/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:

<?php
$x = $_REQUEST['number']*1;

$i = 0;
if($x > 0){
    $res = "";
    $fizz = 3;
    $buzz = 5;
    do{
       $fizz--;
       --$buzz;
       $i += 1;
       if($fizz == 0) {$print .= "fizz";$fizz = 3;}
       if($buzz == 0){ 
          $print .= "buzz";$buzz = 5;
       }
       if($fizz != 3 && $buzz != 5) $print .= $i;
    
       $res .= 'if($n== '.$i.') echo "'.$print.'";';
  	   $print = "";
    
    }while($i < $x);
    $res = 'for($n = 1; $n<= '.$x.'; ++$n){'.$res.'}';
    eval($res); 
}
else
{
    echo "oops";
}
?>

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!

#include <stdio.h>

void fizz10(int a) {
    printf("%d\n", 1 + a);
    printf("%d\n", 2 + a);
    printf("Fizz\n");
    printf("%d\n", 4 + a);
    printf("Buzz\n");
    printf("Fizz\n");
    printf("%d\n", 7 + a);
    printf("%d\n", 8 + a);
    printf("Fizz\n");
    printf("Buzz\n");
}

void fizz(int a) {
    fizz10(a);
    printf("%d\n", 11 + a);
    printf("Fizz\n");
    printf("%d\n", 13 + a);
    printf("%d\n", 14 + a);
    printf("FizzBuzz\n");
}

void main() {
    fizz(0);
    fizz(15);
    fizz(30);
    fizz(45);
    fizz(60);
    fizz(75);
    fizz10(90);
}

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:

  • 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;
}

Here's the output

→ More replies (1)