Skip to content

Commit

Permalink
finish assignment 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamesover committed Jun 26, 2017
1 parent d821f5e commit e770e88
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'https://rubygems.org'
ruby '2.3'
ruby '2.3.3'

gem 'rspec'
29 changes: 29 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)

PLATFORMS
ruby

DEPENDENCIES
rspec

RUBY VERSION
ruby 2.3.3p222

BUNDLED WITH
1.14.6
28 changes: 21 additions & 7 deletions lib/ruby_intro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,47 @@
# Part 1

def sum arr
# YOUR CODE HERE
arr.inject(0, :+)
end

def max_2_sum arr
# YOUR CODE HERE
sum(arr.sort {|x, y| y <=> x}.take(2))
end

def sum_to_n? arr, n
# YOUR CODE HERE
return false if arr.size < 2

arr.permutation(2).any? {|a, b| a + b == n}
end

# Part 2

def hello(name)
# YOUR CODE HERE
"Hello, #{name}"
end

def starts_with_consonant? s
# YOUR CODE HERE
/^[b-df-hj-np-tv-z]/i.match(s) != nil
end

def binary_multiple_of_4? s
# YOUR CODE HERE
return true if s == "0"
/^[10]*00$/.match(s) != nil
end

# Part 3

class BookInStock
# YOUR CODE HERE
attr_accessor :isbn, :price

def initialize isbn, price
raise ArgumentError if isbn.empty? || price <= 0

@isbn = isbn
@price = price
end

def price_as_string
sprintf("$%2.2f", @price)
end
end

0 comments on commit e770e88

Please sign in to comment.