Counting on Hackerman’s House

The other day my friend Paco and I were planning to work together, so we saw Ken’s challenge about counting frequency of words; however, at the time we did not know exactly how to start. By the time we decided to do something about it, a girl from the other TC101 (Dani Güemez) had already solved the challenge. But as we read and tested the code, we found a minor flaw: when counting the words in a text file, it separated and counted them by lines instead of counting them all. Here is the orginial code if you want to check it:

https://github.com/Guemez/TC1014/blob/master/countwords.py

Now, the code works fine for counting words in strings; thus, after a little bit of thought, we concluded that the best way to fix that minor error was to make the code convert the files into strings, and then count the words in them. And worked pretty fine. But we went even further and find the way to sort the counter by values. Here is the code:chidori

As you can see, we respected Dani’s format. And here is the final result, with the text we were using:lyrics

We are satisfied with the outcome.

 

This is the link to my friend Paco’s blog: https://firstprogrammingsteps.wordpress.com/

Also be sure to check out Dani’s blog: https://danigguemez.wordpress.com/

 

 

WHILE waiting FOR a new entry

So, this is really easy to understand; when to use a WHILE loop and when to use a FOR loop. So it basically breaks down to what you know and what you do not know; if you do not know the extension or range of your data structure, then use a while; like for example, when you ask the user for an integer, you really do not know if the user is going to give you a number, so you create a while loop that repeats itself until the users gives you an integer:while

But if you actually know the extension of your data structure (it could be a list, or a tuple), use a for:

for

As simple as that.

BubbleSorting things

So one of Ken challenges required arranging numbers in order; I knew how because it was not the first time I have used a BubbleSort, but one thing that I never learned was to arranging list in alphabetical order… thus, here are my codes.

Bubble sort for numbers, it was super easy:

random

And the output:

bubble

And now for the alphabetical sort:

bubble

And the ouput:

alph

Easier than I thought.

Random Numbers

So I decided to take another exercise from kencourses, and it was kind of tricky, but I could complete it. The challenge was creating a code that selected a number between 1 and 100, and the user had to guess the number, no matter the number of attempts. The code gives you clues as you keep trying:

numbers2

And here is the result:

random

Check out the code at my Github:

Kenxercises/Random.py