20 files changed

+999
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>kumarpadhi_GroupA</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.python.pydev.PyDevBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.python.pydev.pythonNature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<?eclipse-pydev version="1.0"?>
3+
4+
<pydev_project>
5+
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
6+
<path>/kumarpadhi_GroupA</path>
7+
</pydev_pathproperty>
8+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
9+
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
10+
</pydev_project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
'''
2+
Created on July 9, 2015
3+
@author: Kumar Sabyasachi Padhi
4+
'''
5+
import mimetypes as mtype
6+
7+
def take(input_list=None, count=None):
8+
"""
9+
(list, int) -> list
10+
Takes a list "input_list" and a number "count"
11+
and returns the first n elements as list
12+
"""
13+
try:
14+
assert isinstance(input_list, list)
15+
assert isinstance(count, int)
16+
assert(count > 0)
17+
assert(len(input_list) >= count)
18+
for value in input_list:
19+
assert isinstance(value, int)
20+
return input_list[:count]
21+
except:
22+
return []
23+
24+
25+
def flatten(nested_list=None):
26+
"""
27+
(list) -> list
28+
Takes a list of a lists
29+
and returns all elements in the nested list as a single list
30+
"""
31+
try:
32+
assert isinstance(nested_list, list)
33+
final_list = []
34+
for inlist in nested_list:
35+
assert isinstance(inlist, list)
36+
for element in inlist:
37+
assert isinstance(element, int)
38+
final_list.append(element)
39+
return final_list
40+
except:
41+
return []
42+
43+
44+
def indexer(input_filename=None):
45+
"""
46+
(text file) -> dict(map of string, index list)
47+
read a text file, split them into words (seperated by spaces, tabs and newlines)
48+
returning a map of word to line numbers
49+
"""
50+
try:
51+
if mtype.guess_type(input_filename)[0] != 'text/plain':
52+
return {}
53+
ofile = open(input_filename,"r")
54+
count = 0
55+
mapdict = {}
56+
for inp_string in ofile.readlines():
57+
count += 1
58+
word = ''
59+
for char in inp_string:
60+
if char in (" ", "\t", "\n"):
61+
indexer_helper(word, mapdict, count)
62+
word = ''
63+
else:
64+
word += char
65+
indexer_helper(word, mapdict, count)
66+
ofile.close()
67+
return mapdict
68+
except IOError:
69+
return {}
70+
except:
71+
return {}
72+
73+
74+
def indexer_helper(word, mapdict, count):
75+
"""
76+
(str, dict, int)
77+
Its a helper function for indexer
78+
takes the word map and count as input checks the word and inserts word into
79+
map
80+
"""
81+
if word != '':
82+
if mapdict.get(word, "not present") == "not present":
83+
mapdict[word] = [count]
84+
else:
85+
mapdict[word].append(count)
86+
87+
88+
def compress(sorted_list=None):
89+
"""
90+
(list) -> tuple in list
91+
converts a sorted list to a list of pairs,
92+
where each pair contains an element from the list and its count
93+
"""
94+
try:
95+
assert isinstance(sorted_list, list)
96+
result = []
97+
curr_val = None
98+
count = 0
99+
for value in sorted_list:
100+
assert isinstance(value, int)
101+
if curr_val is None:
102+
curr_val = value
103+
count = 1
104+
elif curr_val != value:
105+
assert (curr_val < value)
106+
result.append((curr_val, count))
107+
curr_val = value
108+
count = 1
109+
else:
110+
count += 1
111+
result.append((curr_val, count))
112+
return result
113+
except:
114+
return []
115+
116+
if __name__ == '__main__':
117+
print take([2, 3, 6, 1, 5], 2)
118+
print flatten([[2, 3, 4], [5, 4], [23, 44, 56, 78]])
119+
print compress([-1, 0, 0, 0, 10, 23, 23, 14, 44, 44, 44, 54])
120+
print indexer("test.txt")
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
'''
2+
Created on Apr 30, 2015
3+
@author: Kumar Sabyasachi Padhi
4+
Contains working functions of the interview questions for GROUPA
5+
'''
6+
import mimetypes as mtype
7+
8+
def take(input_list=None, count=None):
9+
"""
10+
(list, int) -> list
11+
Takes a list "input_list" and a number "count"
12+
and returns the first n elements as list
13+
"""
14+
try:
15+
assert isinstance(input_list, list)
16+
assert isinstance(count, int)
17+
assert(count > 0)
18+
assert(len(input_list) >= count)
19+
for value in input_list:
20+
assert isinstance(value, int)
21+
return input_list[:count]
22+
except:
23+
return []
24+
25+
26+
def flatten(nested_list=None):
27+
"""
28+
(list) -> list
29+
Takes a list of a lists
30+
and returns all elements in the nested list as a single list
31+
"""
32+
try:
33+
assert isinstance(nested_list, list)
34+
final_list = []
35+
for inlist in nested_list:
36+
assert isinstance(inlist, list)
37+
for element in inlist:
38+
assert isinstance(element, int)
39+
final_list.append(element)
40+
return final_list
41+
except:
42+
return []
43+
44+
45+
def indexer(input_filename=None):
46+
"""
47+
(text file) -> dict(map of string, index list)
48+
read a text file, split them into words (seperated by spaces, tabs and newlines)
49+
returning a map of word to line numbers
50+
"""
51+
try:
52+
if mtype.guess_type(input_filename)[0] != 'text/plain':
53+
return {}
54+
ofile = open(input_filename,"r")
55+
count = 0
56+
mapdict = {}
57+
for inp_string in ofile.readlines():
58+
count += 1
59+
word = ''
60+
for char in inp_string:
61+
if char in (" ", "\t", "\n"):
62+
indexer_helper(word, mapdict, count)
63+
word = ''
64+
else:
65+
word += char
66+
indexer_helper(word, mapdict, count)
67+
ofile.close()
68+
return mapdict
69+
except IOError:
70+
return {}
71+
except:
72+
return {}
73+
74+
75+
def indexer_helper(word, mapdict, count):
76+
"""
77+
(str, dict, int)
78+
Its a helper function for indexer
79+
takes the word map and count as input checks the word and inserts word into
80+
map
81+
"""
82+
if word != '':
83+
if mapdict.get(word, "not present") == "not present":
84+
mapdict[word] = [count]
85+
else:
86+
mapdict[word].append(count)
87+
88+
89+
def compress(sorted_list=None):
90+
"""
91+
(list) -> tuple in list
92+
converts a sorted list to a list of pairs,
93+
where each pair contains an element from the list and its count
94+
"""
95+
try:
96+
assert isinstance(sorted_list, list)
97+
result = []
98+
curr_val = None
99+
count = 0
100+
for value in sorted_list:
101+
assert isinstance(value, int)
102+
if curr_val is None:
103+
curr_val = value
104+
count = 1
105+
elif curr_val != value:
106+
assert (curr_val < value)
107+
result.append((curr_val, count))
108+
curr_val = value
109+
count = 1
110+
else:
111+
count += 1
112+
result.append((curr_val, count))
113+
return result
114+
except:
115+
return []
116+
117+
if __name__ == '__main__':
118+
print take([2, 3, 6, 1, 5], 2)
119+
print flatten([[2, 3, 4], [5, 4], [23, 44, 56, 78]])
120+
print compress([-1, 0, 0, 0, 10, 23, 23, 14, 44, 44, 44, 54])
121+
print indexer("test.txt")
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello world
2+
universe hello
3+
bye world
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
hello world
2+
universe hello
3+
bye world

0 commit comments

Comments
 (0)