Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101winter2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101winter2015/wp-includes/feed-rss2.php on line 8
Manuel Madrigal’s Articles at Courses by Ken https://kenscourses.com/tc101winter2015 Facilitator of Learning Experiences Tue, 28 Apr 2015 04:14:57 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ WSQ17. The movies https://kenscourses.com/tc101winter2015/2015/wsq17-the-movies/ Tue, 28 Apr 2015 04:14:57 +0000 https://manuelmadrigal.withknown.com/2015/wsq17-the-movies Continue reading ]]>

Before saying anything about the coding, I have to say that this was the most difficult program since I entered this course, I saw it as a challenge and after 5 hours I managed to complete it.

For the program I created three functions.

The first function is in charge of taking the text file a making a dictionary where the keys are the name of the movies and the value of each key is a list containing the name of the actors who acted in that movie.

The second function called check_movies receives as a parameter 2 movies and then calls the first function to get the main dictionary, and creates a second dictionary but rearenged to have the name of the actors as keys and the movies  in which they have participated as the values for each key, Then the function goes over each key of this second dictionary checking the lenght of the list of movies assigned for the actor to decide to which category of the three the actor belongs.

A third function called check_coactors receives as a paremeter the name of an actor and then, as in the second function, it calls the first function to get the main dictionary. After that, it creates a list containing the movies where the actor has participated, then, it creates a list containg the actors of each of these movies, and finally creates a dictionary to check that none of this actors is repeated in this list, and the keys of the dictionaries, which are the actors are passed to a third list that is going to be showed to the user

After these three function I created the interface with the user to check what is he going to request.

Here is an screenshot of how it loks the program running on the terminal:

Here is my code for the solution:

https://github.com/Manuelmv94/TC1014/blob/master/wsq17.py

 

]]>
https://creativecommons.org/licenses/by/4.0/
QUIZ 11 https://kenscourses.com/tc101winter2015/2015/quiz-11-2/ Mon, 27 Apr 2015 21:06:26 +0000 https://manuelmadrigal.withknown.com/2015/quiz-11 Continue reading ]]>

For the question 1 I created a function that opens the file and by using a for loop it goes over each line of the text file adding the numbers to a list and then use that list to to some operations.

For the question 2 I created a function that opens a text file and then by using a for loop goes over each line checking how many times is the word in that line.

Here are my solutions for the quizz 11:

Question 1:https://github.com/Manuelmv94/Quizz11/blob/master/q1.py

Question2:https://github.com/Manuelmv94/Quizz11/blob/master/q2.py

]]>
https://creativecommons.org/licenses/by/4.0/
BONUS QUIZ. ECOS https://kenscourses.com/tc101winter2015/2015/bonus-quiz-ecos-3/ Thu, 23 Apr 2015 21:44:47 +0000 https://manuelmadrigal.withknown.com/2015/bonus-quiz-ecos Continue reading ]]>

Here is a screenshot proving I completed the ECOS

]]>
https://creativecommons.org/licenses/by/4.0/
Quiz10 https://kenscourses.com/tc101winter2015/2015/quiz10-5/ Thu, 23 Apr 2015 21:06:58 +0000 https://manuelmadrigal.withknown.com/2015/quiz10 Continue reading ]]>

The Question 1 of the quizz was just to create a function that received a list and returned the sum of the elements of the list that were evenly divisible by 3. To accomplish the objective, it was just matter of creating a ”for” loop that goes over each element of the list checking the condition, If the condition was true, then, that element was added to a variable containing the sum.

For the question 2, we had to create a function that received to vectors (lists) and returned the dot product. For that, we have to check first if the vectors have the same lenght (using ‘len’), if the lenght of the two vectors is different, the function has to return an error, but if it is the same returns the dot product. To get the dot product of the two vectors I created a ‘for’loop with a range of the lengh of the vectors. the variable changing of the loop was the index of both lists. So I created another variable containing the sum, which for every cycles gets added the product of the corresponding index element of the list.

Here are my solutions for the quizz:

Question 1: https://github.com/Manuelmv94/Quizz10/blob/master/q1.py

