Tutor profile: Nir S.
Subject: Python Programming
Write a python function that accepts a list of 2 or more numbers as a parameter, and returns the second largest number. Use a for loop.
def second_largest_number(lst): largest = None second_largest = None for num in lst: if largest == None or num > largest: #we found a new largest number second_largest = largest #the old largest number is now second largest largest = num elif second_largest == None or num > second_largest #new second largest number second_largest = num return second_largest
Subject: Computer Science (General)
Describe an efficient algorithm to solve the following problem, and analyze it's time complexity: Given a sorted array A of n integers, determine whether some number appears more than n/2 times in A.
First, we observe that since the array is sorted, if some value x appears in it more than n/2 times, it must appear in the middle of the array. That is, A[floor(n/2)] == x. Now we describe the algorithm: Using binary search, we find the indexes of the first and last appearance of the value A[floor(n/2)] in the array. The number of times the value A[floor(n/2)] appears in the array must be exactly: l last_index - first_index+1. We check if that amount is greater than n/2. The algorithm runs in O(log(n)) time, since other than 2 binary searches, everything else runs in constant time.
Find the derivative of the function f(x)=cos(x^3+4x)
Using the chain rule: f(x) = cos(g(x)) where g(x) = x^3+4x First we derive g(x) using the formula for the derivative of a polynomial: g'(x) = 3*x^2+4 Now, using the chain rule, and the fact that the derivative of cos(x) is -sin(x): f'(x) = g'(x)*(-sin(g(x)) = -(3*x^2+4)*sin(x^3+4x)
needs and Nir will reply soon.