Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • rvuillem/inf-tc1
  • hwei/inf-tc1
  • nmbengue/inf-tc1
  • fernanda/inf-tc1
  • mleger/inf-tc1
  • lmeng/inf-tc1
  • gferryla/inf-tc1
  • jconso/inf-tc1
  • smaghsou/inf-tc1
  • emarquet/inf-tc1
  • ecluzel/inf-tc1
  • aaudeoud/inf-tc1
  • tsegond/inf-tc1
  • aetienne/inf-tc1
  • djoly/inf-tc1
  • bcampeas/inf-tc1
  • dnovarez/inf-tc1
  • ruetm/inf-tc1
  • cchenu/inf-tc1
  • cguiotdu/inf-tc1
  • mclouard/inf-tc1
  • gwachowi/inf-tc1
  • qbaalaou/inf-tc1
  • sbrocas/inf-tc1
  • ppupion/inf-tc1
  • kinty/inf-tc1
  • hadomo/inf-tc1
  • tgicquel/inf-tc1
  • rhahn/inf-tc1
  • cguyau/inf-tc1
  • mpairaul/inf-tc1
  • rmuller/inf-tc1
  • rlecharp/inf-tc1
  • asebasty/inf-tc1
  • qmaler/inf-tc1
  • aoussaid/inf-tc1
  • kcherigu/inf-tc1
  • sgu/inf-tc1
  • malcalat/inf-tc1
  • afalourd/inf-tc1
  • phugues/inf-tc1
  • lsteunou/inf-tc1
  • llauschk/inf-tc1
  • langloia/inf-tc1
  • aboucard/inf-tc1
  • wmellali/inf-tc1
  • ifaraidi/inf-tc1
  • lir/inf-tc1
  • ynedjar/inf-tc1
  • schneidl/inf-tc1
  • zprandi/inf-tc1
  • acoradid/inf-tc1
  • amarcq/inf-tc1
  • dcombet/inf-tc1
  • gplaud/inf-tc1
  • mkernoaj/inf-tc1
  • gbichot/inf-tc1
  • tdutille/inf-tc1
58 results
Select Git revision
Show changes
Showing
with 1469 additions and 466 deletions
def selectionSort(alist):
for l in range(len(alist )-1,0,-1):
positionOfMax =0
for location in range (1, l+1):
if alist[location]>alist[ positionOfMax ]:
positionOfMax = location
temp = alist[l]
alist[l] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54 ,26 ,93 ,17 ,77 ,31 ,44 ,55 ,20]
selectionSort(alist)
print(alist)
\ No newline at end of file
def algo(words):
t = []
for word in words:
t.append((len(word), word))
t.sort(key=lambda t: t[1][-1])
res = []
for l, w in t:
res.append(w)
res = filter(lambda t: 'a' in t, res)
return list(res)
if __name__=="__main__":
print(algo(["Pierre", "Jean", "Marie", "Eric", "Nathalie", "Yvonne"]))
\ No newline at end of file
# Un commentaire court commence par un # et se limite à la fin de la ligne
"""
On peut aussi créer un commentaire long = une zone de
commentaires sur plusieurs lignes placées
entre une paire de triple-guillements dont le second suit :
"""
# Remarques :
# Un ';' à la fin d'une ligne rend la ligne muette (pas d'affichage des résultats).
# Il permet également de placer plusieurs expressions sur la même ligne.
# Si vous placez un commentaire immédiatement après la signature d'une fonction}
# (ou une méthode, une classe, etc.) entre un couple de """,
# votre commentaire (appelé docstring) devient accessible avec help
\ No newline at end of file
from math import pi
s = "Python"
s.center(10) # centrer sur 10 positions
print(s)
# Donne -->' Python '
s.center(10,"*") # centrer sur 10 positions en complétant par des '*'
# Donne -->'**Python**'
print(s)
s = "Training"
s.ljust(12) # justifier à gauche sur 12 positions}
# Donne -->'Training '
print(s)
s.ljust(12,":")
# Donne -->'Training::::'
s = "Programming"
s.rjust(15)
# Donne -->' Programming'
s.rjust(15, "~")
# Donne -->'~~~~Programming'
account_number = "43447879"
account_number.zfill(12)
# Donne -->'000043447879'
# Même chose avec rjust :
account_number.rjust(12,"0")
# Donne -->'000043447879'
s = 'Hello, world.'
str(s)
# Donne -->'Hello, world.'
repr(s)
# Donne --> "'Hello, world.'"
str(1/7)
# Donne --> '0.14285714285714285'
r=2
print("La surface pour un rayon {rayon:3d} sera {surface:8.3f}".format(surface=r*r*pi, rayon=r))
# Donne --> 'La surface pour un rayon 2 sera 12.566'
x = 10 * 3.25
y = 200 * 200
s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
print(s)
# Donne --> The value of x is 32.5, and y is 40000...
# Le repr() d'un string ajoute des quotes et backslashes:
hello = 'hello, world\n'
str(hello)
# Donne --> 'hello world\n'
repr(hello)
# Donne --> 'hello, world\\n' %* {\color{magenta} noter le double slash} *)
# L'argument d'appel de repr() peut être n'importe quel objet Python :
repr((x, y, ('spam', 'eggs')))
# Donne -->"(32.5, 40000, ('spam', 'eggs'))"
for x in range(1, 11):
print(repr(x).rjust(2), repr(x*x).rjust(3), repr(x*x*x).rjust(4))
# Donne -->
# 1 1 1
# 2 4 8
# 3 9 27
# 4 16 64
# 5 25 125
# 6 36 216
# 7 49 343
# 8 64 512
# 9 81 729
# 10 100 1000
for x in range(1, 6):
print(repr(x).rjust(2), end='#'*x+'\n') # on ajoute x fois \# + \textbackslash n à la fin de chaque ligne} *)
# Donne -->
# 1#
# 2##
# 3###
# 4####
# 5#####
#
\ No newline at end of file
# 8 Conversion de types
list1 = ['1', '2', '3']
str1 = ''.join(list1)
str1
# '123'
list1 = [1, 2, 3]
str1 = ''.join(str(e) for e in list1) # ou avec "" à la place de ''
str1
# '123'
a = "545.2222"
float(a)
# 545.2222 suivant le système, on peut avoir des '0' après les '2'
int(float(a))
# 545
def is_float(value):
try:
float(value)
return True
except:
return False
# On peut aussi utiliser la prédéfinie eval() qui évalue du code Python
eval("1.5")
# 1.5
# Une alternative plus sûre à eval()
import ast
ast.literal_eval("545.2222")
545.2222
ast.literal_eval("31")
31
# Des cas d'erreur
eval(toto) # toto non défini ou n'est pas un str/octes/code
ast.literal_eval(toto)
# Erreur dans les 2 cas
# Converstion en binaire
st = "hello Debase"
' '.join(format(ord(x), 'b') for x in st)
# '1101000 1100101 1101100 1101100 1101111 100000 1000100 1100101 1100010 1100001 1110011 1100101'
# Conversion de string en hexadécimal
s='Hello Debase of Ecl'
' '.join(format(ord(x), 'x') for x in s)
# '48 65 6c 6c 6f 20 44 65 62 61 73 65 20 6f 66 20 45 63 6c'
# Conversion entier en Unicode
chr(16) # Quelle est la forme hexa de 16 ?
#'\x10'
# Conversions décimal <==> base xx
int('12',8) # Quelle est la valeur décimale du nombre octal '12' (octal car base = 8)
# 10
int('12',16) # Quelle est la valeur décimale du nombre hexa '12' (hexa car base = 16)
# 18
int('12') # Quelle est la valeur décimale du nombre décimale '12' (décimal car base par défaut = 10)
# 12
int('1000',2) # Que donne le nombre binaire '1000' en décimal (binaire car base = 2)
# 8
# Pour convertir un entier en octal
oct(12)
# '0o14' #un 'O' au début (pour Octal)
oct(8)
# '0o10'
# Autres conversions
# octal
eval('0o010') == 10 # La valeur décimale de '0o010' est-elle 10
# False
eval('0o010')
# 8
oct(42) # La valeur octale de l'entier 42
# '0o52'
int('0o52', 0) # La valeur décimale de l'octale '0o52' (si base=0 alors base est dans le string)
# 42
int('1101', 2) # La valeur décimale du binaire '1101' (base explicite = 2)
#13
int('0b1101', 2) # Idem, base explicitée = 2, peut importe '0b'
#13
int('0b1101', 0) # La valeur décimale du binaire '0b1101' (base = 0 --> la base '0b' est dans le string)
#13
from struct import *
pack('hhl', 1, 2, 3)
# b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
# b veut dire byte (2 octes). \x veut dire hexa
pack('b', 1)
# b'\x01'
pack('b', 16) # signed char de taille 1. On convertie 16 en signed char
# b'\x10'
In [39]: pack('B', 16) # unsigned char de taille 1
# b'\x10'
pack('h', 1)
# b'\x01\x00'
calcsize('h')
# 2
calcsize('b')
# 1
unpack('b', b'\x10')
# (16,)
type(unpack('b', b'\x10'))
# tuple
unpack('b', b'\x10')[0] # Première élé. Il y a virgule car taille tuple >= 2
# 16
pack('B',65) # donne b'A' = i.e. le caractère dont le code = 65
\ No newline at end of file
# 7 Types principaux en Python
if type(1) is not int : print('oups')
if type('1') is not int : print(1)
type(3.14)
# float
type(None)
# NoneType
f = lambda c : c if type(c) is str and c.isalpha() else '?'
type(f)
# function
# Utilisation de f (voir plus loin les "lambda expressions") :
f('a') # donne 'a' car 'a' est alphabétique
f('1') # donne '?' car '1' n'est pas alphabétique
f(1) # donne '?' car 1 n'est pas une chaine
import sys;
print(sys.int_info)
# sys.int_info(bits_per_digit=30, sizeof_digit=4)
print(sys.float_info)
# sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
# min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
# mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
print(sys.getsizeof(float)) # La taille de la classe float
# 400
print(sys.getsizeof(float())) # La taille d'un float
# 24 # Ce que coute en octes un réel
# (valeur + infos supplémentaires)
# Vous devez valider ce code qui affiche le numéro de version
# Rien d'autre à faire !
import sys
if not sys.version_info.major == 3 and sys.version_info.minor >= 7:
print("Python 3.7 ou supérieur est nécessaire !")
else:
print("Vous utilisez {}.{}.".format(sys.version_info.major, sys.version_info.minor))
sys.exit(1)
\ No newline at end of file
code = zip([1000,900 ,500,400 ,100,90 ,50 ,40 ,10 ,9 ,5 ,4 ,1], ["M" ,"CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"])
# fonctions
def zipConversionRomain(num):
res = []
for d, r in code:
while num >= d:
res.append(r)
num -= d
return ''.join(res)
# programme principal -----------------------------------------------
for i in range(1, 15):
print(i, zipConversionRomain(i))
\ No newline at end of file
File deleted
This diff is collapsed.
data = []
with open("etudiants.txt") as f:
keys = None
for line in f:
l = [w.strip() for w in line.split(';')]
if keys is None:
keys = l
else:
data.append(l)
print(data)
\ No newline at end of file
#%%
import time
import random
import matplotlib.pyplot as plt
import numpy as np
#%matplotlib inline
nvalues = [100, 500, 1000, 1500, 2000, 2500, 3000]
timesEtudiants1 = []
for i in nvalues:
random.seed()
p = 12**2 # Ordre de grandeur des valeurs
liste = []
for x in range(i): liste.append(random.randint(0, p))
a=time.perf_counter()
e1 = []
for n in liste:
e1.append(n)
b=time.perf_counter()
timesEtudiants1.append(b-a)
plt.plot(nvalues,timesEtudiants1, "r-", label="Etudiants 1")
plt.title("Comparaison des performances")
# %%
from File import *
if __name__=="__main__":
data = []
with open("etudiants.txt") as f:
keys = None
for line in f:
l = [w.strip() for w in line.split(';')]
if keys is None:
keys = l
else:
data.append({k:v for k, v in zip(keys, l)})
file = File()
for d in data:
file.ajoute(d)
e = file.renvoie(lambda x : x['filiere'] == "PC")
print(e['nom'] + " " + e['prenom'])
\ No newline at end of file
import heapq
tas = []
for i in range(5): heapq.heappush(tas, i)
while not len(tas) == 0:
print(heapq.heappop(tas), end=" ")
# 0 1 2 3 4
\ No newline at end of file
import queue
pile = queue.LifoQueue()
for i in range(5): pile.put(i)
while not pile.empty():
print(pile.get(), end=" ")
# 4 3 2 1 0
\ No newline at end of file
from Pile import *
if __name__=="__main__":
data = []
with open("etudiants.txt") as f:
keys = None
for line in f:
l = [w.strip() for w in line.split(';')]
if keys is None:
keys = l
else:
data.append({k:v for k, v in zip(keys, l)})
p = Pile()
for d in data:
p.ajoute(d)
e = p.supprime()
assert(e['nom'] == "Arthaud" and e['prenom'] == "Nathalie")
\ No newline at end of file
from Tas import *
if __name__=="__main__":
data = []
with open("etudiants.txt") as f:
keys = None
for line in f:
l = [w.strip() for w in line.split(';')]
if keys is None:
keys = l
else:
data.append({k:v for k, v in zip(keys, l)})
tas = Tas()
for d in data:
tas.ajoute(int(d['moyenne']))
r = tas.get_racine()
fg, fg_i = tas.get_fils_gauche(0)
ffg, ffg_i = tas.get_fils_droit(0)
assert(r == 19 and fg == 14 and ffg == 16)
\ No newline at end of file
nom;prenom;filiere;moyenne;absences
Dickerson;Chaney;PSI;6;4
King;Shay;PSI;2;2
Chan;Aileen;MP;8;3
Schwartz;Destiny;PSI;16;5
Parrish;Prescott;MP;1;4
Calhoun;Brenda;PSI;10;4
Watts;Shellie;PSI;6;4
Baird;Octavius;PC;4;2
Graves;Vanna;MP;10;0
Decker;Nerea;PSI;9;4
Townsend;Naomi;PC;13;3
Decker;Nichole;PSI;10;3
Mcgee;Blythe;PSI;7;0
Franco;Cyrus;PSI;12;1
Mcdowell;Zelda;MP;6;1
Cherry;Stone;PSI;10;5
Mendoza;Kirsten;PC;18;0
Mathis;Xaviera;MP;12;1
Booker;Gretchen;MP;2;1
Hansen;Slade;PSI;1;1
Mason;Forrest;MP;13;3
Wolfe;Fatima;PC;15;5
Richards;Justine;MP;20;0
Kelley;Abra;PC;2;4
Lang;Ulysses;PSI;2;5
Barker;Austin;MP;15;5
Cannon;Kalia;MP;3;4
Higgins;Charity;PSI;18;5
Parker;Fritz;PC;14;2
Chambers;Yvette;PSI;7;1
Vinson;Denise;PSI;3;5
Mcmillan;Chava;PSI;7;3
Gallegos;Lee;PC;16;0
Ferguson;Solomon;PSI;11;4
Camacho;Zena;MP;4;4
Sexton;Fallon;PSI;16;2
Campbell;Preston;PC;0;0
Greer;Lance;PC;15;5
Oliver;Aileen;PC;10;2
Nielsen;Timon;PC;1;2
Perkins;Roth;PSI;1;2
Beard;Blair;PSI;13;1
Burgess;Keely;PSI;4;4
Wells;Honorato;PSI;17;0
Richardson;Cherokee;PC;20;1
Burgess;Blair;PC;16;4
Rice;Florence;PC;7;4
Villarreal;Delilah;MP;9;5
Cherry;Maite;MP;0;2
Berry;Jermaine;MP;17;4
Shepherd;Alfreda;PSI;20;5
Workman;April;PC;3;1
Carter;Herman;PC;11;1
Carpenter;Teagan;MP;0;1
Fowler;Leo;PSI;19;4
Mcgee;Kristen;PSI;9;4
Mcpherson;Channing;PC;0;0
Diaz;Nathaniel;PC;4;5
Klein;Maisie;PSI;16;3
Graham;Gary;MP;12;4
Jensen;Raja;PC;18;1
Wilkinson;Sawyer;PSI;10;0
Wagner;Lamar;MP;16;2
Abbott;Sybil;MP;17;1
Glenn;Arsenio;PSI;4;3
Moreno;Axel;PSI;7;4
Barrett;Dylan;PC;14;4
Keith;Rae;PC;9;1
Shepard;Rebecca;PC;8;5
Meyers;Illiana;MP;3;3
Bruce;Tatyana;MP;2;0
Mcguire;Olivia;PC;17;4
Merritt;Tatiana;PSI;19;3
Berg;Dale;MP;12;1
Matthews;Tallulah;MP;5;3
Steele;Cain;MP;8;2
Ellison;Britanni;PC;4;1
Richards;Audra;MP;15;0
Hood;Rylee;PSI;1;4
Townsend;Paul;PC;14;4
Cervantes;Thaddeus;MP;10;5
Dudley;Amela;PC;2;0
Lucas;Matthew;MP;4;1
Parrish;Nell;PC;13;3
Patterson;Bertha;PSI;18;0
Knapp;Lawrence;MP;3;5
Stanton;Keith;MP;5;2
Hayes;Mannix;PSI;3;2
Pearson;Ross;PC;10;4
Kaufman;Uta;MP;10;4
Bauer;Naida;PC;20;5
Carrillo;Quamar;PC;15;3
Orr;Lisandra;PSI;6;2
Randolph;Darryl;PSI;16;4
Boyer;Prescott;PC;20;2
Serrano;Ashely;PSI;10;2
Carney;Oren;PC;7;4
Riley;Arden;PC;1;5
Phillips;Joshua;PSI;20;3
Soto;Colt;PC;16;3
\ No newline at end of file
nom;prenom;filiere;moyenne;absences
Dupond;Pierre;MP;19;0
nom;prenom;filiere;note;absences
Dupond;Pierre;MP;19;7
Dupont;Jeanne;MP;19;5
Clavier;Christian;PSI;14;1
Gilles;Eric;PC;16;3
Arthaud;Nathalie;MP;15;0
\ No newline at end of file
File deleted