Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Created May 21, 2013 18:03
Show Gist options
  • Save sw17ch/5621890 to your computer and use it in GitHub Desktop.
Save sw17ch/5621890 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
count = ARGV[0] ? ARGV[0].to_i : 1
opens = "(" * count
txt = STDIN.read.chomp
closes = ")" * count
puts(opens + txt + closes)
# $ echo "this needs more parens" | moar_parens
# (this needs more parens)
# $ echo "this needs more parens" | moar_parens 3
# (((this needs more parens)))
@stylewarning
Copy link

This is O(n^2):

Computing opens and closes is O(n), and the concatenation is O(n^2). Fortunately, there's an easy fix, just do

print(opens) 
print(txt)
puts(closes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment