Quiz #11 pt. 2

--Originally published at Python learning

Here are the other five problems of quiz #11.

Number six was about the fibonacci sequence, also tasked in quiz # 8:

https://pythoncoursesite.wordpress.com/2017/03/05/quiz-8/

The next one was a function that returned the sum of the squares of a list of numbers:

suma_cuadradossuma_cuadrados_run

Problem eight was the same as the babylonian method exercise I did a few days ago:

https://pythoncoursesite.wordpress.com/2017/04/07/babylonian-method/

Next problem consisted of doing a function that finds the number of times the word “banana” is found in a string. My classmate Elu helped me with this, by giving me an idea of how to do it:

bananasbananas_run

And finally, the last question asked for a function that received two numbers and returned their greatest common divisor.

To resolve this, I found the following image in this page:

http://www.eljavatar.com/2014/05/Diagrama-de-Flujo-y-Pseudocodigo-para-Hallar-el-Maximo-Comun-Divisor.html, by a blogger called Andrés Mauricio Barragán Sarmiento “El Javatar”.

Thanks to this flow diagram I was able to write my code:gcdgcd_run

And that’s it! ❤


Quiz # 11 pt. 1

--Originally published at Python learning

Before the exam there was a quiz to be solved that would help us study and know more or less what type of problems could come in it. This quiz consisted of 10 questions, some of which were in previous quizzes or class exercises.

The first one we had already done. It was about getting the distance between two points, and I showed that in the following post:

https://pythoncoursesite.wordpress.com/2017/03/13/quiz-9/

The second asked to create a function that receives an n integer and creates a symetrical triangle in which the largest line has n elements. For example, if the number introduced is three, the function should create something like this:

T

TT

TTT

TT

T

At first I did it like this:triángulo

but my teacher told me I could’nt use the multiplication of strings to solve it, because this doesn’t really exist in other programming languages, so I had to think of another way to do it. I wrote two functions instead; one that would print each line, and one that would make the whole triangle:triángulo2triángulo_run

In the next exercise I created a factorial function, and I already have two posts about that:

https://pythoncoursesite.wordpress.com/2017/02/26/a-look-back-to-my-programs-pt-2-recursion/

https://pythoncoursesite.wordpress.com/2017/02/16/factorial-calculator/

Problem four was very simple. It just asked for a function that returns the average of a list of numbers:

promediopromedio_run

And in number five, a function that returned the minimum number of four:minimumminimum_run

Another way to do it is like this:minimum2minimum2run

I’ll show the other five programs in the next post ?


Babylonian Method!

--Originally published at Python learning

Last week the teacher gave us another problem to work on. As the title suggests, we had to create a function that would return the approximate square root of a number, using the Babylonian Method.

First I had to understand exactly how  the method works, so I watched this video:

However, I still didn’t understand how I would know when it was accurate enough. My classmate Elu explained it to me, telling me that it should be when the difference between two successive answers gotten from the formula differed only by 0.0001 or less.

Having fully understood the mathematics, I proceeded to write my code:

babylonianbabylonian_run


Video session with MacEwan students

--Originally published at Python learning

Last week I participated in a discussion with students of the MacEwan University in Canada, in which we exchanged our views about several apps. The first one we talked about was one that would inform the user which routes were the ideal ones to get to a certain place, and that connected you to other people also going there. Another one would tell you about the people you encountered on the streets so you could meet them.

A disadvantage we found in both of them was the fact that since you don’t really know the people you interact with, it can be considered dangerous. Some measures could be taken while creating it to ensure the safety of the people using it, but it is also the user’s responsability to be careful.

I gave my opinion about an app that provided immediate medical service in an emergency, which I thought could benefit people with a certain health problem. Also, I suggested it could also be constantly checking your vital signs and warning you if something is wrong. Besides, I said it was a good idea to have an app that allowed students to buy and sell used school books.

Finally, we shared our experiences about studying in our universities to make a comparison between Canada and Mexico, and we answered the doubts we had about each other’s culture, which was very interesting and educational.

Here are some pictures my teacher Ken took during the meeting! ❤

 


Dictionaries and 46 exercises: 20-22

--Originally published at Python learning

In Python we can create dictionaries that store values, like what we do with lists, except that instead of using an index to refer to something in it, we use other values called keys.

An example of a dictionary looks like this:

translate = {“hello”:”hola”,”my”:”mi”,”name”:”nombre”,”is”:”es”}

