lunes, 24 de abril de 2017

Estimate e


Estimate e:

In this assignment you will estimate the mathematical constant e. You should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.
You will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).


Here´s my code:
def factorial(x):
    if x==0:
        return 1
    else:
        return x * factorial(x - 1)

def estimate_e(estimation):
    guess= 1
    dif= 10
    e= 2
    x= 2
    while dif > precision:
        e =  e + 1/factorial(x)
        dif= e - guess
        guess= e
        x  =  x + 1
    return e

precision = float(input("Give the precision: "))
print (estimate_e(precision)) 

Fun with numbers

Fun with numbers

  • The sum of the two numbers.
  • The difference of the two numbers.
  • The product of the two numbers.
  • The integer based division of the two numbers (so no decimal point). First divided by second.
  • The remainder of integer division of the two numbers.
So, to solve this exercise I came up with this:




Here´s the code:
x= input ("Write a number")
x=float (x)
y= input ("Write a number")
y=float (y)
n= (x + y)
n= float (n)
print ("The sum of both numbers is", n)
print("")
print ("")
n2=(x - y)
n2= float (n2)
print ("The difference of both numbers is", n2)
print("")
print("")
n3=(x*y)
n3=float(n3)
print ("The product of both numbers is", n3)
print ("")
print ("")
n4=(x/y)
n4= int (n4)
print ("The division between both numbers is", n4)
print("")
print("")
n5=(x%y)
n5=int(n5)
print ("The modulus operator between both numbers is", n5)
print("")




Quiz 8 (Fibonacci)

The task:

  • Write a function that calculates returns the “nth” Fibonacci number where we define a function over the Fibonacci numbers mapping the naturals (starting with zero) to the Fibonacci series. So fibonacci(0) returns 0, fibonacci(1) returns 1, fibonacci(2) returns 1 and so on. Note that we are using the modern definition where the sequence starts with zero. You should try to implement this with two solutions: one with a loop and one with recursion. Which do you think is “better”, which looks more “elegant”, which is more “efficient”?
(Here´s a link to know what a fibonacci series looks like: https://en.wikipedia.org/wiki/Fibonacci_number)

So first let's go to the recursion solution, which in my opinion, is the easiest:




Here´s the code: 

def fib(x):
    if x==0:
        return 0
    elif x==1:
        return 1
    else:
        return fib(x-1)+fib(x-2)

x=int(input("Write a number:"))
print ("The fibonacci is:", fib(x))



Now let´s go to the Looping solution:


Here´s the code for the loop method:

def fib(x):
    if x==0:
        return 0
    if x==1:
        return 1
    a=0
    b=1
    for counter in range(x-1):
        c=a+b
        a=b
        b=c
    return c

x=int(input("Write a number:"))
print ("The fibonacci is:", fib(x))

jueves, 9 de marzo de 2017

Quiz 9

Quiz 9 
This one was pretty easy, we had to find the distance between two points. This obviously using a function.



As you can see, a pretty good thing to do to make things more simple, is to asign variables to the operations you make.

Here´s the code:
import math

def distance (x1,y1,x2,y2):
    a=(x2-x1)
    b=(y2-y1)
    c=math.pow(a,2)+math.pow(b,2)
    d=math.sqrt(c)
    return d

x1=float(input("Enter X1:"))
y1=float(input("Enter Y1:"))
x2=float(input("Enter X2:"))
y2=float(input("Enter Y2:"))

print("The distance between the two points is:",(distance(x1,y1,x2,y2)))


  


martes, 7 de marzo de 2017

QUIZ 4 Minimum and Squares

Quiz 4
For this quiz we were supposed to create two functions:
  • def minimum_three(x, y, z):  # returns the value that is smallest of x, y and z
  • def sum_squares(x, y, z): # returns the value of the sum of squares of x, y, z
For this quiz you should make a main routine that asks the user for three numbers and then calls your functions to which should *RETURN* the value and you print in the main part of your program.
For this as you can see in the pictures below, you must first import math, so you use the "math.pow" function.
Defining a function is pretty simple, you just need to name it, state the variables, and tell it what to do.



As always, here´s my code so you can check it out:
import math

def minimum_three(x,y,z):
    return min (x,y,z)
def sum_squares (x,y,z):
    return math.pow(x,2) + math.pow(y,2) + math.pow(z,2)

x=float(input("type a number:"))
y=float(input("type a number:"))
z=float(input("type a number:"))
r=minimum_three(x,y,z)
s=sum_squares(x,y,z)

print ("the minimum of the three numbers is:", r)
print ("the sum of squares of the three numbers is:", s)

For help with functions, I used the "How to think like a computer scientist" book.

Lists

Lists (#WSQ07)
For our WSQ07 task we were asked to 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.




But what is a list?
Well, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. It can have any number of items and they may be of different types (integer, float, string etc.).
Also with lists, in python we have many "List methods" that are like some type of functions you can use inside lists, like adding more elements to it or to remove some.
Here´s a list with the main list methods you can use:
Table got from: https://www.programiz.com/python-programming/list

Here´s my code so you can check it out on your own text editor:
list =[]
while True:
    x = input("Please add numbers to the list. Whe you finish type "'done'":" )
    if x == "done":
        break
    else:
        list.append(float(x))
total=0
for i in list:
    total += i
print ("The total is:", total )
average= total/(len(list))
print ("The average is:", average)
step=0
for i in list:
    step += ((i-average)**2)
stdev= step/(len(list))
standardDeviation = stdev**(1/2)
print ("The Satndard Deviation is:", standardDeviation)

I found a very good video in my classmate´s blog: elusblog.wordpress.com

jueves, 23 de febrero de 2017

On to functions

For this task we will go back to the fun with numbers exercise, but now we will use a function for each operation we want to make which is pretty easy:

1.- First remember to define your functions before entering the inputs and other stuff:
print("On to functions")
def sumof (x,y):
    return (x+y)

def substract (x,y):
    return (x-y)

def multiply (x,y):
    return (x*y)

def divide (x,y):
    return (x/y)

def modulus (x,y):
    return (x%y)

2.- Now that you have defined the functions you can ask for the values to the user:
x=int(input("Please write a number"))
y=int(input("Please write another number"))

3.- Apply the functions:
a=sumof(x,y)
b=substract(x,y)
c=multiply(x,y)
d=divide(x,y)
e=modulus(x,y)

4.- Print all the answers:
print ("the sum of both numbers is", a)
print ("the difference between both numbers is", b)
print ("the product between both numbers is", c)
print ("the division of both number is", d)
print ("the modulus operator of both numbers is", e)


If you need some help understanding functions go to: https://www.tutorialspoint.com/python3/python_functions.htm 
I found some pretty useful stuff there.


#WSQ05