Skip to content

Commit a186c2e

Browse files
committed
Day 23: simplify the differByOne using python built-ins
1 parent 7d7f276 commit a186c2e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

day23/stringsRearrangement.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@ def stringsRearrangement(inputArray):
66
return True
77
return False
88

9+
# [a, b, c, d, e]
10+
# compare a to b, b to c, c to d, d to e
11+
# return true if each neighbor pair differs by exactly one char
912
def neighborsDifferByOne(inputArray):
1013
if len(inputArray) <= 1:
1114
return True
1215
return differByOne(inputArray[0], inputArray[1]) and neighborsDifferByOne(inputArray[1:])
1316

17+
# compare each character in each word string,
18+
# return True only if there is one exact difference
1419
def differByOne(word, anotherWord):
15-
differencesCount = 0
16-
for i in xrange(len(word)):
17-
if word[i] != anotherWord[i]:
18-
differencesCount += 1
19-
if differencesCount > 1:
20-
return False
21-
22-
return differencesCount == 1
20+
return sum(c1 != c2 for c1, c2 in zip(word, anotherWord)) == 1
2321

2422
def testDifferByOne():
2523
assert not differByOne("", "")

0 commit comments

Comments
 (0)