October 27, 2014

Week of 10/19/14 - Python Part 07: Practice

    The codeacademy course now has me doing many practice problems. Just like how last week I was doing prime numbers and reversing strings, now what I did was removed vowels from strings, calculated scrabble scores, censored using a given word to censor out, counted the number of a given item in a list, removed odd numbers from a list, and multiplied all the items in a list. What I was doing when working with strings was either putting each letter or each word into different entries of a list and then .join()-ing the list to return a string. However, from browsing the user forum, I discovered that I could have created a blank string and then moved every letter into there, like how I did here. For every letter in a given string, I would check whether it was a vowel or not, and then I would add it to the string.


    However, this would not work for some cases, such as when I was censoring out a given word. Instead, what I had to do was use the .split() function to split the string into a list of words. Then I had to create a blank list to be returned. I then looped through the list, where every word that was not the word that I was censoring was added to a blank list. If the word was the word I was supposed to censor, for every letter in that word I would add a "*" character to a string. Once it was done doing that, it would append that to the final list. The .join() function would then be used to convert the list into a complete string.

No comments: