Skip to content
Snippets Groups Projects
Commit 3788df3a authored by David Ramos Salamanca's avatar David Ramos Salamanca
Browse files

Add second problem

parent 6b97fb6b
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
%% Cell type:markdown id: tags:
# David Ramos - UIS
%% Cell type:markdown id: tags:
## Problema: Triángulo de Pascal
%% Cell type:markdown id: tags:
Escriba una rutina en python que reciba como entrada un número entero, *n*, e imprima los números en la *n-ésima* fila del triángulo de Pascal. El programa debe verificar si el número *n* es entero, o arrojar un mensaje informando que ha habido un error del usuario en caso contrario.
%% Cell type:markdown id: tags:
## Solución 1 (mathy)
%% Cell type:markdown id: tags:
Los números en la *n-ésima* (empezando en cero) fila del triángulo de Pascal corresponden a los coeficientes binomiales ${n\choose 0},\,{n\choose 1},\,...,\,{n\choose n}$ donde
$$ {n\choose k} = \frac{n!}{k!(n-k)!} .$$
%% Cell type:code id: tags:
``` python
import numpy as np
```
%% Cell type:code id: tags:
``` python
def pascal1(n):
if int(n)==n: # reviso que el número sea entero
row = ''
for i in range(n+1): # calculo cada uno de los coeficientes binomiales y lo almaceno en fila
row += str(int(np.math.factorial(n)/(np.math.factorial(i)*np.math.factorial(n-i)))) + '\t'
print(row)
else:
print('Error: el número ingresado no es entero')
```
%% Cell type:code id: tags:
``` python
pascal1(0)
```
%% Output
1
%% Cell type:code id: tags:
``` python
pascal1(4)
```
%% Output
1 4 6 4 1
%% Cell type:code id: tags:
``` python
pascal1(3.3)
```
%% Output
Error: el número ingresado no es entero
%% Cell type:markdown id: tags:
## Solución 2 (cody)
%% Cell type:markdown id: tags:
La otra opción es construir el triángulo de pascal hasta la *n-ésima* fila e imprimirla.
%% Cell type:code id: tags:
``` python
def pascal2(n):
if int(n) != n:
print('Error: el número ingresado no es entero')
elif n == 0: # sabemos que la primera fila es un 1
print('1')
else:
prow = (int(1)) # guardando la primera fila, se usan tuplas para que sea inmutable
row =[] # lista donde se crearán las filas
for i in range(1,n+1): # iterando sobre el número de filas
for k in range(i+1): # segunda iteración (shame on me) para moverse en "columnas"
if k == 0 or k == i:
row.append(int(1)) # el primer y el último número es 1
else:
row.append(prow[k-1]+prow[k]) # elementos interiores como suma de elementos de la fila anterior
prow = tuple(row) # re asigno la fila anterior (como una tupla) para la nueva iteración
frow = row # guardo la fila actual
row = [] # vacío la fila para volverla a llenar
print(''.join([str(number) + '\t' for number in frow])) # imprimo los número separados por tab
```
%% Cell type:code id: tags:
``` python
pascal2(0)
```
%% Output
1
%% Cell type:code id: tags:
``` python
pascal2(1)
```
%% Output
1 1
%% Cell type:code id: tags:
``` python
pascal2(2)
```
%% Output
1 2 1
%% Cell type:code id: tags:
``` python
pascal2(3)
```
%% Output
1 3 3 1
%% Cell type:code id: tags:
``` python
pascal2(3.1)
```
%% Output
Error: el número ingresado no es entero
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment