How to Sort Everything in an Array Except Special Chars - Ruby

247 Views Asked by At

I am trying to sort an array of letters alphabetically, but keep the special character in the same spot, in ruby.

For example,

word = ["f", "a", "s", "t", "-", "c", "a", "r", "s"]

How can I sort the whole thing alphabetically, but keep the "-" where it is. If I sort how it is now then the "-" will go to the front which I don't want. I have tried fifteen different ways, and I can't figure it out. Can you help?

2

There are 2 best solutions below

3
On

Some really verbose way to do this, to explain the logic around what you need to achieve. There's some 'cleaner' methods to achieve this, but I feel this gives more understanding.

Adding an additional special character to the array for better test coverage:

let(:input) { ["f", "a", "s", "t", "-", "c", "a", "r", "s", "/"] }
let(:desired_output) { ["a", "a", "c", "f", "-", "r", "s", "s", "t", "/"] }

it "takes the input and gives the desired output" do
  expect(sort_alphanumeric_characters(input)).to eq(desired_output)
end

Call .map and .select on the array to enumerate over the values and then call .with_index as you'll need to retain indicies for later.

def sort_alphanumeric_characters(word_as_array)
  # assuming you mean non-alphanumeric
  # collect those indicies which are 'special' characters
  # the regex matches the string with anything outside of the alphanumeric range. Note the '^'
  special_character_indicies = word_as_array.map.with_index { |val, indx| indx if val =~ /[^a-zA-Z0-9]/ }.compact
  # collect all characters by index that were not yielded as 'special'
  alphanumeric_array = word_as_array.select.with_index { |char, indx| char unless special_character_indicies.include? indx }
  # sort the alphanumeric array
  sorted_alphanumeric_array = alphanumeric_array.sort
  # use Array#insert to place the 'special' by index
  special_character_indicies.each do |special_indx|
    special_char = word_as_array[special_indx]
    sorted_alphanumeric_array.insert(special_indx, special_char)
  end
  # return desired output
  sorted_alphanumeric_array
end
5
On

As soon as I posted I had a lightning bolt (love it when that happens). This is really not that great of a solution, but it did work!!

    def scramble_words(str)
      idx = 0 
      chars = str.delete("^a-z").chars
      first_ele = chars.shift
      last_ele = chars.pop
    
      str.chars.each_with_index {|c, i| idx = i if c =~ /[^a-z" "]/ }
    
      (first_ele + chars.sort.join  + last_ele).insert(idx, str[idx])
    
    end
    
    p scramble_words('card-carrying') == 'caac-dinrrryg'
    p scramble_words("shan't") == "sahn't"
    p scramble_words('-dcba') == '-dbca'