46 Simple Python Exercises (26.-30.)

--Originally published at Elu's Blog

26. Using the higher order function reduce(), write a function max_in_list() that takes a list of numbers and returns the largest one. Then ask yourself: why define and call a new function, when I can just as well call the reduce() function directly?

Captura de pantalla 2017-02-21 a la(s) 15.28.30.png

Captura de pantalla 2017-02-21 a la(s) 15.28.49.png

27. Write a program that maps a list of words into a list of integers representing the lengths of the correponding words. Write it in three different ways: 1) using a for-loop, 2) using the higher order function map(), and 3) using list comprehensions.

Captura de pantalla 2017-02-21 a la(s) 15.46.07.png

Captura de pantalla 2017-02-21 a la(s) 15.46.19.png

28. Write a function find_longest_word() that takes a list of words and returns the length of the longest one. Use only higher order functions.

Captura de pantalla 2017-02-21 a la(s) 15.57.26.png

Captura de pantalla 2017-02-21 a la(s) 15.57.34.png

29. Using the higher order function filter(), define a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.

Captura de pantalla 2017-02-21 a la(s) 16.14.54.png

Captura de pantalla 2017-02-21 a la(s) 16.15.13.png

30. Represent a small bilingual lexicon as a Python dictionary in the following fashion {"merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"år"} and use it to translate your Christmas cards from English into Swedish. Use the higher order function map() to write a function translate() that takes a list of English words and returns a list of Swedish words.

Captura de pantalla 2017-02-21 a la(s) 16.48.19.png

Captura de pantalla 2017-02-21 a la(s) 16.48.40.png


46 Simple Python Exercises (21.-25.)

--Originally published at Elu's Blog

21. Write a function char_freq() that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a Python dictionary. Try it with something like char_freq("abbabcbdbabdbdbabababcbcbab").

Captura de pantalla 2017-02-14 a la(s) 20.27.36.png

Captura de pantalla 2017-02-14 a la(s) 20.28.13.png

22. In cryptography, a Caesar cipher is a very simple encryption techniques in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. ROT-13 (“rotate by 13 places”) is a widely used example of a Caesar cipher where the shift is 13. In Python, the key for ROT-13 may be represented by means of the following dictionary:

key = {'a':'n', 'b':'o', 'c':'p', 'd':'q', 'e':'r', 'f':'s', 'g':'t', 'h':'u', 
       'i':'v', 'j':'w', 'k':'x', 'l':'y', 'm':'z', 'n':'a', 'o':'b', 'p':'c', 
       'q':'d', 'r':'e', 's':'f', 't':'g', 'u':'h', 'v':'i', 'w':'j', 'x':'k',
       'y':'l', 'z':'m', 'A':'N', 'B':'O', 'C':'P', 'D':'Q', 'E':'R', 'F':'S', 
       'G':'T', 'H':'U', 'I':'V', 'J':'W', 'K':'X', 'L':'Y', 'M':'Z', 'N':'A', 
       'O':'B', 'P':'C', 'Q':'D', 'R':'E', 'S':'F', 'T':'G', 'U':'H', 'V':'I', 
       'W':'J', 'X':'K', 'Y':'L', 'Z':'M'}

Your task in this exercise is to implement an encoder/decoder of ROT-13. Once you’re done, you will be able to read the following secret message:

   Pnrfne pvcure? V zhpu cersre Pnrfne fnynq!

Note that since English has 26 characters, your ROT-13 program will be able to both encode and decode texts written in English.

Captura de pantalla 2017-02-14 a la(s) 21.13.06.png

Captura de pantalla 2017-02-14 a la(s) 21.12.08.png

23. Define a simple “spelling correction” function correct() that takes a string and sees to it that 1) two or more occurrences of the space character is compressed into one, and 2) inserts an extra space after a period if the period is directly followed by a letter. E.g. correct("This   is  very funny  and    cool.Indeed!")

Captura de pantalla 2017-02-15 a la(s) 07.26.49.png
Captura de pantalla 2017-02-15 a la(s) 07.27.05.png
Captura de pantalla 2017-02-15 a la(s) 08.15.03.png
Captura de pantalla 2017-02-15 a la(s) 08.15.26.png
Captura de pantalla 2017-02-15 a la(s) 09.02.22.png
Captura de pantalla 2017-02-15 a la(s) 09.03.18.png
Continue reading "46 Simple Python Exercises (21.-25.)"

