Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mjhea0

mjhea0/coding.md Secret

Last active December 18, 2015 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjhea0/3e3901f6a7278c33d824 to your computer and use it in GitHub Desktop.
Save mjhea0/3e3901f6a7278c33d824 to your computer and use it in GitHub Desktop.

Agenda

  1. Talk about problem
  2. Additional problem
  3. Survey/Questions
  4. Next time?

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}

Readability

def double(array):
    double_hash = {}
    for num in array:
        double_hash[num] = doubling(num)
    return double_hash
 
def doubling(n):
    n *= 2
    return n
 
#-----driver code-----#

array = [1,2,3]
print double(array) == {1: 2, 2: 4, 3: 6}

array = [2,3,4]
print double(array) == {2: 4, 3: 6, 4: 8}

array = [3,4,5]
print double(array) == {3: 6, 4: 8, 5: 10}

Pythonic

def doubling(n):
    return (n * 2)

def double(array):
    return {num: doubling(num) for num in array}

One-line

def double(array):
    return {num: num*2 for num in array}

Which do you prefer?

  1. When you get into more complex OOP programming, one function should only do one thing. Get in the habit of that now. No matter how trivial. Sure, you could combine this into one function. But it will help you later on if you start thinking about breaking functions apart, logically.
  2. Think about your audience - Yourself, Future self, Other developers

Next Time

  1. Write a function that takes a number as input and returns its equivalent value in roman numerals.
  2. Twitter API v1.1 - let's grab some tweets!

Survey/Questions

  1. Survey Link
  2. Questions!? RealPython.com Forum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment