# Bai 3.1
input = 1000
print('Ket qua bai tap 3.1 : \n')
print('input : '+str(input)+'\n')
result = str(bin(input))
print('binary: %s \noutput: %s' %(result,result[result.rfind('1'):]))
print('#'*70)
print('\n')
# Bai 3.2
input = 250590
print('Ket qua bai tap 3.2 : \n')
print('input : '+str(input)+'\n')
print('output : ')
if ( input < 0) :
print('%s is negative number \n' %input)
elif (input == 0) :
print('%s is zero \n' %input)
else :
print('%s is positive number \n' %input)
print('#'*70)
print('\n')
# Bai 3.3
input = '....slsslslsls...sls.mp9.jpg.png.gif'
print('Ket qua bai tap 3.3 : \n')
print('input : %s \n' %input)
print('output : %s' %input[:input.rfind('.')])
print('#'*70)
print('\n')
# Bai 3.4
input = list(range(5,16))
print('Ket qua bai tap 3.4 : \n')
print('input : %s \n' %input)
result = list(enumerate(input))
for i in result :
print( '%s %s \n' %(i[0],i[1]) )
print('#'*70)
# Bai 3.5
input = 12
result = ['','January','February','March','April','May','June','July','August','September','October','November','December']
print('Ket qua bai tap 3.5 : \n')
print('input : %s \n' %input)
print('output :')
if(input == 2) :
month_days = 28
elif((input <8) and (input%2 == 0)):
month_days = 30
elif((input<8) and (input%2 != 0)):
month_days = 31
elif(input%2 == 0):
month_days = 31
else:
month_days = 30
print( '%s %s' %(result[input],month_days) )
print('#'*70)
print('\n')
# Bai 3.6
input = 'cho meo ga chuot vit ngan'
print('Ket qua bai tap 3.6 : \n')
print('input : %s \n' %input)
input = list(input)
result = []
for i in range(0,len(input)):
if(input.count(input[i]) == 1):
result.append(input[i])
else:
pass
print(result)
print('#'*70)
# Bai 3.7
input = list(range(0,100))
print('Ket qua bai tap 3.7 : \n')
print('input : %s \n' %input)
l5 = input[0::5]
for i in l5:
print('%s = %s * 5' %(i,int(i/5)) )
print('#'*70)
# Bai 3.8
input = list(range(0,1001))
print('Ket qua bai tap 3.8 : \n')
print('input : list(range(0,1001)) \n')
l3 = input[::3]
l5 = input[::5]
l15 = input[::15]
result = sum(l3) + sum(l5) - sum(l15)
print(result)
print('#'*70)
# Bai 3.9
a = b = c = list(range(0,10))
result = []
print('Ket qua bai tap 3.9 : \n')
print('a : '+str(a)+'\n')
print('b : '+str(b)+'\n')
print('c : '+str(c)+'\n')
for i in range(0,len(a)):
for j in range(0,len(b)):
for k in range(0,len(c)):
if(c[k]==0):
pass
elif((a[i]+b[j]/c[k]) == 10):
result.append([a[i],b[j],c[k]])
print(result)
print('#'*70)
# Bai 3.10
input = list(range(0,100))
print('Ket qua bai tap 3.10 : \n')
print('input : list(range(0,100)) \n')
result = [2]
for i in range(3,len(input)):
# check if this is prime
tmp = 0
for j in range(2,i):
if ((input[i]%j) == 0):
tmp = tmp + 1
if(tmp < 1):
result.append(input[i])
print(result)
print('#'*70)
# Bai 1 Extend
input = 'toi la aia ai la toi toi la ai'
print('Ket qua bai tap 1 extend : \n')
print('input : '+str(input)+'\n')
input = list(input)
result = []
for i in input:
if (i not in result) and (i != ' '):
result.append(i)
for j in result:
print(str(j) + ' : ' + str(input.count(j)))
print('#'*70)
# Bai 2.1 Extend
input = 25051990
print('Ket qua bai tap 2.1 extend : \n')
print('input : '+str(input)+'\n')
hex_char = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
modulo = 0
result = ''
while input>1:
modulo = input%16
result = hex_char[int(modulo)] + result
input = input/16
print('output : ' + result)
print('#'*70)
# Bai 2.2 Extend
print('Ket qua bai tap 2.2 extend : \n')
a = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
b = ['0','1','2','3','4','5','6','7','8','9','G','H','I','J']
print('A : '+str(a))
print('B : '+str(b))
result = []
for i in a :
if ((i not in result) and (i in b)) :
result.append(i)
for j in b :
if ((j not in result) and (j in a)) :
result.append(j)
print('output items in both (A and B): ' + str(result))
result = []
for i in a:
if i not in b:
result.append(i)
print('output items only in A and not in B: ' + str(result))
result = []
for i in b:
if i not in a:
result.append(i)
print('output items only in B and not in A: ' + str(result))
result = []
merged = a + b
for i in merged :
if i not in result :
result.append(i)
print('output items in A and in B: ' + str(result))
result = []
for i in a:
if (i not in (result+b)):
result.append(i)
for j in b:
if (j not in (result+a)):
result.append(j)
print('output items only in A or only in B: ' + str(result))
print('#'*70)