Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
YStrano
GitHub Repository: YStrano/DataScience_GA
Path: blob/master/lessons/lesson_02/code/python-foundations/solution-code/python-functions-solutions.ipynb
1905 views
Kernel: Python 2

Practice Python Functions

_Author: Ryan Dunlap (San Francisco)

Resources: Potentially Helpful Functions

Nowadays, most of the string module's components are already built into Python. However, it still has several constant values that can be useful, including:

string.ascii_uppercase
import string string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

1) Write a function that takes the length of a side of a square as an argument and returns the area of the square.

def area_square(length): area = length ** 2 return area

We can use the assert statement to validate if our function's output is correct.

assert area_square(5) == 25
# If our assert statement is false, assert will return an error. assert area_square(5) == 21
--------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-4-e5f18b8a7bbc> in <module>() 1 # If our assert statement is false, assert will return an error. 2 ----> 3 assert area_square(5) == 21 AssertionError:

2) Write a function that takes the height and width of a triangle and returns the area.

def area_triangle(height,width): area = width * height * (1/2.0) return area
assert area_triangle(2,2) == 2 assert area_triangle(5,5) == 12.5

3) Write a function that takes a string as an argument and returns a tuple consisting of two elements:

  • A list of all of the characters in the string.

  • A count of the number of characters in the string.

def string_list_fun(user_string): string_list = list(user_string) string_count = len(string_list) return (string_list,string_count)
assert string_list_fun('Ryan Rocks') == (['R','y','a','n',' ','R','o','c','k','s'],10) string_list_fun('Ryan Rocks')
(['R', 'y', 'a', 'n', ' ', 'R', 'o', 'c', 'k', 's'], 10)

4) Write a function that takes two integers, passed as strings, and returns the sum, difference, and product as a tuple (with all values as integers).

def math_rocks(number_1, number_2): int_1 = int(number_1) int_2 = int(number_2) my_sum = int_1 + int_2 my_diff = int_1 - int_2 my_prod = int_1 * int_2 return (my_sum, my_diff, my_prod)
assert math_rocks ('5','2') == (7,3,10) math_rocks('5','2')
(7, 3, 10)

5) Write a function that takes a list as the argument and returns a tuple consisting of two elements:

  • A list with the items in reverse order.

  • A list of the items in the original list that have an odd index.

def getting_crazy(my_list): # We can use -1 to reverse index the list: reverse_list = my_list[::-1] # Start from index 1 and grab every other index from there (1,3,5 etc.): odd_list = my_list[1::2] return (reverse_list,odd_list)
assert getting_crazy([1,2,3,4,5]) == ([5,4,3,2,1],[2,4]) getting_crazy([1,2,3,4,5])
([5, 4, 3, 2, 1], [2, 4])

Challenge: Write a function that returns the score for a word. A word's score is the sum of the scores of its letters. Each letter's score is equal to its position in the alphabet.

So, for example:

  • A = 1, B = 2, C = 3, D = 4, E = 5

  • abe = 8 = (1 + 2 + 5)

Hint: The string library has a property ascii_lowercase that can save some typing here.

def score_word(user_word): # Creates a dictionary of letters and associated scores: letter_score = {letter:counter + 1 for counter,letter in enumerate(string.ascii_lowercase)} # Added level to ensure letters used are uniform: lower_word = user_word.lower() # Baseline score to add to: score = 0 # Iterates through the argument word and adds their scores to the total: for letter in lower_word: score += letter_score[letter] return score
assert score_word("abe") == 8