Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Bitnami MediaWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
0272 - Pare Sort
(section)
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Rezolvare == <syntaxhighlight lang="python" line="1"> # 0272 PareSort def validare(nr_elemente, sir_numere): # functia in care se verifica daca datele de intrare sunt valide # in cazul in care nu este respectata vreo restrictie, vom marca acest lucru prin eroarea ValueError if nr_elemente > 255: raise ValueError for numar in sir_numere: if numar <= 0 or numar >= 10000: raise ValueError if nr_elemente != len(sir_numere): raise ValueError file_out.write("Datele de intrare corespund restrictiilor impuse\n") def paresort(sir_numere): sir_pare = [] # creem o lista doar cu numerele pare for numar in sir_numere: if numar % 2 == 0: sir_pare.append(numar) if len(sir_pare) == 0: # daca nu exista, afisam mesajul corespunzator si incheiem programul file_out.write("nu exista") return file_out.write(str(len(sir_pare)) + "\n") # daca am ajuns aici, inseamna ca avem numere pare, afisam cate sunt sir_pare.sort() # sortam sirul deoarece trebuie in ordine crescatoare for numar in sir_pare: file_out.write(str(numar) + " ") # scriem sirul rezultat in fisier if __name__ == '__main__': file_in = open("paresortin.txt", "r") # declararea fisierelor conform cerintei file_out = open("paresortout.txt", "w") try: # in cazul in care datele de intrare nu sunt corecte, va fi eroare n = file_in.readline() sir = file_in.readline() n = int(n) sir = sir.split() # creem o lista cu fiecare element din sir separat prin spatiu sir = list(map(int, sir)) # sirul citit este un sir de caractere, il transformam in sir de int validare(n, sir) # validam datele paresort(sir) # apelam functia de rezolvare a problemei # ne asteptam la 2 tipuri de erori cauzate de datele de intrare, daca apar, le tratam corespunzator except ValueError: file_out.write("Datele de intrare nu corespund restrictiilor impuse") except IndexError: file_out.write("Datele de intrare nu corespund restrictiilor impuse") </syntaxhighlight>
Summary:
Please note that all contributions to Bitnami MediaWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Bitnami MediaWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width