WSQ12 – Estimating e

--Originally published at Elu's Blog

For this assignment these were my instructions:

In this assignment you will estimate the mathematical constant e. You should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.

You will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).

This is what I came up with:

Captura de pantalla 2017-04-20 a la(s) 17.38.54.png

Captura de pantalla 2017-04-20 a la(s) 17.39.12.png


WSQ11 – Go Bananas

--Originally published at Elu's Blog

For this assignment, these were my instructions:

Write a function called find_bananas which receives a single parameter called filename (a string) and returns a positive integer which is the number of times the word (string) “banana”  (or “BANANA” ) is found in the file. The banana can be any case (‘BaNana’ or ‘BANANA’ or ‘banana’, etc) and they can be “stuck together” like “banAnaBANANA” (that counts as two). Create your own test file (plain text) to check your work.

This is what I came up with:

Captura de pantalla 2017-04-17 a la(s) 10.17.20.png

Captura de pantalla 2017-04-17 a la(s) 10.18.28.png

Captura de pantalla 2017-04-17 a la(s) 10.18.49.png


WSQ10 – Babylonian Method

--Originally published at Elu's Blog

For this assignment, these were my instructions:

In this assignment you will write a function to calculate the square root of a number using the Babylonian method. You can search for that method, it will be easy to find.

The function should receive a number and return floating point number. Obviously you should test your function, so create a main program that asks the user a value, calculates the square root and displays that.

This is what I came up with:

Captura de pantalla 2017-03-30 a la(s) 18.38.32.png

And this is how it runs:

Captura de pantalla 2017-03-30 a la(s) 18.39.29.png


Quiz Week 09

--Originally published at Elu's Blog

For this assignment these were my instructions:

Write a function that receives four parameters: x1, y1, x2, y2 which are all floating point values.

The function is called distance and returns (float) the distance between x1,y1 and x2,y2 on the cartesian coordinate plane.

This is what I came up with:

Captura de pantalla 2017-03-09 a la(s) 10.57.40.png

Captura de pantalla 2017-03-09 a la(s) 10.57.56.png


WSQ09 – Files

--Originally published at Elu's Blog

For this assignment these were my instructions:

So for this assignment I would like to see you create a function that receives as parameter the name of a file (this would be a string value like data.txt) and your function counts the number of lines and the number of characters in the file which it returns as a single value (but with two values). You will want to look at how to use and return a tuple from a function and how to open and read text files line by line.

This is what I came up with:

Captura de pantalla 2017-03-06 a la(s) 10.39.34.png

Captura de pantalla 2017-03-06 a la(s) 10.31.38.png

Captura de pantalla 2017-03-06 a la(s) 10.39.48.png

Reading and writing of text files

When using external files in python, there are a few things that can be done to it: read, write, append and the binary versions of the previous three.

To use a file, you have to first  open it with the open() function. open(‘[fileaddress]’,'[opening mode]’ <- this is how to use the function. One example is:

f = open(‘/Users/rafaelelu/Desktop/Tec/Primer Semestre/Fundamentos de Programación (Ken Bauer)/46 Simple Python Exercises/’text.txt’,’r’) <- This will open the ‘text.txt’ file in read mode.

‘r’ = read mode ‘rb’ = read in binary mode

‘w’ = write mode   ‘wb’ = write in binary mode

‘a’ = append mode   ‘ab’ = append in binary mode

Note: when opening a file in write mode, all of its text will be deleted previous to writing on it. Also if you open a file (in write mode) that doesn’t exist, it will create a file with that name in the same address as the programmed.

In my code above, I used ‘txt = f.read()’ in line 4. This assigns all characters of the file in the txt variable. To write in a file, use the .write() function and inside the parenthesis write

Continue reading "WSQ09 – Files"

46 Simple Python Exercises (36.-40.)

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

36. A hapax legomenon (often abbreviated to hapax) is a word which occurs only once in either the written record of a language, the works of an author, or in a single text. Define a function that given the file name of a text will return all its hapaxes. Make sure your program ignores capitalization.

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

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

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

37. Write a program that given a text file will create a new text file in which all the lines from the original file are numbered from 1 to n (where n is the number of lines in the file).

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

Captura de pantalla 2017-03-02 a la(s) 18.47.08.pngCaptura de pantalla 2017-03-02 a la(s) 18.46.48.png

38. Write a program that will calculate the average word length of a text stored in a file (i.e the sum of all the lengths of the word tokens in the text, divided by the number of word tokens).

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

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

39. Write a program able to play the “Guess the number”-game, where the number to be guessed is randomly chosen between 1 and 20. (Source: http://inventwithpython.com) This is how it should work when run in a terminal:

>>> import guess_number
Hello! What is your name?
Torbjörn
Well, Torbjörn, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too low.
Take a guess.
15
Your guess is too low.
Take a guess.
18
Good job, Torbjörn! You guessed my number in 3 guesses!

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

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

40. An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorse, A decimal point = I’m a dot in place. Write a Python program that, when started 1) randomly picks a word w from given list of words, 2) randomly permutes w

Captura de pantalla 2017-03-02 a la(s) 18.51.00.png
Captura de pantalla 2017-03-02 a la(s) 18.52.09.png
Continue reading "46 Simple Python Exercises (36.-40.)"

Fibonacci – Quiz Week 8

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

These were my instructions for this quiz:

  • Write a function that calculates returns the “nth” Fibonacci number where we define a function over the Fibonacci numbers mapping the naturals (starting with zero) to the Fibonacci series. So fibonacci(0) returns 0, fibonacci(1) returns 1, fibonacci(2) returns 1 and so on. Note that we are using the modern definition where the sequence starts with zero. You should try to implement this with two solutions: one with a loop and one with recursion. Which do you think is “better”, which looks more “elegant”, which is more “efficient”?

This is what I came up with:

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

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

I personally think that the recursion function is more efficient and better-looking than the other one. For instance, one was written in 4 lines and the other one in 12 lines.


46 Simple Python Exercises (31.-35.)

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

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

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

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.

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

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

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

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

33. According to Wikipedia, a semordnilap is a word or phrase that spells a different word 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!

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

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

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

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.

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

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

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

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 exercise

Captura de pantalla 2017-02-28 a la(s) 21.22.24.png
Captura de pantalla 2017-02-28 a la(s) 21.24.25.png
Continue reading "46 Simple Python Exercises (31.-35.)"

WSQ08 -Lychrel numbers

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

For this assignment, these were my instructions:

Your job is to create a program that asks the user for two pieces of data:

  • The lower bound of the sequence
  • The upper bound of the sequence
Then you check the values from the lower bound (inclusive) to the upper bound (inclusive) and make a report of them. During the analysis of each number, if a Lychrel number is found it should be reported immediately with something like “Found a Lychrel number: 196”
The report must show:
  • The range of numbers analysed (lower to upper bound)
  • The number of natural palindromes (no addition to inverse needed)
  • The number of non-Lycherels encountered (become palindromes)
  • The number of Lycherel number candidates (that did not converge to palindrome)

Since you will not be able to prove that a number is Lycherel (since you cannot computer forever to check), our definition for a Lycherel candidate will be if a number does not converge after 30 iterations of applying the addition to the inverse.

This is what I came up with:

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

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


WSQ07 – Lists (unfinished)

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

For this assignment theses were my instructions:

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.

Once you have this working, change it so that users keep giving you values until they signal “no more values”.

This is what I came up with:

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

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