Last active
April 26, 2018 07:31
-
-
Save jordi-petit/c43f12e0df73a144a00e569e889f4c14 to your computer and use it in GitHub Desktop.
2017-12-15 Vectors en Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ordenació per fusió (merge sort) | |
from jutge import read | |
from random import random | |
# random genera un nombre real a l'atzar entre 0 i 1 | |
def merge(v1, v2): | |
"""Retorna la fusió de dos vectors ordenats.""" | |
v = [] | |
n1, n2 = len(v1), len(v2) | |
i1, i2 = 0, 0 | |
while i1 < n1 and i2 < n2: | |
if v1[i1] <= v2[i2]: | |
v.append(v1[i1]) | |
i1 += 1 | |
else: | |
v.append(v2[i2]) | |
i2 += 1 | |
v += v1[i1:] | |
v += v2[i2:] | |
return v | |
def msort(v): | |
"""Retorna el vector v ordenat.""" | |
n = len(v) | |
if n < 2: | |
return v | |
else: | |
return merge(msort(v[n//2:]), msort(v[:n//2])) | |
def main(): | |
n = read(int) | |
v = [random() for i in range(n)] | |
print(msort(v)) | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multiplicació de matrius | |
def mult(a, b): | |
"""Multiplica dues matrius quadrades de la mateixa mida.""" | |
n = len(a) | |
c = [[0 for j in range(n)] for i in range(n)] | |
for i in range(n): | |
for j in range(n): | |
for k in range(n): | |
c[i][j] += a[i][k] * b[k][j] | |
return c | |
def main(): | |
a = [ | |
[1, 2, 3], | |
[4, 5, 6], | |
[9, 8, 7], | |
] | |
b = [ | |
[1, 9, 5], | |
[3, 5, 6], | |
[9, 3, 1], | |
] | |
c = mult(a, b) | |
print(c) | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Genera totes les permutacions de n elements | |
from jutge import read | |
def genera(v, u, i): | |
"""Genera totes les permutacions havent fixat les primeres posicions en v i havent utilizat els valors en u.""" | |
n = len(v) | |
if i == n: | |
print(v) | |
else: | |
for j in range(n): | |
if not u[j]: | |
u[j] = True | |
v[i] = j | |
genera(v, u, i + 1) | |
u[j] = False | |
def main(): | |
n = read(int) | |
v = [None for i in range(n)] | |
u = [False for i in range(n)] | |
genera(v, u, 0) | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Genera nombres primers | |
from jutge import read | |
def primers(n): | |
"""Retorna un vector amb tots els nombres primers fins a n.""" | |
# Eratostenes | |
p = [] | |
b = [True for i in range(n+1)] | |
b[0] = b[1] = False | |
for i in range(2, n+1): | |
if b[i]: | |
p.append(i) | |
for j in range(2*i, n+1, i): | |
b[j] = False | |
return p | |
def main(): | |
n = read(int) | |
p = primers(n) | |
print(p) | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pas de paràmetres, exemple 1 | |
def f(x): | |
print("x =", x) | |
x += 1 # vol dir x = x + 1 | |
print("x =", x) | |
y = 12 | |
print("y = ", y) | |
f(y) | |
print("y = ",y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pas de paràmetres, exemple 2 | |
def g(v): | |
print("v = ", v) | |
v.append(99) | |
print("v = ", v) | |
a = [1, 2, 3] | |
print("a =", a) | |
g(a) | |
print("a =", a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A classe he panicat 😱 dient que les crides del
msort
estaven del revés i que ho canviaria. Potser sí que no és l'ordre més natural, però així funcionen bé 😌.Penseu perquè! 👈