WSQ05 – Functions (Python3)

--Originally published at Elu's Blog

For this assignment these were my instructions:

You will go back and do WSQ01 – Fun with Numbers again.

But this time, write a function for each calculation. Each function should define two parameters (in this example of type int) and return the correct value as an integer as well.

You main program needs to ask the user for the input and then call each function to calculate the answer for each of the parts.

This is what I came up with:

Captura de pantalla 2017-02-14 a la(s) 10.14.49.png

Captura de pantalla 2017-02-14 a la(s) 10.16.01.png

As we can see in this program, we first, take two numbers and send them to different functions that return the result of their respective calculation.

If you want an explanation on how functions work, here is a post I made about them:

https://elusblog.wordpress.com/2017/01/31/functions-python3/


WSQ04 – ‘For’ Loops (Python3)

--Originally published at Elu's Blog

For this assignment these were my instructions:

Write a program that asks for a range of integers and then prints the sum of the numbers in that range (inclusive).

You can use a formula to calculate this of course but what we want you to do here is practice using a loop to do repetitive work.

For example, the sum from 6 to 10 would be 0 + 6 + 7 + 8 + 9 + 10

This is what I came up with:

Captura de pantalla 2017-02-14 a la(s) 09.39.47.png

Captura de pantalla 2017-02-14 a la(s) 09.40.59.png

As we can see in the program above, first, we ask for two integers and then we assign the zero value to the “result” variable. After that we use a ‘for’ loop  for adding every number in the range that we assigned.

To use a for loop, we need to call it by writing ‘for’, then a new variable that will represent each thing in the range that we give and then we write the range. the range in a for loop can be strings, lists or traditional ranges.

Alternative Explanation

Python 3 Programming Tutorial – For loop by sentdex


46 Simple Python Exercises (16.-20.)

--Originally published at Elu's Blog

16. Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.

Captura de pantalla 2017-02-13 a la(s) 10.47.56.png

Captura de pantalla 2017-02-13 a la(s) 10.48.06.png

17. Write a version of a palindrome recognizer that also accepts phrase palindromes such as “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!”. Note that punctuation, capitalization, and spacing are usually ignored.

Captura de pantalla 2017-02-13 a la(s) 14.23.08.png

Captura de pantalla 2017-02-13 a la(s) 14.23.47.png

18.A pangram is a sentence that contains all the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to check a sentence to see if it is a pangram or not.

Captura de pantalla 2017-02-13 a la(s) 14.45.30.png

Captura de pantalla 2017-02-13 a la(s) 14.46.17.png

19. “99 Bottles of Beer” is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song’s simple lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.

Your task here is write a Python program capable of generating all the verses of the song.

Captura de pantalla 2017-02-13 a la(s) 14.59.36.png

Captura de pantalla 2017-02-13 a la(s) 15.00.04.png

Captura de pantalla 2017-02-13 a la(s) 15.00.55.png

20. Represent a small bilingual lexicon as a Python dictionary in the following fashion {"merry":"god", "christmas":"jul", "and":"och", "happy":gott", "new":"nytt", "year":"år"} and use it to translate your Christmas cards from English into

Captura de pantalla 2017-02-13 a la(s) 19.05.24.png
Captura de pantalla 2017-02-13 a la(s) 19.06.14.png
Continue reading "46 Simple Python Exercises (16.-20.)"

WSQ03 -Libraries and ranges (Importing and using modules/libraries, and Creation and use of ranges in Python) (Python3)

--Originally published at Elu's Blog

For this assignment, this were the instructions:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

This is my code:

Captura de pantalla 2017-02-13 a la(s) 16.30.45.png

Captura de pantalla 2017-02-13 a la(s) 16.33.42.png

Libraries are collections of tools and resources for programmers. They may include functions or data. To use a library in Python3, you have to use the word “import” before the name of the library, or you can use “from ‘name-of-the-library’ import ‘name-of-the-function'”. In my code I imported the ‘random’ library and I used the random.randrange() function which gets you a number from the range that you specify.

The range function in Python3 is used by telling it the first number and the last number of the range. Personally, I use it very often in for loops when using lists.

 


46 Simple Python Exercises (11.-15.)

--Originally published at Elu's Blog

11. Define a function generate_n_chars() that takes an integer n and a character c and returns a string, n characters long, consisting only of c:s. For example, generate_n_chars(5,"x") should return the string "xxxxx". (Python is unusual in that you can actually write an expression 5 * "x" that will evaluate to "xxxxx". For the sake of the exercise you should ignore that the problem can be solved in this manner.)

Captura de pantalla 2017-02-13 a la(s) 10.05.01.png

Captura de pantalla 2017-02-13 a la(s) 10.05.32.png

12.Define a procedure histogram() that takes a list of integers and prints a histogram to the screen. For example, histogram([4, 9, 7]) should print the following:

****
*********
*******

Captura de pantalla 2017-02-13 a la(s) 10.06.24.png

Captura de pantalla 2017-02-13 a la(s) 10.06.53.png

13. The function max() from exercise 1) and the function max_of_three() from exercise 2) will only work for two and three numbers, respectively. But suppose we have a much larger number of numbers, or suppose we cannot tell in advance how many they are? Write a function max_in_list() that takes a list of numbers and returns the largest one.

