from __future__ import division, print_function
Execute all the following cells in order.
Write a list called my_list
that contains 5 integers.
Print the first element in my_list
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).
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]
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]
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:
Just as an exercise, figure out the complement strand to the following strands:
ATCCGGGATCAA
GGCCCCATTTTC
TTACCAGGGGAT
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'
.
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.
Execute all the following cells in order.
Create a list called vowels
that contains all the vowels from the English alphabet.
Create a variable my_letter
that has the value "a"
.
Now, write a conditional statement that returns True
when my_letter
is equal to "a"
.
Write a function printVowels
that iterates through vowels
and prints each element.
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
.
Write a function isVowel2
that does the same thing as $3.5$, but using only conditional statements.
Create a function called isConsonant
, that returns True
if the input is not a vowel, and False
otherwise.
Try running the code below. Does it do what you expect?
isConsonant("a")
isConsonant("1")
isConsonant(".")
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.
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.
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.
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.
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?
Write a function circleArea
that takes in an integer r
that represents the radius, and returns the area of a circle.
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) $.
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.
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
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?
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
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.
Write a function primesToX
that takes in an integer x
, and returns a list of all the prime numbers smaller or equal to x
.
Write a function nextPrime
that takes in an integer x
, and returns the first prime greater than x
.
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']
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]