forked from alexch/learn_ruby
-
Notifications
You must be signed in to change notification settings - Fork 12
resolve the exercises 1-10 #6
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
Open
zKlau
wants to merge
1
commit into
Agilefreaks:master
Choose a base branch
from
zKlau:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def hello | ||
"Hello!" | ||
end | ||
|
||
def greet(name) | ||
"Hello, " + name + "!" | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
def ftoc(f) | ||
(f - 32.0) * 5.0/9.0 | ||
end | ||
|
||
def ctof(c) | ||
c * 9.0/5.0 + 32.0 | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def add(a,b) | ||
a + b | ||
end | ||
|
||
def subtract(a,b) | ||
a - b | ||
end | ||
|
||
def sum(a) | ||
rez = 0 | ||
a.each { |i| rez += i} | ||
rez | ||
end | ||
|
||
|
||
def mult(a) | ||
rez = 1 | ||
a.each { |i| rez *= i } | ||
rez | ||
end | ||
|
||
def pow(a,b) | ||
a ** b | ||
end | ||
|
||
def fact(a) | ||
rez = 1 | ||
for i in 1..a do | ||
rez *= i | ||
end | ||
rez | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def echo(msg) | ||
msg | ||
end | ||
|
||
def shout(msg) | ||
echo(msg.upcase) | ||
end | ||
|
||
def repeat(msg,n = 2) | ||
((echo(msg) + " ") * n).chop | ||
end | ||
|
||
def start_of_word(word,n = 1) | ||
word[0,n] | ||
end | ||
|
||
def first_word(word) | ||
word.split(" ").first | ||
end | ||
|
||
def titleize(msg) | ||
ltwrds = ["and","the", "over", "in","of", "a", "an", "to", "for", "with"] | ||
ix = 0 | ||
wrds = msg.split(' ') | ||
wrds.each do |w| | ||
if !ltwrds.include?(w) or ix == 0 | ||
w.capitalize! | ||
end | ||
ix += 1 | ||
w | ||
end | ||
wrds.join(" ") | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can simplify method, without using any loops. Only a if else block |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
def translate(text) | ||
vowels = "aeiou" | ||
words = text.split(" ") | ||
|
||
words.map! do |word| | ||
if vowels.include?(word[0]) | ||
word + "ay" | ||
else | ||
if word.start_with?("qu") | ||
word = word[2..-1] + "qu" | ||
elsif word[1..2] == "qu" | ||
word = word[3..-1] + word[0..2] | ||
else | ||
while !vowels.include?(word[0]) do | ||
word = word[1..-1] + word[0] | ||
end | ||
end | ||
word + "ay" | ||
end | ||
end | ||
|
||
result = words.join(" ") | ||
puts result | ||
result | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def reverser | ||
msg = yield | ||
wrds = msg.split(' ') | ||
wrds.map{|w| w.reverse!} | ||
|
||
wrds.join(' ') | ||
end | ||
|
||
def adder(n = 1) | ||
yield + n | ||
end | ||
|
||
|
||
def repeater (n = 1) | ||
for i in 1..n do | ||
yield | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def measure(n = 1) | ||
time1 = Time.now | ||
|
||
n.times { yield } | ||
|
||
time2 = Time.now | ||
(time2 - time1).to_f / n | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name could be asigned to nil, and simplified with using ternary operator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Friend | ||
|
||
def greeting(name = "") | ||
if name == "" | ||
"Hello!" | ||
else | ||
"Hello, #{name}!" | ||
end | ||
|
||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Book | ||
|
||
def titleize(msg) | ||
ltwrds = ["and","the", "over", "in","of", "a", "an", "to", "for", "with"] | ||
ix = 0 | ||
wrds = msg.split(' ') | ||
wrds.each do |w| | ||
if !ltwrds.include?(w) or ix == 0 | ||
w.capitalize! | ||
end | ||
ix += 1 | ||
w | ||
end | ||
wrds.join(" ") | ||
end | ||
|
||
|
||
def title=(title) | ||
@title = titleize(title) | ||
end | ||
|
||
def title | ||
@title | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Timer | ||
|
||
def seconds | ||
@seconds ||= 0 | ||
end | ||
|
||
def seconds=(seconds) | ||
@seconds = seconds | ||
end | ||
|
||
def time_string | ||
h = seconds / 3600 | ||
m = (seconds % 3600) / 60 | ||
s = seconds % 60 | ||
|
||
format("%02d:%02d:%02d", h, m, s) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
class Temperature | ||
def initialize(temp = {}) | ||
if temp[:f] | ||
@in_fahrenheit = temp[:f] | ||
@in_celsius = Temperature.ftoc(temp[:f]) | ||
elsif temp[:c] | ||
@in_fahrenheit = Temperature.ctof(temp[:c])#temp[:c] * 9.0/5.0 + 32.0 | ||
@in_celsius = temp[:c] | ||
end | ||
end | ||
|
||
def self.ftoc(f) | ||
(f - 32.0) * 5.0/9.0 | ||
end | ||
|
||
def self.ctof(c) | ||
c * 9.0/5.0 + 32.0 | ||
end | ||
|
||
def self.from_celsius(ctmp) | ||
Temperature.new(:c => ctmp) | ||
end | ||
def self.from_fahrenheit(ftmp) | ||
Temperature.new(:f => ftmp) | ||
end | ||
def in_fahrenheit() | ||
@in_fahrenheit ||= 0 | ||
end | ||
|
||
def in_celsius() | ||
@in_celsius ||= 0 | ||
end | ||
end | ||
|
||
class Celsius < Temperature | ||
def initialize(c) | ||
super(:c => c) | ||
end | ||
end | ||
|
||
class Fahrenheit < Temperature | ||
def initialize(f) | ||
super(:f => f) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can also leave msg.upcase, without echo