Written by Mitch in Misc on Wed 24 June 2015. Tags: python, basic math,
Python Basic Math
I had been interviewing lately... (its more official now which is why I'm writting about it) I did a bunch of interviews, some wanting to test my python prowess, and well they stumped me. On basic math with python... yeah I was extremely frustred by the experince as I've been doing python for years... never had many on the spot, do some basic math now kind of moments. None the less, it irritated me. So, I went home and revisited the question. Not 5 minutes later I had the answer, but it bugged me the whole drive home. Which is something I do alot, something bugs me reguardless of how small and meaningless (like needing to do basic math in python), I'm posting here for posterity. Question is not word for word what I got, because I didn't copy it, but heres the basics of it:
Write a script that will check numbers 1 through 100, to see if they are divisible by 4, if they are print "word". If they are divisible by 6, print "anotherword", if they are divisible by both, print "both". Otherwise print the number.
for x in range(1,101): is4 = False is6 = False if float(x/4.0).is_integer(): print str(x) + "/4 = " + str(float(x/4.0)) is4 = True if float(x/6.0).is_integer(): print str(x) + "/6 = " + str(float(x/6.0)) is6 = True if is6 is True and is4 is True: print str ...