So, this time you are only allowed to use 2 if blocks with optional else. Every variable you declare should be final / immutable. The only exception is the outer loop.
If you wish extra difficulty: no arrays allowed.
Just a reminder: Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Syntactic sugar to avoid the restrictions is NOT allowed. Examples of what is not allowed: import fizzbuzz test ? if_true : if_false del varname
Tyler Watson
Easy. Print ("1") Print ("2") Print ("Fizz") Etc
Elijah Jenkins
Wonder if that would take more or less CPU cycles than an algorithm that goes through a loop with nested if statements.
Anthony Hughes
why would you ever need more than 2 ifs ?
Wyatt Watson
less obviously, are you retarded ?
Oliver Jones
Nah it will not. Unless you execute it on an ancient CPU with an extremely limited cache.
Robert Taylor
Yes. Explain pls.
Nathan Butler
Final variables only. You are not allowed to change the value of the existing variable. It is getting challenging with only 2 ifs.
Robert Sullivan
how could it possibly take more cpu cycles ? it's just print, no conditionals, no incrementation of counter variables and most likely (depends on if the compiler/interpreter unrolls the loops) no loop (cmp/test + jmp)
Christopher Campbell
[source] #include
int main(int argc, char **argv) { for (int i = 1; i
Carson Turner
So if you wanted to write the most efficient fizzbuzz on an interview you should just do ?
Lincoln Rogers
so i just use a pointer (whichs value doesn't change) and change the memory it points to ? but i guess thats not what you meant so by final variables you mean only constants + no arrays ? i assume strings are considered char arrays ?
Charles Cook
Talking to and waiting for the slow RAM on your [pseudo-] von neumann CPU.
Ryder Perez
I'm new around here.
#include
int main(int argc, char **argv) { for (int i = 1; i
Hudson Campbell
>Three if statements in a challenge that limits you to two if statements Really?
Gavin Bell
cpu efficient? yes. but the binary would be much bigger e.g. on an interview? nope. but fizzbuzz won't be used for interviews anyway.
Consider it to be a pointer on a variable that changes. Forgot to mention it for the c/cpp world, was too long ago... Though strings technically are, you still need to output something. Don't get too captious, I bet you understood the idea of the task, and cheating is no go ;) Did not want to write poems in the first post, readability and shit.
Too many IF-s. Busted.
Logan Powell
import time __ = time.time() while True: time.sleep(1) _ = int(time.time() - __) print('Fizz'[0:int(_%3==0)*4]+'Buzz'[0:int(_%5==0)*4] or _) if _ >= 100: break
Noah Jenkins
if you can't change any value or do any pointer arithmetic and arrays are forbidden, the only option would be but i'm probably missing something.
Jace Mitchell
Dude you exploded my brain.
Now we have a winner in the easy difficulty using arrays (lists). Nice use of if btw. And go make import this
Alexander Butler
You are missing something.
Still waiting for a solution without arrays/lists and other crazy shit stuff. It exists.
Aiden Young
now i'm really curious how it's possible with immutable variables. i mean it's possible without using any variables at all just writing/reading from the stack directly wie bp/sp but thats kinda language specific so i'd say it counts as "cheating"
Isaac Hill
Welcome. And, no, my bio-grep-eye shows you have used 3 IFs.
I will post the solution if I don't fall asleep earlier. Otherwise make a disposable email box and I send it.
import time __ = time.time() while (time.time() - __
Julian Ward
What the fuck is that image?
Lucas Reed
I figure if you're gonna go janky, go for broke:
function f0(x) { document.write("FizzBuzz") } function f1(x) { document.write(x) } function f2(x) { document.write(x) } function f3(x) { document.write("Fizz") } function f4(x) { document.write(x) } function f5(x) { document.write("Buzz") } function f6(x) { document.write("Fizz") } function f7(x) { document.write(x) } function f8(x) { document.write(x) } function f9(x) { document.write("Fizz") } function f10(x) { document.write("Buzz") } function f11(x) { document.write(x) } function f12(x) { document.write("Fizz") } function f13(x) { document.write(x) } function f14(x) { document.write(x) } for(var i = 1; i
Gavin Sanders
import time
sleep(3153600000)
print('fizzbuzz :^)')
sleep(1)
Job offer when
Chase Lewis
FizzBuzz, what the fuck else would it be?
Aaron Johnson
What is up with that padding? That a faggot thing?
Michael Perry
I like this for thinking outside the box.
John Carter
default html padding is 8px
Jayden Johnson
Shit son, what's got your panties all tied up in a bunch?
Kayden Lewis
The image.
Caleb Bailey
I was really overthinking it
def fizzbuzz(start, end): print(((start%3==0 and 'Fizz') or '')+((start%5==0 and 'Buzz') or '') or start) if start < end: fizzbuzz(start + 1, end)
fizzbuzz(1, 100)
Jose Ward
oh, that's a shadow default osx's window screenshot tool makes
can't be bothered to disable it
Asher Ramirez
Mostly reading retarded questions.
Grayson Parker
Sorry, not at all. Cheating. Here you are using extra conditions hidden behind logical functions. x % 3 == 0 and 'fizz' or 'buzz' if (x % 3 == 0) fizz; else buzz;
But I'm glad you find it interesting to explore new features. Check checkio.org
I am not sure whether I hate you or fascinated of the enterprise creativity.
Ayden Hernandez
Look ma, no if blocks! ;Boolean short-circuiting (for ([n (in-range 100)]) (or (and (remainder n 3) (~a "Fizz")) (and (remainder n 5) (~a "Buzz")) (void n))
Asher Martin
Hidden conditions, same as Also fizzbuzz(1, 9000) just realized it's recursive, maniac.
Come one, I want to see here a nice algorithm driven solution finally!
Ryan Wright
>or >and >and
That's one if-statement too many.
>inb4 you don't know that 'and' and 'or' are semantic sugar
Chase Barnes
Sure is pleb in here.
print("\n".join(("Fizz"*(i%3==0)+"Buzz"*(i%5==0)) or str(i) for i in range(1,101)))
Justin Martin
>I am not sure whether I hate you or fascinated of the enterprise creativity. Strictly speaking, my solution is dictionary-based, but this gets into why it's so tricky to impose restrictions like "only two 'if' blocks" (what about languages that use pattern-matching?) and "no arrays" (what about compiler/interpreter internal arrays?). Because if you try restricting symbol table usage, then these modern interpreted languages are straight out, and we'd be writing x86 assembly with restrictions on which/how many instructions we can use.
Josiah White
Do conditions in for loops count as ifs?
Andrew Lewis
#include
int main(int argc, char *argv[]) { for (int i = 1; i
Michael Scott
What do you believe this prints at numbers 3, 5 and 15
Zachary Jenkins
No, no, I seen solutions with the same idea before. But not with functions and eval(). It's actually enough to form a 15-element iterable of any type. That's why I put the "no arrays for extra difficulty" part. Since it is not possible to explain or even find each detail for each possible implementation in each existing programming language, I kept the conditions minimalisting hoping everyone will understand the idea of not using iterables solving the task itself. Otherwise anyone would be disqualified by using array of characters to store the source code.
Levi Powell
In a way, you do. This way to solve it I never seen before. And you are surprisingly close to a clean one, without manipulating the actual output or other language/compiler/environment specific stuff.
Cooper Richardson
look ma, no macros! [CODE] (doseq [t (range 1 101)] (println (match [(mod t 3) (mod t 5)] [0 0] "FizzBuzz" [0 _] "Fizz" [_ 0] "Buzz" :else t))) [/CODE] using clojure with core.match here
Matthew Harris
This solution violates all the restrictions, but it hoists them to the terminal implementation. The '\b' character relies on having a mutable character array (called the terminal buffer), and the terminal's implementation of '\b' requires a condition (to make sure it doesn't backspace past the beginning of the line). It's clever and funny, but it's a more exotic version of "import fizzbuzz."
Noah Barnes
As for me, it would fall under >Syntactic sugar and other cheating solutions.
Reminder: there actually IS a clean solution, implementable in (nearly?) any popular programming language.
Sorry for multiposting.
Michael Reyes
Unfortunately I don't speak Clojure, care to explain the code? Also, you seem to use arrays. Easy difficulty level solved for you.
Bentley Myers
Then would be considered wrong, right?
Evan Mitchell
Fuck wikipedia links, if you wanted that you'd google. Pattern matching is a technique used in functional programming to trick a function into doing a different thing based on the parameters it receives, like in a "switch" instruction, but not really.
Not using arrays here - (doseq [t (range 1 101)] ... ) means do something for every t in a sequence obtained by taking a range 1...100. This data structure is immutable, so if it's not returned it'll be garbage collected. If you are willing to consider it like "using an array or a list" anyways, then this instruction may be substituted by (dotimes [t 101] ...) - yields the same result without creating any data structure.
Josiah Cooper
Right, wrong. But a very beret guy way of thinking I should admit.
Cameron Rivera
The "match" is where all the conditions are buried. It's the standard solution, but with a four-clause match instead of if/else if/else if/else. If you speak Haskell, it's like,
data Result = StrRes String | IntRes Int deriving Show
solve :: Int -> Int -> Int -> Result solve 0 0 _ = StrRes "FizzBuzz" solve 0 _ _ = StrRes "Fizz" solve _ 0 _ = StrRes "Buzz" solve _ _ x = IntRes x
massage :: Int -> Result massage x = solve (mod x 3) (mod x 5) x
main :: IO () main = print (map massage [1..100])
Jose Bennett
Okay, thanks for explaining, stay calm. Maybe today I find a nice reason to finally try out some functional languages.
Henry Robinson
Oh well, this is a kind of problem that is not solvable without conditional constructs. Strictly speaking, (match ...) in clojure is not syntactic sugar for IF, and neither is the macro of the lisp guy before me P.S. for pythonfags, here's your python of the christ: for i in range(1,101): print("FizzBuzz"[i*i%3*4:8--i**4%5] or i) sorry if i seemed angry, I was not
Blake Flores
So effectively there are 4 mutually exclusive branches, which is impossible using 2 ifs (you always get 3 branches).
Colton Parker
>Strictly speaking, (match ...) in clojure is not syntactic sugar for IF Sort of, but I think what OP was going for is that "if" really means "conditional branch." In general for functional languages, match is a very heavyweight conditional branch (heavyweight because they often include safe downcasting). Certainly building this as a pattern-match problem with more than three clauses violates the spirit of OP's restrictions. Indeed, in a nonstrict functional language like Haskell, you can do away with if entirely:
if' :: Bool -> a -> a -> a if' True r _ = r if' _ _ r = r
But note that does rely on nonstrict evaluation.
Juan Taylor
My favorite solution in Python (SPOILERS if you still want to try yourself) pastebin.com/hFfSNa7a
I want to apologize if it became "guess my code" kind of party. Hope still had some fun & g'nite.
Carson Sanders
That's wrong. If your code is short, the pc is maybe able to work almost only on the cache without using the ram that much. Which is a lot faster.
Carter Hill
yeah, worded it wrong, the original question was about cpu cycles, not actual efficiency
John Perez
>implementable in (nearly?) any popular programming language. then what you did contains a for (which uses an if to check the end of the range) and two other ifs, making 3.
for (var i = 1; i < 101; i++) print((["Fizz", "" ,""][(i % 3)] + ["Buzz", "", "", "", ""][(i % 5)]) || i);
Hunter Martin
I'm pretty sure map has two if statements in it
Brody Hughes
Without a loop it would not make any sense therefore >The only exception is the outer loop. not to call the goddamn function a hundred of times.
David Long
If you're already using arrays, why not to implement a single one ["FizzBuzz", 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14] and reference it by array[i % 15] replacing integers with i? Nah, nevermimd, just asking.
> India Trolling? P.S. check the output, compare with FizzBuzz task definition.
Jayden Parker
I think we have a real wiener here.
Asher Hernandez
Let me know if any of you guys have a better solution.
private static void gFizzbuzz() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i
Christian Hill
Abusing the output again. \r overwriting the output plus conditionals hidden using operators. Soz.
Joseph Miller
A better solution would be a solution with the correct output
Grayson Gomez
>stringBuilder explicitly violates > Every variable you declare should be final / immutable. Under the hood Sting operations return new object every time effectively changing the reference variable is. StringBuilder is just a nice wrapper around it with some optimizations. >Let me know Why not this? Cannot see anything wrong with it.
Jordan Bell
I know a solution! It's called brain and hand. It can be implementable in almost any language as an AI, as it is possible to simulate a miniscule portion of a brain at mathematical terms, and the brain does not do conditions! Just pure simple conversions however you imagine it. The hand does most of the outer work for you.
I feel sort of obligated to point out that any solution dependent on nonliteral strings is array-based. The requirement of immutability is a false constraint, because of course any mutation can be replaced by a looping copy. And since you're doing string concatenation as a supposedly primitive operation, you're implicitly allowing looping copies.
Given that, the \b and \r solutions could be reimplemented in ways that follow the rules as much as your version does. I wrote this version in Haskell just to be clear that these are all immutable operations:
solve :: Int -> String solve i = let res = show i in let res2 = if mod i 3 == 0 then "Fizz " else res in let res3 = if mod i 5 == 0 then take (length res2 - 3) res2 ++ "Buzz" else res2 in res3
main :: IO () main = mapM_ putStrLn (map solve [1..100])
The "problem" with this is that the take function contains a condition:
take :: Int -> [a] -> [a] take 0 xs = [] take n (x:xs) = x : take (n - 1) xs
But at the same time, list concatenation contains nearly the same condition:
This is all to say, you violated your own rules, but the ways in which you did so are quite obscure.
Julian Gonzalez
Woops, didn't read properly last time. This one has proper output.
private static void gFizzbuzz() { StringBuilder stringBuilder = new StringBuilder(); for (int i = 1; i
Jayden James
Just agree there could stand 3 print operators instead: to print var1, to print var3 and to print \n and no string operations would be involved.
Samuel Perry
Okay, but the implementation of "print" will do the same thing. I was comparing this to \b and \r solutions, which only have the problem that they implicitly embed loops in the way that terminals are implemented. I brought this up because of the use of nonliteral strings, which is what makes them feel more list-like.
I mean it's all up to interpretation, obviously, but if you think about what's going on: a good compiler could print a string literal without runtime branching by unrolling the loop, while any operation on a nonliteral string has to be a runtime operation, so it must always branch. I think this is why anything that's based on any kind of string manipulation, including printing from pointers, feels like it goes against the spirit of the question.
I'm unemployed, actually. I left LANL after my postdoc ended two-ish years ago to try and figure out what to do with the rest of my life. But if you want to talk about that more, you can use this temporary email address: [email protected]
Ian Taylor
for i in range(1, 101): start = 0 + (i % 3 != 0) * 4 end = 8 - (i % 5 != 0) * 4 if (i % 3 == 0) or (i % 5 == 0): print('FizzBuzz'[start:end]) else: print(i)
Gavin Taylor
>2016 >using if or conditionals on a fizzbuzz ever how is unemployment gıing for you?