Skip to content

Commit 3db8020

Browse files
committed
pushing up ruby-monk exercises
1 parent 9cdede5 commit 3db8020

17 files changed

+347
-21
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Problem Statement
2+
# Given an array containing some strings, return an array containing the
3+
# length of those strings.
4+
#
5+
# You are supposed to write a method named 'length_finder' to accomplish
6+
# this task.
7+
#
8+
# Example:
9+
# Given ['Ruby','Rails','C42'] the method should return [4,5,3]
10+
11+
#https://rubymonk.com/learning/books/1-ruby-primer/problems/5-string-length-finder
12+
13+
14+
def length_finder(words)
15+
lengths = {}
16+
words.each do |word|
17+
lengths[word] = word.length
18+
end
19+
lengths.values
20+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Problem Statement
2+
# Given a sentence containing multiple words, find the frequency of a given
3+
# word in that sentence.
4+
#
5+
# Construct a method named 'find_frequency' which accepts two arguments
6+
# 'sentence' and 'word', both of which are String objects.
7+
#
8+
# Example: The method, given 'Ruby is The best language in the World'
9+
# and 'the', should return 2 (comparison should be case-insensitive).
10+
#
11+
# Hint: You can use the method Array#count to count the frequency of
12+
# any element in the given array. eg:
13+
#
14+
# [9,3,4,9,5].count(9)
15+
# Will return the value 2
16+
#
17+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/6-frequency-finder
18+
19+
def find_frequency(sentence, word)
20+
sentence.downcase.split.count(word.downcase)
21+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Problem Statement
2+
# Create a class Calculator, which performs addition and subtraction of
3+
# two numbers at a time. The sample code explains the expected API.
4+
5+
#https://rubymonk.com/learning/books/1-ruby-primer/problems/9-calculator
6+
7+
class Calculator
8+
def add(a, b)
9+
a + b
10+
end
11+
12+
def subtract(a, b)
13+
a - b
14+
end
15+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Problem Statement
2+
# Create a method named 'sort_string' which accepts a String and
3+
# rearranges all the words in ascending order, by length.
4+
# Let's not treat the punctuation marks any different than other characters
5+
# and assume that we will always have single space to separate the words.
6+
#
7+
# Example: Given a string "Sort words in a sentence", it should return
8+
# "a in Sort words sentence".
9+
#
10+
# Note: You can use the sort method to sort an array. Try the documentation
11+
# at ruby-lang to know more about sort.
12+
#
13+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/14-sort-string-words
14+
15+
def sort_string(string)
16+
words = string.split.sort do |word1, word2|
17+
word1.length <=> word2.length
18+
end
19+
words.join(" ")
20+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Problem Statement
2+
# Given a sentence, return true if the sentence is a palindrome.
3+
#
4+
# You are supposed to write a method palindrome? to accomplish this task.
5+
#
6+
# Note Ignore whitespace and cases of characters.
7+
#
8+
# Example:
9+
# Given "Never odd or even" the method should return true
10+
#
11+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/143-palindrome
12+
13+
def palindrome?(sentence)
14+
downcase_stripped_sentence = sentence.downcase.gsub(" ", "")
15+
downcase_stripped_sentence == downcase_stripped_sentence.reverse
16+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Problem Statement
2+
# Compute the sum of cubes for a given range a through b.
3+
#
4+
# Write a method called sum_of_cubes to accomplish this task
5+
#
6+
# Example Given range 1 to 3 the method should return 36
7+
#
8+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/144-sum_of_cubes
9+
10+
def sum_of_cubes(a, b)
11+
(a..b).inject(0) { |sum, x| sum += (x*x*x) }
12+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Problem Statement
2+
# Given an Array, return the elements that are present exactly once in
3+
# the array.
4+
#
5+
# You need to write a method called non_duplicated_values to accomplish
6+
# this task.
7+
#
8+
# Example: Given [1,2,2,3,3,4,5], the method should return [1,4,5]
9+
#
10+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/147-non-duplicate-values
11+
12+
def non_duplicated_values(values)
13+
results = []
14+
values.each do |value|
15+
results << value if values.count(value) == 1
16+
end
17+
results
18+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Problem Statement
2+
# Given an array, return true if all the elements are Fixnums.
3+
#
4+
# You need to write array_of_fixnums? method to accomplish this task.
5+
#
6+
# Example:
7+
# Given [1,2,3], the method should return true
8+
#
9+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/148-array_of_fixnum
10+
11+
def array_of_fixnums?(array)
12+
array.inject(0) {|result, element| result = result && element.is_a?(Fixnum)}
13+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Problem Statement
2+
# Create a method 'random_select' which, when given an array of elements
3+
# (array) and a number (n), returns n randomly selected elements from
4+
# that array.
5+
#
6+
# Example: Given an array [1, 2, 3, 4, 5] and 2 should return two random
7+
# numbers from the given array. (Note: those two random numbers may be
8+
# the same number.) The method should return those random values in a
9+
# new array.
10+
#
11+
# Calling the method twice should ideally return different sets of values
12+
# (though it may not).
13+
#
14+
# Note: Ruby has the method rand which takes a parameter max. It returns
15+
# a random number lesser than max.
16+
#
17+
# One more note: Ruby also has a method Array#sample which very concisely
18+
# solves this problem. Unfortunately, it's too concise! The exercise is
19+
# almost to write your own Array#sample, so it's been disallowed. Good luck!
20+
#
21+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/15-select-random-elements-from-an-array
22+
#
23+
def random_select(array, n)
24+
result = []
25+
n.times {result << array[rand(n)]}
26+
result
27+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Problem Statement
2+
#
3+
# 9 is a Kaprekar number since
4+
# 9 ^ 2 = 81 and 8 + 1 = 9
5+
#
6+
# 297 is also Kaprekar number since
7+
# 297 ^ 2 = 88209 and 88 + 209 = 297.
8+
#
9+
# In short, for a Kaprekar number k with n-digits, if you square it and add
10+
# the right n digits to the left n or n-1 digits, the resultant sum is k.
11+
# Find if a given number is a Kaprekar number.
12+
# https://rubymonk.com/learning/books/1-ruby-primer/problems/150-kaprekar-s-number
13+
14+
def kaprekar?(k)
15+
no_of_digits = k.to_s.size
16+
square = (k ** 2).to_s
17+
18+
second_half = square[-no_of_digits..-1]
19+
first_half = square.size.even? ? square[0..no_of_digits-1] : square[0..no_of_digits-2]
20+
21+
k == first_half.to_i + second_half.to_i
22+
end

0 commit comments

Comments
 (0)