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