-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask04.py
More file actions
144 lines (124 loc) · 2.68 KB
/
Task04.py
File metadata and controls
144 lines (124 loc) · 2.68 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Section 1
def reverse(name):
rev=''
length=len(name)-1
while length>=0:
rev+=name[length]
length-=1
return rev
print("Enter your name")
name=input()
answer=reverse(name)
print("your reversed name is {}".format(answer))
# Section 2
upper_case=0
lower_case=0
def calculate(name):
global upper_case, lower_case
for i in name:
if i.islower():
lower_case+=1
elif i.isupper():
upper_case+=1
print("Enter any string")
name=input()
calculate(name)
print("No. of Upper case characters :", upper_case)
print("No. of Lower case characters :", lower_case)
# Section 3
def unique(numbers):
return list(set(numbers))
print("Enter any numbers that might be repeated separated by commas")
numbers=input().split(',')
answer=unique(numbers)
print(answer)
print(type(answer))
# Section 4
print("Enter list of words separated by hyphen")
names=input().split("-")
names.sort()
print(names)
print("-".join(names))
# Section 5
lines=[]
print("enter sequence of lines")
while True:
inp = input()
if inp:
lines.append(inp)
else:
break
print(lines)
for i in range(len(lines)):
print(lines[i].capitalize())
# Section 6
def sum(a,b):
return int(a)+int(b)
print("Enter any two numbers")
x=input()
y=input()
result=sum(x,y)
print("The resultant sum is: ", result)
# Section 7
def print_string(a,b):
if len(a)>len(b):
print(a)
elif len(b)>len(a):
print(b)
elif len(a)==len(b):
print(a)
print(b)
print("enter the first string")
str1=input()
print("enter the second string")
str2=input()
print_string(str1,str2)
# Section 8
def squares():
test=[]
for i in range(1,21,1):
test.append(i**2)
print(tuple(test))
squares()
# Section 9
def showNumbers(limit):
for i in range(0,limit+1,1):
if i%2==0:
print(i," EVEN")
elif i%2 !=0:
print(i," ODD")
print("Enter any integer number")
sample=eval(input())
showNumbers(sample)
# Section 10
sample=[]
for i in range(1,21,1):
sample.append(i)
def checker(x):
if x%2==0:
return True
else:
return False
answer = list(filter(checker,sample))
print(answer)
# Section 11
sample=[1,2,3,4,5,6,7,8,9,10]
even=filter(lambda x: x%2==0,sample)
print(even)
answer=map(lambda x: x**2,list(even))
print(list(answer))
# Section 12
try:
5/0
except ZeroDivisionError:
print("Cant divide any number by zero")
# Section 13
from functools import reduce
def test(x1,x2):
return x1+x2
print(reduce(test,[[1,2,3],[4,5],[6,7,8]]))
sample=reduce(test,[[1,2,3],[4,5],[6,7,8]])
print(reduce(lambda x,y: x*10+y, sample))
# Section 14
(i) 2
(ii) NameError: name 'f' is not defined