jmh530 WAY too many lines
1recho I don’t know Python but if that runs you pass
bchad rarther than rambling post your final result but you pass
jmh530 WAY too many lines
1recho I don’t know Python but if that runs you pass
bchad rarther than rambling post your final result but you pass
for (i in 1:100) { ifelse( !(((i %% 5)==0) | ((i %% 3)==0)), print(i), print( ifelse( (i %% 3)==0 , ifelse( (i %% 5)==0, “FizzBuzz”, “Fizz”), “Buzz”) ) ) }
I don’t know how to do it in less than one line of code, sorry.
What you are missing is that the ternary operator assign x to ‘’ (that is two single quotes i.e. a blank string) in the first statement x = ‘Fizz’ if i % 3 == 0 else ”
In other words, x gets set to ‘Fizz’ if i is divisible by 3, but to a blank string otherwise.
Similarly, ‘Buzz’ gets added to x in the next statement if x is divisible by 5, else a blank string ‘’ (two single quotes) gets added.
And then we print i and x.
^ Hey, what happened to bchad’s post asking about how the Python program works?
I have run the Python script and it works fine - beware that (as chad said earlier) AF converts two consecutive single quotes to ‘’ and substitutes intelligent quotes “like this” for plain double quotes, in case you want to copy-paste.
I think you need to assign x to be i if i is not divisible by 3 or 5:
===
for i in xrange(1,101):
x = ‘Fizz’ if i % 3 == 0 else ‘’
x += ‘Buzz’ if i % 5 == 0 else ‘’
output = i if x== ‘’ else x
print( output )
====
maybe there’s a way to get the decision I added directly into the print statement. The inline if was new to me (though I’m kinda new to Python).
I figured it out… the inline if was new to me, and I didn’t notice that you had two single quotes together (it was almost invisble on my screen), so I took my statement out because I figured out what you did.
But don’t worry, I substituted another. I think you have to have an assignment statement in there for when i is not divisible by either 3 or 5, otherwise you’ll get a blank. It’s a nit picky point, but the test said we were supposed to print the number if it wasn’t divisible by 3 or 5 and the words if it was.
^ Good catch, I fail
20 years of programming experience down the drain. Well, at least I can break into finance using my CFA-as-the-noun…
Here is the right answer, too late for me of course…
for i in xrange(1,101): x = ‘Fizz’ if i % 3 == 0 else ‘’ x += ‘Buzz’ if i % 5 == 0 else ‘’ print i if x == ‘’ else x
Ha ha ha.
But you were close. I think the test is relevant because a substantial number of people can’t do it at all.
Of course, it helps to know about the modulo operation/function, though one could do a test on whether (x/n)==int(x/n) or something like that to substitute.
I was wondering if that was a valid way to do an inline if, or if you could only use inline ifs for assignment operations.
Python is like C in that respect - assignments produce a value and it equals the value of the result. That is why statements like this work:
x = y = 10
The interpreter first evaluates ‘y = 10’, assigns 10 to y, then evaluates x = (y = 10) where (y = 10) has a value of 10, so it assigns 10 to x as well.
@Blake, I understand if the goal is to have the fewest lines of code for an interview exercise, but beyond that it is more of a trade-off. Given a bunch of code that accomplishes the same task, I care about speed, generality, readability, and then maybe economizing on code.
Well, they wrote me and invited me for more interviews next week. What? Here we go again…
Well, now you know to ask your potential boss if they can write FizzBuzz.
Good Luck!
So last time was brain teasers? They way these guys go about, this one will probably be all “fit”. You’ll have 20 different people ask you what you like to do in your spare time.
Thanks for the good luck wishes. They actually told me who I will meet. Fortunately, it’s not 20 people… Also, yes, I think it will be mostly a fit and sales meeting. Unfortunately, I have no interesting hobbies such as mountain climbing, salsa dancing, or horse riding. (I have cats, though)
So, your hobby is lion taming. That’s interesting, and not really that much of stretch…