Skip to content

learn_to_program #508

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
wants to merge 21 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
33 changes: 30 additions & 3 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def ask question
# your code here
end
def ask(question)
while true
puts question
reply = gets.chomp.downcase
if reply == "yes"
return true
elsif reply == "no"
return false
end
puts "Please answer 'yes' or 'no'"
end
end

ask "Do you like eating tacos?"


=begin
ask "Do you like eating burritos?"
wets_bed = ask "Do you wet the bed?"
ask "Do you like eating chimichangas?"
ask "Do you like eating sopapillas?"
puts "Just a few more questions..."
ask "Do you like eating drinking horchata?"
ask "Do you like eating flautas?"
puts
puts "DEBRIEFING:"
puts "Thank you for"
puts
puts wets_bed
=end
19 changes: 16 additions & 3 deletions ch09-writing-your-own-methods/old_school_roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
def old_roman_numeral num
# your code here
end
def old_roman_numeral(number)
num = ""
num = num + "M" * (number / 1000)
num = num + "D" * (number % 1000 / 500)
num = num + "C" * (number % 500 / 100)
num = num + "L" * (number % 100 / 50)
num = num + "X" * (number % 50 / 10)
num = num + "V" * (number % 10 / 5)
num = num + "I" * (number % 5 / 1)
end

puts old_roman_numeral(4)
puts old_roman_numeral(5)
puts old_roman_numeral(10)
puts old_roman_numeral(19)
puts old_roman_numeral(22)
49 changes: 46 additions & 3 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
def roman_numeral num
# your code here
end
def roman_numeral(num)
thousands = (num / 1000)
hundreds = (num % 1000 / 100)
tens = (num % 100 / 10)
ones = (num % 10)
roman = "M" * thousands

if hundreds == 9
roman += "CM"
elsif hundreds == 4
roman += "CD"
else
roman += "D" * (num % 1000 / 500)
roman += "C" * (num % 500 / 100)
end
if tens == 9
roman += "XC"
elsif tens == 4
roman += "XL"
else
roman += "L" * (num % 100 / 50)
roman += "X" * (num % 50 / 10)
end
if ones == 9
roman += "IX"
elsif ones == 4
roman += "IV"
else
roman += "V" * (num % 10 / 5)
roman += "I" * (num % 5 / 1)
end
roman
end

puts roman_numeral(1)
puts roman_numeral(2)
puts roman_numeral(3)
puts roman_numeral(4)
puts roman_numeral(5)
puts roman_numeral(6)
puts roman_numeral(7)
puts roman_numeral(8)
puts roman_numeral(9)
puts roman_numeral(10)
puts roman_numeral(14)
puts roman_numeral(19)
25 changes: 22 additions & 3 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
def dictionary_sort arr
# your code here
end
def dictionary_sort(arr)
rec_sort_dic arr, []
end
def rec_sort_dic(unsorted, sorted)
if unsorted.length <= 0
return sorted
end
smallest = unsorted.pop
still_unsorted = []
unsorted.each do |tested|
if tested.downcase < smallest.downcase
still_unsorted.push smallest
smallest = tested
else
still_unsorted.push tested
end
end
sorted.push smallest
rec_sort_dic(still_unsorted, sorted)
end

puts(dictionary_sort(["zebra", "cheetah", "giraffe", "cougar", "leopard", "elephant"]))
67 changes: 65 additions & 2 deletions ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,66 @@
def english_number number
# your code here
def english_number(num)
if num < 0
return "Please enter a positive number."
end
if num == 0
return "zero"
end
num_string = ""
ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
tens = ["ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
teenagers = ["eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
zillions = [["hundred", 2], ["thousand", 3], ["million", 6], ["billion", 9], ["trillion", 12],
["quadrillion", 15], ["quintillion", 18], ["sextillion", 21], ["septillion", 24],
["octillion", 27], ["nonillion", 30], ["decillion", 33], ["undecillion", 36],
["duodecillion", 39], ["tredecillion", 42], ["quattuordecillion", 45],
["quindecillion", 48], ["sexdecillion", 51], ["septendecillion", 54],
["octodecillion", 57], ["novemdecillion", 60], ["vigintillion", 63], ["googol", 100]]
left = num

while zillions.length > 0
zil_pair = zillions.pop
zil_name = zil_pair[0]
zil_base = 10 ** zil_pair[1]
write = left / zil_base
left -= write ** zil_base
if write > 0
prefix = english_number write
num_string += prefix + " " + zil_name
if left > 0
num_string += " "
end
end
end
write = left / 10
left -= write * 10
if write > 0
if write == 1 && left > 0
num_string += teenagers[left - 1]
left = 0
else
num_string += tens[write - 1]
end
if left > 0
num_string += "-"
end
end
write = left
left = 0
if write > 0
num_string += ones[write - 1]
end
num_string
end

puts english_number(1)
puts english_number(2)
puts english_number(3)
puts english_number(4)
puts english_number(5)
puts english_number(6)
puts english_number(7)
puts english_number(8)
puts english_number(9)
puts english_number(10)
puts english_number(222222)
puts english_number(33333)
14 changes: 13 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# your code here
num_at_start = 99
num_now = num_at_start
while num_now > 2
puts "#{english_number(num_now).capitalize} bottles of beer on the wall, #{english_number(num_now)} bottles of beer!"
num_now -= 1
puts "Take on down, pass it around, #{english_number(num_now)} bottles of beer on the wall!"
end
puts """
Two bottles of beer on the wall, two bottles of beer!
Take one down, pass it around, one bottle of beer on the wall!
One bottle of beer on the wall, one bottle of beer!
Take one dow, pass it around, no more bottles of beer on the wall!
"""
31 changes: 28 additions & 3 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
def shuffle arr
# your code here
end
# long way
def shuffle(arr)
shuff = []
while arr.length > 0
rand_index = rand(arr.length) # Randomly pick an element
curr_index = 0
new_arr = []
arr.each do |item|
if curr_index == rand_index
shuff.push item
else
new_arr.push item
end
curr_index += 1
end
arr = new_arr
end
shuff
end

# short way
=begin
def shuffle(arr)
arr.sort_by{rand}
end
=end
animals = ["zebra", "donkey", "monkey", "tarsier", "platypus", "anteater", "koala"]
puts shuffle(animals)
41 changes: 38 additions & 3 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
def sort arr
# your code here
end
# long way
def sort(arr)
rec_sort arr, []
end

def rec_sort(unsorted, sorted)
if unsorted.length <= 0
return sorted
end
smallest = unsorted.pop # starting point
still_unsorted = []
unsorted.each do |tested|
if tested < smallest # comparison
still_unsorted.push smallest # add for further resorting later
smallest = tested # reassign value for tested to be compared with next tested value
else
still_unsorted.push tested # in this case tested is larger
end
end
sorted.push smallest
rec_sort(still_unsorted, sorted)
end

puts (sort(["can", "feel","singing", "like", "a", "can"]))
animals = ["zebra", "donkey", "monkey", "tarsier", "platypus", "anteater", "koala"]
puts
puts sort(animals)

# short way
=begin
def sort(arr)
return arr if arr.length <= 1 # breaks the loop
mid_point = arr.pop # acts as the starting point for comparisons
smaller = arr.select {|x| x < mid_point}
bigger = arr.select {|x| x > mid_point}
sort(smaller) + [mid_point] + sort(bigger)
end
=end
31 changes: 29 additions & 2 deletions ch11-reading-and-writing/build_a_better_playlist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def music_shuffle filenames
# your code here
def music_shuffle(filenames)
filenames = filenames.sort
length = filenames.length
2.times do
l_idx = 0
r_idx = length / 2
shuff = []
while shuff.length < length
if shuff.length % 2 == 0
shuff.push(filenames[r_idx])
r_idx += 1
else
shuff.push(filenames[l_idx])
l_idx += 1
end
end
filenames = shuff
end
arr = []
cut = rand(length)
idx = 0
while idx < length
arr.push(filenames[(idx + cut) % length])
idx += 1
end
arr
end

songs = ["aa/bbb", "aa/ccc", "aa/ddd", "AAA/xxxx", "AA/yyyy", "AAA/zzzz", "foo/bar"]
puts music_shuffle(songs)
8 changes: 7 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# your code here
all_oggs = shuffle(Dir["**/*.ogg"])
File.open "playlist.m3u", "w" do |f|
all_oggs.each do |ogg|
f.write ogg + "\n"
end
end
puts "Finished!"
22 changes: 21 additions & 1 deletion ch12-new-classes-of-objects/birthday_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# your code here
birth_dates = {}
File.read("birthdates.txt").each_line do |line|
line = line.chomp
first_comma = 0
while line[first_comma] != "," &&
first_comma < line.length
first_comma += 1
end
name = line[0..(first_comma - 1)]
date = line[-12..-1]
birth_dates[name] = date
end

puts "Who's birthday would you like to know?"
name = $stdin.gets.chomp
date = birth_dates[name]
if date == nil
puts "Sorry, I don't know that one."
else
puts date[0..5]
end
14 changes: 13 additions & 1 deletion ch12-new-classes-of-objects/happy_birthday.rb
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# your code here
puts "Only enter integers please"
puts "What year were you born?"
year = gets.chomp
puts "What month were you born?"
month = gets.chomp
puts "What date was the day you were born?"
day = gets.chomp

age_result = Time.now - Time.local(year, month, day)
age_in_years = age_result / (60 * 60 * 24 *365)
puts "Just in case you have forgotten, I would like to remind you"
puts "that you are #{age_in_years.floor} years old!"
puts "SPANK!\n" * age_in_years
3 changes: 2 additions & 1 deletion ch12-new-classes-of-objects/one_billion_seconds.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# your code here
age = Time.now - Time.local(1985, 6, 5, 14, 00)
puts "I am #{age} seconds old!"
Loading