Skip to main content
Code Review

Return to Question

Commonmark migration
Source Link

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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]

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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]

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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]
Notice removed Draw attention by A.Lee
Bounty Ended with Peilonrayz's answer chosen by A.Lee
Fix code formatting
Source Link
Toby Speight
  • 87.4k
  • 14
  • 104
  • 322
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]
```
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]
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]
```
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]
Fix code formatting, rephrase/reformat example
Source Link

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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.

ex:Example:
asciidecode('111111')asciidecode('111111') => '\x0b\x0b\x0b''\x0b\x0b\x0b' or 'oo''oo'
because we can split likethe input: 111111 111111 (after reverse back: '111111' 11,11,11) like 11,11,11 or 111,111111,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]```

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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.

ex: asciidecode('111111') => '\x0b\x0b\x0b' or 'oo'
because we can split like input: 111111 after reverse back: '111111' 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]

I am working on this question and have my answer as following?

A string is encoded by performing the following sequence of actions:

  1. Replace each character with its ASCII value representation.
  2. 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 input111111 (after reverse back: '111111') like 11,11,11 or111,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]```
Tweeted twitter.com/StackCodeReview/status/1120885440796663809
Notice added Draw attention by A.Lee
Bounty Started worth 50 reputation by A.Lee
added 288 characters in body
Source Link
A.Lee
  • 361
  • 2
  • 13
Loading
Post Undeleted by A.Lee
Post Deleted by A.Lee
edited body
Source Link
A.Lee
  • 361
  • 2
  • 13
Loading
added 5 characters in body
Source Link
A.Lee
  • 361
  • 2
  • 13
Loading
deleted 8 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
added 170 characters in body
Source Link
A.Lee
  • 361
  • 2
  • 13
Loading
deleted 43 characters in body
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
Loading
Source Link
A.Lee
  • 361
  • 2
  • 13
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /