Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 0 Editorial #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 session_0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Rails. You can refer to books on programming or websites like

> Read the [installation guide](/installation.md) and [setting up local
> workspace](/essential_git.md) to get started.
>
> You can also read the [solution](https://github.com/IRIS-NITK/IRIS-RoR-Bootcamp-2020/pull/62) here.

## Fizz Buzz

Expand Down
17 changes: 16 additions & 1 deletion session_0/fizz_buzz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,20 @@
# We will raise an `ArgumentError` exception to let the caller know that
# their function arguments were incorrect.
def fizz_buzz(n:, x: 3, y: 5)
raise NotImplementedError # TODO
raise ArgumentError if n < 0 || x <= 0 || y <= 0
# map construct helps us transform the contents of arrays, ranges and hash
(1..n).map do |i|
if i % x == 0 && i % y == 0
'FizzBuzz'
elsif i % x == 0
'Fizz'
elsif i % y == 0
'Buzz'
else
i.to_s
end
end
# Note ruby methods implicitly return the value of the last executed intructions
# In this case the modified range
# Read mode about it here https://cutt.ly/XjlMYhJ
end
26 changes: 21 additions & 5 deletions session_0/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ class Item
4 => 'Miscellaneous'
}

# TODO: Store quantity
attr_accessor :name, :price, :category_id, :discount_rate,
:discount_deadline
:discount_deadline, :quantity

def initialize(name: '', price: 0, quantity: 1, category_id: 4,
discount_rate: 0, discount_deadline: Time.now)
Expand All @@ -23,13 +22,14 @@ def initialize(name: '', price: 0, quantity: 1, category_id: 4,
# Returns a boolean value whether than item is discounted i.e. the
# discount deadline has been crossed or not.
def discounted?
raise NotImplementedError # TODO
Time.now <= discount_deadline
end

# If the item is discounted, the current price is
# `price * (100 - discount rate) / 100`. Otherwise, it is same as the price.
#
# TODO: Implement instance method 'current_price'
def current_price
discounted? ? price * (100 - discount_rate)/100 : price
end

# The stock price of item is defined as product of current price and
# quantity.
Expand All @@ -40,6 +40,22 @@ def discounted?
#
# Note: If there are no items for category, stock price for category
# should be zero.
def stock_price
current_price * quantity
end

def self.stock_price_by_category(items)
# .keys method returns an array containing keys of CATEGORIES hash
# .to_h coverts the returned keys array into hash with the array values as keys (category_id) and zero as value.
stock_prices = CATEGORIES.keys.to_h { |category_id| [category_id, 0] }

# We now iterate over every item in the items array
# more about each can be found here https://www.tutorialspoint.com/ruby/ruby_iterators.htm
items.each do |item|
# Add price of individual items to the the stock price of coresponding category_id
stock_prices[item.category_id] += item.stock_price
end
# implicitly return stock prices
stock_prices
end
end
38 changes: 33 additions & 5 deletions session_0/word_processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
# any non-lower case alphabet (A..Z) to corresponding lower case
# alphabet
def lower_case(words)
raise NotImplementedError # TODO
# Equivalent to words.map { |word| word.downcase }
words.map(&:downcase)
end

# Similar to `lower_case`, this function modifies the array in-place
# and does not return any value.
def lower_case!(words)
raise NotImplementedError # TODO
# the '!' symbol, implies inplace operation
# it is equivalent to words = words.map(&:downcase)
words.map!(&:downcase)
end

# Given a prefix and an array of words, return an array containing
Expand All @@ -18,7 +21,9 @@ def lower_case!(words)
# words_with_prefix('apple', ['apple', 'ball', 'applesauce']) would
# return the words 'apple' and 'applesauce'.
def words_with_prefix(prefix, words)
raise NotImplementedError # TODO
# the select method filters a collection, keeping values that
# evaluate to true.
words.select { |word| word.start_with?(prefix) }
end

# The similarity score between two words is defined as the length of
Expand All @@ -34,11 +39,34 @@ def words_with_prefix(prefix, words)
# The function `similarity_score` takes two words and returns the
# similarity score (an integer).
def similarity_score(word_1, word_2)
raise NotImplementedError # TODO
smaller_len = word_1.size < word_2.size ? word_1.size : word_2.size

# Find the first index where the words are different.
# If there is no such index, return the smaller length.
(0...smaller_len).find { |i| word_1[i] != word_2[i] } || smaller_len
end

# Given a chosen word and an array of words, return an array of word(s)
# with the maximum similarity score in the order they appear.
def most_similar_words(chosen_word, words)
raise NotImplementedError # TODO
max_score = 0
similar_words = []

words.each do |word|
# score of current word
score = similarity_score(chosen_word, word)

# if the score of current word > max_score
# update the max score and add word to answers array
if score > max_score
max_score = score
similar_words = [word]
# If a word has the same score as established max score append it to the answers list
elsif score == max_score
similar_words.append(word)
end
end

# return the answer using rubys implicit return feature
similar_words
end