Tutor profile: Sameer S.
Questions
Subject: Python Programming
Write a python function $$\textit{squaredDigitSum(n)}$$ that takes in a positive integer and returns the sum of each digit squared. For example: $$\textit{squaredDigitSum(2)} = 2^2 = 4$$ $$\textit{squaredDigitSum(54)} = 5^2 + 4^2 = 41$$ $$\textit{squaredDigitSum(1234)} = 1^2 + 2^2 + 3^2 + 4^2 = 30$$
The first step I like to take when given a question like this is to dissect how I would solve the question as a human. To find the squared sum of each digit, I isolate each digit, square it, and then add it to my running total. Next, I try to convert this thought process into code. As humans, we read from left to right, however, Python only sees an integer. Without any parsing, we can't isolate the left-most digit in an elegant way. Luckily, it doesn't matter if we sum from the right-most digit to the left. 1 . How can we isolate the left-most digit? Using the modulo operator. Any integer mod 10, will give the remainder when divided by 10, which gives us the left-most digit. 2. Once, we have the left-most digit, we want to square it and keep track of this as we'll be adding values to it as we continue through the integer. This means we'll need to create a variable. 3. Now, we're at a problem. How can we move on to the next digit? If we take the modulo again, we'll end up with the left-most digit again. We must find a way to 'disregard' the left-most digit as we've already dealt with it. How can we do this? Using the floor divider. If we floor divide an integer by 10, we divide a number by 10 and disregard the decimal. For example: 63 // 10 = 6 4. We can now iterate on this process until we've gone through each of the digits: def squaredDigitSum(n): currSum = 0 while n > 0: currSum += (n % 10) ** 2 n = n // 10 return currSum
Subject: Statistics
The number of students living together in a given dormitory is given by the following probability distribution. $$P(x) = \textit{probability x students are living together}$$ $$P(1) = 0.25$$ $$P(2) = 0.50$$ $$P(3) = 0.15$$ $$P(3) = 0.10$$ $$\textbf{Find the mean and variance of this distribution}$$
$$\textbf{Finding the mean}$$ The mean, or the expected value, of a distribution, is our best prediction of what a given random variable will be. We find this by summing over all possible values for the distribution and multiplying each value by the probability of its occurrence: $$E[X] = \sum_i x_i * P(x_i)$$ $$E[X] = (1 * 0.25) + (2 * 0.50) + (3 * 0.15) + (4 * 0.1)$$ $$E[X] = (1 * 0.25) + (2 * 0.50) + (3 * 0.15) + (4 * 0.1)$$ $$E[X] = 2.10 $$ $$\textbf{Finding the variance}$$ Variance is how we measure how far a given random variable can be from it's expected value. It is calculated as follows: $$\sigma^2 = Var(X) = E[(X - E[X]) ^ 2]$$ Plugging in our value for $$E[X]$$ from above we can solve for variance as follows: $$\sigma^2 = Var(X) = E[(X - 2.1) ^ 2]$$ $$\sigma^2 = [(1 - 2.1)^2 * 0.25] + [(2 - 2.1)^2 * 0.50] + [(3 - 2.1)^2 * 0.15] + [(4 - 2.1)^2 * 0.10]$$ $$\sigma^2 = Var(X) = 0.79$$
Subject: Algebra
Find the roots of the following polynomial: $$2x^2 + 20x + 8$$
Finding the roots of a polynomial means we want to find values of $$x$$ for which the polynomial evaluates to 0. In graphical terms this would mean answering the question: At which values of x does this polynomial cross the $$x$$ axis. We can start by setting the polynomial equal to 0: $$2x^2 + 20x + 8 = 0$$ $$\textbf{Step 1: Dividing out integer factors}$$ We can see that each coefficient of the polynomial is divisible by 2 so we can divide both sides by 2 to simplify the polynomial: $$\frac{2x^2 + 20x + 8}{2} = \frac{0}{2}$$ $$ = x^2 + 10x + 4$$ $$\textbf{Step 2: Checking if polynomial is factorable}$$ Since the largest degree coefficient is 1, we are looking for two integers which satisfy the following: $$x_1 + x_2 = 10$$ $$x_1 * x_2 = 4$$ The factors of 4 are $$ (1, 4) (2, 2)$$ and neither of these pairs will sum to 10, thus we must solve this by either the quadratic formula or by completing the square. Let's try the latter: $$\textbf{Step 3: Completing the Square}$$ 1. We begin by moving the number term (0 degree coefficient) in our polynomial to the left hand side: $$x^2 + 10x = -4$$ 2. Next, we complete the square on the left hand side and then balance both sides of the equation. The intuition behind this step can be built by the following observation: $$x^2 + 10x + 25 = (x+5)^2 $$ $$\rightarrow$$ If we divide the coefficient of the $$x$$ term by 2 and then square it, we get the value needed to complete the square. Let's apply this to our problem: $$x^2 + 10x + 25 = -4 + 25$$ $$(x+5)^2 = 21$$ Now we can take the square root of both sides and solve for x. Remember to consider both the positive and the negative root: $$x + 5 = \sqrt{21} \rightarrow x = \sqrt{21} - 5$$ $$x + 5 = - \sqrt{21}\rightarrow x = -5 - \sqrt{21} $$ And we're done! For these two values of $$x$$ the polynomial evaluates to 0, thus these two values are the roots. We can verify this solution by plugging in the values in the original equation.
Contact tutor
needs and Sameer will reply soon.