# Onit India Pvt Ltd - Python Developer Interview questions Experienced

Round 1: Online hackerearth.com Coding Test

Round 2:

Give a valid list of words- ['bat', 'cat', 'dog']
Implement a simple autocorrect which takes in a list of words as input
a return list of correct word
returns True if either any of point 1, 2 & 3 is satisfied.
1. if word matches correctly for eg.- "cat" -> "cat"
2. if all the characters input word is one of a valid word. for eg.
"tac" is valid word as it will return correct "cat" as correct word.
3. Similar to sql like operator %word would also be valid. for example "at" is a valid as "%at" -> "cat"

Return correct word if any of points is satified otherwize None

for example. valid words = ['bat', 'cat', 'dog']
Input = ['bat', 'atb', 'act', 'go', 'ect', 'tad']
Output = ['bat', 'bat', 'cat', 'dog', None, None]

### Solution

```def autocorrect(input_words=None):
valid_words = ['bat', 'cat', 'dog']
# step 1
output = []
for w in input_words:
if w in valid_words:
output.append(w)
elif w:
for x, vw in enumerate(valid_words, 1):
vl = len(vw)
if vl < len(w):
if vl == x:
output.append(None)
continue
f = False
for c in w:
if c in vw:
f = True
continue
else:
f = False
break
if f:
output.append(vw)
break
elif len(valid_words) == x:
output.append(None)
else:
output.append(None)
print(output)
return output

assert autocorrect(['atccccccttaaa', 'cat', 'dgo']) == [None, 'cat', 'dog']
assert autocorrect(['ab', 'dog', 'cd']) == ['bat', 'dog', None]
assert autocorrect(['bat', 'cat', 'fg']) == ['bat', 'cat', None]
assert autocorrect(['abc', 'xyz', 'cd']) == [None, None, None]
assert autocorrect(['bat', 'cat', 'dog']) == ['bat', 'cat', 'dog']
assert autocorrect([]) == []
assert autocorrect(['', '', None]) == [None, None, None]
assert autocorrect(['bat', 'atb', 'act', 'go', 'ect', 'tad']) == ['bat', 'bat', 'cat', 'dog', None, None]
```

Imran on Sep 05, 2020 at 11:09 am

×