为什么我的"Fizz Buzz"没有显示测试在R工作? [英] Why isn't my "Fizz Buzz" test in R working?
本文介绍了为什么我的"Fizz Buzz"没有显示测试在R工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我听说这是一个常见的面试问题,有什么想法吗,谢谢.
I heard this was a common interview question, any ideas what is off here, thank you.
for(i in 1:100){
if(i%15==0){
print('fizzbuzz')
} else
if (i%3==0){
print("fizz")
} else
if (i%5==0) {
print("buzz")
} else
(print(i))
}
}
推荐答案
我将花括号放在不同的位置,您需要更正运算符-%%
而不是%
.
I'd place the curly braces in different spots, and you need to correct the operator -- %%
instead of %
.
for(i in 1:100) {
if(i%%15==0){
print('fizzbuzz')
} else if (i%%3==0){
print("fizz")
} else if (i%%5==0) {
print("buzz")
} else {
print(i)
}
}
但是基本思想是合理的:首先清除特殊的"fizzbuzz"案例,然后再处理其余(专有)案例.
But the basic idea is sound: get the special 'fizzbuzz' case out the way first, then deal with remaining (exclusive) cases.
以下是前16个结果:
edd@max:~$ r /tmp/fizzbuzz.R | head -16
[1] 1
[1] 2
[1] "fizz"
[1] 4
[1] "buzz"
[1] "fizz"
[1] 7
[1] 8
[1] "fizz"
[1] "buzz"
[1] 11
[1] "fizz"
[1] 13
[1] 14
[1] "fizzbuzz"
[1] 16
edd@max:~$
这篇关于为什么我的"Fizz Buzz"没有显示测试在R工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文