Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ruby-coding-exercises-v2
from rails-camp
7 changes: 7 additions & 0 deletions december/20.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@
]

def string_parser string_array
new_array = []
string_array.each do |s|
new_array << s.scan(/\d+/).last.to_i
end
new_array
end

string_parser(string_array)

describe "String Parser" do
it 'can take a string and output the correct values' do
expect(string_parser(string_array)).to eq([100, 100, 50, 100, 100, 20, 1, 100, 55])
Expand Down
2 changes: 1 addition & 1 deletion december/21.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'rspec'

def sentence_reverser str

str.split.reverse.join(" ")
end

describe "Sentence reverser" do
Expand Down
6 changes: 6 additions & 0 deletions december/22.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@
]

def position_filter(headers, *data)
headers.zip(*data)
end

# .zip walks over each of the collections, collecting one element from each and returning the resulting collected array. It then repeats that process until it’s made one pass over each element from the collections. The resulting arrays from each of these passes is collected and returned as an array of those collected arrays.


position_filter(headers, astros, rangers)

describe 'Position Filter' do
it 'lines up players with their positions' do
test_headers = ['1B', '2B', 'P']
Expand Down
11 changes: 11 additions & 0 deletions december/23.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
require 'rspec'

def random_numbers
array = []
20.times do
r = rand 0..999
array << r
end
array
end

#shorter answer:
# def random_numbers
# Array.new(20) { rand 1000 }
# end

describe 'Random number collection generator' do
it 'creates a collection of random numbers ranging from 0 to 999' do
# Not a perfect test since there is a slight chance for duplicate
Expand Down
1 change: 1 addition & 0 deletions december/24.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rspec'

def new_max array_of_elements
array_of_elements.max
end

describe 'New Max method' do
Expand Down
1 change: 1 addition & 0 deletions december/25.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rspec'

unchangeable_elements = [1, 2, 3]
unchangeable_elements.freeze

describe 'Immutable array' do
it 'does not allow an array to be altered' do
Expand Down
10 changes: 10 additions & 0 deletions december/26.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
require 'rspec'

years = (1900..1999).to_a
leap_years = []


years.each do |year|
if (year % 4 == 0) && !(year % 100 == 0) || (year % 400 == 0)
leap_years << year
end
leap_years
end


describe 'Leap year calculation' do
it 'properly renders the leap years for the 20th century' do
Expand Down
2 changes: 2 additions & 0 deletions december/27.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rspec'

def palindrome? word
word.downcase == word.downcase.reverse

end

describe 'Check if a word is a palindrome' do
Expand Down
3 changes: 3 additions & 0 deletions december/28.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'rspec'

def array_converter *arrays
arrays.flatten.map {|i| i[0].to_i }
# this also works and is easier to read:
# arrays.flatten.map(&:to_i)
end

describe 'Combine arrays and convert strings to integers' do
Expand Down
8 changes: 8 additions & 0 deletions december/29.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
require 'rspec'

class Array

def bubble_sort
self.sort!
end

end

describe 'Adding bubble sort to the Array class' do
it 'Properly sorts an array' do
arr = [4, 1, 6, 10, 44, 2, 3]
Expand Down
9 changes: 9 additions & 0 deletions december/30.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
require 'rspec'

class Hash
def param_converter
# self.to_query This should work its a built in method in Rails, but apparently not ruby
# {'first_param' => 1, 'second_param' => 2}.to_query
# => "first_param=1&second_param=2"
self.map { |i| i * "=" }.join("&")
end
end

describe 'HTML Param Converter' do
it 'Adds an HTML param converter to the Hash class' do
hash = { :topic => "baseball", :team => "astros" }
Expand Down
7 changes: 7 additions & 0 deletions december/31.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
# $

def currency_converter amount, location
if location == 'US'
'$' + amount.to_s + '.00'
elsif location == 'Japan'
'¥' + amount.to_s
elsif location == 'UK'
'£' + amount.to_s + ',00'
end
end

describe 'Currency converter' do
Expand Down
1 change: 1 addition & 0 deletions january/1.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'rspec'

def title_creator title
'<h1>' + title + '</h1>'
end

describe 'HTML h1 converter' do
Expand Down
14 changes: 14 additions & 0 deletions january/2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
# </html>

def html_generator title
<<HTML
<!doctype html>

<html lang="en">
<head>
<meta charset="utf-8">

<title>'#{title}'</title>
</head>

<body>
</body>
</html>
HTML
end

describe 'HTML generator' do
Expand Down
10 changes: 10 additions & 0 deletions january/3.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
require 'rspec'

class String

def total_words
array = self.gsub(/[^a-zA-Z]/, " ").split(" ")
array.length
end

def word_list
result = Hash.new(0)
array = self.gsub(/[^a-zA-Z]/, " ").split(" ")
array.each do |word|
result[word] += 1
end
result
end

end

describe 'Word Reporter' do
Expand Down
3 changes: 3 additions & 0 deletions january/5.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'rspec'

# https://www.crondose.com/2017/01/coding-exercise-generate-alphabet-ruby/

def generate_alphabet
('a'..'z').to_a
end

describe 'Alphabet generator' do
Expand Down
2 changes: 2 additions & 0 deletions january/6.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rspec'
# https://www.youtube.com/watch?v=s2iSmKPECHA

def increment_value str
str + str.next.slice(-1)
end

describe 'Increment string value sequence' do
Expand Down
6 changes: 4 additions & 2 deletions january/7.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
require 'rspec'

# https://www.youtube.com/watch?v=LzbYNN48JZg
menu = {
'appetizers': ['Chips', 'Quesadillas', 'Flatbread'],
'entrees': ['Steak', 'Chicken', 'Lobster'],
'dessers': ['Cheesecake', 'Cake', 'Cupcake']
'desserts': ['Cheesecake', 'Cake', 'Cupcake']
}

def daily_special hash
array = []
hash.map { |category| array << category.last }.flatten.sample
end

describe 'Nested hash element selector' do
Expand Down