Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[ENTERPRISE] FizzBuzz (part MAXINT) [CODING]

Name: Anonymous 2012-04-08 10:41

Is this ENTERPRISE QUALITY?

#include <stdbool.h>
#include <stdio.h>

struct rule {
  bool (*pred)(int);
  char *word;
};

void count(struct rule rules[], int min, int max) {
  int num, i;
  bool found;

  for (num = min; num <= max; num++) {
    found = false;

    /* search for words */
    for (i = 0; rules[i].pred != NULL; i++) {
      if ((*rules[i].pred)(num)) {
        printf("%s", rules[i].word);
        found = true;
      }
    }

    /* else just print the number */
    if (!found) printf("%d", num);
    putchar('\n');
  }
}

bool Fizz(int x) { return (x % 3 == 0); }
bool Buzz(int x) { return (x % 5 == 0); }

struct rule fizzbuzz_rules[] = {
  {&Fizz, "Fizz"},
  {&Buzz, "Buzz"},
  {NULL, NULL},
};

int main() {
  count(fizzbuzz_rules, 1, 100);
  return 0;
}

Name: Anonymous 2012-04-08 10:53

Haskell version:

check rules num = if (null c) then (show num) else c
  where c = concatMap snd $ filter (\(p,w) -> p num) rules

count rules min max = unlines $ map (check rules) [min..max]

rules = [((\x -> x `mod` 3 == 0), "Fizz"),
         ((\x -> x `mod` 5 == 0), "Buzz")]

main = putStrLn $ count rules 1 100

Name: Anonymous 2012-04-08 11:05

FIOC version:

def count(rules, low, high):
    for num in range(low, high + 1):
        words = []
        for pred, word in rules:
            if pred(num): words.append(word)
        if words:
            print ''.join(words)
        else:
            print num

fizzbuzz_rules = [
  (lambda x: x % 3 == 0, "Fizz"),
  (lambda x: x % 5 == 0, "Buzz"),
]

count(fizzbuzz_rules, 1, 100)

Name: Anonymous 2012-04-08 11:15

GolfScript version:

{),\>{1${1$\~@@~*}%''+\or}%\;n*}:count;

[[{3%!} "Fizz"]
 [{5%!} "Buzz"]] 1 100 count

Name: Anonymous 2012-04-08 13:41

>>1
Is this ENTERPRISE QUALITY?
 
You're missing:
 - a design document with requirements tracing
 - a work breakdown document with time estimates
 - timesheet entries for the work done
 - minutes document from daily scrum meetings
 - doxygen comments in the code
 - a set of unit tests
 - a code review report and signoff document

Name: Anonymous 2012-04-08 17:56

>>2

fizzBuzz = map (\x -> let s = fb x in if null s then show x else s) [1..]
    where fb x = concatMap (\(p, s) -> if p x then s else "") rules
          rules = [(\x -> x `mod` 3 == 0, "Fizz")
                  ,(\x -> x `mod` 5 == 0, "Buzz")]

now just take 100 fizzBuzz

Name: Anonymous 2012-04-08 18:19

fizzbuzz is Cancer

Name: Anonymous 2012-04-08 19:44

>>5
missing TPS reports

Name: Anonymous 2012-04-08 20:51

have some verbose scheme


(define (range i j)
  (letrec ((helper (lambda (j acc)
                     (if (> i j)
                       acc
                       (helper (- j 1) (cons j acc))))))
    (helper j '())))

(define (divisibility-checker n) (lambda (x) (eq? (mod x n) 0)))

(define (curry f . fixed-args)
  (lambda new-args
    (apply f (append fixed-args new-args))))

(define (reduce op id lis)
  (if (null? lis)
    id
    (reduce op (op id (car lis)) (cdr lis))))

(define (compose . fns)
  (lambda (x)
    (reduce (lambda (val f) (f val)) x (reverse fns))))

(define (conditional-fn predicate true-fn false-fn)
  (lambda (x)
    (if (predicate x)
      (true-fn x)
      (false-fn x))))

(define (true-conditional-fn predicate true-fn)
  (conditional-fn predicate true-fn identity))

(define (compose-true predicate true-fn)
  (conditional-fn predicate true-fn (constant-fn #f)))

(define (constant-fn constant)
  (lambda (x) constant))

(define (identity x) x)

(define (evalutator x)
  (lambda (f) (f x)))

(define (value-collector . fns)
  (lambda (x) (map (evalutator x) fns)))

(define (filter predicate lis)
  (letrec ((helper (lambda (lis acc)
                     (cond ((null? lis) (reverse acc))
                           ((predicate (car lis)) (helper (cdr lis) (cons (car lis) acc)))
                           (else (helper (cdr lis) acc))))))
    (helper lis '())))

(define (filterer predicate)
  (curry filter predicate))

(define (applier f) (curry apply f))

(define (line-printer value)
  (display value) (newline))

(define (for-each proc lis)
  (if (not (null? lis))
    (begin (proc (car lis))
           (for-each proc (cdr lis)))))

(define (exists? predicate lis)
  (cond ((null? lis) #f)
        ((predicate (car lis)) #t)
        (else (exists? predicate (cdr lis)))))

(define (all? predicate lis)
  (cond ((null? lis) #t)
        ((predicate (car lis)) (all? predicate (cdr lis)))
        (else #f)))

(define (exists-checker predicate)
  (curry exists? predicate))

(for-each (compose line-printer
                   (conditional-fn (compose not (exists-checker string?))
                                   car
                                   (compose (applier string-append)
                                            (filterer string?)))
                   (apply value-collector
                          (map true-conditional-fn
                               (map divisibility-checker '(3 5))
                               (map constant-fn '("Fizz" "Buzz")))))
          (range 1 100))

Name: sage 2012-04-08 21:05

I see no factories.

Name: Anonymous 2012-04-08 21:36

(([] STRUCT(PROC(INT)BOOL pred, STRING word) rule list, INT min, max) VOID:
    FOR n FROM min TO max DO
        BOOL found := FALSE;
        FOR i FROM LWB rule list TO UPB rule list DO
            (rule list[i][pred](n)|
                print(rule list[i][word]);
                found := TRUE)
        OD;
        (NOT found|print(n));
        print(new line)
    OD
)((((INT x)BOOL: (x MOD 3) = 0, "Fizz"),
    ((INT x)BOOL: (x MOD 5) = 0, "Buzz")), 1, 100)

Name: Anonymous 2012-04-08 21:51

>>11
is dat sum LISP 1.5?

Name: Anonymous 2012-04-08 22:01

>>11
YOU ME RITE BIG FOBOL OK? MICSOFT DOS ONE POINT ONE

Name: Anonymous 2012-04-08 22:02

FAWBOL

Name: Anonymous 2012-04-09 2:17

>>1
That's a pretty smart hack.
Fun thing is that after so much years we're still hacking around in it.

Name: Anonymous 2012-04-09 3:52

>>1
Make the action depend on the rule instead.

#include <stdbool.h>
#include <stdio.h>

struct rule {
  bool (*pred)(int);
  void (*action)(int);
};

void apply_range(struct rule rules[], int min, int max) {
  int num, i;

  for (num = min; num <= max; num++) {
    /* apply actions */
    for (i = 0; rules[i].pred != NULL; i++) {
      if ((*rules[i].pred)(num)) {
        (*rules[i].action)(num);
      }
    }
  }
}

bool fizzp(int x) { return (x % 3 == 0); }
void fizza(int x) { printf("Fizz"); }

bool buzzp(int x) { return (x % 5 == 0); }
void buzza(int x) { printf("Buzz");

bool numberp(int x) { return !Fizz(x) && !Buzz(x); }
void numbera(int x) { printf("%d", x); }

bool alwaysp(int x) { return 1; }
void newline(int x) { putchar('\n'); }

struct rule fizzbuzz_rules[] = {
  {fizzp, fizza},
  {buzzp, buzza},
  {numberp, numbera},
  {alwaysp, newline},
  {NULL, NULL},
};

int main() {
  apply_range(fizzbuzz_rules, 1, 100);
  return 0;
}

Name: Speedy Gonzalis 2012-04-09 4:28

I win, I win.


#include <stdio.h>

enum { FIZZ = 3, BUZZ = 5 };

int main(void)
{
    int fizz = FIZZ;
    int buzz = BUZZ;
    int i;

    for (i = 1; i <= 100; i++)
        switch ((i == fizz) | (i == buzz) << 1) {
        case 0:
            printf("%d\n", i);
            break;
        case 1:
            printf("Fizz\n");
            fizz += FIZZ;
            break;
        case 2:
            printf("Buzz\n");
            buzz += BUZZ;
            break;
        case 3:
            printf("FizzBuzz\n");
            fizz += FIZZ;
            buzz += BUZZ;
            break;
        }
    return 0;
}

Name: Anonymous 2012-04-09 4:41

LOOOOL LOOKS LIKE KODAK DIGIVOLVED.


#include <stdio.h>

enum { FIZZ = 3, BUZZ = 5 };

static const char *tab[] = {
    0, "1", "2", "3", "4", "5", "6", "7", "8", "9",
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
    "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
    "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
    "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
    "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
    "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
    "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
    "90", "91", "92", "93", "94", "95", "96", "97", "98", "99",
    "100"
};

int main(void)
{
    int fizz = FIZZ;
    int buzz = BUZZ;
    int i;

    for (i = 1; i <= 100; i++)
        switch ((i == fizz) | (i == buzz) << 1) {
        case 0:
            puts(tab[i]);
            break;
        case 1:
            puts("Fizz");
            fizz += FIZZ;
            break;
        case 2:
            puts("Buzz");
            buzz += BUZZ;
            break;
        case 3:
            puts("FizzBuzz");
            fizz += FIZZ;
            buzz += BUZZ;
            break;
        }
    return 0;
}

Name: Anonymous 2012-04-09 4:42

Gotta be fass like indian. Use Turbo C, fastest compiler on the block.

Name: Anonymous 2012-04-09 5:13

>>16
sexp

Name: Anonymous 2012-04-09 5:17

Who's up for a bit of the old Lameda Calcyaless?

Name: Anonymous 2012-04-09 5:22

BTW, you know how when you format an ext2 partition you have a lost+found? With MS-DOS you should have "DONKEY". EEE-AWWWWWW EEEEEE-AWWWWWWWWWw.

Name: Anonymous 2012-04-09 7:53

Surprised no one has posted this yet:
http://code.google.com/p/fizzbuzz/

Name: Anonymous 2012-04-09 8:21

>>23
Why do you find that surprising exactly?

Name: bampu pantsu 2012-05-29 4:15

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List