-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmid_assessment.py
More file actions
165 lines (96 loc) · 3.46 KB
/
Copy pathmid_assessment.py
File metadata and controls
165 lines (96 loc) · 3.46 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Write a decorator to count the number of decorated functions
"""
count = 0
def counter(func):
global count
count += 1
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
@counter # add = counter(add)
def add(a, b):
return a + b
@counter # sub = counter(sub)
def sub(a, b):
return a - b
# add(1, 2)
# add(1, 2)
# sub(1, 2)
# print(count)
# Write a program to print only even lines in a file( consider filename as sample.txt)
# with open("file") as f:
# for line_no, line in enumerate(f, start=1):
# if line_no % 2 == 0:
# print(line)
# Write a function that returns the nth line and last n lines from a file.
from itertools import islice
from collections import deque
n = 2
with open("file") as file:
# method 1
for line_no, line in enumerate(file, start=1):
if line_no == n:
# print(line)
break
# method 2
line = islice(file, n-1, n)
# last n lines
lines = deque(file, n)
###########################################################################
# Write a program to print longest non repeated word in the sentence,
s = "see and saw went to see a sea"
words = s.split()
d = {word: len(word) for word in words if words.count(word) == 1}
# print(sorted(d.items(), key=lambda item: item[-1])[-1])
##########################################################################
# sort the dictionary based on the last character of the key.
prices = {'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75 }
res = sorted(prices.items(), key=lambda item: item[0][-1])
# print(dict(res))
#########################################################################
# Build a list with only even length string using filter function
names = ['apple', 'google', 'yahoo', 'facebook', 'yelp', 'flipkart', 'gmail', 'instagram', 'microsoft']
def even_(item):
if len(item) % 2 == 0:
return item
# print(list(filter(even_, names)))
############################################################################
# Write a dictionary comprehension to create a dictionary with word as its key
# and if the word is of numeric type reverse it else add the word as it is in
# the value.
sentence = "12 plus 18 equals to 30"
words = sentence.split()
d = {word: word[::-1] if word.isdigit() else word for word in words}
# print(d)
"""
############################################################################
# Write a program to get the following output.
# o/p : 2A2B3C1D2A1C1D
s = "AABBCCCDAACDA"
i = 0
res = s
op = ""
while i < len(s):
res = res.lstrip(s[i])
count = len(s) - len(res) - i
op += str(count) + s[i]
i += count
# print(op)
###########################################################################
# Write a program to get the following output from the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
# [1, 2]
# [3, 4]
# [5, 6]
# [7, 8]
# [9]
l = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(0, len(l), 2):
print(l[i: i+2])
#########################################################################
# Find all max numbers from the below list
numbers = [1, 2, 3, 0, 4, 3, 2, 4, 2, 1, 0, 4]
max_num = max(numbers)
print(max_num)
for num in numbers:
if num == max_num:
print(num)