Give Me Your Fizzes And Buzzes

ITT: We write FizzBuzz in any of our favorite languages. General discussion behind showcased languages is encouraged.

Forth:
: foo? DUP 3 MOD 0 = IF ." Fizz" THEN ;

: bar? DUP 5 MOD 0 = IF ." Buzz" THEN ;

: neither? DUP 3 MOD 0 IF DUP 5 MOD 0 IF DUP . THEN THEN CR ;

: fizzBuzz! DUP 0 IF foo? bar? neither? 1- RECURSE THEN ;

100 fizzBuzz!

Attached: johnny_automatic_bending_bee_from_side.png (267x300, 33K)

Other urls found in this thread:

rosettacode.org/wiki/FizzBuzz
pastebin.com/6yTbYQpi
pastebin.com/YanyVE1e
twitter.com/SFWRedditVideos

Go:
package fizzbuzz
import "fmt"
func Fire(ceiling uint) {
var iterator uint
for ; ceiling > iterator; iterator++ {
if iterator % 3 == 0 {
fmt.Printf("Fizz")
}
if iterator % 5 == 0 {
fmt.Printf("Buzz")
}
if iterator % 3 != 0 && iterator % 5 != 0 {
fmt.Printf("%d", iterator)
}
fmt.Printf("\n")
}
}

if number = divisible by 3
print fizz
if number = divisible by 5
print buzz
number = number + 1

>if iterator % 3 != 0 && iterator % 5 != 0
this should be the first condition in your loop, since the loop will be read sequentially

```lisp
(defun f()
(defvar l)
(setq l 1)
(loop
(FizzBuzz l)
(setq l (+ l 1))
(when (> l 100) (return))))

(defun FizzBuzz(n)
(cond ((and (= (mod n 3) 0) (= (mod n 5) 0)) (write-line "FizzBuzz"))
((= (mod n 3) 0) (write-line "Fizz"))
((= (mod n 5) 0) (write-line "Buzz"))
(t (format t "~a~%" n))))
```

Rust

for i in 0..1000 {
match (i % 3, i % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
(_, _) => println!("{}", i)
}
}

why?
That's not his condition to print fizzbuzz

Common Lisp:

