WSQ10 – Babylonian Method

--Originally published at Programming

Hello!

This week we have this instructions: In this assignment you will write a function to calculate the square root of a number using the Babylonian method. The function should receive a number and return floating point number.

First of all, we should know what the Babylonian method is. I could recommend visit Wikipedia or this video too: Solving Square Roots: Babylonian Method

Ok, now…

Here is the code:

Captura de pantalla 2017-04-27 a la(s) 13.43.32

And it runs like this:

Captura de pantalla 2017-04-27 a la(s) 13.43.59

Hope it helps ?


Quiz 11

--Originally published at Programming

Hello, today we have some practice for the exam in this quiz. It’s about problems to solve.

1.- It is like quiz 09, clic to review.

2.- Instructions: Escribe un función que se llama triangulo cual recibe un parámetro size y imprime un triangulo derecho como el siguiente. El renglón mas grande debe llevar size numero de “T”. SOLO imprime los “T”s y los endlines. Nota que no hay characteres (espacios) a la derecha de los T’s. Debe usar un ciclo “for” para controlar el repetición.

Code:
Captura de pantalla 2017-04-26 a la(s) 09.19.43
It runs like this:
Captura de pantalla 2017-04-26 a la(s) 09.19.35
3.-  It also he have already seen, in blog wsq06.
4.- Instructions: Escribe una función que se llama promedio_lista que recibe un parámetro (una lista (Py thon) o arreglo/Vector de C++) de valores float y regresa como float el promedio de los números en la lista.
Code:
Captura de pantalla 2017-04-26 a la(s) 09.25.15
How it runs:
Captura de pantalla 2017-04-26 a la(s) 09.27.06
5.- This we have seen too in an other blog, but let’s review here by the way.
Instructions: Escribe una función que se llama promedio_lista que recibe un parámetro (una lista (Python) de valores float y regresa como float el promedio de los números en la lista.
Code:
Captura de pantalla 2017-04-26 a la(s) 09.28.09Captura de pantalla 2017-04-26 a la(s) 09.28.39
6.- This problem is in blog Quiz 8.
7.- And finally, we have this:
Instructions: Escribe una función que se llama sumsquares_list cual recibe una lista (list en Python) de números y regresa la suma de los números cuadrados.
 Captura de pantalla 2017-04-26 a la(s) 09.33.03
Captura de pantalla 2017-04-26 a la(s) 09.33.56

Lists – WQS07

--Originally published at Programming

The instructions of this excersise are: “Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers.”

Well, first of all, I learn about the standard deviation here: Standar Deviation. That was the way I could generate the function you will see in my code.

Maybe the most difficult part of this was just the standard deviation, but is not to much after you understand how it works.

Here is the code:

Captura de pantalla 2017-03-20 a la(s) 09.23.05

Ok, let’s explain. We import math because we need square root in the last step of the standard deviation. Then we made a function to perform standard deviation too. I will not stop to much in this step, because all is just the steps you can look for  in the link I gave you up here.

The main part of this excersise if to know lists. So, as you see we ask the user for ten numbers, that is what we save in the thing that we will call list.

If you want to know and understand more about it, clic here.

Well, after saving in a list we can work all the values like a package. So, then we just perfom the math operations and finally we print them.

That’s it, and this is how it runs:

Captura de pantalla 2017-03-20 a la(s) 09.23.45

Hope it helps ?


Factorial Calculator – WSQ06

--Originally published at Programming

Hello! Today we are doing a factorial calculator. This is about calculate the factorial of a number, asking the user for a non-negative integer. If you don’t know is the factorial or you want to refresh, you can know about it in Wikipedia-Factorial.

Well, as usual, here is the code: (and then I will explain)

Captura de pantalla 2017-03-20 a la(s) 08.26.41

First we print an introduction phrase for the user. The objetive of this exercise is to use loops. To start the loop our coindition needs to be true, thats why we declared repeat as “yes” and the we start the loop.

Inside this loop, we ask for the number like an int. Then we inicialize the counter like one and the fact too, fact is the number that will be multipliying and incrementing. For that happens we made another loop. The condition as we see is that the counter is different than the number (that the user give us) plus one. This way then we see that fact will be multiplicating the counter and after that it will increase one.

After this math operations we print the result with a sentence to advise, and finally we ask the user if he want to repeat the cycle.

If yes, all start again. If no, we just print Thanks.

And that’s it. This is how it runs:

Captura de pantalla 2017-03-20 a la(s) 08.27.14

Hope it helps ?


Quiz 09 – Distance

--Originally published at Programming

In this quiz we have to ask the user for the coordinates of two points in the cartesian plane, and then, calculate the distance between them. Let’s see the code:

Captura de pantalla 2017-03-16 a la(s) 10.51.12

First we importh math because we need the square root then. Our function that will do the magic is call distance, and here is where we write the variables. Finally in this part we write what we want it return, that is the formula of distance between two points. You can find more information about this here: Distance Between 2 Points.

Now, the second part is just to ask the user for the inputs like floats. There will be 2x and 2y, one by each point. Finally, we print the result. Original instructions didn’t ask for this step, but I included it because I think it helps to see how it works. You can avoid to follow the instructions correctly if you prefer.

And this is how it runs:

Captura de pantalla 2017-03-16 a la(s) 10.50.56 Hope it helps  ?

 


Quiz Week 8 – Fibonacci

--Originally published at Programming

First of all, you could learn more about Fibonacci in Wikipedia.

26gsorjuepnymjby4

Gif from: http://giphy.com/gifs/loop-blue-spiral-26gsoRJuEPNymjbY4

With a function we will return the Fibonacci number. For example, if we introduce 12, it returns 144, because in the sequence this is the value of the position.

But in this sequence, the numbers before 2 are the same. Remember that and we can start making our code. Let’s see:

captura-de-pantalla-2017-03-06-a-las-10-45-43

Well, look that we implemented two solutions. The first one is with recursion, we defined a function, then an if to consider what about  if the user choose a number before 2. If not, we return the result of fibonacci’s formula.

The second one, is the same, but we are using a loop. For this we declared our counter like num, it starts at 1 because the serie starts at this number. After that declare x like zero because it doesn´t count for the first sum we want to do, and like one, because the serie starts here. If the number is before the 2, you should return the same number, but if not  we start with the loop until while the counter is smaller than input. Inside here, z will be the first value of the serie plus y, the second one. Then, they will change as you see in the code to continue moving inside the line of numbers. Finally here we return just z, that works like the final result.

Just print, as you see both ways give us the same, so they will give you exactly the same every time, but we are printing both to confirm.

This is how it runs:

captura-de-pantalla-2017-03-06-a-las-10-46-48

Hope it helps ?


Functions in Python

--Originally published at Programming

In this post we will:

¡Recycle!

2fazaqyizlltuiie0

Gif from: http://giphy.com/gifs/helpsgood-2FazaqYizLltuIIE0

But not as the usual way. We will use the programm we made in this post: Fun with numbers. We will do the same, but now with functions. First look how it runs:

Captura de pantalla 2017-03-09 a la(s) 10.53.42

It’s the same you already saw before. But now look at the code:

Captura de pantalla 2017-03-09 a la(s) 10.51.10

Let’s explain how it works.

(This video may help you to understand better Python functions: Python Tutorial for Beginners).

The first part as you see, is just declare functions. Thats why you write def and after the name and the variables in brackets. We need it give us the operation, that why we write return and the operation to perform, that we already describe in the post Fun with numbers. The inputs will be the same way, and the thing that changes is the math operations. We are using functions, so we don’t need to perfom it like the traditional way. We just call the function and finally we print it ?


Sum of numbers

--Originally published at Programming

In our program we will ask for a range of numbers, and then prints the sum of the numbers. For example, if you introduce 6 as de lower and 10 as the higher, we will print 40. Because 6+7+8+9+10=40.

captura-de-pantalla-2017-02-27-a-las-11-07-22

First we declarate the low and the high input like int, because they will be integer numbers. Then, we declarate the result like cero to start. We need a counter to stop the loop when we raise the higher number. This is the a and we start like low, because we will start in the lower number that the user introduce. We also declarate new like low, because is the number we will be adding and it also going to star in the lower number. Top will be the number where we stop, but it doesn’t come into the loop, that’s the reason why we sum 1, to avoid stop 1 number before we want.

Inside the loop, all we are doing, is adding, incrementing  the counter a to comparate with the top, and also incrementing new to do the sum.

Finally, we print the result with a sentence. It also remember us the original numbers we introduced.


Pick a number

--Originally published at Programming

The instructions was: Write a program that picks a random integer in the range of 1 to 100.

Then, you will try to guess that number. If you are up the value write something like “too big” and “too small” if you are under.

Here is the code:

captura-de-pantalla-2017-02-21-a-las-06-58-36

First, we import random. Then we declared that num will be the number we will being trying to guess. In that part we write the range (the orange numbers). After that, we print a little description for the user from our programm. Then we declared guess that is the number the user introduce (remeber, put like and int), and a that is our counter of tries.

The program continues to run until the user guesses the integer and at the end we print how many tries he/she made.

And now, let’s play! ?

captura-de-pantalla-2017-02-21-a-las-06-59-43

 


Temperature

--Originally published at Programming

In this post we are going to create a programm that convert Farenheit degrees to Celsius.

Here is the code:

captura-de-pantalla-2017-02-08-a-las-18-10-58

But let’s explain: First we define our function and indicate what we want it give us.  If you have doubts with the formula, I recomend you to check this link: http://www.pythonforbeginners.com/code-snippets-source-code/python-code-celsius-and-fahrenheit-converter

Then, we declate f that will be the way we call Farenheit. It is an int, and also and input, because we ask the user for the value of farenheit he want. With that, we continue declarating c, the way we are calling Celsius degrees. For this, we call our function that has the same name.

In this moment,  we already have the results and we only need to print them out.

Water boils at 100 ºC, right? so, now that we have or information in degrees we could tell this information to the user to. We use a conditional to indicate and after that, we just print the statement.

Finally, in this youtube video is the process I had explain in this blog: Fahrenheit to Celsius Program in Python

And this is how the code runs:

captura-de-pantalla-2017-02-08-a-las-18-11-41

?