I am working on this question and have my answer as following?
A string is encoded by performing the following sequence of actions:
- Replace each character with its ASCII value representation.
- Reverse the string.
For example, the table below shows the conversion from the string "
HelloWorld
" to the ASCII string "7210110810811187111114108100
":Character H e l l o W o r l d ASCII Value 72 101 108 108 111 87 111 114 108 100
The ASCII string is then reversed to get the encoded string "
0018014111117811180180110127
".The characters in encoded string are within the range 10 - 126 which include special characters.
Example:
asciidecode('111111')
=> '\x0b\x0b\x0b'
or 'oo'
because we can split the
input 111111
(after reverse back: '111111') like 11,11,11
or 111,111
from typing import List
def asciidecode(self, s: str) -> List:
if not s: return ''
s = s[::-1]
n = len(s)
ch_map = {str(i): chr(i) for i in range(10, 127)}
dp = [[''] for _ in range(n + 1)]
dp[2] = [ch_map[s[:2]]] if s[:2] in ch_map else ''
dp[3] = [ch_map[s[:3]]] if s[:3] in ch_map else ''
dp[4] = [dp[2][0] + ch_map[s[2:4]]] if s[2:4] in ch_map else ''
for i in range(5, n + 1):
p1 = s[i - 2: i]
p2 = s[i - 3: i]
tmp = []
if p1 in ch_map:
tmp += [i + ch_map[p1] for i in dp[i - 2]]
if p2 in ch_map:
tmp += [i + ch_map[p2] for i in dp[i - 3]]
dp[i] = tmp
return dp[-1]
1 Answer 1
return ''
doesn't have the typeList
.List
meansList[Any]
, you should useList[str]
.Your variable names are garbage.
s
->string
n
->length
ch_map
->char_map
orcharacter_map
dp
-> what does this even mean?p1
->position_1
?
Don't put
return
statements on the same line as anif
.- Again strings shouldn't be assigend to lists, yes you can iterate over them because they're both sequences. But they're not the same type.
[ch_map[s[:2]]] if s[:2] in char_map else ''
ch_map
should be a constant outside the function.- It's far easier to understand your code if it's written using recursion.
- Recursion has some problems, and so it should be written in a way that allows you to easily convert it to a while loop.
CHAR_MAP = {str(i): chr(i) for i in range(10, 127)}
def asciidecode(string: str) -> List[str]:
if not string:
return []
string = string[::-1]
length = len(string)
def inner(index):
if index == length:
yield ''
else:
for size in (2, 3):
if length < index + size:
break
letter = CHAR_MAP.get(string[index:index + size])
if letter is not None:
for word in inner(index + size):
yield letter + word
return list(inner(0))
def asciidecode_pre_while(string: str) -> List[str]:
if not string:
return []
string = string[::-1]
length = len(string)
output = []
def inner(index, word):
if index == length:
output.append(word)
return
for size in (2, 3):
if length < index + size:
break
letter = CHAR_MAP.get(string[index:index + size])
if letter is not None:
inner(index + size, word + letter)
inner(0, '')
return output
From the second one it's easy to convert it to a while loop:
CHAR_MAP = {str(i): chr(i) for i in range(10, 127)}
def asciidecode(string: str) -> List[str]:
if not string:
return []
string = string[::-1]
length = len(string)
output = []
stack = [(0, '')]
while stack:
index, word = stack.pop()
if index == length:
output.append(word)
continue
for size in (2, 3):
if length < index + size:
break
letter = CHAR_MAP.get(string[index:index + size])
if letter is not None:
stack.append((index + size, word + letter))
return output
-
\$\begingroup\$ so what is time complexity? \$\endgroup\$A.Lee– A.Lee2019年04月24日 18:50:02 +00:00Commented Apr 24, 2019 at 18:50
-
\$\begingroup\$ @A.Lee When did you or I say anything about time complexity? \$\endgroup\$2019年04月24日 18:56:28 +00:00Commented Apr 24, 2019 at 18:56
-
\$\begingroup\$ i mean for your while loop solution what is time complexity? O(2 ^ n)? \$\endgroup\$A.Lee– A.Lee2019年04月24日 19:00:22 +00:00Commented Apr 24, 2019 at 19:00
-
\$\begingroup\$ @A.Lee You've not answered my question. \$\endgroup\$2019年04月24日 19:01:46 +00:00Commented Apr 24, 2019 at 19:01
-
\$\begingroup\$ i do not think I mention anything about time complexity on my post but want to know what is it for your while loop solution \$\endgroup\$A.Lee– A.Lee2019年04月24日 19:24:08 +00:00Commented Apr 24, 2019 at 19:24
print(asciiencode('HelloWorld'))
doesn't work at all! \$\endgroup\$asciidecode()
instead. \$\endgroup\$