#Quiz3

The third quiz of the course comes with two more situations to be solved. 

The first of these consists in defining a function that finds the distance between two points, therefore, it should accept four parameters(an x and y for each point) and return one output value. To get the distance between two points you should first get the difference between the two values of x and the two values of y, respectively, get the square of each, add them and get the square root of that whole value. As in the pythagorean theorem, where c = √(a² + b²). Powers in python are expressed with the same symbol as multiplication, but doubled. For example, 6 ** 3 = 216. In this case, instead of a and b, we have (x1-x2) and (y1-x2).

Our defined function should look like this :

————————————————————————–

Screen Shot 2016-02-18 at 3.13.56 PM

————————————————————————–

As you can see, I have imported the math module to use sqrt, short for square root. Although this could be easily replaced by **(1/2), as raising the power of a number to a half is the same as getting its square root. 

This is our defined function, once we call it with a set of user input parameters, we’ll be able to test if our definition was correct. First, let’s type in those inputs :

————————————————————————–

Editor …

Screen Shot 2016-02-18 at 4.50.08 PM 

————————————————————————–

As you could have already noticed, I called float on each input, this is because the user may want to input decimals and not only integers, plus we want the square root’s output, and that of the function in general, to be as exact as possible.

Next, we should call our function with those inputs as values for the function’s parameters:

————————————————————————–

Editor :

Screen Shot 2016-02-18 at 5.33.01 PM

————————————————————————–

Of course, we have to print to actually see

Screen Shot 2016-02-18 at 5.37.29 PM
Screen Shot 2016-02-18 at 5.38.13 PM
Screen Shot 2016-02-18 at 5.46.22 PM

value that our function returns. Now, let’s test it:

————————————————————————–

Terminal :

Screen Shot 2016-02-18 at 5.37.29 PM

Screen Shot 2016-02-18 at 5.38.13 PM

————————————————————————-

Great, our function does work with floats as inputs too! 

Now, our second part involves the fibonacci sequence. Don’t worry if you’re not familiar with fibonacci’s series, it is a very simple looping pattern, though it is not as simple to simulate using python, you may find it easy to calculate. The series consists of the first two numbers being 0 and 1, the next numbers are the sum of the two numbers before it. The code must receive any integer input that will be a position of the fibonacci series and the output must be the value of the number in that position. I really broke my head with this one, but after solving it, I realised I just had to see the situation in a different way. I’ll show you the code first and then explain how I got to it :

————————————————————————-

Editor :

Screen Shot 2016-02-18 at 5.46.22 PM

————————————————————————-

As you can see, after return in our defined function we have the variable z followed by a string. So z is the output, the value that we’ll return. c is a counter, to end the while loop. Friendly advice, you can also use a for loop with range(input-value). 

Now, fib1 is the first number and fib2 the next, so fib1 starts as 0 and fib2 as 1 inside the function, those are constant. The values given to the variables outside the function actually don’t matter at all, they’re just to initialise the variables. Now, we start the loop with z = fib1, in the case that x = 0, the loop will only run once and z will equal the first value of fib1, which is 0. When the loop runs, fib1’s value will turn into fib2’s value and fib2 will equal fib1(replaceable by fib2, since they’re equal at this point) plus the original value of fib1, which was stored in z. You may have noticed that z isn’t exactly calculated as a sum, but fib2 is and z is later , since we couldn’t give a 0 to z’s value that way. This simulates the fibonacci series, the trick is making the loop run a certain number of times, this is achieved with c, the counter, it starts in 0 and  adds one to itself each time the loop runs. When the value of c gets over the value of x, the loop stops running and z’s is returned.

I’ll edit this later.

CC BY-SA 4.0 #Quiz3 by esenombredeusua is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.