我正在尝试了解Active Patterns,所以我正在玩FizzBuzz:
let (|Fizz|_|) i = if i % 3 = 0 then Some Fizz else None
let (|Buzz|_|) i = if i % 5 = 0 then Some Buzz else None
let (|FizzBuzz|_|) i = if i % 5 = 0 && i % 3 = 0 then Some FizzBuzz else None
let findMatch = function
| Some Fizz -> "Fizz"
| Some Buzz -> "Buzz"
| Some FizzBuzz -> "FizzBuzz"
| _ -> ""
let fizzBuzz = seq …Run Code Online (Sandbox Code Playgroud) 我遇到了以下错误:
./main.go:76: invalid indirect of Fizzbuzz (type func(int) string)
Run Code Online (Sandbox Code Playgroud)
据我所知,Fizzbuzz函数不满足writeString.我的直觉告诉我,这可能是因为我应该使用Fizzbuzz的界面?有人可以给我一些指导如何执行此操作?我该怎么做才能使这个代码变得惯用?
// -------------------------------INPUT--------------------------------------
// Your program should read an input file (provided on the command line),
// which contains multiple newline separated lines.
// Each line will contain 3 numbers which are space delimited.
// The first number is first number to divide by ('A' in this example),
// the second number is the second number to divide by ('B' in this example)
// and the third number is where you …Run Code Online (Sandbox Code Playgroud) 我们今天谈到了“fizz buzz”编程测试,我想用 C++ 来实现它,但是用元编程。理想情况下,它会在编译期间生成输出。
我当前的代码使用模板,但它仍然必须执行才能产生输出。在Ideone上查看。
我仍然使用std::cout <<为了在屏幕上打印输出。在FizzBuzz<i>::value将给要么i,-1(嘶嘶声), -2(巴兹),或-3(FizzBuzz)。然后word<value>为负数定义并给出char const *:
template <int i>
struct Print {
static void print() {
Print<i - 1>::print();
auto const value = FizzBuzz<i>::value;
if (value < 0) {
std::cout << word<value>() << std::endl;
} else {
std::cout << value << std::endl;
}
}
};
Run Code Online (Sandbox Code Playgroud)
由于递归,循环消失了,有一个Print<0>可以阻止它。
是否有某种方法可以打印出word<value>()或value,哪些是在编译期间已知的?这可能不适用于每个编译器,因此我对适用于 GCC 和/或 Clang 的解决方案感到满意。
我正在尝试使用列表推导在haskell中编写'fizzbuzz'.
以下为什么不工作,应该怎么做?
[ if x `mod` 5 == 0 then "BUZZFIZZ"
if x `mod` 3 == 0 then "BUZZ"
if x `mod` 4 == 0 then "FIZZ" | x <- [1..20],
x `mod` 3 == 0,
x `mod` 4 == 0,
x `mod` 5 == 0 ]
Run Code Online (Sandbox Code Playgroud)
- 更新 -
好的,这有效:
[ if x `mod` 5 == 0 then "BUZZFIZZ"
else
if x `mod` 3 == 0 then "BUZZ"
else
if x `mod` 4 == 0 then "FIZZ" else show …Run Code Online (Sandbox Code Playgroud) 在clojure中FizzBuzz的两个解决方案中,为什么第一个更快?
; #1
(defn fizzbuzzer [z]
"fizzbuzz checker."
(let [fizz? (zero? (rem z 3))
buzz? (zero? (rem z 5))
fizzbuzz? (and fizz? buzz?)]
(cond fizzbuzz? "FizzBuzz"
buzz? "Buzz"
fizz? "Fizz"
:else z)))
; #2
(defn fizzbuzzer2 [z]
"fizzbuzz checker."
(let [fb (str
(if (zero? (rem z 3)) "Fizz" "")
(if (zero? (rem z 5)) "Buzz" ""))]
(if (seq fb) fb z)))
; profiling ;
; (time (fizzbuzzer 1500))
; => "Elapsed time: 0.098058 msecs"
; (time (fizzbuzzer2 1500))
; => …Run Code Online (Sandbox Code Playgroud) 运行方法fizzbuzz1产生一个100个成员的数字1到100的列表,其中3的每个倍数被"fizz"替换,5个被"buzz"替换,3个和5的每个倍数被"fizzbuzz"替换:
def fizzbuzz1()
result = Array.new(100, "")
result.each_index do |index|
value = index + 1
result[index] += "fizz" if value % 3 == 0
result[index] += "buzz" if value % 5 == 0
result[index] = "#{value}" if result[index].size == 0
end
end
2.0.0-p195 :055 > fizzbuzz1
=> ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", ...and so on.
Run Code Online (Sandbox Code Playgroud)
但每个开关+=的<<产量意想不到的事情:
def fizzbuzz2()
result = Array.new(100, "")
result.each_index do |index|
value = index + 1
result[index] << …Run Code Online (Sandbox Code Playgroud) 我很难弄清楚下面的代码是如何工作的:
import random
for i in range(0, 100):
if not i % 15:
random.seed(1178741599)
print [i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)]
Run Code Online (Sandbox Code Playgroud)
我明白,当i被整除时15,random将使用相同的seed,因此randint(0, 3)将始终返回相同的索引,但当我尝试
for i in range(100):
random.seed(1178741599)
print random.randint(0,3) #always print 0
Run Code Online (Sandbox Code Playgroud)
上面的代码总是打印0,不应该是(这是正确的行为,并3(作为"FizzBuzz"列表中的索引[i+1, "Fizz", "Buzz", "FizzBuzz"]?i+1在列表中对应).
又是怎么回事的时候i是整除3或5?
种子的魔力是1178741599什么?
编辑:正如jonrsharpe所指出的,fizzbuzz代码仅适用于python 2.x.
哪个fizzbuzz实现效率更高?
public static void fizzBuzz1(int n)
{
boolean fizzed, buzzed;
for(int i = 1; i <= n; i++)
{
fizzed = buzzed = false;
if(i % 3 == 0)
fizzed = true;
if(i % 5 == 0)
buzzed = true;
if(fizzed && buzzed)
System.out.println("FizzBuzz");
else if(fizzed)
System.out.println("Fizz");
else if(buzzed)
System.out.println("Buzz");
else
System.out.println(i);
}
}
public static void fizzBuzz2(int n)
{
for(int i = 1; i <= n; i++)
{
if(i % 3 == 0 && i % 5 …Run Code Online (Sandbox Code Playgroud) 我相信对于大多数人来说,您应该熟悉FizzBuzz是什么。
对于那些不知道我在这里说什么的人。这是FizzBuzz:
编写一个程序,打印从1到100的数字。但是,对于三个数字的倍数,请打印“ Fizz”(而不是数字),对于五个数字的倍数,请打印“ Buzz”。对于三和五的倍数的数字,请打印“ FizzBuzz”。
对于大多数人来说,这可能很容易创建。
尽管在网上浏览后,我发现有些帖子在不使用模运算符的情况下受到挑战。
我发现此python代码有趣:
m = [None, "Fizz", "Buzz", "FizzBuzz"]
v = 0x30490610
for i in range(1, 101):
j = v & 3
print(m[j] if j else i)
v = v >> 2 | j << 28
Run Code Online (Sandbox Code Playgroud)
结果:
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 …Run Code Online (Sandbox Code Playgroud) 我正在尝试用 C++ 编写一个缩小的 FizzBuzz 程序,因为我现在正在学习它。我想知道是否有一种速记方式可以说“如果此字符串存在,则返回此字符串,否则,返回下一部分”
在 JavaScript 中,使用||运算符的工作方式与我想的一样:
function fizzbuzz() {
const maxNum = 20; // Simplified down to 20 just for example
for (let i = 1; i <= maxNum; i++) {
let output = "";
if (i % 3 == 0) output += "Fizz";
if (i % 5 == 0) output += "Buzz";
console.log(output || i); // If the output has something, print it, otherwise print the number
}
}
fizzbuzz();Run Code Online (Sandbox Code Playgroud)
当我在 C++ 中尝试这个时,
#include <iostream> …Run Code Online (Sandbox Code Playgroud)