Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Writing functions in Ruby Part 1 Write a function called count_sentences() that counts the number of sentences in a file. File found here: http://www.rubyinside.com/book/oliver.txt Sentences

Writing functions in Ruby

Part 1

Write a function called count_sentences() that counts the number of sentences in a file. File found here: http://www.rubyinside.com/book/oliver.txt

Sentences end with the characters ., !,?. The function should return the count the number of sentences in the text. If there are no sentences, your function will return nil.

Some sentences are grammatically complex and will not end with a simple ., !, or ?. For example, this sentence:

If the parish vould like him to learn a right pleasant trade, in a good spectable chimbley-sweepin bisness, said Mr. Gamfield, I wants a prentis, and I am ready to take him.

The sentence ends with ." rather than .. Write code that would count these sophisticated sentences.

Starting code:

def count_sentences(path_to_some_file) file_content = open(some_file).read() # your code goes here. Return the number of lines or false. end

Part 2

Using the same text from http://www.rubyinside.com/book/oliver.txt,

write a function called count_paragraphs() that counts the number of paragraphs in a block of text.

The paragraph delimiter consists of two or more consecutive newline characters, like this: , , or even .

Your function should return either the number of paragraphs or nil.

Part 3

Write a function called that collects all of the anagrams (words composed of the same letters) into arrays and returns them contained in an array.

  • If the function fails to find any anagrams, return nil.
  • Ignore case when checking words
  • For this problem, words are simply strings of characters, not necessarily real words in any human language. For example, for and ofr are considered equal even though ofr isnt word in the English language.
  • Using the function, print the arrays of anagrams in the array of words below:

Use the words array below to test your anagram() function. Notice, too, that the array of words is created using the %W[], one or Rubys percentage strings. Read more about Rubys useful percentage strings in the Ruby percentage strings documentation.

words = %W[Alan lana aaln bard brad drAb for ofr roF barE bear brae abet Bate beat brag gArb grab acme caME mace acre care race Cars raCs Scar arcs relish shilre shiler beta reac ceam TEAb]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

More Books

Students also viewed these Databases questions