Hackerrank Ruby - Strings - Methods I Solution
2 min read

Hackerrank Ruby - Strings - Methods I Solution

Hackerrank Ruby - Strings - Methods I Solution

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue}

Text info can be read from varied sources and is often unsuitable for direct processing or usage by core functions. This necessitates methods for post-processing and data-fixing. In this tutorial, we'll learn how to remove flanking whitespace and newline from strings.

  • String.chomp(separator=$/): Returns a new string with the given separator removed from the end of the string (if present). If $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is, it will remove \n, \r, and \r\n).> "Hello World!  \r\n".chomp"Hello World!  "> "Hello World!".chomp("orld!")"Hello W"> "hello \n there".chomp"hello \n there"
  • String.strip - Returns a new string with the leading and trailing whitespace removed.> "    hello    ".strip"hello"> "\tgoodbye\r\n".strip"goodbye"
  • String.chop - Returns a new string with the last character removed. Note that carriage returns (\n, \r\n) are treated as single character and, in the case they are not present, a character from the string will be removed.> "string\n".chop"string"> "string".chop"strin"

In this challenge, your task is to code a process_text method, which takes an array of strings as input and returns a single joined string with all flanking whitespace and new lines removed. Each string has to be separated by a single space.> process_text(["Hi, \n", " Are you having fun?    "])"Hi, Are you having fun?"

Solution in ruby

Approach 1.

def process_text(arr)
    arr.map {|s| s.strip}.join(" ")


Approach 2.

def process_text(words)
  words.collect {|word| word.strip}.join(' ')

Approach 3.

# Enter your code here. Read input from STDIN. Print output to STDOUT
def process_text(array)
    array.map{|x| x.strip}.join(" ")

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.