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
2375 - PalindromSD
(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> #2375 def is_palindrome(x): """Verifică dacă un număr este palindrom.""" aux = x og = 0 while aux != 0: og = og * 10 + aux % 10 aux //= 10 return x == og def validate_input(numbers): """Verifică dacă numărul de numere și limitele sunt conforme.""" if len(numbers) < 1 or len(numbers) > 1000: print("Datele introduse nu corespund cerintelor.") return False for n in numbers: if n < 1 or n >= 1000000000: print("Datele introduse nu corespund cerintelor.") return False return True def read_file(filename): """Citirea fișierului și returnarea unei liste cu numere.""" with open(filename, 'r') as fin: try: numbers = [int(n) for n in fin.read().split()] if not validate_input(numbers): return None except ValueError: print("Datele introduse nu corespund cerintelor.") return None return numbers def print_result_to_file(numbers, filename): """Afișează rezultatul într-un fișier.""" with open(filename, 'w') as fout: if not numbers: fout.write("-1") return st = 0 dr = len(numbers) - 1 while st <= dr: if st % 2 == 0: fout.write(str(numbers[st]) + ' ') st += 1 else: fout.write(str(numbers[dr]) + ' ') dr -= 1 if __name__ == '__main__': numbers = read_file("palindromsd.in") if numbers is not None: print("Datele introduse corespund cerintelor.") palindromes = [n for n in numbers if is_palindrome(n)] print_result_to_file(palindromes, "palindromsd.out") </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