#WSQ10 – Babylonian Method

--Originally published at マルコ

Background

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.

What to Do

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.

10.1

10.2


Exercises (16-20)

--Originally published at マルコ

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.

16.116.2

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.

17.117.2

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.

18.118.2

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.

19.119.219.3

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

20.1
20.2
Continue reading "Exercises (16-20)"

Exercises (11-15)

--Originally published at マルコ

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

11.111.2

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:

  1. ****
    *********
    *******

    12.112.2

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.

13.113.2

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

14.114.2

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

15.115.2

Featured image:


Quiz #11

--Originally published at マルコ

1. Escribe el función distancia cual recibe 4 números (x1, y1, x2, y2) cuales representan dos puntos en espacio (x1,y1) y (x2,y2). El método debe regresar la distancia entre los dos puntos. Recuerda que el valor cuadrada del hipotenusa del triangulo es igual que la suma de las cuadradas de los otro dos lados del triangulo (the hypotenuse squared is equal to the sum of the squares of the other two sides).

Examen.1Examen.1.2

2. (5 puntos) Escribe un función que se llama triangulo cual recibe un parámetro size y imprime un triangulo derecho como el siguiente. El renglón mas grande debe llevar size numero de “T”. SOLO imprime los “T”s y los endlines. Nota que no hay characteres (espacios) a la derecha de los T’s. Debe usar un ciclo “for” para controlar el repetición.

Examen.2Examen.2.2

3. Escribe la función factorial cual recibe un entero “x” y regresa el valor de x! Recuerda que 0! = 1, 1! = 1, 2! = 2, 3!= 6, 4! = 24, etc. Para los de Python: NO PUEDES usar el factorial como parte del module “math”

Examen.3Examen.3.2

4. Escribe una función que se llama promedio_lista que recibe un parámetro (una lista (Python) o arreglo/Vector de C++) de valores float y regresa como float el promedio de los números en la lista.

Examen.4Examen.4.2

5. Escribe una función que se llama smallest_of_four cual recibe cuatro valores float como parametros y regresa el minimo (más pequeño) de los valores. Ojo: puede recibir unos valores iguales.

Examen.5Examen.5.2

6. Escribe una función que se llama fibonacci cual recibe un número n (puedes dar por cuenta que valor mayor o igual que cero) y regresa y valor correspondiente del serie de fibonacci, por ejemplo: fibonacci(0) regresa 0 fibonacci(1) regresa 1 fibonacci(2) regresa 1 fibonacci(3) regresa 2 fibonacci(4) regresa 3 fibonacci(5) regresa 5 fibonacci(6) regresa

Examen.6
Examen.6.2
Examen.7
Examen.7.2
Continue reading "Quiz #11"

Quiz #8

--Originally published at マルコ

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”?

Quiz08Quiz08.2

Featured image:


Quiz #6 (6-10)

--Originally published at マルコ

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.

6.16.1.2

 

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

6.26.2.2

 

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.

6.36.3.2

 

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

6.46.4.2

 

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.

6.56.5.2

Featured image:

http://abduction.ml/post/157801322005

 

 


# WSQ09 – Multipart Data and Files

--Originally published at マルコ

Your Assignment

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.

files

files2

Featured image:

http://www.pixiv.net/member_illust.php?mode=medium&illust_id=25862501

 


# WSQ08 – Yo Soy 196

--Originally published at マルコ

What to Do

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”

Details

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.

lyrchel

lyrchel2

Featured image:

# 196 Dex


# WSQ07 – Lists

--Originally published at マルコ

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.

Details

For the Python group, you want to be using Lists. For the C++ group you can do this with arrays or Vectors, but you will need to know eventually how to do both.

Once you have this working, change it so that users keep giving you values until they signal “no more values”. How would you implement this and in particular for the C++ group, how to you deal with an unknown size to your array during compilation?

lists

lists2

Featured image:

http://www.pixiv.net/member_illust.php?mode=medium&illust_id=14071780