0% found this document useful (0 votes)
13 views

Eecs Assignment2

Uploaded by

9rdn5k72m9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Eecs Assignment2

Uploaded by

9rdn5k72m9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction to EECS

Assignment-2

Assignment.3.1.1
1. Int – 3
2. Int – 3
3. noneType – error
4. noneType - error

Assignment.3.1.2
def compare(x, y):
if x>y:
return 1
elif x==y:
return 0
elif x<y:
return -1

Assignment.3.1.3
def arithmeticIf(v, a, b, c):
if v>0:
return a
elif v==0:
return b
elif v<0:
return c

Assignment.3.1.4
def clip(lo, x, hi):
if x<lo:
return lo
if x>hi:
return hi
else:
return hi

Assignment.3.1.5
def clip(lo,x,hi):
a=min(x, lo)
if a==x:
return lo
else:
return hi
b=max(x, hi)
if b==x:
return hi
else:
return hi

Assignment.3.2.1
def multIA(m, n):
s=0
while n>0:
s=s+m
n=n-1
return s

Assignment.3.2.2
def multIAgen(m, n):
s=0
while n>0:
s=s+m
n-=1
return s

Assignment.3.2.3
def mod(m, n):
a=m//n
s=m-n*a
return s

Assignment.3.2.4

Assignment.3.2.5
def prime(n):
for i in range (2,n):
if (n%i)==0:
return False
else:
return True

Assignment.3.2.6
def p2(x):
if x>1:
n=1
while 2**n<=x:
n+=1
return n-1
else:
return 0

Assignment.3.2.7
import math
def perfectSquare(n):
a=math.sqrt(n)
if a*a==n:
return True
else:
return False
Assignment.4.1.1
import math
def quadraticRoots(a, b, c):
if a!=0:
d = (b*b)-(4*a*c)
if d>0:
r1 = ((-b + math.sqrt(d))/(2*a))
r2 = ((-b - math.sqrt(d))/(2*a))
print r1
print r2
else d==0:
r = (-b/2*a)
print r
else:
r = -c/bq
return r

Assignment.4.1.2
import math
def quadraticRoots(a, b, c):
if a!=0:
d = (b*b)-(4*a*c)
if d>0:
r1 = ((-b + math.sqrt(d))/(2*a))
r2 = ((-b - math.sqrt(d))/(2*a))
print r1
print r2
elif d==0:
r = (-b/2*a)
print r
else:
real = (-b/(2*a))
imag = (math.sqrt(-d)/2*a)
print complex(real,imag)
print complex(real,-imag)
else:
r = -c/bq
return r

Assignment.5.1.1
1. string - hi
2. string – h
3. string - i
4. noneType – error
5. int – 1
6. list – [3, 'John', 4]
7. string – hi
8. string – John
9. 4
10. noneType – error
11. list – [1]
12. list – [1, 2, [3, ‘John’, 4]]
13. int – 4
14. list – [3, ‘John’, 4]
15. list – [0, 1, 2]
16. list – [3, 4, 5, 6, 7, 8, 9]
17. list – [ 3, 6, 9]
18. list – []
19. list – [10, 9, 8, 7, 6, 5, 4]
20. list – [0, 1, 2, 3]
21. Boolean – False
22. Boolean - True
23. Boolean – False
24. Boolean – False
25. Boolean – True
26. Int – 6
27. List – [0, 1, 2]
28. List – [0, 2, 4]
29. Int – 6
30. List – [-8, -2, -1]
31. List – [10, 4, 3]
32. List – [0, 2, 4]

Assignment.5.1.2
1. range (0, 5)
2. range (44, 47)
3. range (-3, 2)
4. [x for x in range (1, 8) if x%2 != 0]
5. range (5, 1, -1)

Assignment.5.2.1
import math
def pointDist (x1, y1, x2, y2):
d = math.sqrt((x2-x1)**2 + (y2-y1)**2)
return d

Assignment.5.2.2
import math
def perpDist (px, py, a, b, c):
d1 = abs(a*px + b*py + c)
d2 = math.sqrt(a**2+b**2)
d = d1/d2
return d

Class:206
St: Temulen B

You might also like