(defmacro fizzbuzz (max &body cases)
`(loop for i from 1 to ,max do
(let ((output ""))
,@(loop for (divisor text) in cases collecting
`(when (= 0 (mod i ,divisor))
(setf output (concatenate 'string output ,text))))
(if (string-equal output "") (setf output i))
(format t "~A~%" output))))

(fizzbuzz 100
(3 "Fizz")
(5 "Buzz")
(7 "Fluzz"))

Common Lisp
(loop for n from 1 to 100
do (format t "~&~[~[FizzBuzz~:;Fizz~]~*~:;~[Buzz~*~:;~D~]~]~%"
(mod n 3) (mod n 5) n))

Datalog:

.decl InRange(n: number)
InRange(1).
InRange(n+1) :- InRange(n), n < 100.

.decl FizzBuzz(n: number, s: symbol)
FizzBuzz(n, "FizzBuzz") :- InRange(n), n%3 = 0, n%5 = 0.
FizzBuzz(n, "Fizz") :- InRange(n), n%3 = 0, n%5 != 0.
FizzBuzz(n, "Buzz") :- InRange(n), n%3 != 0, n%5 = 0.
FizzBuzz(n, to_string(n)) :- InRange(n), n%3 != 0, n%5 != 0.

// This last relation is necessary so that the output is sorted by number instead of by string
.decl OrderedPairs(n: number, s: symbol)
OrderedPairs(n, s) :- InRange(n), FizzBuzz(n, s).
.output OrderedPairs(IO=stdout)

Python, the only language i know, written to be easily expandable
nums = [3,5]
names = ['Fizz','Buzz']

for a in range(1,101):
out = ''
for b in range(0,len(nums)):
if a%nums[b]==0:
out = out + names[b]
out = str(a) if len(out) == 0 else out
print(out)

POSIX shell:
#!/bin/sh

[ "$1" -le "$2" ] 2> /dev/null || {
echo "usage: ${0##*/} from to"
exit 1
}

i="$1"
while [ "$2" -ge "$i" ]
do
if [ $(( i % 15 )) -eq 0 ]
then
echo fizzbuzz
elif [ $(( i % 5 )) -eq 0 ]
then
echo buzz
elif [ $(( i % 3 )) -eq 0 ]
then
echo fizz
else echo "$i"
fi
i=$(( i + 1 ))
done

## Functions ###
range = $(if $(filter $1,$(lastword $3)),$3,$(call range,$1,$2,$3 $(words $3)))
make_range = $(foreach i,$(call range,$1),$(call range,$2))
equal = $(if $(filter-out $1,$2),,$1)


### Variables ###
limit := 101
numbers := $(wordlist 2,$(limit),$(call range,$(limit)))

threes := $(wordlist 2,$(limit),$(call make_range,$(limit),2))
fives := $(wordlist 2,$(limit),$(call make_range,$(limit),4))

fizzbuzz := $(foreach v,$(numbers),\
$(if $(and $(call equal,0,$(word $(v),$(threes))),$(call equal,0,$(word $(v),$(fives)))),FizzBuzz,\
$(if $(call equal,0,$(word $(v),$(threes))),Fizz,\
$(if $(call equal,0,$(word $(v),$(fives))),Buzz,$(v)))))


### Target ###
.PHONY: all
all: ; $(info $(fizzbuzz))

U8 i;
for (i = 1; i

Attached: 800px-Terry_A._Davis_in_front_of_Richland_WA_library.jpg (800x600, 207K)

kek

if(fizz) {
printf("fizz") ;
}
else{
printf("buzz") ;
}

Fuck POSIX
#!/bin/bash

declare -i start="$1"
declare -i end="$2"

(( start >= end )) && { echo "Error: start ($start) >= end ($end)"; exit; }

for (( i=start; i

how do i post code ?
test

four spaces
is this code
i hope so

ok is code ???

print '1'
print '2'
print 'fizz'
print '4'
print 'buzz'
print 'fizz'
print '7'
print '8'
print 'fizz'
print 'buzz'
print '11'
print 'fizz'
print '13'
print '14'
print 'fizzbuzz'

> tfw my name is Terry
> tfw i have a drug rug that looks like that

Attached: 1560728175447.jpg (640x958, 84K)

Also post other programming questions for your job interviews

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

related
rosettacode.org/wiki/FizzBuzz

Attached: title.png (110x157, 22K)

|fizzBuzz 'U
'& '/ |
[ 20 fizzBuzz ]

|fizzBuzz 0
'& cr
i> |
[ 20 fizzBuzz ]
[ '~~~ ]

green - call word
blue - literal
purple - machine code
yellow - loop, if

Attached: noi.png (984x166, 3K)

pastebin.com/6yTbYQpi

The code was longer than the text limit, so here is a pastebin link. I wrote this in ARM assembly for fun a few weeks ago so there might be bugs, but I am pretty sure it works as expected, it stores the outputs in memory as hex. Enjoy :)

fizzbuzz

|counter| %2
|c++ counter ++ | !!
|FBZ ( a u n i -- 0/1 ) 'X mod 0 '=

Attached: noi2.png (1065x411, 6K)

Python
fizz = [x for x in range(3,101,3)]
buzz = [x for x in range(5,101,5)]
fizzbuzz = [x for x in range(15,101,15)]
for x in range(1,101):
if x in fizzbuzz:
print(x, "fizzbuzz")
continue
if x in fizz:
print(x, "fizz")
if x in buzz:
print(x, "buzz")

Don't hate me cuz u ain't me

All these literal pajeets and pic relateds using modulo not even considering a different approach, not even realizing not using modulo in fizzbuzz is possible. You all disgust me.

Attached: Ambiguous+pathetic+effeminate+cucumber_a6dcc3_6627941.jpg (750x831, 102K)

d = {x:'fizz' for x in range(3, 101, 3)}
d.update({x:'buzz' for x in range(5, 101, 5)})
d.update({x:'fizzbuzz' for x in range(15, 101, 15)})
[print(x[0], x[1]) for x in sorted(d.items())]

Am I hired?

Sub fizzbuzz()
Dim i As Long, N As Long

N = Range("C2").Value
Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Clear
For i = 1 To N
If i Mod 15 = 0 Then
Cells(i, 1).Value = "fizzbuzz"
ElseIf i Mod 3 = 0 Then
Cells(i, 1).Value = "fizz"
ElseIf i Mod 5 = 0 Then
Cells(i, 1).Value = "buzz"
Else
Cells(i, 1).Value = i
End If
Next i
End Sub

Attached: fizzbot.png (336x819, 16K)

scoring by amount of editing required for addion of fluzz 7
lower is better
every edit op == 1 point
add a line of 'fluzz?'
change definitio of 'niether?'
add word to 'fizzBuzz!'
3 point
need to a 3 lines of a new if
nee to add condition to las if
2 point
need to add new line of fluzz
need to change line of cond
2 points
can rust match (_,_,0)?
need to add more line to cover all permutations
3~4 points
need to add single line
1 point , good job
need to do some black vodoo on the format
1 point, voodoo job unsacalable
need add lines for new permutations
3~4 points
need to change values of nums and names
1 point, good job
need to add more permutations
3~4 points
profit?
need to add lines for more permutations
3~4 points
same
need to add line with new fluzz
need to add another and opertaion
2 points
need to add a single line
1 point , good job
need to add a single line
1 point , good job
need to add a line for new condition
need to add a line for new if and permutations
3~4 points
need to add single line for new condition
1 point, good job

i was wrong, needs to add lines for more permutations
3~4 points, no good job
need more permutations
3~4 points

What can we learn from this?
Anyone who had in his/her code the word "fizzbuzz" or "buzzfizz" will encounter a problem of permutations when asked for addition of a new condition.
Therefore having those words in your code is a mistake.
The word doesn't appear in the specs and is only a logical conclusion of the technecalities of the problem and should be kept that way. Meaning it should not appear in the code straight forward and just be implied by the logic and produced only in the execution of the program.

Bet I'm learning Forth still and am gonna come at you with a superior solution when I wake up.

I can learn, senpai!
Sub fizzbuzz()
Dim i As Long, N As Long, Val As Variant

N = Range("C2").Value
Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row).Clear
For i = 1 To N
Val = ""
If i Mod 3 = 0 Then
Val = Val & "fizz"
End If
If i Mod 5 = 0 Then
Val = Val & "buzz"
End If
If i Mod 7 = 0 Then
Val = Val & "fluzz"
End If
If Len(Val) = 0 Then
Val = i
End If
Cells(i, 1).Value = Val
Next i
End Sub

Attached: fizzbot4senpai.png (361x912, 21K)

how about Power assembly?

Attached: fizzbuzz.png (950x1850, 32K)

>trivial
>circular linked lists of ["" "" "fizz"] etc
>circular iterators
>functional solution (take n (map max (range) (map join (cycle fizzes) (cycle buzzes))) ;; OTOH

Fine. New conditions only need to be added to the array now.
pastebin.com/YanyVE1e
(pastebin, because Jow Forums refuses to let me post my code)

If adding a whole line still gives the same score as adding a value pair, then here a cleaner version.
#!/bin/bash

(( $# == 0 )) && { echo "Usage: $0 START END"; exit; }

declare -i start="$1"
declare -i end="$2"

(( start > end )) && { echo "Error: start ($start) > end ($end)"; exit; }

for (( i=start; i

Posting the official SwiftTM Protocol Oriented ProgrammingTM solution. This is what peak FizzBuzz looks like
protocol Fizzable {
func canBeFizzed() -> Bool
}

protocol Buzzable {
func canBeBuzzed() -> Bool
}

protocol Fizzbuzzable: Fizzable, Buzzable {
func canBeFizzBuzzed() -> Bool
}

extension Fizzbuzzable {
func canBeFizzBuzzed() -> Bool {
return canBeBuzzed() && canBeFizzed()
}
}

extension Int: Fizzbuzzable {
func canBeFizzed() -> Bool {
return self % 3 == 0
}

func canBeBuzzed() -> Bool {
return self % 5 == 0
}
}

protocol Factory {
func makeString() -> String
}

struct FizzFactory: Factory {
func makeString() -> String {
return "fizz"
}
}

struct BuzzFactory: Factory {
func makeString() -> String {
return "buzz"
}
}

struct FizzBuzzFactory: Factory {
private let fizzFactory = FizzFactory()
private let buzzFactory = BuzzFactory()

func makeString() -> String {
return fizzFactory.makeString() + buzzFactory.makeString()
}
}

struct IntFactory: Factory {
let intValue: Int

func makeString() -> String {
return "\(intValue)"
}
}

// This is where the magic happens
func fizzbuzz(_ range: ClosedRange = 0...100) {
for i in range {
let factory: Factory
if i.canBeFizzBuzzed() {
factory = FizzBuzzFactory()
} else if i.canBeFizzed() {
factory = FizzFactory()
} else if i.canBeBuzzed() {
factory = BuzzFactory()
} else {
factory = IntFactory(intValue: i)
}
print(factory.makeString())
}
}

Updated and cleaner version

R8 / H8 Jow Forums

equals = $(if $(filter $1,$2),$1)
slice = $(wordlist 2,$(words $1),$1)
map = $(if $(call equals,$(words $2),0),$(strip $3),$(call map,$1,$(call slice,$2),$3 $(call $1,$(firstword $2),$(words $3))))
replicate = $(if $(call equals,$(words $3),$1),$(strip $3),$(call replicate,$1,$2,$3 $2))
range = $(if $(call equals,$(words $2),$1),$(strip $2),$(call range,$1,$2 $(words $2)))

repeat = $(if $(call equals,$(words $4),$1),$3,$(call repeat,$1,$2,$(call $2,$3),$4 $1))
rotate = $(call slice,$1) $(firstword $1)

cycle = $(firstword $(call repeat,$1,rotate,$2))
select = $(if $1,$1,$2)
concat = $(if $(call equals,$1,_),,$1)$(if $(call equals,$2,_),,$2)
add = $(words $(call replicate,$1,_) $(call replicate,$2,_))
addOne = $(call add,1,$1)

numbers := $(call map,addOne,$(call range,100))
fizz := $(call replicate,2,_) fizz
buzz := $(call replicate,4,_) buzz

print = $(call add,$1,$2)
fizzbuzz = $(call select,$(call concat,$(call cycle,$2,$(fizz)),$(call cycle,$2,$(buzz))),$1)
result := $(call map,fizzbuzz,$(numbers))


.PHONY: all
all: ; $(info $(result))

>POSIX
>${0##*/}

What if number is divible by both?

If print doesn't automatically add a newline, then it would still print fizzbuzz.
Well, actually it would print fizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzzfizzfizzbuzzfizzbuzzfizzfizzbuzz, but that's a different story.