Skip to content

Instantly share code, notes, and snippets.

Created March 17, 2018 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/50aaa98b852d23d513e157cd8f3f2c63 to your computer and use it in GitHub Desktop.
Save anonymous/50aaa98b852d23d513e157cd8f3f2c63 to your computer and use it in GitHub Desktop.
const origin = '(a,(b,(c,(d,(e,(f))))))'
function reorder(str) {
let matches = str.match(/^\((\w),(.*)\)$/)
if (matches) {
const [left, right] = reorder(matches[2])
return [`${left},(${matches[1]}`, `)${right}`]
} else {
let matches = str.match(/^\((\w)\)$/)
return [`(${matches[1]}`, ')']
}
}
let [left, right] = reorder(origin)
console.log(`${left}${right}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment