miércoles, 25 de noviembre de 2015

WSQ16 - Cars

Hi, this is my last WSQ. This WSQ was hard for me because i had no idea how to read a file properly by lines. Frida´s blog helped me a lot to figure this out, check it on this link: http://tc1014.blogspot.mx/2015/11/cars.html

The link to my code on Github is this: https://github.com/Jocapear/TC1014/blob/master/WSQ16.py

martes, 24 de noviembre de 2015

WSQ15 - Images

I won´t be able to do this WSQ because i would have to uninstall python and install it in 32 bits to be able to download the library that allows me to manipulate images.
The link to the library site is this:
http://docs.python-guide.org/en/latest/scenarios/imaging/

Mastery14 - Creating Python modules

A custom module is just a python file with fiunctions in it. You can create a python file and put in the same location as the program where you are going to call it. Here is an example:
I will create a new file called mymodule.py
def banana(filename):   
    name = open(filename)
    content = name.read()
    list = content.split(" ")
    print (list)
    counter = 0
    for i in list:
        if i.lower() == "banana":
            counter += 1
    print (counter)

def calculate_e(precision):
    counter = 1
    accumulative = 1
    variable = 1
    while True:
        accumulative += variable*(1/counter)
        variable = (1/counter)*variable
        new = accumulative + variable*(1/counter)
        if (new - accumulative) < precision:
            break
        counter += 1
    return accumulative

And now i´m going to call that file just like if it were a module. This file is called file.py and is located in the same directory as mymodule.py:
import mymodule.py
a = mymodule.banana("text.txt")  #You must have a file named "text" called "text"
b = mymodule.calculate_e(35)
print (a)
print(b)
2
2.71825 
As you can see in file.py i used the functions inside of mymodule.py by calling them as if they were a module(well, it is a module now)

Mastery30 - Reading and writing files

Reading files:
To read a file you should know this functions: open("filename","r") and filename.read()
The first one is used to open a file, read it and pass that information to a variable ( if you want) or to simply work with it directly.
The second one will just read a file, to use this function ou should have used the open function first like this:
a = open("filename") 
print (a.read()) 
As you can see im not using the "r" because that means that i read it at the same time i open it, if you just want to open the file but not read it right there and use that information for later you should do something like what i did above. This has a lot of functionalities if you mix this with strings manipulation. You look for a specific word, count the lines, the total words etc. Here is an aplication for that, this documento opens and reads a file and counts how many times the word "banana" is in it (WSQ14):
def process_file(filename):
    name = open(filename)
    content = name.read()
    list = content.split(" ")
    print (list)
    counter = 0
    for i in list:
        if i.lower() == "banana":
            counter += 1
    print (counter)
process_file("file.txt")

To be sure that this file works correctly i must have a text file named "file" and be sure that each word written in it is separated by a space. If you noticed, reading a file is reading a file is really simple. Is just taking a value from a different file and assigning it to a varible, works like an input. What matters is how you control or process that information.

 Writing a file:
To write a file you have to use the same function as before but this time with a different parameter: open("filename","w")
This "w" thing will thell python that you want to edit/write the file, be careful because this will erase the content of that file and create a new file. To write a line use fout.write() and use as a parameter a string (or a variable with a string), when you are done use fout.close(). It is very important that you understand that the file will star writing in the last empty line. Here is an example:
 def process_file(filename):
    name = open("filename","w")

    a = "no"
    while a == "yes":

       line = input("Write here the new data")
       fout.write(line)
       a = input("want to close?")
    fout.close() 
process_file("file.txt")

This function will add new strings to the "file.txt" document until the user decides to stop.

lunes, 23 de noviembre de 2015

Mastery 5 - Linux sufficient for exams

To execute a file in Linux all you have to do is go to the terminal and write "python3" and then the location of the file. Be sure to write the name of the file and ".py" after it. To make the location shorter save the file in the main directory. An example is this:
python3 filename.py
If you are having trouble try to star again but this time write first "lb", this will take you to the main directory. Then you can continue writing as usual. 

Mastery 22 - Types of repetition

There are 3 types of repetition:
-For loop
-While loop
-Recursion

The FOR loop is used when you know the number of repetitions. It can be used with ranges, list tuples or dictionaries(iterable objects).  It takes a value inside the object, work with it, and then do the same thing with the next value until the iterable object is finished.

The WHILE loop is used when you dont how the number of repetitons. It works with a condition and it will repeat until the condition is false.

Recursion is when you call a function inside the same function. It can be used in special cases where you need to take a value from the function itself, this job can be done with FOR or WHILE loops but it´s shorter to write a recursion.

WSQ14 - Estimating e

This WSQ is easy if you understand what you have to do.
Here is the program working witha  precision of 35
If you want to see my code i will post the Github link here:
https://github.com/Jocapear/TC1014/blob/master/WSQ14.py

domingo, 22 de noviembre de 2015

Mastery 8 - Python conventions

Here i will show you what are the python conventions.
First of all, in python the indentation does matter. Indentantion is the way that python recognize if a code block has ended. For example:
if (a < b):
counter + =1
print (counter)
This is wrong because the IF has nothing inside it, instead it will always add 1+ to the counter because is at the same level as the IF. This is not C++ or JS where you can tell the program when your if ends with {}. The right way to do it is this:
if ( a < b):
     counter += 1
print(counter)
Also here are some tips to give a standar python style to your code:
-Just do one statement per line
Please don´t do this:
 
def ejercicio3(diccionario): return (("Homework promedio:", sum(diccionario["homework"])/len(diccionario["homework" ])), ("Quizzes promedio:", sum(diccionario["quizzes"])/len(diccionario["quizzes"])), ("Tests promedio:", sum(diccionario["tests"])/len(diccionario["tests"])))
Yes, this is a python program that actually works. As you can see it is very hard to read and you will make others to have a bad time trying to understand what you want to do. Don´t be a smart a** and put order to your code so that everyone can understand it. 
-Keep spaces between variables if necessary, just to make it more readable
-Build your functions on the top part of the code
I think that this is clear, declare your functions first and call them below. Try to declare all the functions you can on the top part and write the main program after that, this will make your code easier to read because if someone wants to follow the executuion order of your code he/she doesn´t have to look for a specific function on all the code; otherwise, he/she will know where the functions are and you will save him/her time.
-Keep your code simple
Here is an example of the last point:
a = input("Write a number"))
a = int(a)
for i in range(0,50)
     if i/a % 0:
          counter = counter + 1
b = counter*2
c = b+18
print (c)
This code is really simple but it can be done in less words and space, the proper way to do it is:
a =  int (input("Write a number" ))
for i in range( 0 , 50 )
     if (i/a) % 0:
          counter += 1
print(counter*2+18)


 

Masteries 24 and 27 - Creation and use of tuples and diccionaries

 A tuple is like a list and is used in the same way, the only difference is that a tuple cannot be modified or changed.
A diccionarie is a colleccion of anything that can be labeled with any other thing. It is used as a list too but to use a slot in the diccionarie you have to call the key instead of the value.
Here is some code where i show that i know how to use tuples and diccionaries:

viernes, 20 de noviembre de 2015

Quiz11

The first excercise was the same as a WSQ, i just had to change it a little bit. I got in trouble when i tried to make excercise 2 because i didn´t know how to work with files.
I researched a little bit on the book and then i played a little bit with what i found, to make the excercise i just had to combine my new knowledge with my list and string knowledge and that was all. At the end the excercise was very simple, i just had to know how to open a file.
Hint (thinks that you need to know to open a file):
open( "filename.txt")     This will return a file value so put it on a variable
file = open( "filename.txt")
file.read()        This read what is inside that file, returns whatever is written on it so i recommend you to put in on a variable too

If you want to see my code i will post it on Github, here is the link:
https://github.com/Jocapear/TC1014/blob/master/Quiz11.py

martes, 10 de noviembre de 2015

WSQ13 - Babylonian method

Hello there, this WSQ was an easy one; howhever, it was useful to research a bit from this page about the baylonian method http://mathlesstraveled.com/2009/05/18/square-roots-with-pencil-and-paper-the-babylonian-method/
Here is the complete formula of the babylonian method, you just have to do it on code. EZPZ
 Here is my program working:
If you want to see my code is on this link on Github:
https://github.com/Jocapear/TC1014/blob/master/WSQ13.py

viernes, 6 de noviembre de 2015

WSQ12 - Greatest common divisor

This WSW was a littel tricky because i had to look for the algorith in google. Here is the code working:
If you want to see my code in Hithub is on this link:
https://github.com/Jocapear/TC1014/blob/master/WSQ12.py