Monthly Archives: April 2015

WSQ 17

 

Scilab is a pretty cool and friendly open source software that alouds you to interact with a really sweet system of maths and simulations. It also lets you play around with 2 and 3D simulations which is amazing due to compatively issues in other programs. 

This software is amazing for engineering purposes because it alouds you to interact using tools to perform data analisis and modeling. Also, it features a set of tools for mechanics modeling and control systme duty. 

One of the best parts from scilab is that you can interact with it using external tools. And coding is really confortable, because its simple and clear.

 

WSQ 16

Link to github: https://github.com/carlosgreen/TC1017/blob/master/cars.cpp

This program needs to read a file which has a data base with various information relevant to cars. By reading the information, the program needs to print out the follow data:

  • average gas mileage in city (City MPG)
  • average gas mileage on highway (Highway MPG)
  • average midrange price of the vehicles in the set.

The program opens the file and then uses a string variable in order to read stuff from the subline (substring aka substr).

Also, the program has a if inside a loop, which permits the file to be read several times until getting the average midrange price of a vehicle. 

Here is how the code should look:

<iostream>

<fstream>

<sstream>

<cstdlib>

<string>

 

using namespace std;

int cars(string carstext){

  string range, city, highway;

  int counter=0;

  float range2=0.0, city2=0.0, highway2=0.0, sumcity=0.0, sumrange=0.0, sumhigh=0.0;

  ifstream cars;

  string line;

  cars.open(carstext.c_str());

  while(getline(cars, line)){

    if((counter%2)==0){

      range=line.substr(42, 47);

      range2=atoi(range.c_str());

 

      city=line.substr(53, 54);

      city2=atoi(city.c_str());

 

      highway=line.substr(56, 57);

      highway2=atoi(highway.c_str());

 

      sumrange=sumrange + range2;

      sumcity=sumcity + city2;

      sumhigh=sumhigh + highway2;

 

    counter++;

  }

  }

  cout<<“city mileage: “<<sumcity/93<<endl;

  cout<<“highway mileage: “<<sumhigh/93<<endl;

  cout<<“midrange vehicle price: “<<sumrange/93<<endl;

}

int main(){

  cout<<cars(“93cars.txt”);

 

}

 

User input (text based) in Python (basic)

                                                                                                              @PablO_CVi

An user input is to give values to the variables, this inputs are in the programs that need the user to interact with it, asking numbers to do operations or actios, to do this you have to type the variable you are going to give a value like this x=, then you have to use the comand input followed by parenthesis and in the middle of this any phrase to request the user to type a value. Example: x=input(“Ingresa un valor”)

Estimating-e

Reading and writing of files in Python

                                                                                                                     @PablO_CVi

For reading and writing of files in python, the first step is to get a file object, and how we do this? well using the open” function.

The open function has two parameter, the first one is the name of the file that we are creating and the other parameter is going to check if you are going to read(r) or write(w) the file. Now for write inside of the file.write the name of the object we created followed by a dot with the word “write” adn you open parenthesis where you are going to write the text.Also is important to mark that the end line character is given by “/n”.And for save it we have to close our object.

this is the syntax: file = open(“newfile.txt”, “w”)

                                           file.write(“hello world in the new filen”)

                                           file.write(“and another linen”)

                                           file.close() 

the object name is file and the text file is example.

Now we know how to use write(), I can explain read().

usisng read() is use it for read the text file. the syntax is very simple like in write() method, just where we wrote “w” now we have to write “r”.

this is the syntax of this:

                                              file = open(‘newfile.txt’, ‘r’)

                                              print file.read()

                                              file.close()

In this example show us the two parameters, the first one tell us what text file is going to open and the second parameter tell us what is going to do, in this case “r” means that is going to read.And print mean that when you are in the terminal will print the text that is in the file. In this case it will print:

                                              hello world in the new file
                                              and another line

We can also specify how many characters the string should return, by using
file.read(n), where “n” determines number of characters.

This reads the first 5 characters of data and returns it as a string.

this will print : hello

I have learned all of these here: http://www.pythonforbeginners.com/files/reading-and-writing-files-in-python.

Validated user input in Python

                                                                                                                      @PablO_CVi

This is very useless, because helps to make a good and clean code without too many error to fix, like this example:

while  True:
        try:
            q=int(input(“Please enter a non-negative integer number: “))
            break
        except ValueError:
            print(“this is not a integer number, try again: “)

 

while q<0:
        try:
            q=int(input(“this is not a positive number,try again: “))
        except ValueError:
            print (“this is not a integer number, try again:  “)
            while True:
                try:
                    q=int(input(“Please enter a non-negative integer number: “))
                    break
                except ValueError:
                    print (“this is not a integer number, try again: “)

 

When to use what type of repetition in a program

                                                                                                                      @PablO_CVi

The for statement iterates through a collection or iterable object or generator function.

The while statement simply loops until a condition is False.

It isn’t preference. It’s a question of what your data structures are.

Often, we represent the values we want to process as a range (an actual list), or xrange (which generates the values). This gives us a data structure tailor-made for the for statement.

Generally, however, we have a ready-made collection: a set, tuple, list, map or even a string is already an iterable collection, so we simply use a for loop.

In a few cases, we might want some functional-programming processing done for us, in which case we can apply that transformation as part of iteration. The sorted and enumerate functions apply a transformation on an iterable that fits naturally with the for statement.

 

If you don’t have a tidy data structure to iterate through, or you don’t have a generator function that drives your processing, you must use while.

 

 

Use of recursion for repetitive algorithms

                                                                                                                      @PablO_CVi

The use of recursion for repetitive algorithms means calling a function inside the function, like in my example.

here is my code: https://github.com/PablOCVi/Mastery/blob/master/Mastery21.py

Use of “else” with a conditional

                                                                                                                          @PablO_CVi

Introducing a conditional with an else.

Here is my code: https://github.com/PablOCVi/Mastery/blob/master/Mastery16.py

                           

Quiz11

Quiz11
enel primer programa teniamos que hacer un progrma que leyera numeros de un archivo y apartir de estos calcular su promedio y su derivacion estandar
En el otro teniaos que hacer que contara cuantas beces aparecia una palabra en un archivo.

here is the two codes: https://github.com/juanheuforico/WSQ/commit/b411d7416c2e79672fdceaea92e2d61da31a57bf