1 - URI 1015


In [3]:
from math import *

x1 = input()
y1 = input()
x2 = input()
y2 = input()

delta_x = (x2 - x1)
delta_y = (y2 - y1)
distancia = sqrt(delta_x ** 2 + delta_y ** 2)
print("%.6f" % distancia)


1
1
2
2
1.414214

2 - URI 1279


In [7]:
ano = input()

if ano % 4 == 0:
    print("Ano bissexto")
else:
    print("Ano normal")


2013
Ano normal

3 - URI 1214


In [13]:
n_alunos = input()
nota_primeiro_aluno = input()
media = nota_primeiro_aluno

for i in range(n_alunos - 1):
    nota = input()
    media += nota
    
media = media / n_alunos

if nota_primeiro_aluno > media:
    print("Aluno acima da media")
else:
    print("Aluno precisa melhorar")


4
6
5
4
3
4

4 - Palindromo


In [21]:
string = raw_input()
if str(string).lower() == str(string).lower()[::-1]:
    print("E PALINDROMO")
else:
    print("NAO E PALINDROMO")


Hannah
E PALINDROMO

5 - Array bidimensional


In [31]:
import numpy as np
matrix = np.random.randint(0, 100, (3,3))

print matrix.dtype
print type(matrix)
print matrix.shape


int64
<type 'numpy.ndarray'>
(3, 3)

6 - Normalização


In [40]:
import numpy as np

matrix = np.random.random((3,3))

print matrix

soma_linhas = matrix.sum(axis=1)
matrix = matrix / soma_linhas[:, np.newaxis]

print "\n\n", matrix


[[ 0.73860354  0.16066955  0.93508937]
 [ 0.51798113  0.57609764  0.08195502]
 [ 0.43123168  0.57621934  0.91388361]]


[[ 0.40264863  0.08758877  0.5097626 ]
 [ 0.44044749  0.48986487  0.06968764]
 [ 0.22444382  0.29990577  0.47565041]]

7 - Sistema linear


In [46]:
import numpy as np 

matrix_A = np.array([[1,2,4],[5,3,7],[9,0,-8]])
matrix_B = np.array([9,18,1])

x = np.linalg.solve(matrix_A, matrix_B)
print x
# print np.allclose(np.dot(matrix_A, x), matrix_B)


[ 1.  2.  1.]

8 - Grafico


In [50]:
%matplotlib inline
import numpy as np
import pylab as plt

def f(x):
    return 2 * (x ** 2) + 3

x = np.linspace(-3, 3, 100)
y = f(x)

plt.xlabel("Eixo X")
plt.ylabel("Eixo Y")
plt.title("Grafico")
plt.plot(x,y)


Out[50]:
[<matplotlib.lines.Line2D at 0x1130ef550>]

9 - Multiplo plot


In [72]:
%matplotlib inline
import numpy as np
import pylab as plt
import math

def f(x):
    return x**2 - 1

def g(x):
    return np.exp(-x) * -x

def k(x):
    return 1/x


x = np.linspace(-3, 3, 100)
y = f(x)
g = g(x)
k = k(x)

plt.xlabel("Eixo X")
plt.ylabel("Eixo Y")
plt.title("Grafico")
plt.grid(True)

plt.plot(x,y, '-b', label="Linha Azul")
plt.plot(x,g, ':y', label="Pontilhado Amarelo")
plt.plot(x,k, '--g', label="Tracejado Verde")

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()


10 - 6 gráficos


In [80]:
%matplotlib inline
import numpy as np
import pylab as plt
import math

def f(x, e):
    return x**e

x = np.linspace(2, 3, 100)

plt.xlabel("Eixo X")
plt.ylabel("Eixo Y")
plt.title("Grafico")
plt.grid(True)

styles = ['-b', '-k', '-y', '-g', '-r', '-m'] 

for i in range(6):
    y = f(x, i)
    label = "Linha " + str(i)
    plt.plot(x,y, styles[i], label=label)

plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

plt.show()

x = np.linspace(-3, 3, 100)
for i in range(6):
    title = "Grafico x**" + str(i)
    plt.title(title)
    y = f(x, i)
    plt.plot(x,y, styles[i])
    plt.show()



In [ ]: