-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlab01_b.py
More file actions
86 lines (66 loc) · 1.67 KB
/
lab01_b.py
File metadata and controls
86 lines (66 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import math
def GCD(n1,n2):
if n1>n2:
n1,n2=n2,n1
div = 1
for n in range(1,n1+1):
if n1%n==0 and n2%n == 0:
div = n
return div
def dist(x1,y1,x2,y2):
return math.sqrt((x1-x2)**2 + (y1-y2)**2)
def sumOfDigits(n):
sum = 0
while n>0:
sum+=n%10
n//=10
return sum
def uniqueSortedWords(list):
list.sort()
newList = []
for str in list:
if str not in newList:
newList.append(str)
return newList
def first_n(str,n):
if len(str) <= n:
return str
else:
res = str[:n]
return res
# def main():
#EX1:
a = int(input('Give a number: '))
while a != 0:
if a%2 == 0:
print('{} is even.'.format(a))
else:
print('{} is odd.'.format(a))
a = int(input('Give a number: '))
#Ex2:
str = input('Give two numbers and seperate with colon: ')
a,b = str.split(',')
print(a,b)
print(GCD(int(a),int(b)))
#Ex3:
s1 = input('Give the (x,y) coordinates of the first point: ')
s2 = input('Give the (x,y) coordinates of the second point: ')
x1,y1 = s1.split()
x2,y2 = s2.split()
print('The Euclidean distance between ({},{}) and ({},{}) points is {:.3f}'.format(x1,y1,x2,y2,dist(int(x1),int(y1),int(x2),int(y2))))
#Ex4:
n = int(input('Give a decimal number: '))
print('The sum of digits: {:d}'.format(sumOfDigits(n)))
#Ex5:
ex1List = ['red', 'white', 'black', 'red', 'green', 'black']
print(uniqueSortedWords(ex1List))
#Ex6:
print(first_n(input('Give a word:'),int(input('Give n:'))))
# sum = 0
# for i in range(0,5):
# n = int(input('Give {}. number: '.format(i)))
# sum+=n
# print('The sum: ',sum)
# main()
# if __name__ == "__main__":
# main()