Subiect A


In [1]:
i = [i for i in range(1,100) if len(str(i+len(str(i))))>len(str(i))]
print (i)


[9, 98, 99]

In [7]:
print (sorted([2,10,15,3,7,9,11], key = lambda i: i%4,reverse=True))


[15, 3, 7, 11, 2, 10, 9]

In [9]:
import time

tobj= time.localtime()
print(time.strftime("%I%p:%M:%S -%B",tobj))


06PM:24:38 -February

In [10]:
import re

print (re.split("[aeiou]+","Python exam today"))


['Pyth', 'n ', 'x', 'm t', 'd', 'y']

In [11]:
class A:
    x = [10,20,30]
    def Add(self,n): self.x += [n]

a1 = A();a2 = A()
a1.Add(10)
print (a1.x+a2.x)


[10, 20, 30, 10, 10, 20, 30, 10]

In [ ]:
'''
import threading
l = threading.Lock()
def Th1(lock):
    with lock: print("f1")

def Th2(lock):
    with lock: print("f2"); Th1(lock)

t1 = threading.Thread(target=Th1, args=(l,))
t2 = threading.Thread(target=Th2, args=(l,))

t1.start (); t2.start (); t1.join (); t2.join ()
'''

Subiect B


In [1]:
i = [i for i in range(1,100) if (i*len(str(i))) % 19 == 0]
print (i)


[19, 38, 57, 76, 95]

In [2]:
i = {i:i*4 for i in range(0,100)}

print (i[100])


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-2-ffbc29310c09> in <module>()
      1 i = {i:i*4 for i in range(0,100)}
      2 
----> 3 print (i[100])

KeyError: 100

In [4]:
s = ""

for i in range(3,6):
    s = s+str(i)*(i>>1)

print (s)


34455

In [5]:
try: x = 5 / 0

except: print("Generic exception")

except ArithmeticError: print("ArithmeticError")

else: print("All ok")


  File "<ipython-input-5-48f4d6bdc194>", line 1
    try: x = 5 / 0
SyntaxError: default 'except:' must be last

In [6]:
print (sorted([2,10,15,3,7,9,11], key = lambda i: i%4,reverse=True))


[15, 3, 7, 11, 2, 10, 9]

In [7]:
import re

print (re.findall("(\d{2})(\d*)",

"Meeting from 20:30 is at meeting room 112"))


[('20', ''), ('30', ''), ('11', '2')]

In [8]:
def fnc(self,v1,v2): return v1+v2

class A: pass

m = A()

m.Test= fnc

print (m.Test(1,2))


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-88a15c64aaac> in <module>()
      7 m.Test= fnc
      8 
----> 9 print (m.Test(1,2))

TypeError: fnc() takes exactly 3 arguments (2 given)

In [ ]:
import threading

b = threading.Barrier(3)

def WorkerThread(b):

b_id = b.wait()

print (b_id)

t = []

for i in range(0,10):

t += [threading.Thread(target=WorkerThread, args=(b,))]

for _th in t: _th.start()

for _th in t: _th.join()

In [12]:
import struct

for i in struct.pack("@chci",b'0', 1, b'1',2):
    print (int(i))


0
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-12-a822b307f5a6> in <module>()
      2 
      3 for i in struct.pack("@chci",b'0', 1, b'1',2):
----> 4     print (int(i))

ValueError: invalid literal for int() with base 10: ''

In [12]:
import ctypes
'''
lib = ctypes.cdll.LoadLibrary("e.dll")
size = ctypes.c_int()
lib.ParseString.restype = ctypes.c_wchar_p
result = lib.ParseString(ctypes.c_wchar_p("...") , ctypes.byref(size))
'''


Out[12]:
'\nlib = ctypes.cdll.LoadLibrary("e.dll")\nsize = ctypes.c_int()\nlib.ParseString.restype = ctypes.c_wchar_p\nresult = lib.ParseString(ctypes.c_wchar_p("...") , ctypes.byref(size))\n'

In [ ]: