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
1359 - Ecuatie 2
(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> # 1359 - Ecuatie 2 def validare(ecuatie1): # functia de validare a datelor de intrare if len(ecuatie1) > 1000: raise ValueError # daca datele de intrare nu corespund, ridicam eroarea ValueError for caracter in ecuatie1: if not (caracter.isdigit() or caracter in ['+', '-', '*', ':', 'x', '=']): raise ValueError # extract a and b from the equation parts = ecuatie1.split('=') a = int(''.join(filter(str.isdigit, parts[0]))) b = int(''.join(filter(str.isdigit, parts[1]))) # check if a and b are within the given range if not (1 <= a <= 30000 and 1 <= b <= 30000): raise ValueError file_out.write("Datele de intrare corespund restrictiilor impuse\n") def rezolva_ecuatie(ecuatie1): # functia de rezolvare if "+x=" in ecuatie1: a, b = map(int, ecuatie1.split("+x=")) return 1, b - a elif "x+" in ecuatie1: a, b = map(int, ecuatie1.split("x+")) return 2, b - a elif "-x=" in ecuatie1: a, b = map(int, ecuatie1.split("-x=")) return 3, a - b elif "x-" in ecuatie1: a, b = map(int, ecuatie1.split("x-")) return 4, a + b elif "*x=" in ecuatie1 and int(ecuatie1.split("*x=")[1]) % int(ecuatie1.split("*x=")[0]) == 0: a, b = map(int, ecuatie1.split("*x=")) return 5, b // a elif "x*" in ecuatie1: _, temp = ecuatie1.split("x*") a, b = map(int, temp.split("=")) return 6, b // a elif ":x=" in ecuatie1 and int(ecuatie1.split(":x=")[0]) % int(ecuatie1.split(":x=")[1]) == 0: a, b = map(int, ecuatie1.split(":x=")) return 7, a // b elif "x:" in ecuatie1: _, temp = ecuatie1.split("x:") a, b = map(int, temp.split("=")) return 8, a // b if __name__ == '__main__': file_in = open("ecuatie2in.txt", "r") # declararea fisierelor file_out = open("ecuatie2out.txt", "w") # fisierul out trebuie declarat cu optiunea "w" (write) # din cauza datelor de intrare pot aparea 2 tipuri de erori, valueError sau IndexError pe care le tratam try: ecuatie = file_in.readline() # citirea ecuatiei se face ca sir de caractere validare(ecuatie) # apelul functiei de validare tip_ecuatie, solutie = rezolva_ecuatie(ecuatie) # apelul functiei de rezolvare file_out.write(str(tip_ecuatie) + '\n') file_out.write(str(solutie) + '\n') except ValueError: file_out.write("Datele de intrare nu corespund restrictiilor impuse") except IndexError: file_out.write("Datele de intrare nu corespund restrictiilor impuse") </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