Skip to content

Commit

Permalink
Added python solution for day 7 (#77)
Browse files Browse the repository at this point in the history
* Add @vishalshirke7 as a contributor

* Update @vishalshirke7 as a contributor

* Added python solutions for Day 4

* modified readme

* Add @Vishal

* reverted adding as a contributor

* Update README.md

* Update CONTRIBUTORS.md

* Update CONTRIBUTORS.md

* Added python solutions for day 7
  • Loading branch information
vishalshirke7 authored and MadhavBahl committed Dec 28, 2018
1 parent a12717a commit 7e8ed6d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.md
@@ -1,5 +1,7 @@

[![All Contributors](https://img.shields.io/badge/all_contributors-15-orange.svg?style=flat-square)](#contributors)


## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
Expand Down
2 changes: 2 additions & 0 deletions day4/README.md
Expand Up @@ -273,6 +273,8 @@ for i in b:
print(count)
```

<hr/>

## C++ Implementation

### [NumVowelsPartA.cpp](./C++/NumVowelsPartA.cpp)
Expand Down
29 changes: 29 additions & 0 deletions day7/Python/onewordaway.py
@@ -0,0 +1,29 @@
"""
@author : vishalshirke7
@date : 28/12/2018
"""


def check_one_edit_away(str1, str2):
l1, l2 = len(str1), len(str2)
if abs(l1 - l2) > 1:
return False
if l1 == l2:
count = 0
for i in range(l1):
if str1[i] != str2[i]:
count += 1
if count > 1:
return False
else:
return True
else:
if len(set(str1) & set(str2)) == min(l1, l2):
return True
else:
return False


check_one_edit_away(*(input().split()))


44 changes: 44 additions & 0 deletions day7/README.md
Expand Up @@ -121,4 +121,48 @@ oneEditAway ("abcdef", "abcde"); // true
oneEditAway ("aaa", "abc"); // false
oneEditAway ('bc', 'abc'); // true
oneEditAway ('abc', 'abced'); // false
```

## Python Implementatiom

### [Solution 1](./Python/onewordaway.py)

```python
"""
@author : vishalshirke7
@date : 28/12/2018
Method - There are 3 test cases
1) Difference of length between 2 characters is more than one, then answer is False
2) If length of both strings is equal then we'll have to check for 2 cases
2.1) if count of different characters at each position is > 1 then answer is False
2.2) if count of different characters at each position is < 2 then answer is True
3) if difference in length of both strings in 1 then we check for intersection of
both strings and if it is equal to min(length1, length2) then answer is true
as we can add a character to minimum length string or remove one from bigger length string
"""


def check_one_edit_away(str1, str2):
l1, l2 = len(str1), len(str2)
if abs(l1 - l2) > 1:
return False
if l1 == l2:
count = 0
for i in range(l1):
if str1[i] != str2[i]:
count += 1
if count > 1:
return False
else:
return True
else:
if len(set(str1) & set(str2)) == min(l1, l2):
# check for intersection of two strings
return True
else:
return False


check_one_edit_away(*(input().split())) # here list unpacking is used
```

0 comments on commit 7e8ed6d

Please sign in to comment.