My own library??

--Originally published at Home Page

As you can see, in python we use the libraries that comes with the python3 package, but, if you want to create a function to use in different programs (For example, in the 46 exercises sometimes we should use functions created in a previous program). you just need to create a program and the define the functions you want to use, is not necessary to write a main function, so just defining the functions we think will be useful, is ok.

There you have an example, these are 2 functions that we will use later.

functionsaa


46 Python exercises: 31-35!

--Originally published at Home Page

31. Implement the higher order functions map(), filter() and reduce(). (They are built-in but writing them yourself may be a good exercise.)

EJ31

32. Write a version of a palindrome recogniser that accepts a file name from the user, reads each line, and prints the line to the screen if it is a palindrome.

EJ32

 

33. According to Wikipedia, a semordnilap is a word or phrase that spells a differentword or phrase backwards. (“Semordnilap” is itself “palindromes” spelled backwards.) Write a semordnilap recogniser that accepts a file name (pointing to a list of words) from the user and finds and prints all pairs of words that are semordnilaps to the screen. For example, if “stressed” and “desserts” is part of the word list, the the output should include the pair “stressed desserts”. Note, by the way, that each pair by itself forms a palindrome!

EJ33

 
34. Write a procedure char_freq_table() that, when run in a terminal, accepts a file name from the user, builds a frequency listing of the characters contained in the file, and prints a sorted and nicely formatted character frequency table to the screen.

EJ34

 

35. The International Civil Aviation Organization (ICAO) alphabet assigns code words to the letters of the English alphabet acrophonically (Alfa for A, Bravo for B, etc.) so that critical combinations of letters (and numbers) can be pronounced and understood by those who transmit and receive voice messages by radio or telephone regardless of their native language, especially when the safety of navigation or persons is essential. Here is a Python dictionary covering one version of the ICAO alphabet:

d = {'a':'alfa', 'b':'bravo', 'c':'charlie', 'd':'delta', 'e':'echo', 'f':'foxtrot',
     'g':'golf', 'h':'hotel', 'i':'india', 'j':'juliett', 'k':'kilo', 'l':'lima',
     'm':'mike', 'n':'november', 'o':'oscar', 'p':'papa', 'q':'quebec', 'r':'romeo',
     's':'sierra', 't':'tango', 'u':'uniform', 'v':'victor', 'w':'whiskey', 
     'x':'x-ray', 'y':'yankee', 'z':'zulu'}

Your task in this

EJ35
Continue reading "46 Python exercises: 31-35!"

46 python Exercises! 26-30!!

--Originally published at Home Page

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?

 

EJ26

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.

 

EJ27

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.

 

EJ28

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.EJ29

 

 

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.EJ30


Lambda functions!

--Originally published at Home Page

In python3 we use the lambda operator, you can use it and is like declaring a function in only one line! But, here’s the question. Why we use the lambda operator? Is simple, there are many functions in python3 that we can use with this operator, to work with lists!

We declare the function like this:

lambda x,y:  x if x<y else y

this, will declare a function that takes those 2 parameters (x and y), and returns x if x is minor than y, and if it’s not, return y.

Soon, we are going to use this operator for more interesting programs. If it’s not clear yet, you can use this page where i learned it!

 

http://www.python-course.eu/python3_lambda.php

 

Or check this video:


46 exercises!!! 21-25!

--Originally published at Home Page

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

EJ21

Note: The use of the last two lines i took because a friend recommended me to, you should check his blog, he has interesting stuff!

https://elusblog.wordpress.com/

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.

EJ22

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

EJ23
EJ24
EJ25
Continue reading "46 exercises!!! 21-25!"

Using Dictionaries in python3!

--Originally published at Home Page

Let’s explain an useful tool we are going to use in the future, the dictionaries.

In python 3 we can use a dictionary as a way to manipulate information, we define it in this form, for example:

Name={“red”:”rojo”,”blue”:”azul”,”yellow”:”amarillo”}

The way we use this information is in keys (the first word of each element) and values(the second word).

You can see this video if you didn’t understand! (I know, sometimes i don’t know how to explain this stuff):


46 Exercises: 16-20!!

--Originally published at Home Page

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.

 

EJ16

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.

EJ17

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.

 

EJ18

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.EJ19

 

 

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

EJ20
Continue reading "46 Exercises: 16-20!!"

WSQ07:Lists

--Originally published at Home Page

What to Do

Create a program that asks the user for 10 numbers  (floating point). Store those numbers in a list. Show to the user the total, average and standard deviation of those numbers

This is my code:

wsq07.

 

In python 3 we can use data by creating a list of information, it can be numbers, words, whatever.

If you want to see a different explanation, you can check this video:

And this for an explanation about list manipulation:


46 simple exercises: 11-15!

--Originally published at Home Page

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

Here’s my code:

EJ11

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:

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

Here’s my code:EJ12

 

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.

This is my code: EJ13

 

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

This is my code:EJ14

 

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

This is my code: EJ15


46 simple python exercises: 6-10!

--Originally published at Home Page

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.

EJ6

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”.EJ7

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.EJ8

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

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.

EJ10