Week 1 Practice Programming
Assignment
Week 1 Practice Programming Assignment
Problem 1
Solution
Problem 2
Solution
Problem 1
Twin primes are pairs of prime numbers that differ by 2. For example (3, 5), (5, 7), and (11,13) are
twin primes.
Write a function Twin_Primes(n, m) where n and m are positive integers and n < m , that
returns all unique twin primes between n and m (both inclusive). The function returns a list of
tuples and each tuple (a,b) represents one unique twin prime where n <= a < b <= m .
Sample Input 1
1 1
2 15
Output
1 [(3, 5), (5, 7), (11, 13)]
Sample Input 2
1 11
2 25
Output
1 [(11, 13), (17, 19)]
Solution
1 def prime(n):
2 if n < 2:
3 return False
4 for i in range(2,n//2+1):
5 if n%i==0:
6 return False
7 return True
8
9 def Twin_Primes(n, m):
10 Res=[]
11 for i in range(n,m-1):
12 if prime(i)==True:
13 if prime(i+2)==True:
14 [Link]((i,i+2))
15 return(Res)
Suffix Code(Visible)
1 n=int(input())
2 m=int(input())
3 print(sorted(Twin_Primes(n, m)))
Public Test Cases
Input 1
1 1
2 15
Output
1 [(3, 5), (5, 7), (11, 13)]
Input 2
1 11
2 25
Output
1 [(11, 13), (17, 19)]
Input 3
1 5
2 50
Output
1 [(5, 7), (11, 13), (17, 19), (29, 31), (41, 43)]
Private Test Cases
Input 1
1 1
2 100
Output
1 [(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73)]
Input 2
1 1
2 10
Output
1 [(3, 5), (5, 7)]
Input 3
1 60
2 200
Output
1 [(71, 73), (101, 103), (107, 109), (137, 139), (149, 151), (179, 181), (191,
193), (197, 199)]
Input 4
1 50
2 73
Output
1 [(59, 61), (71, 73)]
Input 5
1 17
2 19
Output
1 [(17, 19)]
Problem 2
Create a Triangle class that accepts three side-lengths of the triangle as a , b and c as
parameters at the time of object creation. Class Triangle should have the following methods:
Is_valid() :- Returns Valid if triangle is valid otherwise returns Invalid .
A triangle is valid when the sum of its two side-length are greater than the third one.
That means the triangle is valid if all three condition are satisfied:
a+b>c
a+c>b
b+c>a
Side_Classification() :- If the triangle is invalid then return Invalid . Otherwise, it returns
the type of triangle according to the sides of the triangle as follows:
Return Equilateral if all sides are of equal length.
Return Isosceles if any two sides are of equal length and third is different.
Return Scalene if all sides are of different lengths.
Angle_Classification() :- If the triangle is invalid then return Invalid . Otherwise, return
type of triangle using Pythagoras theorem.
Assume a <= b <= c . then
If return Acute
If return Right
If return Obtuse
Area() :- If the triangle is invalid then return Invalid . Otherwise, return the area of the
triangle.
Where
Sample Input 1
1 2
2 3
3 4
Output
1 Valid
2 Scalene
3 Obtuse
4 2.9047375096555625
Sample Input 2
1 10
2 3
3 5
Output
1 Invalid
2 Invalid
3 Invalid
4 Invalid
Solution
1 class Triangle:
2 def __init__(self,a,b,c):
3 self.a = a
4 self.b = b
5 self.c = c
6 def is_valid(self):
7 if self.a >= self.b + self.c:
8 return 'Invalid'
9 if self.b >= self.a + self.c:
10 return 'Invalid'
11 if self.c >= self.b + self.a:
12 return 'Invalid'
13 return 'Valid'
14 def Side_Classification(self):
15 if self.is_valid() == 'Valid':
16 if self.a == self.b == self.c:
17 return 'Equilateral'
18 elif (self.a == self.b) or (self.b == self.c) or (self.a ==
self.c):
19 return 'Isosceles'
20 else:
21 return 'Scalene'
22 else:
23 return 'Invalid'
24 def Angle_Classification(self):
25 if self.is_valid() == 'Valid':
26 l = [self.a**2, self.b**2, self.c**2]
27 [Link]()
28 if l[0] + l[1] > l[2]:
29 return "Acute"
30 elif l[0] + l[1] == l[2]:
31 return "Right"
32 elif l[0] + l[1] < l[2]:
33 return "Obtuse"
34 else:
35 return 'Invalid'
36 def Area(self):
37 if self.is_valid() == 'Valid':
38 s = (self.a + self.b + self.c)/2
39 return (s*(s-self.a)*(s-self.b)*(s-self.c)) ** 0.5
40 else:
41 return 'Invalid'
Suffix code(visible)
1 a=int(input())
2 b=int(input())
3 c=int(input())
4 T=Triangle(a,b,c)
5 print(T.is_valid())
6 print(T.Side_Classification())
7 print(T.Angle_Classification())
8 print([Link]())
Public Test Cases
Input 1
1 2
2 3
3 4
Output
1 Valid
2 Scalene
3 Obtuse
4 2.9047375096555625
Input 2
1 10
2 3
3 5
Output
1 Invalid
2 Invalid
3 Invalid
4 Invalid
Input 3
1 5
2 5
3 5
Output
1 Valid
2 Equilateral
3 Acute
4 10.825317547305483
Private Test Cases
Input 1
1 2
2 12
3 3
Output
1 Invalid
2 Invalid
3 Invalid
4 Invalid
Input 2
1 14
2 5
3 12
Output
1 Valid
2 Scalene
3 Obtuse
4 29.230762904857617
Input 3
1 32
2 12
3 21
Output
1 Valid
2 Scalene
3 Obtuse
4 61.89456761299815
Input 4
1 10
2 10
3 5
Output
1 Valid
2 Isosceles
3 Acute
4 24.206145913796355
Input 5
1 3
2 4
3 5
Output
1 Valid
2 Scalene
3 Right
4 6.0