Captura de pantalla 2017-02-13 a la(s) 10.17.04.png

Captura de pantalla 2017-02-13 a la(s) 10.17.29.png

14. Write a program that maps a list of words into a list of integers representing the lengths of the corresponding words.

Captura de pantalla 2017-02-13 a la(s) 10.23.32.png

Captura de pantalla 2017-02-13 a la(s) 10.23.52.png

15. Write a function find_longest_word() that takes a list of words and returns the length of the longest one.

Captura de pantalla 2017-02-13 a la(s) 10.35.34.png

Captura de pantalla 2017-02-13 a la(s) 10.36.14.png


Quiz Week 4

--Originally published at Elu's Blog

This were my instructions:

For this quiz I want you to (in class) create a program with two functions:

  • def minimum_three(x, y, z):  # returns the value that is smallest of x, y and z
  • def sum_squares(x, y, z): # returns the value of the sum of squares of x, y, z

You implement these function in your own program in a file quiz4.py

You should make a main routine that asks the user for three numbers and then calls your functions to which should *RETURN* the value and you print in the main part of your program.

Captura de pantalla 2017-02-02 a la(s) 11.13.13.png

And this is how it runs:

Captura de pantalla 2017-02-02 a la(s) 11.14.43.png


Functions (Python3)

--Originally published at Elu's Blog

Functions in programming are like commands that you tell the computer. We already saw the int() and input() functions. For example, the input() function tells the computer to store anything that the user writes.

Creating a function

If you want to create a specific instruction for the computer, you’ll have to create a function. To do this, first you’ll have to tell the program that you’re defining a function by writing “def” before the function’s name. The next step is to write the name of the function. Then, you’ll have to tell the program if the function is going to receive any variable (that will be inside the brackets “()”), if not, don’t write anything inside the brackets. Next is to write a colon “:” to tell the function that the next things that are tabbed will be the core of the function.

Calling a function

To call a function means to merely use the name of the function and (if it is the case) write the variables needed.

Let’s look at an example:

Captura de pantalla 2017-01-31 a la(s) 15.55.33.png

In the image above we can see that the “myfunction()” function is being defined by writing “def” before it. We can also see that no variable is going to be needed in order for the function to work. The function itself only creates a variable “x” and assigns the 55 value to it, then it prints the value of “x”. After that, in line 5, that function is being called by just writing it’s name.

When you run that code, it will just simply print a number 55 on the screen. If in line 6 there was another caling for “myfunction()”, the program will instead print two number 55’s.

Another explanation

As always, if you didn’t understand my explanation or just didn’t like it, here is another

Continue reading "Functions (Python3)"

Quiz Week 3

--Originally published at Elu's Blog

This were my instructions:

For this quiz I want you to (in class) create a program with two functions:

  • def square_root(x):  // returns the square root of x (float)
  • def cube_root(x): // returns the cube root of x (float)

You should make a main routine that asks the user for a number and then calls your functions to calculate the square and cube roots of that number and prints them out.

This is my code:

Captura de pantalla 2017-01-30 a la(s) 16.18.55.png

Captura de pantalla 2017-01-30 a la(s) 16.24.28.png

In another post I’ll explain everything that went on this code.