2

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?

Arun Kumar Mohan
  • 9,934
  • 3
  • 16
  • 37

2 Answers2

1

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
benjessop
  • 1,349
  • 1
  • 5
  • 11
0

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'
  • ```scramble_words 'this-HAS-more-THAN-one-SPECIAL-character-AND-capslocks-WORDS' IndexError: index 59 out of string``` so you're removing a lot of characters with the regex and only really expecting one special character in that solution – benjessop Apr 26 '21 at 07:08
  • The first and last character are treated differently. If this is the actual output you expect, you should update your question accordingly. (also include those examples in your question) – Stefan Apr 26 '21 at 08:11
  • Oh yeah! I didn’t tell that part. The first and last letter stay in the same place and the special character. Then the middle scrambles. – anothernewbiecoder Apr 26 '21 at 15:32
  • The reason I didn’t add that part is because I knew that part. The part I was struggling with was keeping the special character there and sorting everything else. I showed the whole problem but I can change it? – anothernewbiecoder Apr 26 '21 at 15:34
  • @benjessop that is right! I didn't think of that at first, and then I ran into a test case that had multiple special characters and had a major facepalm moment. I ended switching this to a hash. So, I iterated over the string putting all special characters into a hash. Then at the end I inserted them. – anothernewbiecoder Apr 27 '21 at 20:30