From cad5cb0c0bd2f1706961e0088020406f2862d65d Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Fri, 31 May 2024 09:35:37 +0900 Subject: [PATCH 01/11] Create step1.py --- Encode and Decode Strings/step1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Encode and Decode Strings/step1.py diff --git a/Encode and Decode Strings/step1.py b/Encode and Decode Strings/step1.py new file mode 100644 index 0000000..3fe08d7 --- /dev/null +++ b/Encode and Decode Strings/step1.py @@ -0,0 +1,18 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + return ' '.join(strs) + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + if s == '': + return [""] + else: + return s.split() + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) From 3bc6dda50009c01dd68a81fb837422f512f54a50 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Fri, 31 May 2024 09:45:23 +0900 Subject: [PATCH 02/11] Create step2-using-non-ascii-char.py --- .../step2-using-non-ascii-char.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Encode and Decode Strings/step2-using-non-ascii-char.py diff --git a/Encode and Decode Strings/step2-using-non-ascii-char.py b/Encode and Decode Strings/step2-using-non-ascii-char.py new file mode 100644 index 0000000..c730fce --- /dev/null +++ b/Encode and Decode Strings/step2-using-non-ascii-char.py @@ -0,0 +1,16 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + return "あ".join(strs) + + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + return s.split('あ') + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) From 557f4b89a6a81651bc2aefaddf8747132be1fd48 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Fri, 31 May 2024 10:32:51 +0900 Subject: [PATCH 03/11] Create step2-using-non-ascii-delimeter.cpp --- .../step2-using-non-ascii-delimeter.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Encode and Decode Strings/step2-using-non-ascii-delimeter.cpp diff --git a/Encode and Decode Strings/step2-using-non-ascii-delimeter.cpp b/Encode and Decode Strings/step2-using-non-ascii-delimeter.cpp new file mode 100644 index 0000000..f1fdca1 --- /dev/null +++ b/Encode and Decode Strings/step2-using-non-ascii-delimeter.cpp @@ -0,0 +1,29 @@ +class Codec { +public: + + // Encodes a list of strings to a single string. + string encode(vector& strs) { + string res = ""; + for (int i = 0; i < strs.size(); i++) { + string str = strs[i]; + res += str + "あ"; + } + return res; + } + + // Decodes a single string to a list of strings. + vector decode(string s) { + vector res; + int pos = 0; + while (pos < s.size()) { + pos = s.find("あ"); + res.push_back(s.substr(0,pos)); + s.erase(0, pos+3); + } + return res; + } +}; + +// Your Codec object will be instantiated and called as such: +// Codec codec; +// codec.decode(codec.encode(strs)); From d46eec828187ad2949a4dc524eae6fe3af817eaf Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Fri, 31 May 2024 11:18:21 +0900 Subject: [PATCH 04/11] Create step2-using-escape.py --- .../step2-using-escape.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Encode and Decode Strings/step2-using-escape.py diff --git a/Encode and Decode Strings/step2-using-escape.py b/Encode and Decode Strings/step2-using-escape.py new file mode 100644 index 0000000..af172f8 --- /dev/null +++ b/Encode and Decode Strings/step2-using-escape.py @@ -0,0 +1,36 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + res = "" + + for s in strs: + res += s.replace("/", "//") + "/:" + return res + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + res = [] + current_string = "" + + i = 0 + + while i < len(s): + if s[i:i+2] == "/:": + res.append(current_string) + current_string = "" + i += 2 + elif s[i:i+2] == "//": + current_string += "/" + i += 2 + else: + current_string += s[i] + i += 1 + + return res + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) From 439534a2c92a7b08c55990f4ad37c3947230eac2 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 3 Jun 2024 08:57:01 +0900 Subject: [PATCH 05/11] Create step2-using-chunked-transfer-encoding.py --- .../step2-using-chunked-transfer-encoding.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Encode and Decode Strings/step2-using-chunked-transfer-encoding.py diff --git a/Encode and Decode Strings/step2-using-chunked-transfer-encoding.py b/Encode and Decode Strings/step2-using-chunked-transfer-encoding.py new file mode 100644 index 0000000..187d2b3 --- /dev/null +++ b/Encode and Decode Strings/step2-using-chunked-transfer-encoding.py @@ -0,0 +1,26 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + encoded_string = "" + + for s in strs: + encoded_string += str(len(s)) + "/:" + s + + return encoded_string + + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + decoded_strings = [] + i = 0 + + while i < len(s): + delimeter = s.find("/:", i) + word_length = int(s[i:delimeter]) + string = s[delimeter+2 : delimeter+2+word_length] + decoded_strings.append(string) + i = delimeter + 2 + word_length + + return decoded_strings From f6534855b7507ff83ba3dce810a7ff37f0f60788 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:04:28 +0900 Subject: [PATCH 06/11] Create step3.py --- Encode and Decode Strings/step3.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Encode and Decode Strings/step3.py diff --git a/Encode and Decode Strings/step3.py b/Encode and Decode Strings/step3.py new file mode 100644 index 0000000..3140aca --- /dev/null +++ b/Encode and Decode Strings/step3.py @@ -0,0 +1,26 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + encoded_string = "" + + for s in strs: + encoded_string += str(len(s)) + "#" + s + + return encoded_string + + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + decoded_strings = [] + i = 0 + + while i < len(s): + delim = s.find("#", i) + word_length = int(s[i:delim]) + decoded_str = s[delim+1 : delim + 1 + word_length] + decoded_strings.append(decoded_str) + i = delim + 1 + word_length + + return decoded_strings From 3e982b3229e9c77a17622a224f21f6d261a95c03 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:18:42 +0900 Subject: [PATCH 07/11] Create note.md --- Encode and Decode Strings/note.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Encode and Decode Strings/note.md diff --git a/Encode and Decode Strings/note.md b/Encode and Decode Strings/note.md new file mode 100644 index 0000000..b4d3482 --- /dev/null +++ b/Encode and Decode Strings/note.md @@ -0,0 +1,31 @@ +# 方法論 + +## Non ascii value + +インプットされる値はASCIIのみなので、delimeterとしてnon-ascii値を採用する方法。(ex: π, あ) + +時間計算量: o(n) +空間計算量: o(k) + +## Escaping + +エスケーピングを使用して、delimeterが元の文字列か判別がつくようにする手法。 + +- https://en.wikipedia.org/wiki/Escape_sequence + +時間計算量: o(n) +空間計算量: o(k) + +## Chunked transfer encoding + +もとの文字列の長さとdelimeterを使用して、chunk毎にもとの文字列長さがわかるようencodeする方法。 + +- https://en.wikipedia.org/wiki/Chunked_transfer_encoding + +時間計算量: o(n) +空間計算量: o(k) + + +# 振り返り + +## Step1 From f2c225ab45981f11196883e080ae0843d577b8d9 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:20:55 +0900 Subject: [PATCH 08/11] Update note.md --- Encode and Decode Strings/note.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Encode and Decode Strings/note.md b/Encode and Decode Strings/note.md index b4d3482..ccde868 100644 --- a/Encode and Decode Strings/note.md +++ b/Encode and Decode Strings/note.md @@ -29,3 +29,27 @@ # 振り返り ## Step1 + +値がnon-asciiであるため、non-ascii値を使用してjoin,splitを使用としたが、スペースもascii値であることに気づいておらず失敗。 + +```step1.py +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + return ' '.join(strs) + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + if s == '': + return [""] + else: + return s.split() + + +# Your Codec object will be instantiated and called as such: +# codec = Codec() +# codec.decode(codec.encode(strs)) +``` + From a88d552037f84432fd602247c1a2ef04aa4ad526 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:22:00 +0900 Subject: [PATCH 09/11] Update note.md --- Encode and Decode Strings/note.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Encode and Decode Strings/note.md b/Encode and Decode Strings/note.md index ccde868..617cb26 100644 --- a/Encode and Decode Strings/note.md +++ b/Encode and Decode Strings/note.md @@ -16,6 +16,10 @@ 時間計算量: o(n) 空間計算量: o(k) +エスケープ関連 +- [double-terminated-string](https://devblogs.microsoft.com/oldnewthing/20091008-00/?p=16443) +- [Protocol Buffers](https://ja.wikipedia.org/wiki/Protocol_Buffers) + ## Chunked transfer encoding もとの文字列の長さとdelimeterを使用して、chunk毎にもとの文字列長さがわかるようencodeする方法。 From b1615f36358c81224c70eb4be8aa6a7a9083ffbe Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Tue, 4 Jun 2024 09:36:07 +0900 Subject: [PATCH 10/11] Update note.md --- Encode and Decode Strings/note.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Encode and Decode Strings/note.md b/Encode and Decode Strings/note.md index 617cb26..3856928 100644 --- a/Encode and Decode Strings/note.md +++ b/Encode and Decode Strings/note.md @@ -18,7 +18,7 @@ エスケープ関連 - [double-terminated-string](https://devblogs.microsoft.com/oldnewthing/20091008-00/?p=16443) -- [Protocol Buffers](https://ja.wikipedia.org/wiki/Protocol_Buffers) +- [Protocol Buffers](https://ja.wikipedia.org/wiki/Protocol_Buffers), [例](https://github.com/protocolbuffers/protobuf/blob/1194440c2489fc58051a245b5db74c0fd1bbf4b0/upb/json/decode.c#L356) ## Chunked transfer encoding From 978357ca35f87bfdc5123a4e164a580c283e1ff6 Mon Sep 17 00:00:00 2001 From: Takeshi Ooka <74577802+t-ooka@users.noreply.github.com> Date: Tue, 2 Jul 2024 15:23:44 +0900 Subject: [PATCH 11/11] Create step4.py --- Encode and Decode Strings/step4.py | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Encode and Decode Strings/step4.py diff --git a/Encode and Decode Strings/step4.py b/Encode and Decode Strings/step4.py new file mode 100644 index 0000000..fbef891 --- /dev/null +++ b/Encode and Decode Strings/step4.py @@ -0,0 +1,31 @@ +class Codec: + def encode(self, strs: List[str]) -> str: + """Encodes a list of strings to a single string. + """ + encoded_string = "" + + for s in strs: + encoded_string += s.replace('/', '//') + '/:' + + return encoded_string + + def decode(self, s: str) -> List[str]: + """Decodes a single string to a list of strings. + """ + decoded_strings = [] + current_string = "" + i = 0 + + while i < len(s): + if s[i:i+2] == "/:": + decoded_strings.append(current_string) + current_string = "" + i += 2 + elif s[i:i+2] == "//": + current_string += "/" + i += 2 + else: + current_string += s[i] + i += 1 + + return decoded_strings