Last active
April 26, 2018 07:27
-
-
Save jordi-petit/d6e3a0fb2715744d237dd353771a26ba to your computer and use it in GitHub Desktop.
AP1 2017-09-29 Python 2
This file contains 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
from jutge import read | |
# La funció maxim, donats dos enters retorna un enter que és el seu màxim. | |
def maxim(a, b): | |
if a > b: | |
return a | |
else: | |
return b | |
# Programa que llegeix dos enters i n'escriu el seu màxim, cridant maxim(). | |
x, y = read(int, int) | |
print(maxim(x, y)) |
This file contains 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
from jutge import read | |
# La funció maxim, donats dos enters retorna un enter que és el seu màxim. | |
def maxim(a, b): | |
if a > b: | |
return a | |
else: | |
return b | |
# La funció maxim3, donats tres enters retorna un enter que és el seu màxim. | |
def maxim3(a, b,c): | |
return maxim(maxim(a, b), c) | |
# Programa que llegeix tres enters i n'escriu el seu màxim, cridant maxim3(). | |
x, y, z = read(int, int, int) | |
print(maxim3(x, y, z)) |
This file contains 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
# La funció power(x, y) retorna x^y, donat que y>=0. | |
def power(x, y): | |
p = 1 | |
i = 0 | |
while i < y: | |
p = p * x | |
i = i - 1 | |
return p | |
# La funció primer(n) retorna si el natural n és o no un nombre primer. | |
def primer(n): | |
if n <= 1: | |
return False | |
# Mira si algun nombre entre 2 i √n divideix n. | |
c = 2 | |
while c*c <= n: | |
if n % c == 0: | |
return False | |
c = c + 1 | |
return True | |
# Els booleans en Python funcionen com en C++, però es diuen False i True enlloc de false i true. | |
This file contains 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
# Suma d'una seqüència de nombres | |
# (recordeu que cal picar Control+D per finalitzar l'entrada al terminal) | |
# Compte: En Python no hi ha while (cin >> x). | |
# Quan read() no pot llegir, retorna un valor especial anomenat None. | |
from jutge import read | |
s = 0 | |
x = read(int) | |
while x is not None: | |
s = s + x | |
x = read(int) | |
print(s) |
This file contains 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
# Mitjana d'una seqüència de nombres | |
from jutge import read | |
s = 0 | |
n = 0 | |
x = read(int) | |
while x is not None: | |
s = s + x | |
n = n + 1 | |
x = read(int) | |
print(s/n) # (divisió de reals) |
This file contains 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
# Nombre d'elements successius repetits en una seqüència d'enters. | |
from jutge import read | |
r = 0 | |
a = read(int) | |
if a is not None: | |
b = read(int) | |
while b is not None: | |
if b == a: | |
r = r + 1 | |
a = b | |
b = read(int) | |
print(r) |
This file contains 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
# Per a cada tres nombres en una seqüencia, dir si són o no ternes pitagòriques | |
def sqr(x): | |
return x * x | |
def es_terna_pitagorica(a, b, c): | |
return sqr(a) * sqr(b) == sqr(c) | |
a, b, c = read(int, int, int) | |
while c is not None: | |
if es_terna_pitagorica(a, b, c): | |
print("sí") | |
else: | |
print("no") | |
a, b, c = read(int, int, int) | |
This file contains 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
# Escriure les arrels quadrades amb 6 digíts de precisió | |
from jutge import read | |
import math | |
x = read(float) | |
while x is not None: | |
s = math.sqrt(x) | |
print("%.6f" % s) | |
x = read(float) |
This file contains 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
# Escriure un rectangle amb accions | |
from jutge import read | |
# acció que escriu una fila amb n símbols c | |
def escriure_fila(n, c): | |
i = 1 | |
while i <= n: | |
print(c, end='') | |
i = i + 1 | |
print() | |
# acció que escriu un rectangle de m files i n columnes amb símbols c | |
def escriure_rectangle(m, n, c) | |
i = 1 | |
while i <= m: | |
escriure_fila(n, c) | |
i = i + 1 |
This file contains 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
# Rellotge | |
import time | |
def escriure_num(x): | |
if x < 10: | |
print(0, end='') | |
print(x, end='') | |
def escriure_hora(h, m, s): | |
escriure_num(h) | |
print(':', end='') | |
escriure_num(m) | |
print(':', end='') | |
escriure_num(s) | |
print() | |
# Malgrat que en Python tots els paràmetres es passen per referència, | |
# els enters són immutables. Per tant, haurem d'usar una funció enlloc | |
# d'una acció. | |
def sumar_un_segon(h, m, s): | |
s = s + 1 | |
if s == 60: | |
s = 0 | |
m = m + 1 | |
if m == 60: | |
m = 0 | |
h = h + 1 | |
if h == 24: | |
h = 0 | |
return h, m, s | |
# Per a programes llargs, millor no tenir variables globals. | |
# Per tant, millor ficar el programa principal en una acció. | |
def main(): | |
hores, mins, segs = 23, 59, 55 | |
while True: | |
escriure_hora(hores, mins, segs) | |
hores, mins, segs = sumar_un_segon(hores, mins, segs) | |
time.sleep(1) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gràcies!!