#!/usr/bin/env python # coding: utf-8 # # Lecture 4, Part 1 Exercises # # ## ***Before starting, please run the following cell*** # In[1]: from __future__ import division, print_function # ## Question 1 # _Execute all the following cells in order._ # ### 1.1 # Write a list called `my_list` that contains 5 integers. # In[ ]: # ### 1.2 # Print the first element in `my_list` # In[ ]: # ### 1.3 # Remember from previous labs that you can use negative indices for lists. Print the second to last element in `my_list` using negative indices (if you don't remember how to do it, look back at lecture 1, part 2). # In[ ]: # ### 1.4 # Now, write a function `reverseTwo` that returns a two element list containing the last two elements of `my_list` in reversed order. # # **Ex:** `reverseTwo([1, 2, 3, 4]) ==> [4, 3]` # In[ ]: # ### 1.5 # Write a function `reverseList` that takes in a list `lst` and returns the reversed list. # # **Ex:** `reverseList([1, 2, 3, 4])` => `[4, 3, 2, 1]` # In[ ]: # ## Question 2 # ### 2.1 # DNA has 4 bases: Adenine (A), Thymine (T), Cytosine (C), Guanine (G). In the DNA, each base fits like a puzzle piece, with another base. So, Adenine will pair up with Thymine (A-T base pairing) and Cytosine will pair up with Guanine (C-G base pairing), and vice versa. DNA is double stranded, so if we look at one side side of the strand, we can figure out the other side that matches. For example: # # - ATACGTACGATCA #this is the strand we know # - TATGCATGCTAGT #this is the _complement strand_, that we can figure out. # # Just as an exercise, figure out the complement strand to the following strands: # # 1. # ATCCGGGATCAA # # 2. # GGCCCCATTTTC # # 3. # TTACCAGGGGAT # # In[ ]: # ### 2.2 # Given a string `x` that contains only A, C, G, and T characters, write a function called `containsA` that returns `True` if the string contains an Adenine, expressed as an `'A'`. # In[ ]: # ### 2.3 # Given a string `x` that contains only A, C, G, and T characters, write a function called `complementStrand` that will return the complement DNA strand explained in 2.1. # In[ ]: # ## Question 3 # _Execute all the following cells in order._ # ### 3.1 # Create a list called `vowels` that contains all the vowels from the English alphabet. # In[ ]: # ### 3.2 # Create a variable `my_letter` that has the value `"a"`. # In[ ]: # ### 3.3 # Now, write a conditional statement that returns `True` when `my_letter` is equal to `"a"`. # In[ ]: # ### 3.4 # Write a function `printVowels` that iterates through `vowels` and prints each element. # In[ ]: # ### 3.5 # Now, write a function called `isVowel` that takes in a string `letter` and returns `True` if `letter` is a vowel and `False` otherwise. Do this by looping through `vowels`. # In[ ]: # ### 3.6 # Write a function `isVowel2` that does the same thing as $3.5$, but using only conditional statements. # In[ ]: # ### 3.7 # Create a function called `isConsonant`, that returns `True` if the input is not a vowel, and `False` otherwise. # # In[ ]: # ### 3.8 # Try running the code below. Does it do what you expect? # In[ ]: isConsonant("a") isConsonant("1") isConsonant(".") # ### 3.9 # Write `isConsonant` so that it returns `False` when it is not part of the alphabet. # # _Hint: Use >, <, >=, and/or <= as string comparators to test if it's part of the alphabet._ # In[ ]: # ## Question 4 # ### 4.1 # Write a function ``inTheList`` that takes in a list `lst` and a target element `t` that returns `True` if `t` is in `lst` and `False` otherwise. # In[ ]: # ### 4.2 # Write a function `overlaps` which takes in two lists, `lst1` and `lst2`, and returns `True` if any element of `lst1` is in `lst2` and `False` otherwise. # In[ ]: # ### 4.3 # Write a function `intersection` which takes in two lists, `lst1` and `lst2`, and returns a list containing the all the elements present in both lists. # In[ ]: # ## Question 5 # ### 5.1 # Write a function `rectangleArea` that takes in two integers, `x` and `y`, which represent the side lengths of the rectangle, and returns the area of the rectangle. Can you use this function to find the area of a square? # In[ ]: # ### 5.2 # Write a function `circleArea` that takes in an integer `r` that represents the radius, and returns the area of a circle. # In[ ]: # ### 5.3 # Write a function `triangleArea` that takes in three integers, `a`, `b`, and `c` that represent the traingle side lengths, and returns the area of the triangle. # # Note: the formula for the area of a triangle is - # $$ A = \sqrt{s \times (s-a) \times (s-b) \times (s-c) } $$ # where $s$ is the semi-perimeter represented as $ s = 0.5 \times (a + b + c) $. # In[ ]: # ### 5.4 # If a square, circle, and equilateral triangle ($a=b=c$) have the same perimeter `P`, find which figure has the largest area. Return the largest area as a float. # # _Hint: You should use the functions from 5.1 to 5.3._ # In[ ]: def largestArea(P): # r = ? (find the circle's radius by using P) # x = ? (find the square's side by using P) # a = ? (find the triangle's side by using P) return # ## Question 6 # ### 6.1 # Write a function `cumulativeSum` that takes in a list `lst`, and returns the list that represents the cumulative sum. In other words, the i-th element in the final list should be equal to the sum of the elements from 0 to i in the input list. # # **Ex:** `[1, 2, 3]` => `[1, 3, 6]`, `[5, 10, 15]` => `[5, 15, 30]` # # _Hint: You don't need 2 loops. If you have found the i-th element in the final list, how would use it to find the (i+1)-th element?_ # In[ ]: # ### 6.2 # Write a function `largestCumulativeSum` that takes in a list `lst`, and returns an integer `i` for which the sum of the elements from $0$ to $i$ is the largest. # # **Ex:** `[10, -6, 4, 8, -5]` => `3`, `[20, -15, 3, 1, 1, 1, 5, 2]` => `0` # In[ ]: # ## Question 7 # ### 7.1 # Write a function `isPrime` that takes in an integer `x`, and returns the boolean `True` if the number is prime, and false otherwise. As a reminder, a prime number is a number that is only divisible by itself and 1. # # _Hint: Use the modulo operator to check if x is divisble by a number._ # In[ ]: # ### 7.2 # Write a function `primesToX` that takes in an integer `x`, and returns a list of all the prime numbers smaller or equal to `x`. # In[ ]: # ### 7.3 # Write a function `nextPrime` that takes in an integer `x`, and returns the first prime greater than `x`. # In[ ]: # ## Question 8 # ### 8.1 # Write a function `hashExtract` that takes in a string `s`, and returns a list of all the words that start with a hashtag (#). # # **Ex:** `'I'm so excited to code for #AddisCoder! Looking to learn lots of #CS."` => `['AddisCoder', 'CS']` # In[ ]: # ## Question 9 # ### 9.1 # Write a function `interweave` that takes in two lists, `lst1` and `lst2`, and returns a list of the elements from `lst1` and `lst2` that first places an element from `lst1`, then places an element from `lst2`, and continues to do that until all the elements have been placed in the new list. You may assume that the list lengths are equivalent. # # **Ex:** `[1,2,3], [4, 5, 6]` => `[1, 4, 2, 5, 3, 6]`, `[13, 4, 6, 8], [17, 2, 1, 5]` => `[13, 17, 4, 2, 6, 1, 8, 5]` # In[ ]: