Estimate e

--Originally published at My programming class blog


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

--Originally published at My programming class blog

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)

--Originally published at My programming class blog

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))

Quiz 9

--Originally published at My programming class blog

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)))


  


QUIZ 4 Minimum and Squares

--Originally published at My programming class blog

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

--Originally published at My programming class blog

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

On to functions

--Originally published at My programming class blog

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

Sum of numbers

--Originally published at My programming class blog

Sum of numbers
For this task we needed a code that asks for a range of integers and then prints the sum of the numbers in that range (inclusive). You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

1.- First write the upper and lower bound:

print ("Sum of Nubers")

x=int(input(("Give me the lower bound")))
y=int(input(("Give me the upper bound")))
sum = 0

2.- State the range of the numbers using the "for a in ()" function:
for a in range(x,y+1): this states the number of times you want the sum of numbers to happen
    sum+=a
print ("The sum between the numbers is",sum)

Check out http://www.python-course.eu/python3_for_loop.php for more info and to find some help


#WSQ04

Pick a number

--Originally published at My programming class blog

Pick a number
For our #WSQ03 task we were asked for a program that picks a random integer between 1 and 100, the it prompts the user for a guess of the value, and it will hint "too low" or "too high". The program will continue to run until the user guesses the integer. As an extra credit we were asked to show the user how many tries it took him to guess the number.

1.- For this exercise we need to import the "random library" by typing "import random", with these we can use the random function just like this:

import random
n=random.randint(1,100)   notice how the function has to be written. 
print("Shhhhh. the number is",n)  this is for you to know what the number is, so it will make it easier test your code.

2.- Ask the user for an integer:
print ("I have choosen a random number between 1 and 100, can you guess it?")
guess=input("Please write a number")
guess=int(guess)
count=1  since we want to show the user how many guesses it took him to guess the answer , we need to declare a variable and give it the value of one.

3.- Now we have to make a loop function to make it go over the process over and over again until the user guesses the number:
while n!=guess: this tells the program to keep making a cycle while n is different from the guess
    count=count+1this is the counter
    if guess>n:
        print ("too big")
    else:
        print ("too small")
    print("Keep trying")
    guess=int(input("Try another number"),)

print("it took you",count, "guesses")

TEMPERATURE

--Originally published at My programming class blog

TEMPERATURE

For this task we were asked to create a program in which the user will prompt a temperature in Fahrenheit and convert it into Celsius The program also had to sate if water would boil at the temperature given by the user.

In this exercise we´ll be using function to strength the knowledge from the past post.

1.- First define your function and make it return a value:
                 def fahrenheit (x):
                      return (5*(x-32)/9)
2.- Declare your variable, remember to make an input on it so the user can type a value:
x= float(input(("Temperature you would like to convert")))
c=fahrenheit (x)

3.- Print the answer:
print ("That´s", c, "celsisus degrees")

4.- Use an "If" function to get the program to tell you if water will boil or not:
if x<212:
    print ("Water does not Boil at this temperature")
else:
    print ("Water will Boil")




Complete code: 

print ("Converting Fahrenheit to Celsius")
def fahrenheit (x):
    return (5*(x-32)/9)

x= float(input(("Temperature you would like to convert")))
c=fahrenheit (x)

print ("That´s", c, "celsisus degrees")
if x<212:
    print ("Water does not Boil at this temperature")
else:
    print ("Water will Boil")



#WSQ02