Buscar

Fernando's blog

Programming class

Categoría

WSQ

Cars

This was my code fo the cars WSQ. Once i learn to use files, this becomes easy.

#Jorge Fernando Saldaña Cabal
#A01350730

archivo = open(«carros.txt», «r»)
lineas = archivo.readlines()
#def primero:
y = 0
contcity = 0

for i in lineas:
x = (i[52:54])
if (x != «»):
x = float(x)
y = y + x
contcity = contcity + 1
city = y//contcity
print(‘The average gas mileage in city is: ‘,city)
p = 0
conthigh = 0
for i in lineas:
z = (i[55:57])
if (z != «»):
z = float(z)
p = p + z
conthigh = conthigh + 1
high = p // conthigh
print(‘The average gas mileage in highway is: ‘,high)

f = 0
contmind = 0
for i in lineas:
a = (i[42:46])
if (a != «»):
a = float(a)
f = f + a
contmind = contmind + 1
mind = y//contmind
print(‘The average mind-range price is: ‘,mind)

#52

Estimating e

This task was not that hard. We had to make a function that computes an estimation of e, which is a number very used in finances. The trick was our number must have the accuracy given by the user.

Here is my code:

https://github.com/sabaldaba/TC101/blob/master/Estimating_e

Yo soy 196

This task was of the most challenging ones of the entire course. We had to make a programm to find candidates of lychrel numbers in a given range. First of all i had to look what a lychrel number was because i never heard of it. Then i found out that theres a theory that when you add a number to its inverse ‘n’ times eventually it will become palyndrome which means, if you read it backward it would be the same as if you read it normally.

But there are some numbers that havent been able to prove this theory, i even read that ones, scientifics let a computer programm running for three years in search for its palyndrome, eventually they stopped the programm and found a number of one million digits which is a lot, but never the palyndrome.

I get confused a lot with variables but at the end Ken helped me to fix the last problems, here is my code: https://github.com/sabaldaba/TC101/blob/master/wsq11_soy:196

 

Hope you like it!

Greatest common divisor

This task was about doing a programm that could calculate the GCD of two given integers using the euclids algorithm. At first i got confused because i though i could do it by using the % operator, but after doing some research about the euclids algorithm i found that is way more easy to do it that way.

The code i wrote is in here: https://github.com/sabaldaba/TC101/blob/master/wsq12-GCD

 

Basicaly you use two «if» inside two while loops. You just have to sustract the smaller to the bigger and thats pretty much all of it.

Hope you enjoy it!

 

Factorial Calculator

For this task i had to make a program that calculates the factorial of a given number, and of course to create a function.

The code i wrote is this: https://github.com/sabaldaba/TC101/blob/master/Wsq09-Factorial

The program at the end also gives you the option of doing another operation or to end, this was done thanks to a while loop while the factorial calculator was done with a for loop since in there i knew a specific range.

Hope you like it!

 

Standard deviation

To get this task done we had to make a programm in python that would ask the user for 10 numbers, then to save those numbers in a list. Those numbers had to be floating point. After we get that done, we had to print the total, the average and the standard deviation of those numbers. The trouble i found doing this is that i never heard of standard deviation and when i look for it, i kind of missunderstood the meaning. But i found an awesome page which you can go by clicking here, that help understand what it it. I used two for loops and the math module.

This is my code:

#Jorge Fernando Saldaña Cabal
#A01350730
import math
count = 0.0
lista = []
for i in range(10):
x = int(input(‘Introduce a number: ‘))
count = count + x
lista.append(x)

average = count/10
print(‘The sum of your numbers is: ‘, count)
print()
print(‘The average of your list of numbers is: ‘, average)
print()
resdev = 0.0
for i in range(10):
dev = average-lista[i]
dev = dev ** 2
resdev = resdev + dev
varianza = resdev/10

deviation = math.sqrt(varianza)

print(‘The deviation standard of your numbers is: ‘, deviation)

Any comments feel free to comment.

On to functions

For this task we were meant to create a programm that performs the same as the WSQ03. This means the programm would have to calculate the addition, substraction, multiplication, division and reminder of two INTEGERS given by the user, but this tame we had to declare one function for each calculation.

To define a function you must type the special word «def» and then write the name of the function and after tht you can decide to put o or no to put parameters inside parenthesis, like this:

def sum(a,b)

The code i wrote is this:

#Jorge Fernando Saldaña Cabal
# A01350730

def add(x,y):
z = x+y
return z
def sus(x,y):
z = x-y
return z
def mul(x,y):
z = x*y
return z
def div(x,y):
z = x//y
return z
def rem(x,y):
z = x%y
return z
print ()

print (‘Hey!, Do you want to make some math’)
print(‘Lets make the basic operations: Addition, Sustraction, Multiplication and’)
print(‘Division’)
print()
x = int(input(‘Please enter your firste integer: ‘))
print()
y = int(input(‘Please enter your second integer: ‘))
print(‘The addition of your two numbers is: ‘, add(x,y), ‘, the sustracion is: ‘, sus(x,y))
print(‘the multiplication is: ‘, mul(x,y), ‘, the division is: ‘, div(x,y))
print(‘and its reminder is: ‘, rem(x,y))
print (‘Thanks for using us.’)

Which will be seen as this with the values 3 and 6:

Captura de pantalla de 2015-10-22 16:08:45

Sum of numbers

This task was about making a programm that asks the user for two integers. The user will read that the first number she/he types will be the begining of a serie of numbers and the second will be the end of the serie. Then the program will take all the numbers inclued in that serie, and add them all, so it will be an inclusive sum of numbers.

The task is meant for us to learn to use loops, and i selected to use the For Loop. This is so far one of the hardest parts of programing i’ve learned and according to what i´ve read is one of the most powerful.

A for loop consts of various parts, i understand it like this:

for (variable we´ll use for repeating the process) in (under what conditions it will repeat):

what to do once we’re inside the loop

The code i wrote is in this link: https://github.com/sabaldaba/TC101/blob/master/wsq07–Sum_of_numbers

I was thinking of ther solutions but once i figured how to do it with for, i realize it only takes me two lines.

So, the explaining of my code is that first i asked the numbers to the user. Then i had to set the first number that i had to add, i choose 0 because it wont affect my operation. Then wrote the for loop. I started with ‘for’ and the i followed with the variable that will repeat the process of the loop, i selected the letter ‘i’. Then i type ‘in range (x,(y+1))’ which means that the loop must repeat in the range of the numbers the user gave me. If you are wondering why i set ‘y+1’ thats because the function range is composed to start where it is told (or zero if it doesn´t say where to start) and to end where it is told minus one. So to fix this i simple add it one.

The the body of the loop is that c = c+i. Lets remember that C starts being zero, so the first will be ‘0 + i’ where ‘i’ will start being the first number the user gave me. So after that the loop will start all over again, now ‘i’ will be ‘i+1’ because of the body and ‘c’ again will be the previous c we saved on memory plus the actual value of i. This will be performed until ‘i’ reaches the second value the user gave us. Then the sum of all number, inclusive, will be done and ready to be printed.

Temperature

Termometro.

For this task we had to create a programm that allows the user to enter a number, it could be floating-point, which represents a temperature measured in farenheit degrees. The the computer will «translate» that meausure to Celisius degrees. The way to do this is with a simple equation which is :

C = (5 ∗ (F − 32))/9.

Where C is equal to Celsius and F to Farenheit. The way to do this is solving first the parenthesis whose are depeer. In this case we must solve first «F-39», then multiply the result times «5» and at the end divide all of it over «9» and we will have converted from Farenheit to Celsius.

The way to get this done is give the programm the correct order in which the operations  must be done.

The code i wrote is:

#Jorge Fernando Saldaña Cabal
#A01350730

print()
print(«Whats up! I’m going to help you to convert from Farenheit to Celcius»)
print()
F = float(input(‘Please introduce a temperature in Farenheit degrees: ‘))
C = (F – 32.0)
C = (C * 5.0)
C = (C / 9)
print ()
print(F, ‘ Farenheit degrees is equal to: ‘, C, ‘ Celcius degrees’)
print()
if (C < 100):
print(«Water at», C, «Celcius degrees does not boil»)
print(«under typical conditions»)
else:
print(«Water at», C, «Celcius degrees would boil under typical conditions»)

Blog de WordPress.com.

Subir ↑