In this case, hello, my, name, and is are called keys, and each key has a corresponding value: hola, mi, nombre, and es.

I read about this topic in the book How to Think Like a Computer Scientist: Learning with Python by Allen Downey, Jeffrey Elkner, and Chris Meyers, which I had mentioned before in this post. You can check it out here.

I used dictionaries to solve the exercises 20-22 of the 46 showed in the past posts.

In number 20 I had to create a program that would take a list of English words and return a list of those words in Swedish, like this:

20

The get() method I used in the loop has two parameters: the first one is the key of the dictionary, the second one is the value to return if said key is not found.

20run

Number 21 was about determining the frequency in which each character of a string is used:

2121run

And 22 was to create an encoder & decoder of ROT-13 that would be able to decipher the following message:

Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!

2222run


46 exercises: 16-19

--Originally published at Python learning

Here’s another blog of four more of the 46 exercises I have been working with ?

In number sixteen the task was to write a program that would take an integer and a list of words, and return a list of the words that their length is longer than the integer:

1616run

The next one was similar to problem eight, in which the program recognized palindromes, only this time it had to work with sentences as well, like “Go hang a salami I’m a lasagna hog.”, “Was it a rat I saw?”, “Step on no pets”, “Sit on a potato pan, Otis”, “Lisa Bonet ate no basil”, “Satan, oscillate my metallic sonatas”, “I roamed under it as a tired nude Maori”, “Rise to vote sir”, or the exclamation “Dammit, I’m mad!”.

17

As you can see, I used the function string.punctuation in the string module to eliminate all punctuation from the sentence, as suggested from a friend of mine in the class. For this, I also used the replace() method to change it for an empty space (“”), as well as to eliminate spaces between words.

17run

Number nineteen was about creating a program that would tell whether a phrase was a panagram or not:

18

In match = set(alphabet) & set(letters) the program is comparing the two lists and finding elements in common, which it then puts into a list. I read about that in this page:

http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches

And this is how it runs:

18run

And the following exercise asked to write a program that generated all the verses in the popular song “99 bottles of beer” :

1919run119run2

I’ll continue showing more of these exercises in future posts ?


46 exercises: 11-15

--Originally published at Python learning

It has been a long time since I made a post about the 46 Python exercises, which I started in quiz #5 and quiz # 6, so here I am going to show the next five.

Problem eleven was about using a number n and a character c to generate a string of cs  n times. For example, if n = 3 and c = a, the answer should be “aaa”.

I did my code like this:

11

And it ran like this:

11run

The next problem was about taking a list of integers and creating its histogram:

1212run

Exercise 13 consisted on returning the maximum value of a list of numbers. For this, I used Python´s max() method.

1313run

The following one was about taking a list of integers and returning a list of their lengths:

1414run

Finally, in exercise 15 I had to make a program that would take a list of words and return the length of the longest one:

15 15run

And that’s it for now, I’ll be showing the rest of the problems I have made in other posts! ❤

 


Quiz # 9

--Originally published at Python learning

This week’s quiz consisted of creating a program to determine the distance between two points. You can check the mathematical way to do that here.

I did this quiz in two different ways. The first one was by defining a function with four different parameters -(x1, y1, x2,y2)- and asking the user for each value one by one:

quiz9

In the final output, I first had it displaying all the decimals in the answer. However, I didn’t like the way it looked, so I asked my teacher Ken how to make it so it only showed two decimals. We searched on the internet and found in this page the following image that shows how to do it:

General way of working of the string modulo operator

I did it, and this is how my program ran:

quiz9run

The second way I in which I solved this quiz was by defining the function with only two parameters, each containing two values: the x and the y of the point. To do this, I used lists, like this:

quiz9.2

And here is the way it ran:

quiz9.2run


Files!

--Originally published at Python learning

The problem for this week consisted of defining a function that opens a .txt file and counts the number of lines and characters in it ? .

To do it, I first researched about how to read and write files in python, and I found this very useful page:

https://www.digitalocean.com/community/tutorials/how-to-handle-plain-text-files-in-python-3

I then proceeded to start writing my code, but when I was almost done I faced a small problem, which was that it was counting the enters between lines as characters. I remembered that my teacher Ken had told us the day before about a function that eliminates additional spaces, but I couldn’t recall its name. I asked a friend from that class about it, and he reminded me of it: it is .strip()

I used said function, and lastly my program worked as it should ❤ :files

The text file I used was this:

texto

And the program ran like this:files_run