Question 2: https://github.com/Manuelmv94/Quizz10/blob/master/q2.py

 

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ16. Cars https://kenscourses.com/tc101winter2015/2015/wsq16-cars/ Mon, 20 Apr 2015 19:58:38 +0000 https://manuelmadrigal.withknown.com/2015/wsq16-cars Continue reading ]]>

For this WSQ we had to create a program that read a text file and printed the average of some specific data contained in it.

In order to accomplish this WSQ, first, we had to open and read this text file from our program, which I explained in this post: https://manuelmadrigal.withknown.com/2015/mastery-30-reading-and-writing-of-files-in-python

The, by using a for loop we can go over the text file, line by line, collecting the data we care about by using a property of strings, with which we can get just the part of it that we desire.

Finally we get the average of the data by dividing the sum of all the values by the number of cars.

Here is the link to my program:https://github.com/Manuelmv94/TC1014/blob/master/wsq16.py

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 18. Nested conditional Statements https://kenscourses.com/tc101winter2015/2015/mastery-18-nested-conditional-statements/ Fri, 17 Apr 2015 17:17:36 +0000 https://manuelmadrigal.withknown.com/2015/mastery-18-nested-conditional-statements Continue reading ]]>

For this mastery I created avideo where I explain what Nested conditional statements are and how to use them.

Here is the link to my video: https://www.youtube.com/watch?v=RDteRZ56dPQ

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 27. Creation and use of Dictionaries https://kenscourses.com/tc101winter2015/2015/mastery-27-creation-and-use-of-dictionaries/ Fri, 17 Apr 2015 16:56:14 +0000 https://manuelmadrigal.withknown.com/2015/mastery-27-creation-and-use-of-dictionaries Continue reading ]]>

Dictionaries are another helpful data built into python.

Unlike other data types which are indexed by range of numbers, dictionaries are indexed by keys, which can be any immutable value, such as strings, numbers or even tuples (Lists wouln’d work for keys because they can always be changed).

The syntax for a dictionary is the following:

>>Dict={‘key1’=value1, ‘key2’=value2, ‘key3’=value3}

If we wanted to add a new value into the dictionary, we have to add it by entering a new key, for example:

>>Dict[‘key4’]= value4

And we would assume that this new value is added to the end of the dictionary, but unfortunately this wont happen, but it doesnt matter, because even if this new value is stored randomly into the dictionary we can always find it by his key.

For example, if we want to print value3, we type the following:

>>print (Dict[‘key3’])

and as ‘key3’ is the key for value3, value3 will get printed.

In the same way we can modify the value of a key and we can even add as many values we want for only one key by adding a list as the value of the refered key. For example, if we wanted to modify the value of ‘key1’ for a list containing 3 values, the syntax would be the following:

>>Dict[‘key1’]=[val0,val1,val2]

And if we wanted to acces specifically to the second value of a given key (key1, for example), the syntax would be the following:

>>print (Dict[‘key1’][1])

Also we can add a dictionary as the value of the key of a main dictionary, it is just matter of the needs of your program.

If you want to learn more about dictionaries, you can go to this link: https://docs.python.org/2/tutorial/datastructures.html

 

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 26. Creation and use of strings https://kenscourses.com/tc101winter2015/2015/mastery-26-creation-and-use-of-strings/ Fri, 17 Apr 2015 04:23:12 +0000 https://manuelmadrigal.withknown.com/2015/mastery-26-creation-and-use-of-strings Continue reading ]]>

Strings are bits of text, and the syntax for them is anything written between quotes, as the following example:

>> string1=”Hello World”

In this example we are assigning the string to a variable called string1 which we can print, as we have learned in later posts, but printing it is not the only action we can do with strings, there are many more operations we can perform with strings and I will show you some of them in this post.

One operation that we can do is getting to know the lenght of our string by the len() statement. Example:

>>print len(string1)

This will print 11 because the string is 11 characters long, including spaces.

 

Another operation with strings is performed by the index staement, this will tell us the position of a given letter. Example:

