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

Pull Request for Week 2 - Learn to Program #493

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
15 changes: 14 additions & 1 deletion ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
def ask question
# your code here
while true
puts question
reply = gets.chomp.downcase
if (reply == 'yes' || reply == 'no')
if reply == 'yes'
return true
else
return false
end
break
else
puts 'Please answer "yes" or "no".'
end
end
end
12 changes: 11 additions & 1 deletion ch09-writing-your-own-methods/old_school_roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
def old_roman_numeral num
# your code here
puts "Oops! Try using a positive integer!" if num <= 0
roman = ''
roman << 'M' * (num / 1000)
roman << 'D' * (num % 1000 / 500)
roman << 'C' * (num % 500 / 100)
roman << 'L' * (num % 100 / 50)
roman << 'X' * (num % 50 / 10)
roman << 'V' * (num % 10 / 5)
roman << 'I' * (num % 5 / 1)

return roman
end
37 changes: 36 additions & 1 deletion ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
def roman_numeral num
# your code here
puts "Oops! Try using a positive integer!" if num <= 0

thous = (num / 1000)
hunds = (num % 1000 / 100)
tens = (num % 100 / 10)
ones = (num % 10)

roman = 'M' * thous

if hunds == 9
roman = roman + 'CM'
elsif hunds == 4
roman = roman + 'CD'
else
roman = roman + 'D' * (num % 1000 / 500)
roman = roman + 'C' * (num % 500 / 100)
end

if tens == 9
roman = roman + 'XC'
elsif tens == 4
roman = romann + 'XL'
else
roman = roman + 'L' * (num % 100 / 50)
roman = roman + 'X' * (num % 50 / 10)
end

if ones == 9
roman = roman + 'IX'
elsif ones == 4
roman = roman + 'IV'
else
roman = roman + 'V' * (num % 10 / 5)
roman = roman + 'I' * (num % 5 / 1)
end
return roman
end
8 changes: 7 additions & 1 deletion ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
def dictionary_sort arr
# your code here
return arr if arr.length <= 1

middle = arr.pop
less = arr.select{|x| x.downcase < middle.downcase}
more = arr.select{|x| x.downcase >= middle.downcase}

dictionary_sort(less) + [middle] + dictionary_sort(more)
end
87 changes: 85 additions & 2 deletions ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,86 @@
def english_number number
# your code here
end
if number < 0
return "Oops! Please use a positive integer!"
end
if number == 0
return "zero"
end

num_string = ''

ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']

tens_place = ['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 = number

while zillions.length > 0
zil_pair = zillions.pop
zil_name = zil_pair[0]
zil_base = 10 ** zil_pair[1]
write = left/zil_base
left = left - write*zil_base

if write > 0
# Now here's the recursion:
prefix = english_number write
num_string = num_string + prefix + ' ' + zil_name

if left > 0
num_string = num_string + ' '
end
end
end

write = left/10
left = left - write*10

if write > 0
if ((write == 1) and (left > 0))
num_string = num_string + teenagers[left-1]
left = 0
else
num_string = num_string + tens_place[write-1]

end

if left > 0
num_string = num_string + '-'
end
end

write = left
left = 0

if write > 0
num_string = num_string + ones_place[write-1]
end

num_string
end
16 changes: 15 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# your code here
require './ch10-nothing-new/english_number.rb'

def pluralize(num, word)
num == "one" ? num + " " + word : num + " " + word + "s"
end

count = 9999
while count > 0
number_bottles = english_number(count)
bottles = pluralize(number_bottles, "bottle")
puts "#{bottles.capitalize} of beer on the wall, #{bottles} of beer."
count -= 1
bottles = pluralize(english_number(count), "bottle")
puts "Take one down and pass it around, #{bottles} of beer on the wall."
end
2 changes: 1 addition & 1 deletion ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def shuffle arr
# your code here
arr.shuffle
end
8 changes: 7 additions & 1 deletion ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
def sort arr
# your code here
return arr if arr.length <= 1

middle = arr.pop
less = arr.select{|x| x < middle}
more = arr.select{|x| x >= middle}

sort(less) + [middle] + sort(more)
end
55 changes: 53 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,54 @@
def music_shuffle filenames
# your code here
end
songs_and_paths = filenames.map do |s|
[s, s.split('/')] # [song, path]
end

tree = {:root => []}

# put each song into the tree
insert_into_tree = proc do |branch, song, path|
if path.length == 0 # add to current branch
branch[:root] << song
else # delve deeper
sub_branch = path[0]
path.shift # like "pop", but pops off the front

if !branch[sub_branch]
branch[sub_branch] = {:root => []}
end

insert_into_tree[branch[sub_branch], song, path]
end
end

songs_and_paths.each{|sp| insert_into_tree[tree, *sp]}

# recursively:
# - shuffle sub-branches (and root)
# - weight each sub-branch (and root)
# - merge (shuffle) these groups together
shuffle_branch = proc do |branch|
shuffled_subs = []

branch.each do |key, unshuffled|
shuffled_subs << if key == :root
unshuffled # At this level, these are all duplicates.
else
shuffle_branch[unshuffled]
end
end

weighted_songs = []

shuffled_subs.each do |shuffled_songs|
shuffled_songs.each_with_index do |song, idx|
num = shuffled_songs.length.to_f
weight = (idx + rand) / num
weighted_songs << [song, weight]
end
end

weighted_songs.sort_by{|s,v| v}.map{|s,v| s}
end
shuffle_branch[tree]
end
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
music = Dir['**/*.mp3']

File.open('playlist.m3u', 'w') do |f|
music.each { |song| f.write(song + "\n") }
end

puts "Finished!"
30 changes: 29 additions & 1 deletion ch11-reading-and-writing/safer_picture_downloading.rb
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# your code here
Dir.chdir '/This_PC/Users/Joe/Pictures'

pic_names = Dir['This_PC/F:/*.jpg']

puts 'What are we calling this group of snaps?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "

pic_number = 1
pic_names.each do |name|

print '.'
new_name = if pic_number < 0
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end

if File.exist?(new_name)
puts 'File name conflict. Aborting renaming process.'
exit
else
File.rename name, new_name
pic_number = pic_number + 1
end
end
puts
puts 'Done and dusted!'
19 changes: 18 additions & 1 deletion ch12-new-classes-of-objects/happy_birthday.rb
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
# your code here
puts 'What year were you born?'
b_year = gets.chomp.to_i

puts 'What month were you born? (1-12)'
b_month = gets.chomp.to_i

puts 'What day of the month were you born?'
b_day = gets.chomp.to_i

b = Time.local(b_year, b_month, b_day)
t = Time.new

age = 1

while Time.local(b_year + age, b_month, b_day) <= t
puts 'SPANK!'
age = age + 1
end
2 changes: 1 addition & 1 deletion ch12-new-classes-of-objects/one_billion_seconds.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# your code here
puts(Time.gm(1993, 6, 4, 10, 31) + 10**9)
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def roman_to_integer roman
# your code here
def roman_to_integer(roman)

roman = roman.upcase

return 0 if roman == "NULLA"
numerals = { M: 1000,
D: 500,
C: 100,
L: 50,
X: 10,
V: 5,
I: 1 }

number = 0
previous_value = 0

i = roman.length - 1
while i >= 0
value = numerals[roman[i].to_sym]
return "Not a valid roman numeral" if value.nil?
if value >= previous_value
number += value
else
number -= value
end
previous_value = value
i -= 1
end
number
end
Loading