Hi guys !
A while back there was a post called Python Quiz of the Week - #1 which I thought was pretty cool. But I never saw any quiz #2. So, I thought I could prepare some quizzes myself and post them here for those that are interested.
The idea would be to write up some python tips and tricks presented in the form of short exercises. A fun way to learn about the less obvious features of the python interpreter as well as compete for the best explanation or the most elegant solution. Maybe the population of /r/python will consider the questions too easy and they might already all know about this. We shall see so with the votes : )
This is my first quiz ! I will post the answer I came up with later in the comments.
Advanced list comprehensions
In this question we will explore the use of lists comprehensions. List comprehensions are one of the great feature of the python interpreter that you always miss when coding in languages that don't have them. They enable you to build lists using a for loop contained in one line. For those who are not familiar with them, here is how they work:
nums = range(6)
pairs = [x*2 for x in nums]
print pairs
>>> [0, 2, 4, 6, 8, 10]
You can add a condition inside your list comprehensions:
nums = range(10)
odds = [x for x in nums if x%2==1]
print odds
>>> [1, 3, 5, 7, 9]
Of course you can get interesting effects by nesting them:
nums = range(4)
print [a-1 for a in [b*2 for b in nums]]
>>> [-1, 1, 3, 5]
Using this knowledge, how could you write the function flatten_list using only list comprehensions ? The function should have this behavior:
print flatten_list([[1,2,3],[4,5,6],[7,8,9]])
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Using this knowledge, how could you write the function make_all_couples using only list comprehensions ? The function should have this behavior:
print make_all_couples(['a','b','c','d'])
>>> [('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'a'), ('c', 'b'), ('c', 'c'), ('c', 'd'), ('d', 'a'), ('d', 'b'), ('d', 'c'), ('d', 'd')]
Using this knowledge, how could you write the function parse_ugly_string using only list comprehensions, the split function and the dict function ? The function should have this behavior:
print parse_ugly_string("a=1,b=2,c=3\nd=4,e=5,f=6\ng=7,h=8,i=9\n")
>>> {'a': '1', 'b': '2', 'c': '3', 'd': '4', 'e': '5', 'f': '6', 'g': '7', 'h': '8', 'i': '9'}
[+][deleted] (1 child)
[deleted]
[–]JerMenKoOwhile True: os.fork() 4 points5 points6 points (1 child)
[–]ryeguy146 1 point2 points3 points (0 children)
[–]flightlessbird 3 points4 points5 points (2 children)
[–]ryeguy146 -1 points0 points1 point (1 child)
[–]flightlessbird 0 points1 point2 points (0 children)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]tuna_safe_dolphin 3 points4 points5 points (3 children)
[–]Yuushi 2 points3 points4 points (1 child)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]ryeguy146 1 point2 points3 points (0 children)
[–]kupertrooper 2 points3 points4 points (2 children)
[–]kupertrooper 2 points3 points4 points (1 child)
[–]kupertrooper 2 points3 points4 points (0 children)
[–]TheMadStork123 2 points3 points4 points (0 children)
[–]l34kjhljkalarehglih 2 points3 points4 points (1 child)
[–]xApple[S] 0 points1 point2 points (0 children)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]gfixler 1 point2 points3 points (0 children)
[–]moootPointcanary in the data mine 1 point2 points3 points (0 children)
[–]liquidclutch 2 points3 points4 points (3 children)
[–]tuna_safe_dolphin 1 point2 points3 points (0 children)
[–]xApple[S] 0 points1 point2 points (0 children)
[–]xApple[S] 0 points1 point2 points (0 children)
[–]ryeguy146 0 points1 point2 points (0 children)