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