python interview quuestions

Peter Otten __peter__ at web.de
Sat Aug 7 13:53:58 CEST 2010


Tim Chase wrote:

> On 08/06/10 15:37, James Mills wrote:
>> On Sat, Aug 7, 2010 at 6:28 AM, geremy condra<debatem1 at gmail.com>  wrote:
>>> If I had to wait 5 minutes while a candidate tried to solve this
>>> problem I would not hire them.
>>
>> Yes you do raise a valid point. It should really only take
>> you a mere few seconds or so to write a solution to this.
>>
>> More over, it can be done in just a single line of Python.
>>
>> 7 if you're not very familiar with Python.
> 
> While it *can* be done in one line, I'm not sure it's the most
> legible solution.  Though I must say I like this one-line python
> version:
> 
> for i in range(1, 101): print ((i%3==0 and 'fizz' or '') +
> (i%5==0 and 'buzz' or '')) or i
> 
> (adjust "3" and "5" for your local flavor of fizzbuzz)
> 
> I'm not sure I'd hire a candidate that proposed this as a
> solution in earnest, but I'd have fun chatting with them :)

You mean you'd go for the candidate who took the conservative approach and 
got it right:

print 1
print 2
print 'Fizz'
print 4
print 'Buzz'
print 'Fizz'
print 7
print 8
print 'Fizz'
print 'Buzz'
print 11
print 'Fizz'
print 13
print 14
print 'FizzBuzz'
print 16
print 17
print 'Fizz'
print 19
print 'Buzz'
print 'Fizz'
print 22
print 23
print 'Fizz'
print 'Buzz'
print 26
print 'Fizz'
print 28
print 29
print 'FizzBuzz'
print 31
print 32
print 'Fizz'
print 34
print 'Buzz'
print 'Fizz'
print 37
print 38
print 'Fizz'
print 'Buzz'
print 41
print 42
print 43
print 44
print 'FizzBuzz'
print 46
print 47
print 'Fizz'
print 49
print 'Buzz'
print 'Fizz'
print 52
print 53
print 'Fizz'
print 'Buzz'
print 56
print 'Fizz'
print 58
print 59
print 'FizzBuzz'
print 61
print 62
print 'Fizz'
print 64
print 'Buzz'
print 'Fizz'
print 67
print 68
print 'Fizz'
print 'Buzz'
print 71
print 'Fizz'
print 73
print 74
print 'FizzBuzz'
print 76
print 77
print 'Fizz'
print 79
print 'Buzz'
print 'Fizz'
print 82
print 83
print 'Fizz'
print 'Buzz'
print 86
print 'Fizz'
print 88
print 89
print 'FizzBuzz'
print 91
print 92
print 'Fizz'
print 94
print 'Buzz'
print 'Fizz'
print 97
print 98
print 'Fizz'
print 'Buzz'

Well, almost right, but one error per 100 lines at the first try is near-
genius anyway...

Peter




More information about the Python-list mailing list