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
2228 - expresie10
(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="python3" line="1"> from typing import List def verifica_restricții(expresie: str) -> bool: if len(expresie) > 200: return False for c in expresie: if c.isdigit(): if not 0 <= int(c) <= 30000: return False return True def proceseaza_expresie(input_file: str, output_file: str): with open(input_file, 'r') as f: continut = f.read().replace('\n', '') if not verifica_restricții(continut): with open(output_file, 'w') as f: f.write("Datele nu corespund restrictiilor impuse") return stiva: List[int] = [] ok = True e = False nr1 = nr2 = x = sum = 0 x = -1 numar_curent = "" for c in continut: if c == '[': nr1 += 1 elif c == ']': nr2 += 1 if nr1 or nr2: e = True if c == '[' and x == -1: stiva.append(x) else: if c.isdigit(): numar_curent += c # Construim numărul curent if len(numar_curent) > 5 or int(numar_curent) > 30000: # Verificăm lungimea și valoarea numărului with open(output_file, 'w') as f: f.write("Datele nu corespund restrictiilor impuse") return x = int(numar_curent) else: numar_curent = "" # Resetează numărul curent când întâlnești un non-digit if c == ']' and x != -1 and stiva[-1] == -1 and stiva: stiva.pop() stiva.append(x >> 1) x = -1 nr1 -= 1 nr2 -= 1 elif c == ']' and stiva and stiva[-1] >= 0 and len(stiva) >= 2: minus = True while minus: a = stiva.pop() if not stiva: break b = stiva.pop() if b != -1: stiva.append(a + b) else: minus = False break stiva.append(a >> 1) nr1 -= 1 nr2 -= 1 if nr2 > nr1: ok = False else: ok = False if nr1 or nr2 or not e: ok = False while stiva and ok: if stiva[-1] != -1: sum += stiva.pop() else: ok = False break with open(output_file, 'w') as f: if ok: f.write(str(sum)) else: f.write("expresie gresita") if __name__ == "__main__": input_file = 'expresie10IN.txt' output_file = 'expresie10OUT.txt' proceseaza_expresie(input_file, output_file) </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