Quiz # 8

--Originally published at Python learning

The quiz for this week consisted on returning a certain term of the Fibonacci sequence.

To solve this problem there were two solutions. The first one was using recursion:

quiz8quiz8run

The second one was using a while loop:

quiz8-2quiz8-2run

And that’s it! ❤

I didn’t face any major problems with this quiz because I had already studied both of the topics I used. You can read about them in the links I inserted above! ?

 


Lycherels: Yo Soy 196!

--Originally published at Python learning

This week I worked with a problem called Yo Soy 196, which consisted of receiving a range of numbers from the user and determining the ones that were natural palindromes, non-lycherels and lycherels.

I didn’t know the definition of a lycherel number, so of course I started by reading about that:

https://en.wikipedia.org/wiki/Lychrel_number

After I fully understood what I had to do, I started writing my code. When I was done, it had a problem because it ran, but it displayed some numbers as lycherels that were not actually correct. I spent a lot of time trying to figure out why, and I asked several classmates for help. The person who at the end made me see my mistake was a fellow computer science student who took Ken’s class last semester (you can check out his blog here). Turns out, I was not initializing to zero my variable times every time it changed number, so the next one took the last value it got in the while loop ? .

I corrected it and it finally did the right thing! ?

lycherel

As you can see, I used the range() function in my for loop. It was my first time using it, and I learned how it works from this page:

http://pythoncentral.io/pythons-range-function-explained/

And lastly, this is how it worked:

lycherel_run

 


A look back to my programs pt 2: recursion

--Originally published at Python learning

To learn more about Python, I was reading the book How to Think Like a Computer Scientist: Learning with Python by Allen Downey, Jeffrey Elkner, and Chris Meyers -which you can get from here– and in chapter 4.9  I read about recursion.

Basically, recursion is when you create a function that calls itself. This solves some kind of problems that would otherwise require the use of a loop. In the book there was an example of this in chapter 5.5, in which a factorial function was defined using recursion. As perhaps you may remember (or not), I had already done a program that calculated the factorial of a number, and I showed it on my post called Factorial Calculator!

So, after I learned about this topic I decided to redo the program, but using recursion to see how it worked.

I did it like this:

factorial_corrected

Notice I also used .upper() to get the result to the question of whether the user wished to do it again or not. I talked about this method on my last post.

Anyway, the program runned exactly like the first one I made:

factorial_correct-run

Here is a useful tutorial to understand this better:

 


A look back to my programs

--Originally published at Python learning

A couple of days ago I had a particular session with my programming teacher, Ken Bauer, and I asked him to give me feedback on the programs I have been showing you in this blog. He pointed some things out to me, so I decided to redo two of them to show you what I learned. They are not major changes, but it´s the small details that can make a difference!

Anyway, the first change was made to problem number four in quiz # 5. In my original program, I used as options all the vowels both in lower and upper case, but Ken told me I could just change the input into upper case using .upper(), so then I didn’t have to take into account the lower case letters. You can read more about that in this page:

http://stackoverflow.com/questions/9257094/how-to-change-a-string-into-uppercase 

Here is how I modified my program:

quiz5-4corrected

And how it runned (just the same as the original one):

quiz5-4correct-run

The other change I made was to problem number eight, in quiz # 6. I was doing it using a conditional (if the word was the same as its reverse, then the answer was True), but that was not necessary since word == reverse is a boolean expression, and the answer to all boolean expressions is either True or False. So, I could have just done it like this:

quiz6-8corrected

And it should run the same as the original:

quiz6-8correct_run


Quiz # 6

--Originally published at Python learning

This quiz consisted of the next five exercises from the page of quiz # 5.

In problem number six I had to add and multiply the elements in a list. My programming teacher helped me do this by making me think first about the process, step by step, the program has to make in order to get the expected result.

When I finished, it looked like this:

quiz6-1

And runned like this:

quiz6-1run

Problem number seven was about reversing a string. I learned how to do that by looking into these two pages:

http://stackoverflow.com/questions/931092/reverse-a-string-in-python

https://docs.python.org/2/whatsnew/2.3.html#extended-slices

This is how I did it:

quiz6-2

And how it runned:

quiz6-2run

The following problem was about indicating if a word is a palindrome (like ‘radar’) or not. After doing problem seven, eight was easy:

quiz6-8

Here is how it runned:

quiz6-8run

Problem nine was about indicating if a value was or not in a list, without using the operator in. Here is how I did it:

quiz6-9

And how it runned:

quiz6-9run

And finally, problem number ten was about comparing two lists and indicating if they had an element in common. After doing problem nine, this one was simple enough:

quiz6-10

Here is how it runned:

quiz6-10run


Reading recommendation: Unlocking the Clubhouse

--Originally published at Python learning

Have you ever wondered why computer science is such a male-dominated environment? Does it have to do with biology, psychology or something more? What is actually behind the gender gap in software development careers?

Unlocking the Clubhouse: women in computing is a book by Jane Margolis and Allan Fisher that analyzes this subject with the purpose of developing a solution. They did this by conducting a five-year-long study in Carnegie Mellon University, interviewing several compuser science students, both male and female, in order to collect data.

I am currently reading this book and I am finding it very interesting, not only for girls in engineering but for everyone who is learning, teaching or working in a computing environment. If you are among any of these categories, go pick it up! And if you are not, go pick it up anyway!

You can learn more about the book here. And remember, knowledge is power!

book


Comments!

--Originally published at Python learning

In this post I’m going to make a quick and simple explanation about comments in our programs. First of all, when you are programming it is very useful to write lines of comments to explain what something does, both for you and someone else who reads it.

In python, we start our comments with the symbol “#”, and they finish when we change lines. When the program runs, they don’t appear and they also don’t make it do anything. They are just notes on the code we made, and they look like this:

#This would be a comment in python!

Here is an example:

comments

And here is how it should run:

comments-run

Notice how we don’t see the comment when we run it! ?


Lists!

--Originally published at Python learning

Another program I made was one that would ask the user to introduce 10 numbers, then it would store those numbers in a list and output the total, average, and standard deviation of them.

First things first, I learned about lists in python from this page:

https://www.tutorialspoint.com/python/python_lists.htm

Also, I didn’t actually know the formula for standard deviation, so I looked it up and found these useful pages:

https://www.mathsisfun.com/data/standard-deviation-formulas.html

http://www.gmatfree.com/module-999/gmat-standard-deviation/

Finally, this other page helped me by giving me an idea of how to do it:

http://stackoverflow.com/questions/15389768/standard-deviation-of-a-list

At the end, my program looked like this:

lists

And worked like this:

lists-run


Factorial calculator!

--Originally published at Python learning

In this exercise I had to ask the user for a positive number and display its factorial. The program had to continue running until the user indicated it to stop by answering “n” to the question “would you like to try again?”.

For this, I used if-else and a loop, both of which I have discussed in previous exercises (You can check it out specifically here!).

You can look at the code I made for this:factorial

And how it runs:

factorial-run


On to functions!

--Originally published at Python learning

Remember by any chance my post fun with numbers? (If not, you can check it out here!) Well guess what, I did it again ? ,except this time I wrote a function for every operation. It was great practice on creating and calling functions, which I had already done in previous posts.

Without any further do, here is the changes I made on my original code:numbers2-0

I know it looks long and more complicated, but like I said, it was just for practice!

Anyhow, here’s how it runs:

numbers2-0run