Skip to content

Instantly share code, notes, and snippets.

@mjhea0
Last active December 18, 2015 17:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mjhea0/847f12dd8adf6cf679f2 to your computer and use it in GitHub Desktop.
Save mjhea0/847f12dd8adf6cf679f2 to your computer and use it in GitHub Desktop.

Doubling

  1. Create a function called doubling. It should take a number as input, and return the number multipled by two.
  2. Create a function called double. It should take a list of numbers as input. Iterate through each element of the list, passing each to the doubling function. Return a dictionary with the elements of the list as keys and the associated doubled resuls as values - e.g., {2:4, 5:10}
@dwaq
Copy link

dwaq commented Jun 20, 2013

That was a fun way to test my knowledge of python. I'm only about halfway through the "Real Python" book and it took me about a half hour to get it right. I'm looking forward to more code challenges!

@skilbjo
Copy link

skilbjo commented Jun 20, 2013

Great challenge!

@swarajd
Copy link

swarajd commented Jun 20, 2013

Simple, but still good.

@technillogue
Copy link

a = range(10); doubling = lambda x:2*x; double = lambda a: {x:doubling(x) for x in a}; print(double(a))

Am I doing thing right?

@mjhea0
Copy link
Author

mjhea0 commented Jun 21, 2013

please don't post any answers/questions. email me at michael@realpython.com - thanks!

@graingert
Copy link

open("a variable", "w").write("[1,]")
double("this list")
for i in "the list":
    pass


def doubling(number):
    return number * 2


for element in "the for loop":
     doubling(element)

open("a dictionary", "rw").write({"the original numbers": "the doubled results"})

@graingert
Copy link

@mjhea0 your specification is somewhat confusing:

  1. using terms such as "save" for storing variables
  2. mentioning things a for loop should do (pt 5) long after originally specified (pt 3)
  3. requesting calls to functions that were never defined, (pt 2)
  4. using sloppy definitions such as "this helper function"

Is this what you mean:
https://gist.github.com/graingert/7d5a592f8477d845569d

@Ivoz
Copy link

Ivoz commented Jun 21, 2013

My go at writing clearer instructions. Note how the instructions start from the most atomic function the code performs (the double procedure), but we give an overview of the task beforehand, so people know why they need to perform each step to complete the entire program.

https://gist.github.com/Ivoz/21385eb29e2e1617fd1a

@mjhea0
Copy link
Author

mjhea0 commented Jun 22, 2013

Thanks. I updated the instructions. Yes, sometimes I can get in a bad habit of using jargon.

@ojengwa
Copy link

ojengwa commented Jun 22, 2013

pythonic...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment