Created
September 8, 2019 13:32
-
-
Save m2kar/4aabf092e6e7ba0fb75eef598018c912 to your computer and use it in GitHub Desktop.
bytedance exam decode numbers to characters
This file contains 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
from queue import Queue | |
s=input().strip("\n") | |
l=len(s) | |
q=Queue() | |
# flag | |
# 0: nothing | |
# 1: in 1 | |
# 2: in 2 | |
q.put(("",0,0),) | |
d0=dict([(i,c)for i,c in zip("123456789","ABCDEFGHI")]) | |
d1=dict([(i,c)for i,c in zip("0123456789","JKLMNOPQRS")]) | |
d2=dict([(i,c)for i,c in zip("0123456","TUVVWXYZ")]) | |
while not q.empty(): | |
o,n,flag=q.get() | |
if flag==0: | |
if n==l: | |
print(o) | |
continue | |
c=s[n] | |
if c=="1": | |
q.put((o+"A",n+1,0),) | |
q.put((o,n+1,1),) | |
elif c=="2": | |
q.put((o+"B",n+1,0),) | |
q.put((o,n+1,2),) | |
else: | |
q.put((o+d0[c],n+1,0),) | |
elif flag==1: | |
if n==l: | |
continue | |
c=s[n] | |
q.put((o+d1[c],n+1,0),) | |
elif flag==2: | |
if n==l: | |
continue | |
c=s[n] | |
if c in "0123456": | |
q.put((o+d2[c],n+1,0),) | |
else: | |
continue |
Author
m2kar
commented
Sep 8, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment