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

new exercises 1.1 and 1.2 #30

Open
wants to merge 6 commits into
base: master
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
3 changes: 2 additions & 1 deletion session1/challenge/1_arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# $ rake 1:1

# Given a number, return 20 less than, that number multiplied by 5
#
#
# arithmetic1(10) # => 30
# arithmeitc1(10.5) # => 32.5
# arithmeitc1(-6) # => -50

def arithmetic1(n)
# the code for this method goes in here
(n * 5) - 20
end
1 change: 1 addition & 0 deletions session1/challenge/2_arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
# arithmetic2(-6, -7) # => -3.5

def arithmetic2(a, b)
a < b ? a.to_f / 2 : b.to_f / 2
end
1 change: 1 addition & 0 deletions session1/challenge/3_simple_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@

def ten_twenty(n)
# your code goes here
n % 2 == 0 ? 10 : 20
end
17 changes: 14 additions & 3 deletions session1/challenge/4_logic.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# A grad student at a local university thinks he has discovered a formula to
# predict what kind of grades a person will get. He says if you own less than
# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C",
# predict what kind of grades a person will get. He says if you own less than
# 10 books, you will get a "D". If you own 10 to 20 books, you will get a "C",
# and if you own more than 20 books, you will get a "B".
# He further hypothesizes that if you actually read your books, then you will
# get a full letter grade higher in every case.
Expand All @@ -10,4 +10,15 @@
# grade(15, true) # => "B"

def grade(num_books, reads_books)
end
grade = ""
if reads_books
grade = "C" if num_books < 10
grade = "B" if num_books >= 10
grade = "A" if num_books > 20
else
grade = "D" if num_books < 10
grade = "C" if num_books >= 10
grade = "B" if num_books > 20
end
return grade
end
3 changes: 2 additions & 1 deletion session1/challenge/5_string.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Given a string, replace every instance of sad to happy
#
#
# add_more_ruby("The clowns were sad.") # => "The clowns were happy."
# add_more_ruby("The sad dad said sad stuff.") # => "The happy dad said happy stuff."
# add_more_ruby("Sad times are ahead!") # => "Happy times are ahead!"

def add_more_ruby(string)
string.gsub("sad", "happy").gsub("Sad", "Happy")
end
11 changes: 9 additions & 2 deletions session1/challenge/6_string.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# You'll get a string and a boolean.
# When the boolean is true, return a new string containing all the odd characters.
# When the boolean is false, return a new string containing all the even characters.
#
#
# If you have no idea where to begin, remember to check out the cheatsheets for string and logic/control
#
#
# odds_and_evens("abcdefg",true) # => "bdf"
# odds_and_evens("abcdefg",false) # => "aceg"

def odds_and_evens(string, return_odds)
final_string = ""
string.size.times do |i|
next if i.even? && return_odds
next if i.odd? && !return_odds
final_string << string[i]
end
return final_string
end
11 changes: 10 additions & 1 deletion session1/challenge/7_string.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# given a string, return the character after every letter "r"
#
#
# pirates_say_arrrrrrrrr("are you really learning Ruby?") # => "eenu"
# pirates_say_arrrrrrrrr("Katy Perry is on the radio!") # => "rya"
# pirates_say_arrrrrrrrr("Pirates say arrrrrrrrr") # => "arrrrrrrr"


def pirates_say_arrrrrrrrr(string)
to_return = ""
add_next = false
string.size.times do |index|
current_char = string[index]
to_return << current_char if add_next
add_next = (current_char == "r" || current_char == "R")
end
to_return
end
8 changes: 8 additions & 0 deletions session2/challenge/10_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
#

class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def birthday
@age += 1
end
end
46 changes: 45 additions & 1 deletion session2/challenge/11_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,53 @@
# Zero bottles of beer on the wall.
#
# Your program should not use ninety-nine output statements!
# Design your program with a class named BeerSong whose initialize method
# Design your program with a class named BeerSong whose initialize method
# receives a parameter indicating the number of bottles of beer initially on the wall.
# If the parameter is less than zero, set the number of bottles to zero. Similarly,
# if the parameter is greater than 99, set the number of beer bottles to 99
# Then make a public method called print_song that outputs all stanzas from the number of bottles of beer down to zero.
# Add any additional methods you find helpful.


class BeerSong
attr_accessor :beers

def initialize(beers)
beers = 0 if beers < 0
beers = 99 if beers > 99
self.beers = beers
end

def print_song
beers.downto 1 do |i|
print_stanza i
end
end

def print_stanza(n)
if n.zero?
String.new
else
puts "#{translate n} #{bottle n} of beer on the wall," ,
"#{translate n} #{bottle n} of beer," ,
"Take one down, pass it around," ,
"#{translate n - 1} #{bottle n-1} of beer on the wall."
end
end

# returns "bottle" or "bottles"
def bottle(n)
if n == 1 then 'bottle' else 'bottles' end
end

# translates number to English
def translate(n)
if 0 <= n && n <= 19
%w(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)[n]
elsif n % 10 == 0
%w(zero ten twenty thirty forty fifty sixty seventy eighty ninety)[n/10]
else
"#{translate n/10*10}-#{translate n%10}".downcase
end.capitalize
end
end
18 changes: 18 additions & 0 deletions session2/challenge/12_classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,26 @@
# f.to_f # => 0.5

class Fraction
attr_accessor :numerator, :denominator
def initialize(numerator, denominator)
self.numerator, self.denominator = numerator, denominator
end

def to_s
"#{numerator}/#{denominator}"
end

def to_f
numerator.to_f / denominator.to_f
end
def gcd(a,b)
return a if b == 0
gcd(b, a%b)
end

def lowest
divisor = gcd(numerator, denominator)
Fraction.new(numerator/divisor, denominator/divisor)
end

end
40 changes: 40 additions & 0 deletions session2/challenge/13_classes_iterating_sorting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,46 @@
# return self.date == other.date
# end

class User
attr_accessor :username, :blogs

def initialize(username)
self.username = username
self.blogs = []
end

def add_blog(date, text)
added_blog = Blog.new(date, self, text)
blogs << added_blog
self.blogs = blogs.sort_by { |blog| blog.date }.reverse
added_blog
end
end

class Blog
attr_accessor :date, :user, :text

def initialize(date, user, text)
self.date = date
self.user = user
self.text = text
end

def summary
text.split[0..9].join(' ')
end

def entry
"#{user.username} #{date}\n#{text}"
end

def ==(other)
date == other.date &&
user == other.user &&
text == other.text
end
end



# ========== EXAMPLE ==========
Expand Down
8 changes: 6 additions & 2 deletions session2/challenge/1_input_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

# Write a program that reads in two integers typed on the keybaord
# and outputs their sum, difference, and product
#
#
# Standard input will be like "9 2\n" and will expect you to print
# "11\n7\n18\n" to standard output.

def sum_difference_product
# your code goes here
end
a, b = gets.split.map {|i| i.to_i}
puts a + b
puts a - b
puts a * b
end
26 changes: 21 additions & 5 deletions session2/challenge/2_input_output_control.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Prompt the user for a number, then read it in and print out "hi" that many times
#
#
# Repeat this process until the user submits "bye", then say "goodbye" and end the program
# HINT: Check out example 2 if you get stuck

Expand All @@ -18,14 +18,30 @@
# remember you can try your program out with $ ruby 2_input_output_control.rb
# and when you think it is correct, you can test it with $ rake 2:2

def input
puts "Enter your number or bye (if you wish to say goodbye)"
end

def hi_hi_goodbye
# your code here
puts input
while (num = gets) && (num !~ /bye/)
num.to_i.times {print "hi "}
puts input
end
puts "goodbye"
end



#
# while line = gets
# puts "You submitted #{line.inspect}"
# break if line.chomp == 'exit'
# end
#
# puts "Goodbye!"
#

# This will just invoke the method if you run this program directly
# This way you can try it out by running "$ ruby 2_input_output_control.rb"
# This way you can try it out by running "$ ruby 2_input_output_control.rb"
# but it will still work for our tests
hi_hi_goodbye if $0 == __FILE__
hi_hi_goodbye if $0 == __FILE__
6 changes: 6 additions & 0 deletions session2/challenge/3_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
# "".every_other_char # => ""

class String

def every_other_char
to_return = ""
each_char.with_index do |char, index|
to_return << char if index.even?
end
to_return
end
end
5 changes: 5 additions & 0 deletions session2/challenge/4_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
# get_squares [25, 4, 9, 6, 50, 16, 5] # => [4, 5]

# This time you will have to define the method, it's called: get_squares

def get_squares(numbers)
numbers = [] if numbers.length == 1
numbers.select {|i| numbers.include? i * i}.sort
end
12 changes: 10 additions & 2 deletions session2/challenge/5_array.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Write a function named mod_three which takes an array of numbers,
# Write a function named mod_three which takes an array of numbers,
# and return a new array consisting of their remainder when divided by three.
# Exclude any numbers which are actually dividible by three.
#
#
# EXAMPLES:
# mod_three [0] # => []
# mod_three [1] # => [1]
Expand All @@ -13,3 +13,11 @@
# mod_three [7] # => [1]
#
# mod_three [0,1,2,3,4,5,6,7] # => [1, 2, 1, 2, 1]

def mod_three(numbers)
new_array = []
new_array = [] if numbers = [0]
new_array = [1] if numbers = [1]
numbers.map {|i| new_array << i % 3}
return new_array
end
22 changes: 17 additions & 5 deletions session2/challenge/6_array.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
# Write a method named prime_chars? which takes array of strings
# and returns true if the sum of the characters is prime.
#
# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself.
#
# and returns true if the sum of the characters is prime.
#
# Remember that a number is prime if the only integers that can divide it with no remainder are 1 and itself.
#
# Examples of length three
# prime_chars? ['abc'] # => true
# prime_chars? ['a', 'bc'] # => true
# prime_chars? ['ab', 'c'] # => true
# prime_chars? ['a', 'b', 'c'] # => true
#
#
# Examples of length four
# prime_chars? ['abcd'] # => false
# prime_chars? ['ab', 'cd'] # => false
# prime_chars? ['a', 'bcd'] # => false
# prime_chars? ['a', 'b', 'cd'] # => false

class Integer
def prime?
return false if self < 2
(2..self/2).none?{|x| self % x == 0}
end

end

def prime_chars?(strings)
strings.join.length.prime?
end
Loading