Determining length of array of strings ignoring commas/periods (letters only)

I'm trying to find the best way to determine the letter count in an array of strings. I'm splitting the string, and then looping every word, then splitting letters and looping those letters.

When I get to the point where I determine the length, the problem I have is that it's counting commas and periods too. Thus, the length in terms of letters only is inaccurate.

I know this may be a lot shorter with regex, but I'm not well versed on that yet. My code is passing most tests, but I'm stuck where it counts commas.

E.g. 'You,' should be string.length = "3"

Sample code:

def abbr(str)
  new_words = []
  str.split.each do |word|
    new_word = []
    word.split("-").each do |w| # it has to be able to handle hyphenated words as well
      letters = w.split('')
       if letters.length >= 4
         first_letter = letters.shift
         last_letter = letters.pop
         new_word << "#{first_letter}#{letters.count}#{last_letter}"
         new_word << w
   new_words << new_word.join('-')
 new_words.join(' ')

I tried doing gsub before looping the words, but that wouldn't work because I don't want to completely remove the commas. I just don't need them to be counted.

Any enlightenment is appreciated.


arr = ["Now is the time for y'all Rubiests",
       "to come to the aid of your bowling team."]
  #=> 74  

Without a regex

def abbr(arr)
  str = arr.join
  str.size - str.delete([*('a'..'z')].join + [*('A'..'Z')].join).size

abbr arr
  #=> 58

Here and below, arr.join converts the array to a single string.

With a regex

R = /
    [^a-z] # match a character that is not a lower-case letter
    /ix    # case-insenstive (i) and free-spacing regex definition (x) modes

def abbr(arr)

abbr arr
  #=> 58

You could of course write:

  #=> 58

Try this:

def abbr(str)
  str.gsub /\b\w+\b/ do |word|
    if word.length >= 4
      "#{word[0]}#{word.length - 2}#{word[-1]}"

The regex in the gsub call says "one or more word characters preceded and followed by a word boundary". The block passed to gsub operates on each word, the return from the block is the replacement for the 'word' match in gsub.

You can check for each character that whether its ascii value lies in 97-122 or 65-90.When this condition is fulfilled increment a local variable that will give you total length of string without any number or any special character or any white space.

You can use something like that (short version): { |x| x.chars.reject { |char| [' ', ',', '.'].include? char } }

Long version with explanation:

a = ['a, ', 'bbb', 'c c, .']     # Initial array of strings
excluded_chars = [' ', ',', '.'] # Chars you don't want to be counted do |str|                   # Iterate through all strings in array
  str.chars.reject do |char|     # Split each string to the array of chars
    excluded_chars.include? char # and reject excluded_chars from it 
  end.size # This returns [["a"], ["b", "b", "b"], ["c", "c"]] 
end        # so, we take #size of each array to get size of the string

# Result: [1, 3, 2]

Need Your Help

QSignalSpy can not be used with threads

c++ qt qthread qtestlib qsignalspy

I wrote a thread that executes a worker object. Everything works fine. Also the resulting signals are emitted as they should. Of course I took care of the usual mistakes regarding thread/object aff...

Javac -Xmx to limit VM usage

memory virtual javac

is there any way to limit maximum virtual memory usage of javac? When running java, I can run it with "-XmxAm" (where A is number of free megabytes) to limit it. Is there anything for javac like th...