WSQ14 Scilab

“Scilab is an open source, cross-platform numerical computational package and a high-level, numerically oriented programming language. It can be used for signal processing, statistical analysis, image enhancement, fluid dynamicssimulations, numerical optimization, and modeling, simulation of explicit and implicit dynamical systems and (if the corresponding toolbox is installed) symbolic manipulations.”

Source: https://en.wikipedia.org/wiki/Scilab. Yep, I used Wikipedia. I wasn’t in the mood of doing a long research to define Scilab.

Ok. Let’s start with the blog post.

Scilab is a really useful tool that will help me from NOW to make my math calculations. Besides I’m not quite good at math, It’s funny to use a math software with the programming knowledge that we already have.

Next Saturday is my math Final Exam. I’m definitely using this to check my exercises while I study.

scilab

Tanks Ken to give us alternative tools and tips to survive our life in college, not just in your subject, but all the others.

 

Quiz 7

Hey there! Let’s do the quiz #7.

Quiz 7 is about this:

“Create a function called dot_product that receives two lists of numbers (say list1 and
list2). The function returns what is the dot product of the two lists.

For full marks, if the lists are not the same size, then the function should return the
special value of NaN (which represents not a number). For Python, you can create this value with the expression: float(‘NaN’)”

Let me explain what is the dot product:

“In mathematics, the dot product, or scalar product (or sometimes inner product in the
context of Euclidean space), is an algebraic operation that takes two equal-length
sequences of numbers (usually coordinate vectors) and returns a single number.

Algebraically, it is the sum of the products of the corresponding entries of the two
sequences of numbers” Source: http://kenscourses.com/tc101winter2016/2016/04/quiz-07/

quiz7

Heeere’s my code. I’ll explain it step by step.

First of all we define our function “dot_product” that will determine if the lists that my user gave me are the same length or not.

If not, the program will return the NaN result. This will tell the user that he/she is doing something wrong.

Otherwise, the code will do the dot product.

Here’s the link to my code. Enjoy coding!!!! Byeeee!

WSQ13 Exam 2

Good morning! This blog post will explain the exercise that we did for Exam 2.

Exercise number 1

Is about get the distance between two points. The code is the following one:

quiz1parcial

O line 1 we import the “sqrt” function form the “math” library, we’ll need it because the main formula to get the distance is .

From line 2 to line 5, we ask the user for the four values to get the location of the points. Two values for “x” and two values for “y”.

Then, let’s convert them all to floats so we can manipulate them later.

The magic comes out in line 11, where we ‘translate’ the algebraic formula to a code. As you can notice, we use the ‘sqrt’ function on line 13, and finally, let’s print the result.

Exercise number 2

quiz2parcial

Can you see the red chart? Yep, don’t do that. It was my failing code at the test.

 

 

Quiz 6

Heeey there!!!!! Here’s my 6th quiz. Yup. I know It’s pretty late for it. I will reflect that on my rubric.

Aaaanyway, the fact is that the instructions were these: “Write a function to calculate the greatest common denominator of two positive integers using Euclid’s algorithm.”

Here’s the page if you want to check what’s the Euclid’s algorithm about: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm

quiz601

So, here’s my code. Do you see the red chart? Dont’t do something like that. Your code won’t work.

I visited Emanuel’s blog and it was really helpful. I was kind of lost with this code.

I’ll be submitting a flowchart to explain it better.

Here’s my code if you want to check it out. Have a nice day!!!

https://drive.google.com/open?id=0BxQYx0WBiNoRYjZCeDJQemdXZTA

🙂

 

 

WSQ12 Word count

This code is not really hard. It’s about asking the user for a word and count how many times does it appear in a .txt file.

Let’s start.

I’ll explain the code that Ken helped me with and then I’ll explain my code.

To count words in a text file you need to consider several things:

The word might have lower and upper case letters on it, or it could be next to other word without any space, so the “split” function won’t help a lot.

So here’s Ken’s code:

wsq1202

As you can see, our big_string has the word “bananas”, but not al the “bananas” have the same structure. So first of all. we shall convert every word to lowercase (line 3).

To give more flexibility to the program, we convert the letters of BaNana to lowercase.

Then, python has this beautiful function that will find the word that we’re looking for (line 7).

After that, what we’ll do is look for the word checking each character, and while the string is not empty, the program will check it and find all the bananas. Kind of funny, right? loved this exercise.

Aaaaand here we go. Here’s my code:

wsq1203

Check that I added an input to catch the wanted word. Also, as my file has several lines, it’s obvious that I need to evaluate them all. That’s why I inserted the “for” loop (line 5)

That’s all for today, fellows. Have a nice day.

(WSQ11) Yo soy 196

After a brain-burning-process, I finally got it. After some appointments to Ken’s office and tons of coffee, Yo soy 196 is here.

wsq1101

First of all, we shall create functions that will help us to find which number is a palindrome, which one can become one and which one is a Lycherel number.

The second part is a BIG piece of code. Here it is:

wsq1102

From line 13 to line 21 I created three lists to add the values depending on it’s category. And of course, I wrote the inputs to get our upper and lower bound to evaluate the range between them.

And trust me, there´s no better way to explain the following lines than with a flowchart, a magical flowchart:wsq1103.png

yosoy196image

Last lines are written to print the results.

wsq1103

 

Lists (WSQ10 )

Hey there! We’ll be developing the WSQ10 assignment in this blogpost.

This code is not difficult but is longer than the rest. Here’s the description:

“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.”

