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
2442 - Cifre 006
(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 determina_numar_aparitii(numar, cifra_data): count = 0 for cifra in numar: if int(cifra) == cifra_data: count += 1 return count def muta_cifre(numar, cifra_data): prefix = [] rest = [] for cifra in numar: if int(cifra) == cifra_data: prefix.append(cifra) else: rest.append(cifra) return prefix + rest if __name__ == '__main__': try: with open("cifre.in", "r") as fin, open("cifre.out", "w") as fout: line = fin.readline().strip().split() if len(line) != 2: raise ValueError("Linia de intrare nu contine 2 valori separate de spatiu") numar, cifra_data = line if not numar.isdigit() or not cifra_data.isdigit(): raise ValueError("Datele de intrare trebuie sa fie numere intregi") numar = list(numar) cifra_data = int(cifra_data) count = determina_numar_aparitii(numar, cifra_data) fout.write(str(count) + "\n") rezultat = muta_cifre(numar, cifra_data) fout.write("".join(rezultat) + "\n") except Exception as e: print("A intervenit o eroare: ", e) </syntaxhighlight> === Rezolvare ver. 2 === <syntaxhighlight lang="python" line> # deschidem fisierele de intrare si iesire try: with open("cifre.in", "r") as fin, open("cifre.out", "w") as fout: # citim numarul si cifra line = fin.readline().strip().split() if len(line) != 2: raise ValueError("Linia de intrare nu contine 2 valori separate de spatiu") numar, cifra_data = line if not numar.isdigit() or not cifra_data.isdigit(): raise ValueError("Datele de intrare trebuie sa fie numere intregi") numar = list(numar) cifra_data = int(cifra_data) # a) determinam de cate ori apare cifra in numarul dat count = 0 for cifra in numar: if int(cifra) == cifra_data: count += 1 # scriem in fisierul de iesire numarul de aparitii ale cifrei in numarul dat fout.write(str(count) + "\n") # b) mutam cifrele egale cu cifra data la inceput si restul cifrelor in ordine prefix = [] rest = [] for cifra in numar: if int(cifra) == cifra_data: prefix.append(cifra) else: rest.append(cifra) # concatenam prefixul format din cifrele egale cu k si restul cifrelor in ordine si scriem rezultatul in fisierul de iesire fout.write("".join(prefix + rest) + "\n") except Exception as e: print("A intervenit o eroare: ", e) </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