Sunteți pe pagina 1din 6

PB39.

Pe baza exemplului de la curs (sursa modulului de simulator clc:


openbookproject.net/py4fun/logic/logic.py), s se implementeze n Python
calculul funciei din figura:

from logic import *

a = eval(raw_input('A= '))
b = eval(raw_input('B= '))
c = eval(raw_input('C= '))

not1 = Not("N1")
not2 = Not("N2")

and1 = And("A1")
and2 = And("A2")
and3 = And("A3")
and4 = And("A4")
and5 = And("A5")

not1.B.connect(and2.B)
not2.B.connect(and2.A)

not2.B.connect(and3.B)

not1.B.connect(and4.A)

and4.C.connect(and5.A)

or1 = Or("Or1")
or2 = Or("Or2")
or3 = Or("Or3")

and1.C.connect(or1.A)
and2.C.connect(or1.B)

and3.C.connect(or2.A)
and5.C.connect(or2.B)

or1.C.connect(or3.A)
or2.C.connect(or3.B)
or3.C.monitor = 1

not1.A.set(a)
not2.A.set(b)

and1.A.set(b)
and1.B.set(c)
and3.A.set(a)
and4.B.set(b)
and5.B.set(c)

PB40.

Pe baza exemplului de la curs (sursa modulului de simulator clc:


openbookproject.net/py4fun/logic/logic.py), s se implementeze n Python
calculul funciei din figura:

from logic import *

alpha = eval(raw_input("A = "))


betta = eval(raw_input("B = "))
gamma = eval(raw_input("C = "))

xor2 = Xor("XorGate, Pin")


xor1 = Xor("Xor1")

and1 = And("And1")
and2 = And("And2")

or1 = Or("OrGate, Pin")

xor1.C.connect(xor2.A)
xor1.C.connect(and2.A)

and2.C.connect(or1.A)
and1.C.connect(or1.B)

xor2.C.monitor = 1
or1.C.monitor = 1

xor1.A.set(alpha)
xor1.B.set(betta)

and1.A.set(alpha)
and1.B.set(betta)

xor2.B.set(gamma)
and2.B.set(gamma)

PB41.

Pe baza exemplului de la curs (sursa modulului de simulator clc:


openbookproject.net/py4fun/logic/logic.py), s se implementeze n Python
calculul funciei din figura:

from logic import *

A = eval(raw_input("A = "))
B = eval(raw_input("B = "))
C = eval(raw_input("C = "))

and1 = And("A1")
and2 = And("A2")
and3 = And("A3")

not1 = Not("N1")
not2 = Not("N2")

or1 = Or("O1")

xor1 = Xor("X1")

and1.C.connect(and3.A)

or1.C.connect(not1.A)

not1.B.connect(not2.A)
not1.B.connect(and2.A)

and2.C.connect(xor1.B)

not2.B.connect(and3.B)

and3.C.connect(xor1.A)

xor1.C.monitor = 1

and1.A.set(A)
and1.B.set(B)

or1.A.set(A)
or1.B.set(C)
and2.B.set(B)

PB42.

Folosind exemplul din curs, s se implementeze n Python urmtorul automat


finit:

from logic import *

def main():
#definire celor 5 stari
state1 = "0 cents deposited"
state2 = "5 cents deposited"
state3 = "10 cents deposited"
state4 = "15 cents deposited"
state5 = "20 cents or more deposited"

#succesiunea starilor in functie de nickel sau dime


nickel = [state1, state2, state3, state4, state5, state2]
dime1 = [state1, state3, state5, state3]
dime2 = [state2, state4, state5, state3, state5]
# state3 si state5 pe care le-am declarat in dime2 sunt redundante
# la felul in care am implementat(pentru ca, atunci cand ajung intr-o
# stare care se gaseste in dime2 si nu in dime1, la urmatoarea iteratie
# din bucla while se verifica mai intai in dime1

#fisierul din care citesc starea initiala


fisier = open("fisier.txt", "r")
stareaInitiala = fisier.read()
currentState = stareaInitiala
print "Starea curenta: ", currentState # afisarea starii curente

while True:
nr = input("0-nickel; 1-dime; 2-iesire din program\n")
if nr == 2:
break
if nr == 0: # in nickel sunt toate starile(dupa schema)
aux = nickel.index(currentState)
currentState = nickel[aux+1] # trec la starea urmatoare
if nr == 1:
if currentState in dime1:
aux = dime1.index(currentState)
currentState = dime1[aux+1]
else: # dupa schema, daca nu e in dime1, cu siguranta e in
dime2
aux = dime2.index(currentState)
currentState = dime2[aux+1]
print "Starea curenta: ", currentState # afisarea starii curente

main()

PB43.

S se creeze un program Python care primete un fiier oarecare (exe, dll, doc
etc) la intrare i produce ntr-un fiier text cu acelai nume un hash cu md5 peste
el.

from Crypto.Hash import MD5

FileName = raw_input("Enter file name: ")


readFile = open(FileName,'r')

writeFileName = FileName.partition(".")
writeFileName = writeFileName[0]+".txt"
writeFile = open(writeFileName,'w+')

for line in readFile:


hashLine = MD5.new(line).hexdigest()
writeFile.write(hashLine)

PB44.

Pe baza exemplului (folosind TCP) din curs. S se realizeze n Python o aplicaie


de chat simplu ntre doi parteneri.

# TCP Client Code

host="127.0.0.1" # Seteaza variabila host


port=4446 # Seteaza variabila port 4446
from socket import * # Importa socket module

s=socket(AF_INET, SOCK_STREAM) # Creaza socket-ul

s.connect((host,port)) # Se conecteaza la server

while 1:
msg=s.recv(1024)

print "Mesajul primit : " + msg


data=raw_input("Introduceti mesajul : ")
if not data: break

s.send(data)

s.close() # Inchide socket-ul


# TCP Server Code

host="127.0.0.1" # Seteaza variabila host


port=4446 # Seteaza variabila port 4446
from socket import * # Importa socket module

s=socket(AF_INET, SOCK_STREAM)

s.bind((host,port))
s.listen(1)

print "Se asteapta conexiunea.. "

q,addr=s.accept()

while 1:
data=raw_input("Introduceti mesajul : ")
q.send(data)

msg=q.recv(1024)
if not msg: break

print "Mesajul primit : " + msg

s.close()