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.)"

WSQ06 – ‘While’ Loops (Python3)

--Originally published at Elu's Blog

For this assignment these were my instructions:

Create a program that asks the user for a non-negative integer (let’s call that number n) and display for them the value of n! (n factorial).

After showing them the answer, ask them if they would like to try another number (with a simple y/n response) and either ask again (for y) or quit the program and wish them a nice day (if they answered n).

This is what I came up with:

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

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

A ‘while’ loop is very similar to a ‘for’ loop in a way that it repeats a certain task until you tell it to stop. In this case, I made two loops. The first one takes the variable ‘counter’, and the variable ‘n’ and it subtracts 1 to it. The instructions inside that loop will repeat until the argument ‘counter < n-1’ stops being true. To stop this loop for being an infinite loop, every time the loop runs, it will add 1 to the variable counter.

The second loop has the argument ‘True’, so in theory it will run forever. The thing is that in line 14 I added the word ‘break’, what this does is that it stops the loop. So whenever the variable ‘follow’ is assigned the value ‘n’, the loop will stop.

Alternative Explanation

“Python 3 programming tutorial: While Loop” by sentdex


WSQ05 – Functions (Python3)

--Originally published at Elu&#039;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&#039;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&#039;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&#039;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&#039;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


46 Simple Python Exercises (6.-10.)

--Originally published at Elu&#039;s Blog

In the previous post I showed the first batch of exercises (1.-5.). Here are the next 5:

6. Define a function sum() and a function multiply() that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4]) should return 10, and multiply([1, 2, 3, 4]) should return 24.

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

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

7. Define a function reverse() that computes the reversal of a string. For example, reverse("I am testing") should return the string "gnitset ma I".

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

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

8. Define a function is_palindrome() that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar") should return True.

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

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

9. Write a function is_member() that takes a value (i.e. a number, string, etc) x and a list of values a, and returns True if x is a member of a, False otherwise. (Note that this is exactly what the in operator does, but for the sake of the exercise you should pretend Python did not have this operator.)

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

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

10. Define a function overlapping() that takes two lists and returns True if they have at least one member in common, False otherwise. You may use your is_member() function, or the in operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.

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

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


Quiz Week 5 – First 5 exercises

--Originally published at Elu&#039;s Blog

For this quiz I was given 46 simple python for me to do until the end of the semester. In this post I’ll show the first 5.

  1. Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Python. (It is true that Python has the max() function built in, but writing it yourself is nevertheless a good exercise.)

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

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

2. Define a function max_of_three() that takes three numbers as arguments and returns the largest of them.

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

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

3. Define a function that computes the length of a given list or string. (It is true that Python has the len() function built in, but writing it yourself is nevertheless a good exercise.)

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

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

4. Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.

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

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

5. Write a function translate() that will translate a text into “rövarspråket” (Swedish for “robber’s language”). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

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

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