Created
August 28, 2018 09:09
-
-
Save woodRock/94e1a968981b998e40e9b712cbb920cc to your computer and use it in GitHub Desktop.
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded. For example, the message '111' would give 3, since it could be decoded as 'aaa', 'ka', and 'ak'. You can assume that the messages are decodable. For example, '001' is not allowed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mapping=["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"] | |
encodedCases = ["918", "739", "1142", "56", "82", "118", "1219", "6", "862", "1111"] | |
def decodeNumber(decoded, mapping): | |
count=0 | |
checkFirstLast=0 | |
for i in range(9, len(mapping)): | |
if mapping[i] in decoded: | |
count+=1 | |
if decoded[:2]!=decoded[-2:]: | |
if len(decoded)>3 and mapping[i] in decoded[:2]: | |
checkFirstLast+=1 | |
elif len(decoded)>3 and mapping[i] in decoded[-2:]: | |
checkFirstLast+=1 | |
if checkFirstLast==2: | |
count+=1 | |
print (count + 1) | |
for test in encodedCases: | |
test = test.strip() | |
decodeNumber(test, mapping) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment