We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents ed5899d + e86e2dc commit 66dcc64Copy full SHA for 66dcc64
ruby/271-Encode-and-Decode-Strings.rb
@@ -0,0 +1,23 @@
1
+# Encodes a list of strings to a single string.
2
+#
3
+# @param {string[]} strs
4
+# @return {string}
5
+def encode(strs)
6
+ strs.reduce('') do |acc, cur|
7
+ "#{acc}\n#{cur.chars.map{_1.ord.to_s}.join(',')}"
8
+ end[1..] + "\n"
9
+end
10
+
11
+# Decodes a single string to a list of strings.
12
13
+# @param {string} s
14
+# @return {string[]}
15
+def decode(s)
16
+ s.each_line.map do |nums|
17
+ nums[...-1].split(',').map(&:to_i).map(&:chr).join('')
18
+ end
19
20
21
22
+# Your functions will be called as such:
23
+# decode(encode(strs))
0 commit comments