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
1502 - Virgule
(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"> #1502 Virgule def insert_commas(digits): n = len(digits) # Inițializăm o listă pentru a memora pozițiile unde inserăm virgule comma_positions = [] # Adăugăm o virgulă la finalul șirului dacă este necesar for i in range(1, n): if digits[i] <= digits[i - 1]: comma_positions.append(i) # Construim secvența cu virgule result_with_commas = [] start = 0 for pos in comma_positions: result_with_commas.extend(map(str, digits[start:pos])) result_with_commas.append(',') start = pos # Adăugăm ultima grupare de cifre result_with_commas.extend(map(str, digits[start:])) # Eliminăm virgulele consecutive final_result = [] for i in range(len(result_with_commas) - 1): if result_with_commas[i] != ',' or result_with_commas[i + 1] != ',': final_result.append(result_with_commas[i]) # Adăugăm ultimul element final_result.append(result_with_commas[-1]) return final_result def validate_input(digits): if not digits or not all(char.isdigit() for char in digits): return False, "Introduceți un șir valid de cifre." return True, None if __name__ == "__main__": # Citim șirul de cifre din fișierul de intrare with open("virgule.in", "r") as file: digits = file.readline().strip() # Validăm datele de intrare is_valid, error_message = validate_input(digits) if not is_valid: with open("virgule.out", "w", encoding="utf-8") as file_out: file_out.write(error_message) exit() # Calculăm și afișăm rezultatul în fișierul de ieșire digits = list(map(int, digits)) result = insert_commas(digits) with open("virgule.out", "w", encoding="utf-8") as file_out: file_out.write("".join(result)) </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