Last active
October 1, 2015 04:47
-
-
Save kangkyu/19953b778f6d80266530 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Please answer in either | |
| # Ruby, | |
| # Perl, | |
| # PHP, | |
| # Python or | |
| # Java HTMLCONTROL Forms.HTML:Hidden.1 | |
| # 1. Convert this string | |
| # string = "{key:[[value_1, value_2],[value_3, value4]], 5:10:00AM]}" | |
| # to this hash: | |
| # h = {"key" => [["value_1", "value_2"],["value_3", "value4"]], 5=>"10:00AM"} | |
| # then convert h to JSON. | |
| # Please note that the brackets are unbalanced on purpose. | |
| string = "{key:[[value_1, value_2],[value_3, value4]], 5:10:00AM]}" | |
| def integerify(str) | |
| str.to_i.to_s == str ? str.to_i : str | |
| end | |
| def arrayify(str) | |
| arr = [] | |
| /\A\[\[(.+),\s*(.+)\],\s*\[(.+),\s*(.+)\]\]\z/.match(str) do |n| | |
| arr << [n[1], n[2]] | |
| arr << [n[3], n[4]] | |
| end | |
| arr | |
| end | |
| def hashify(str) | |
| hsh = {} | |
| /\A\{(.+)\:\s*(.+)\,\s*(.*?)\:\s*(.+)\W+\}\z/.match(str) do |m| | |
| m2 = arrayify(m[2]) | |
| m3 = integerify(m[3]) | |
| hsh[m[1]] = m2 | |
| hsh[m3] = m[4] | |
| end | |
| hsh | |
| end | |
| p result_h = hashify(string) | |
| require 'json' | |
| p result_h.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment