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
0371 - Numarare
(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 == === Rezolvare ver. 1 === <syntaxhighlight lang="python" line> def citeste_nr_de_numere(): while True: try: nr_de_numere = int(input("Introduceti numarul de numere: ")) if nr_de_numere < 1 or nr_de_numere > 100: raise ValueError("Numarul de numere trebuie sa fie intre 1 si 100.") return nr_de_numere except ValueError as e: print(e) print("Introduceti un numar valid intre 1 si 100.") def valideaza_numar(num): if num < 0 or num > 999999999: raise ValueError("Fiecare numar trebuie sa fie un numar natural cu cel mult 9 cifre.") def citeste_lista_numere(nr_de_numere): numere = [] for i in range(nr_de_numere): while True: try: num = int(input("Introduceti numarul {}: ".format(i + 1))) valideaza_numar(num) numere.append(num) break except ValueError as e: print(e) print("Introduceti un numar valid pentru numarul {}: ".format(i + 1)) return numere def numara_perechi_cu_aceeasi_suma_cifre(numere): pair_count = 0 for i in range(1, len(numere)): suma1 = sum(int(digit) for digit in str(numere[i - 1])) suma2 = sum(int(digit) for digit in str(numere[i])) if suma1 == suma2: pair_count += 1 return pair_count if __name__ == '__main__': nr_de_numere = citeste_nr_de_numere() numere = citeste_lista_numere(nr_de_numere) pair_count = numara_perechi_cu_aceeasi_suma_cifre(numere) print("Numarul de perechi de numere cu aceeasi suma a cifrelor este: {}".format(pair_count)) </syntaxhighlight> === Rezolvare ver. 2 === <syntaxhighlight lang="python" line> # Citim numarul de numere nr_de_numere = int(input("Introduceti numarul de numere: ")) # Validam numarul de numere while True: try: if nr_de_numere < 1 or nr_de_numere > 100: raise ValueError("Numarul de numere trebuie sa fie intre 1 si 100.") break except ValueError as e: print(e) nr_de_numere = int(input("Introduceti un numar valid intre 1 si 100: ")) # Citim lista de numere numere = [] for i in range(nr_de_numere): num = int(input("Introduceti numarul {}: ".format(i + 1))) while True: try: if num < 0 or num > 999999999: raise ValueError("Fiecare numar trebuie sa fie un numar natural cu cel mult 9 cifre.") break except ValueError as e: print(e) num = int(input("Introduceti un numar valid pentru numarul {}: ".format(i + 1))) numere.append(num) # Initializam un contor pentru perechile de numere cu aceeasi suma a cifrelor pair_count = 0 # Parcurgem lista de numere si verificam perechile consecutive for i in range(1, nr_de_numere): # Calculam suma cifrelor primului numar din pereche suma1 = sum(int(digit) for digit in str(numere[i - 1])) # Calculam suma cifrelor celui de-al doilea numar din pereche suma2 = sum(int(digit) for digit in str(numere[i])) # Daca cele doua sume sunt egale, incrementam contorul if suma1 == suma2: pair_count += 1 # Afisam numarul de perechi cu aceeasi suma a cifrelor print("Numarul de perechi de numere cu aceeasi suma a cifrelor este: {}".format(pair_count)) </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