>> print string1.index(“W”)

For this example, it will get printed 6, because W is in the 6th position, if we start counting from 0.

 

Also, a cool operation that we can do with strings is printing just a part of the whole string, for example:

>>print string1[4:8]

This will print “o Wo” because is the part of the string that goes from the 4th letter (starting from 0) to the 7th letter ( the end boundary it is not taken into account)

 

Another interesting functions are the following:

>>print string1.upper()
>>print string1.lower()

Which will print a new string with all letters converted to uppercase and lowercase respectively.

 

There are so many other operations that we can do with strings, such as concatenation, multiplication, etcetera.

If you are interested in learning more about strings, you can check this link: https://docs.python.org/2/library/string.html

 

 

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 30. Reading and Writing of files in python https://kenscourses.com/tc101winter2015/2015/mastery-30-reading-and-writing-of-files-in-python/ Fri, 17 Apr 2015 02:21:18 +0000 https://manuelmadrigal.withknown.com/2015/mastery-30-reading-and-writing-of-files-in-python Continue reading ]]>

In this post we will se how to use read() and write() methods in order to read and write files.

First of all we have to create the file in which we are going to write the text, for that, we have to create an object to which we are going to assign the “open” function.

The open function has two parameters, the first one is the name of the file you are creating and the second one is to check if it would be destinated to write or read. After the file is created we can write in it by first typing the name of the object we created, followed by a dot and the “write” function which has as a parameter the actual text you want to write in this new file, also is important to mark that the end line character is given by “/n”. At the end we have to close our object in order to save memory; this can be done by typing the name of our object followed by dot and the function close() without any parameter. Lets see the next example:

In this case we created an object “fw” with which we created a text file called “example”, in which we wrote some stuff. By running this code from our terminal the next file is created:

  And if we open it we can se that what we wrote in our code was actually written in this new file.

 

Now that we know how to use the write() method to write in a file, lets se how to use the read() method to read it.

As in the write() method we hace to cretae a new object to which we can assign the open function, but in this case, we are going to use the second parameter to read the file.

Using the read() method we have to create a new cariable to which we can assign the object, and then we can do whatever we want with this new variable, such as print it. Also, as in the write() method, we have to close our object in order to save momory from out computer. Lets see the next example:

In this example we are creating an ‘fr’ object with a first parameter ‘example.txt’ which is the file we want to read, and a second parameter ‘r’ indicating that we are going to read it.

the text variable is getting the actual text from the file, and then by print it we can see what was in it. By running this code from our terminal we get the following:

Which as we can see, is exactly what we had written at the beginning by using the wite() method.

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 29. Validated User input in Python https://kenscourses.com/tc101winter2015/2015/mastery-29-validated-user-input-in-python/ Thu, 16 Apr 2015 19:30:24 +0000 https://manuelmadrigal.withknown.com/2015/mastery-29-validated-user-input-in-python Continue reading ]]>

There are some cases where we create a function in our program destined to receive an special type of value. Here is where we want to ensure that the user enters the correct value and not any other type that will cause our program to crash, thats why we validate the user input by using some tools that I will show you.

Lets see the next example:

In this case we want our user to enter a positive integer number.

There are 3 possible cases in this input:

a) The user enters a positive integer number: In this case, the input will be correct and Python will get out of the loop, continuinh with the rest of the code.

 

b) The user enters a negative integer number: In this case the input will be validated with the built-in type “int” function and it will not return any error, but as it is not positive, it will enter in the loop, asking the user to enter a positive number instead of a negative one.

 

c) The user does not enter an integer number: Any value different from an integer value will return “ValueError” because we are using the built-in type “int” function. As the program tries this value and returns the “ValueError” it will passes to the “except” statement, where it will tell the user his mistake and will send the user to the beginning again, entering in the “try” statement.

 

This is just an example of validating user inputs, but there may be many variants, depending on what value you want to get from the user. You can use as many different of built-in type functions as you wish, it is just matter of playing with the “try” and “except”statements.

 

]]>
https://creativecommons.org/licenses/by/4.0/