Created
May 5, 2014 03:44
-
-
Save fundon/44a804f0a4a019792cf4 to your computer and use it in GitHub Desktop.
fizzbuzzwhizz http://cnodejs.org/topic/535f19f64eac85ae11011d50
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"time" | |
) | |
import "math/rand" | |
const ( | |
F = 1 << iota | |
B | |
W | |
FIZZ = "Fizz" | |
BUZZ = "Buzz" | |
WHIZZ = "WHIZZ" | |
) | |
func random(r *rand.Rand) []int { | |
return r.Perm(9) | |
} | |
func permission(p, n int) { | |
switch p { | |
case F: | |
fmt.Println(FIZZ) | |
case B: | |
fmt.Println(BUZZ) | |
case W: | |
fmt.Println(WHIZZ) | |
case F | B: | |
fmt.Println(FIZZ + BUZZ) | |
case F | W: | |
fmt.Println(FIZZ + WHIZZ) | |
case B | W: | |
fmt.Println(BUZZ + WHIZZ) | |
case F | B | W: | |
fmt.Println(FIZZ + BUZZ + WHIZZ) | |
default: | |
fmt.Println(n) | |
} | |
} | |
func main() { | |
var ( | |
now = time.Now() | |
r = rand.New(rand.NewSource(now.Unix())) | |
randNums = random(r) | |
input = []int{randNums[0] + 1, randNums[1] + 1, randNums[2] + 1} | |
n = 1 | |
count = 100 | |
) | |
fmt.Println("Input: ", input) | |
for n < count { | |
n++ | |
var res int | |
if strings.Index(string(n), string(input[0])) == -1 { | |
if n%input[0] == 0 { | |
res |= F | |
} | |
if n%input[1] == 0 { | |
res |= B | |
} | |
if n%input[2] == 0 { | |
res |= W | |
} | |
} else { | |
res |= F | |
} | |
permission(res, n) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash node --harmony | |
const FIZZ = 1 | |
, BUZZ = 2 | |
, WHIZZ = 4 | |
function *random(nums) { | |
while (nums.length) { | |
var i = Math.floor(Math.random() * nums.length) | |
yield nums[i] | |
nums.splice(i, 1) | |
} | |
} | |
function generate(n) { | |
var res = [] | |
while (n > 0) { | |
n-- | |
res.push(rgen.next().value) | |
} | |
return res | |
} | |
function permission(p, n) { | |
switch (p) { | |
case FIZZ: | |
console.log('Fizz') | |
break | |
case BUZZ: | |
console.log('Buzz') | |
break | |
case WHIZZ: | |
console.log('Whizz') | |
break | |
case FIZZ|BUZZ: | |
console.log('FizzBuzz') | |
break | |
case FIZZ|WHIZZ: | |
console.log('FizzWhizz') | |
break | |
case BUZZ|WHIZZ: | |
console.log('BuzzWhizz') | |
break | |
case FIZZ|BUZZ|WHIZZ: | |
console.log('FizzBuzzWhizz') | |
break | |
default: | |
console.log(n) | |
} | |
} | |
var nums = '1 2 3 4 5 6 7 8 9'.split(' ') | |
var rgen = random(nums) | |
// generates three numbers | |
var input = generate(3) | |
var n = 0 | |
var count = 100 | |
console.log('input: ', input) | |
while (n < count) { | |
n++ | |
var res = 0 | |
if (n.toString().search(input[0]) === -1) { | |
if (n % input[0] === 0) | |
res |= FIZZ | |
if (n % input[1] === 0) | |
res |= BUZZ | |
if (n % input[2] === 0) | |
res |= WHIZZ | |
} else { | |
res |= FIZZ | |
} | |
permission(res, n) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function (p, m, i, c, l) { | |
while (i++ < c) { | |
l(((i + '').indexOf(p[0]) + 1) ? m[0] : (p.map(function (n, j) { | |
return i % n ? '' : m[j] | |
}).join('') || i)) | |
} | |
}([ 3, 5, 7 ], [ 'Fizz', 'Buzz', 'Whizz' ], 0, 100, console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment