Created
June 13, 2013 23:33
-
-
Save bennuttall/5778328 to your computer and use it in GitHub Desktop.
Answers to some short fizzbuzz-style interview questions - found via @jackweirdy on Stack Overflow: http://stackoverflow.com/questions/117812/alternate-fizzbuzz-questions/117891#117891
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#1 reverse a string | |
assert 'Hello'[::-1] == 'olleH' | |
#2 reverse a sentence | |
assert ' '.join('bob likes dogs'.split(' ')[::-1]) == 'dogs likes bob' | |
#3 find the minimum in a list | |
assert min([10, 8, 6, 7, 3, 4]) == 3 | |
#4 find the maximum in a list | |
assert max([10, 8, 6, 7, 3, 4]) == 10 | |
#5 calculate a remainder | |
assert 9 % 7 == 2 | |
#6 return distinct values from a list including duplicates | |
values = [1, 3, 5, 3, 7, 3, 1, 1, 5] | |
assert list(set(values)) == [1, 3, 5, 7] | |
#7 return distinct values and their counts | |
assert {value: values.count(value) for value in set(values)} == {1: 3, 3: 3, 5: 2, 7: 1} | |
#8 given a string of expressions, and a set of variable/value pairs, return the result of the expression | |
assert eval("a + b+c -d".replace('a', '1').replace('b', '7').replace('c', '3').replace('d', '14')) == -3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment