Question: Ruby Help: For the code below require 'fun_with_strings' require 'byebug' RSpec.configure do |config| config.filter_run_excluding :disabled => true end describe 'palindrome detection' do it 'should work

Ruby Help: For the code below

require 'fun_with_strings' require 'byebug'

RSpec.configure do |config| config.filter_run_excluding :disabled => true end

describe 'palindrome detection' do it 'should work for simple strings [10 points]' do expect('redivider').to be_palindrome expect('abracadabra').not_to be_palindrome end it 'should be case-insensitive [10 points]' do expect('ReDivider').to be_palindrome end it 'should ignore nonword characters [10 points]' do expect('A man, a plan, a canal -- Panama').to be_palindrome expect("Madam, I'm Adam!").to be_palindrome end end

describe 'word count', :disabled => true do it 'should return a hash [5 points]' do expect('now is the time'.count_words).to be_a_kind_of(Hash) end it 'works on simple strings [10 points]' do expect('Doo bee doo bee doo'.count_words).to be == {'doo' => 3, 'bee' => 2} end it 'ignores punctuation [5 points]' do expect('A man, a plan, a canal -- Panama!'.count_words).to be == {'man' => 1, 'plan' => 1, 'canal' => 1, 'a' => 3, 'panama' => 1} end it 'works on the empty string [10 points]' do expect(''.count_words).to be == {} end it 'ignores leading whitespace [10 points]' do expect(" toucan".count_words).to be == {'toucan' => 1 } end it 'ignores embedded whitespace [10 points]' do expect("four four four \t four!".count_words).to be == {'four' => 4} end end

describe 'anagram grouping', :disabled => true do describe 'sanity checks' do it 'should work on the empty string [5 points]' do expect(''.anagram_groups).to eq([]) end it 'should return an array of arrays for nonempty string [5 points]' do 'x'.anagram_groups.each { |element| expect(element).to be_a_kind_of(Array) } end end it 'for "scream cars for four scar creams" [10 points]' do @anagrams = 'scream cars for four scar creams'.anagram_groups @anagrams.each { |group| group.sort! } [%w(cars scar), %w(four), %w(for), %w(creams scream)].each do |group| expect(@anagrams).to include(group) end end end

1)

Write a method 'palindrome?' that returns true if its input is a palindrome.

Notes:

Your solution shouldn't use loops or iteration of any kind. Instead, you will find regular-expression syntax very useful; it's reviewed briefly in the recommended textbook, and the Rubular website (Links to an external site.)Links to an external site. lets you try out Ruby regular expressions "live".

Some methods that you might find useful (which you'll have to look up in the Ruby documentation (Links to an external site.)Links to an external site.) include 'String#downcase', 'String#gsub', and 'String#reverse'.

Run rspec in the terminal window to test. Once everything is passing, code is complete.

2)

Remove the , :disabled => true from the 'word count' collection in the spec/fun_with_strings_spec.rb file. Don't forget to save your changes!

Define a function 'count_words' that, given an input string, will return a hash whose keys are words in the string and whose values are the number of times each word appears:

"To be or not to be" # => {"to"=>2, "be"=>2, "or"=>1, "not"=>1} 

Notes:

Your solution shouldn't use for-loops, but iterators like 'each' are permitted.

As before, nonwords and case should be ignored. A word is defined as a string of characters between word boundaries.

Run rspec in the terminal window to test. Once everything is passing, the code is complete.

3)

Remove the , :disabled => true from the 'anagram grouping' collection in the spec/fun_with_strings_spec.rb file. Don't forget to save your changes!

An anagram group is a group of words such that any one can be converted into any other just by rearranging the letters. For example, "rats", "tars" and "star" are an anagram group.

Given a space separated list of words in a single string, write a method called 'anagram_groups' that groups these words into anagram groups and returns a nested array of those groups (an array of arrays).

Hint: Look at the spec file to see what kind of return value is expected!

Notes:

Case doesn't matter in classifying string as anagrams (but case should be preserved in the output)

The order of the anagrams in the groups doesn't matter.

Run rspec in the terminal window to test. Once everything is passing, the code is complete.

Copy and paste edit the following code for this exercise:

module FunWithStrings def palindrome? # your code here end def count_words # your code here end def anagram_groups # your code here end end

# make all the above functions available as instance methods on Strings:

class String include FunWithStrings end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!