TutorMe homepage
Subjects
PRICING
COURSES
Start Free Trial
Faisal U.
Teaching assistant at UofT for three years now Software Engineer at Achievers
Tutor Satisfaction Guarantee
Python Programming
TutorMe
Question:

Write a $$\textbf{recursive}$$ function $$perm(s)$$ that takes a string and returns a list of all the permutations of the given string. For example $$>>>perm('cat')$$ $$>>> ['cat', 'act', 'atc', 'cta', 'tca', 'tac']$$

Faisal U.

Before writing any code we shall first come up with a recursive algorithm. To do that, it helps thinking about how can we solve this problem if we had the solution to the simpler problem. In other words, if the string is '$$cat$$' and we had the list of permutations of the smaller string, lets say '$$at$$' ('$$at$$' is substring of '$$cat$$') which is $$['at', 'ta']$$ how can we construct the final answer. With a little observation, you can see that we can obtain the final answer by taking the remaining letter '$$c$$' and inserting it into all the possible places in the provided string. With '$$at$$' we can construct three new permutation by inserting the letter '$$c$$' before, middle and end of '$$at$$' to get '$$cat$$', '$$act$$', and '$$atc$$'. Doing the same to '$$ta$$' to get '$$cta$$', '$$tca$$', and '$$tac$$' which is all the permutation of the string. Now that we have an algorithm we will go ahead with writing the code. $( \text{def} \ perm(s): \ \ \ \ \ \ \ \ \ \ \ \ \ \\ \text{if (len(}s\text{) == 0):} \\ \ \text{return [s]} \\ \text{else:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\ res = [] \ \ \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{for } term \text{ in }perm(s[1:]): \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \text{for } i \text{ in range(len(} s \text{)):} \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \hspace{4.5 cm} res\text{.append(}term[:i] + s[0] + term[i:]\text{)} \\ \text{return }res$) $$\textbf{Note:}$$ this can be done in less line of code using list comprehension. $( \text{def} \ perm(s): \hspace{12 cm} \\ \text{if (len(}s\text{) == 0):} \hspace{11 cm} \\ \ \text{return [s]} \hspace{12 cm} \\ \text{else:} \hspace{11 cm} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \\ \ \ \ \ \ \ \ \ \text{return} \text{ [} term[:i] + s[0] + term[i:] \text{ for } term \text{ in }perm(s[1:]) \text{ for } i \text{ in range(len(} s \text{))]} \\$)

Physics (Electricity and Magnetism)
TutorMe
Question:

Suppose there is a metal sphere of radius $$R$$ and total charge of $$Q$$. Prove that the electric field outside of the sphere is the same as a point charge $$Q$$ centered at the center of the sphere.

Faisal U.

First, we notice that the charge $$Q$$ is distributed evenly so there is a spherical symmetry of the electric field that points radially outward (positive charge Q). To take advantage of the symmetry we shall consider a Gaussian surface to be a spherical surface of radius $$r > R$$ concentric with the charged sphere in question. Gauss's law is $(\Phi_e = \oint \vec{E} \cdot d\vec{A} = \frac{Q_{in}}{\epsilon_0}$) where $$\vec{A}$$ area vector and $$Q_{in}$$ is the total enclosed charge inside the Gaussian surface. Because the Gaussian surface encloses the entire sphere of charge, $$Q_{in} = Q$$ To calculate the flux, notice that the electric field is perpendicular to the surface everywhere. And due to spherical symmetry, the electric field magnitude $$E$$ must have the same value along the surface of the Gaussian surface. This means that $$E$$ can be pulled out of the integral, so $(E\oint dA = \frac{Q}{\epsilon_0}$) The integral now is just the area of the Gaussian surface, which is just the surface is a sphere with radius $$r$$, so $$A_{sphere} = 4\pi r^2$$. $(EA_{sphere} = E4\pi r^2 = \frac{Q}{\epsilon_0}$) and solving for the electrical filed $$E$$ gives, $(E = \frac{1}{4\pi \epsilon_0}\frac{Q}{r^2}$) Which is exactly the field of a point charge Q?

Linear Algebra
TutorMe
Question:

Assume vector space of polynomials of degree 2 with coefficients of module 7. Let $$W = span(1+6x+2x^2, 3+x, 5+6x+4x^2, 5+5x+2x^2)$$. Find the basis for W.

Faisal U.

We know that basis must be linearly independent of one another. In this case, $$W$$ is a subspace that is a span of 4 vectors. Since space has polynomials of degree 2 then any basis must have three vectors. Therefore, we must a subset of $$1+6x+2x^2, 3+x, 5+6x+4x^2, 5+5x+2x^2$$ that spans $$W$$. To achieve this will turn the four vectors as columns for a matrix then row reduce the matrix and find which columns have pivots once the matrix is in row echelon form. The question is how do we turn a polynomial into a vector. To do that we write each polynomial as a coefficient vector of the natural basis $$(1, x, x^2)$$. The matrix will look like $(\begin{bmatrix} 1 & 3 & 5 & 5\\ 6 & 1 & 6 & 5 \\ 2 & 0 & 4 & 2 \end{bmatrix}$) and the row echelon form $(\begin{bmatrix} 1 & 3 & 5 & 5\\ 0 & 1 & \frac{24}{17} & \frac{25}{17} \\ 0 & 0 & 1 & \frac{1}{3} \end{bmatrix}$) So from this we can deduce that $$(1+6x+2x^2, 3+x, 5+6x+4x^2)$$ can be a basis for $$W$$.

Send a message explaining your
needs and Faisal will reply soon.
Contact Faisal