Skip to content

Latest commit

 

History

History
105 lines (59 loc) · 4.82 KB

README.md

File metadata and controls

105 lines (59 loc) · 4.82 KB

Hands-On Introducción a Python

Ejercicios para poner en práctica las nociones básicas de Python.

Los siguientes ejercicios fueron extraidos de http://projecteuler.net/problems.

1- Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3,5,6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

2- Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

3- Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

4- Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even) n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

5- Number letter counts

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.

Tip:

  • Usar dicionarios

6- Square root convergents

It is possible to show that the square root of two can be expressed as an infinite continued fraction.

√2 = 1 + 1/(2 + 1/(2 + 1/(2 + ... ))) = 1.414213...

By expanding this for the first four iterations, we get:

1 + 1/2 = 3/2 = 1.5 1 + 1/(2 + 1/2) = 7/5 = 1.4 1 + 1/(2 + 1/(2 + 1/2)) = 17/12 = 1.41666... 1 + 1/(2 + 1/(2 + 1/(2 + 1/2))) = 41/29 = 1.41379...

The next three expansions are 99/70, 239/169, and 577/408, but the eighth expansion, 1393/985, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.

In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?

Extracción de datos

Una parte importante de nuestro trabajo cotidiano es realizar análisis de post procesamiento de datos que obtenemos de otras herramientas. En los siguientes ejercicios vamos a trabajar con un archivo de salida del programa LAMMPS.

La idea de las siguientes 3 tareas es que sean escritas de forma modular para luego poder ser reutilizadas.

7- Extracción

Escribir un script en python que extraiga del archivo data/log.melt-berendsen los datos del progreso de la simulación. Estos datos son los que se encuentran entre la linea que comienza con "Step" y la linea que comienza con "Loop". Luego guardar estos datos (con el mismo formato) en un nuevo archivo.

Tips:

  • Usar la función open

  • El objeto archivo de python tiene un método readline que permite leer de a una linea.

  • Es posible preguntar si un string contiene alguna palabra.

      >>> busqueda = 'comprar'
      >>> texto = 'Vamos a comprar una cerveza'
      >>> busqueda in texto
      True
    

8- Extraer dos columnas

Al script anterior, agregarle una nueva función que extraiga del archivo recortado, la primera y la cuarta columna a un nuevo archivo.

Tip:

9- Integrando a la linea de comando

Generalmente, nos va a resultar muy útil poder pasar los argumentos necesario desde la linea de comando. Modificar el script anterior para que el usuario le pueda el archivo que desea recorta, y que como parámetro optativo le pueda pasar las dos palabras que me limitan mis datos, el nombre del archivo de salida y que columnas extraer.

Tip:

  • Python posee dos formas de pasar argumentos por linea de comando: input() y argparse (argparse es bastante más completa).