# started 11:44 am # rev1: 11:55 am # refactored: 12:08 pm import random # ------------------------------------------- # Doubling challenge spec # ------------------------------------------- # Doubling # - Create a list of numbers, save it to a variable # - Pass this list to a function called double() # - Use a for loop to iterate through each element of the list # - Write a separate (helper) function, doubling(), that returns any number it is given multiplied by two # - Pass each element from the for loop to this helper function # - Save the original numbers and the doubled results as key-value pairs in a dictionary # def doubling(n): '''This makes more sense if named double(n)''' return n * 2 def double(numbers): ''' Doubles each n, returns them in a dict. Spec is ambiguous as to whether they wanted this in one or a composition of functions ;) :param numbers: a list of numbers to double :return A dict of n -> (n*2) for each n in numbers (This would be more idiomatic as a list comprehension) Implementation per challenge spec - Use a for loop to iterate through each element of the list - Write a separate (helper) function, doubling(), that returns any number it is given multiplied by two - Pass each element from the for loop to this helper function - Save the original numbers and the doubled results as key-value pairs in a dictionary ''' doubled = [] for n in numbers: doubled.append(doubling(n)) return dict([ (n, d) for n, d in zip(numbers, doubled) ]) # ------------------------------------------- # Refactored and rewritten to be more idiomatic. # ------------------------------------------- # Using _foo prefixing to avoid conflicting with the challenge spec def _double(n): '''Returns two times a number''' return n*2 def double_list(numbers): ''' Doubles each n, returns them in a list. :param numbers: a list of numbers to double :return A list of 2n ''' return [ _double(n) for n in numbers ] def double_list_into_dict(numbers): ''' Doubles each n, returns them in a dict. :param numbers: a list of numbers to double :return A dict of n -> (n*2) for each n in numbers ''' # We could reduce numbers into set(numbers) so that we avoid processing the same thing more than once: # e.g.: return dict([ (n, doubling(n)) for n in set(numbers) ]) return dict([ (n, d) for n, d in zip(numbers, double_list(numbers)) ]) # ------------------------------------------- # Driver # ------------------------------------------- def main(): numbers = [ random.randrange(1,25) for i in range(1,12) ] # numbers = [ 0, 1, 2, 3, 4, 5 ] doubled = double(numbers) print "Doubling per Challenge spec:" print(doubled) print "Doubling refactored:" print(double_list_into_dict(numbers)) if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question