Skip to main content
deleted 208 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

Try it online in the CJam interpreter: [Chrome] | [Firefox][CJam interpreter][1].

[Chrome][1]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2 [Firefox]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%2525%60La%60%22%5B%20%5D%20aC~*%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%2525%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2

Try it online in the CJam interpreter: [Chrome] | [Firefox]

[Chrome]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2 [Firefox]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%2525%60La%60-S%2525%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2

Try it online in the [CJam interpreter][1].

[1]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~*%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2

added 1856 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

How it works

q             e# Read all input from STDIN.
{             e# For each character:
  :Ci         e#   Save it in C and cast to integer.
  32/         e#   Divide the code point by 32. This pushes
              e#   2 for uppercase, 3 for lowercase and 1 for non-letters.
  ")("C#      e#   Find the index of C in that string. (-1 if not found.)
  -           e#   Subtract. This pushes 0 for (, 1 for ), 2 for digits,
              e#   3 for uppercase letters and 4 for lowercase letters.

 "[ ] aC~* Ca C+"

 S/           e#   Split it at spaces into ["[" "]" "aC~*" "Ca" "C+"].
 =~           e#   Select and evaluate the corresponding chunk.
              e#     (   : [    : Begin an array.
              e#     )   : ]    : End an array.
              e#     0-9 : aC~* : Wrap the top of the stack into an array
              e#                  and repeat that array eval(C) times.
              e#     A-Z : Ca   : Push "C".
              e#     a-z : C+   : Append C to the string on top of the stack.
}%            e#
`             e# Push a string representation of the resulting array.
              e# For input (Au(CH)2)2, this pushes the string
              e# [[["Au" [["C" "H"] ["C" "H"]]] ["Au" [["C" "H"].["C" "H"]]]]]
La`           e# Push the string [""].
-             e# Remove square brackets and double quotes from the first string.
S%            e# Split the result at runs of spaces.
$e`           e# Sort and perform run-length encoding.
{             e# For each pair [run-length string]:
  ~           e#   Dump both on the stack.
  ": "        e#   Push that string.
  @N          e#   Rotate the run-length on top and push a linefeed.
}/            e#

How it works

q             e# Read all input from STDIN.
{             e# For each character:
  :Ci         e#   Save it in C and cast to integer.
  32/         e#   Divide the code point by 32. This pushes
              e#   2 for uppercase, 3 for lowercase and 1 for non-letters.
  ")("C#      e#   Find the index of C in that string. (-1 if not found.)
  -           e#   Subtract. This pushes 0 for (, 1 for ), 2 for digits,
              e#   3 for uppercase letters and 4 for lowercase letters.

 "[ ] aC~* Ca C+"

 S/           e#   Split it at spaces into ["[" "]" "aC~*" "Ca" "C+"].
 =~           e#   Select and evaluate the corresponding chunk.
              e#     (   : [    : Begin an array.
              e#     )   : ]    : End an array.
              e#     0-9 : aC~* : Wrap the top of the stack into an array
              e#                  and repeat that array eval(C) times.
              e#     A-Z : Ca   : Push "C".
              e#     a-z : C+   : Append C to the string on top of the stack.
}%            e#
`             e# Push a string representation of the resulting array.
              e# For input (Au(CH)2)2, this pushes the string
              e# [[["Au" [["C" "H"] ["C" "H"]]] ["Au" [["C" "H"].["C" "H"]]]]]
La`           e# Push the string [""].
-             e# Remove square brackets and double quotes from the first string.
S%            e# Split the result at runs of spaces.
$e`           e# Sort and perform run-length encoding.
{             e# For each pair [run-length string]:
  ~           e#   Dump both on the stack.
  ": "        e#   Push that string.
  @N          e#   Rotate the run-length on top and push a linefeed.
}/            e#
deleted 56 characters in body
Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825

CJam, 5959 57 bytes

q{__i32:Ci32/\'0<-"'")(='[']?~"C#-"[ ~\a*] aaC~* +"SCa C+"S/=~}%`La`-S%$e`{~": "@N}/

Try it online in the CJam interpreter: [Chrome] | [Firefox]

[Chrome]: http://cjam.aditsu.net/#code=q%7B__i32%2F%5C'0%3C-%22'(%3D'%5B'%5D%3F~%20~%5Ca*%20a%20%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)2 [Firefox]: http://cjam.aditsu.net/#code=q%257B__i32%252F%255C'0%253C-%2522'(%253D'%255B'%255D%253F~%2520~%255Ca*%2520a%2520%252B%2522S%252F%253D~%257D%2525%2560La%2560%22C%23-S%2525%2524e%2560%257B~%2522%253A%2520%2522%2540N%257D%252F&input=Co3%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2 [Firefox]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%2525%60La%60-S%2525%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2

CJam, 59 bytes

q{__i32/\'0<-"'(='[']?~ ~\a* a +"S/=~}%`La`-S%$e`{~": "@N}/

Try it online in the CJam interpreter: [Chrome] | [Firefox]

[Chrome]: http://cjam.aditsu.net/#code=q%7B__i32%2F%5C'0%3C-%22'(%3D'%5B'%5D%3F~%20~%5Ca*%20a%20%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2 [Firefox]: http://cjam.aditsu.net/#code=q%257B__i32%252F%255C'0%253C-%2522'(%253D'%255B'%255D%253F~%2520~%255Ca*%2520a%2520%252B%2522S%252F%253D~%257D%2525%2560La%2560-S%2525%2524e%2560%257B~%2522%253A%2520%2522%2540N%257D%252F&input=Co3(Fe(CN)6)2

CJam, 59 57 bytes

q{:Ci32/")("C#-"[ ] aC~* Ca C+"S/=~}%`La`-S%$e`{~": "@N}/

Try it online in the CJam interpreter: [Chrome] | [Firefox]

[Chrome]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%25%60La%60-S%25%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2 [Firefox]: http://cjam.aditsu.net/#code=q%7B%3ACi32%2F%22)(%22C%23-%22%5B%20%5D%20aC~%20Ca%20C%2B%22S%2F%3D~%7D%2525%60La%60-S%2525%24e%60%7B~%22%3A%20%22%40N%7D%2F&input=Co3(Fe(CN)6)2

Source Link
Dennis
  • 210.6k
  • 41
  • 370
  • 825
Loading