Before we get started, let’s define the average and the standard deviation.

  1. Standard deviation: Is a measure that is used to quantify the amount of variation or dispersion of a set of data values. (Source: https://en.wikipedia.org/wiki/Standard_deviation)
  2.  Average: Is the sume of all the elements of a list divided by the number of elements.

Now, let’s start.

list01

We’ll build this code in three parts. The yellow one is about functions, the green one is the part that will ask the user for the 10 numbers and append them in a list, and the red one will print the results, calling the functions.

The first function is really easy, we just sum the elements form the list in line 20. The second function is the most difficult one. The formula to get the standard deviation, we need to compare each value with the total average, then square that sum, and at the end, divide everything by the number of elements on the list, in this case, 10.
The last function will be the average, we just sum all the elements divided by 10.

Line 27, 28 & 29 are going to call our three functions and print the results.

list02

Here are the results. The program asks the user correctly for 10 numbers and then the list gets printed. after that, as we were waiting, we get the total, the average and the standard deviation.

Here’s my code. If you have a suggestion about how to improve my code, pleas comment bellow.

Have a nice day!!!

Quiz 5

Hello everyone! Quiz 5 has 2 really fun exercises to develop,  here is the description:

  1. Create a function called is_palindrome which receives a string as a parameter and returns true if that string is a palindrome, false otherwise. Remember that a palindrome is a word that is the same forward or backward. For full points your function must ignore case and must work with any character (not just letters). So (“Dad$dad” is a palindrome even though the D is capital and d is lower case).
  2. Create a function called find_threes that receives as a parameter a list of numbers and returns the sum of all numbers in that list that are evenly divisible by 3.  So if the list was [0,4,2,6,9,8,3,12], the function would return 30 (0+6+9+3+12)

Exercise 1

Here’s the first code. Don’t freak out, I’ll explain each line.

quiz501

First, we define the function. Remember that capital letters shouldn’t be considered, so we convert all the letters to lowercase. I got the advice of how to do this here.

Then, in line 4, we reverse the string. stackoverflow might help you out with this step.

Then an ‘if’ and an ‘else’ clause are inserted, because we need to compare if the string is equal to the reversed one, and determine if it’s a palindrome or not.

The rest is a piece of cake. We need an input (line 10) to ask the user for the string and we print the function.

Now let’s try the code. quiz502

GIVE ME 5, it worked.

 

Exercise 2

This code is really easy. Let’s develop it fast and efficiently.

quiz6

I’m creating a list so I can save every number divisible by 3 in that list. This list will be used in line 12, where our function called ‘find_threes’ will do it’s work.

In line 2, we have a ‘for’ loop that will make the selection of 8 numbers given by the user. As you can notice, each number is evaluated with the %  sign with an ‘if’ clause, that decides if a number is divisible by 3 or not. I found how to do this in stackoverflow.

In line 6 we notice that if the number is a multiple of 3, it’ll be added to ‘the_numbers’.

Let’s check if this works.

quiz61

YUP, it worked.

Here are my codes if you want to check them out. Have a nice week!

Exercise 1

Exercise 2

 

Quiz 4

Hello everyone, quiz 4 will be about this:

“Create a function called euler_calc with a single parameter precision. The value of precision is used to determine when to stop calculating. Your calculation will stop when the two consecutive values estimating e differ by less than precision (remember to use absolute value when calculating the difference between two values here)”

First of all, we need to know what the euler number is. According with the internet’s messiah, Wikipedia, “The number e is an important mathematical constant that is the base of the natural logarithm. It is approximately equal to 2.71828,and is the limit of (1 + 1/n)n as n approaches infinity, an expression that arises in the study of compound interest. It can also be calculated as the sum of the infinite series”

The following image describes the formula:

euler

Let’s start with the code.

euler01

In line 3, I’m defining the function with the variable (pre) that means ‘precision’, which will be the parameter of precision that the code will allow.

Then, let’s proceed with the math: I’ll explain form line 7 and then we’ll see why the ‘x’, ‘e’, and ‘dif’ variables are defined like that.

Ok, line 6 says that as long as the difference is bigger than the precision, our code will keep running. Then, we add a new value: ‘e_viejo’ to assign the las value that ‘e’ had, (We ought to be conscious that 2 euler variable are needed so we can compare their values and decide if it matches with the ‘precision’ ok?)

As expected,  we need to import ‘factorial’ form the ‘math’ library. I’ll add that in line 1. euler 02

Aaaand we make the operations in line 9. Line 10 will increase the value of ‘x’ as the While loop gets executed over and over again. To finish the loop, we define the value of ‘dif’ so the condition can be evaluated.

As I told you, now I’ll explain line 4, 5 and 6. The ‘x’ and ‘e’ variables need to have a ‘0’ value before they get into the loop. And it’s REALLY important that the ‘dif’ variable has a bigger number than ‘pre’ because if not, the code won’t run.

Now let’s prove the function.

euler 03

YAAAAY it worked.

Here’s my code, have fun with it. See you later…!

On to functions (WSQ08)

Hellooooooo! I’m about to do the most enjoyable code of the semestrer. It’s enjoyable because  we’ll copy and paste most part of the code from WSQ03.

Do you remember WSQ03? If not, here’s the post.

Let’s start.

First of all we need do define all the functions.

otf2

We need a function for each result. Don’t forget to indicate the type of object for each variable:

otf1

After that, we’ll copy and paste the WSQ03 code. You can buid it from the ground, it’s not complicated, but of course is easier to copy and paste.

Theeeeen we’ll edit the part that prints the result replacing the mathematical process by the corresponding function.

Check line 16, 18 and 20.

The final result would be something like this:

otf3

That’s all for today. Here’s my code,as always. Have a nice weekend! 😀