Skip to content

Added ask.rb and old_roman_numeral.rb #477

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 16 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
35 changes: 33 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
def ask question
# your code here
end
while true
puts question
input = gets.chomp.downcase
if input == 'yes'
return true
elsif input == 'no'
return false
else
puts "please answer yes or no"
end
end
end

puts "Hello and thank you for participating"
puts

ask "Do you like tacos?"
ask "Do you like burritos?"
wets_bed = ask "Do you wet the bed"
ask "Do you like chimichangas?"
ask "Do you like sopapillas?"
puts "just a couple more..."
ask "Do you like horchata?"
ask "Do you like flautas?"

puts
puts "Debriefing:"
puts "Thank you for participating"
puts
puts
puts

puts wets_bed
15 changes: 13 additions & 2 deletions ch09-writing-your-own-methods/old_school_roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
def old_roman_numeral num
# your code here
end
roman = ''

roman = roman + 'M' * (num / 1000)
roman = roman + 'D' * (num % 1000 / 500)
roman = roman + 'C' * (num % 500 / 100)
roman = roman + 'L' * (num % 100 / 50)
roman = roman + 'X' * (num % 50 / 10)
roman = roman + 'V' * (num % 10 / 5)
roman = roman + 'I' * (num % 5 / 1)
roman
end

# puts(old_roman_numeral(1999))
36 changes: 34 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
def roman_numeral num
# your code here
end
raise 'Must use positive integer' if num <= 0

digit_vals = [['I', 5, 1],
['V', 10, 5],
['X', 50, 10],
['L', 100, 50],
['C', 500, 100],
['D', 1000, 500],
['M', nil, 1000]]

roman = ''
remaining = nil

# Build string "roman" in reverse.
build_rev = proc do |l,m,n|
num_l = m ? (num % m / n) : (num / n)
full = m && (num_l == (m/n - 1))

if full && (num_l>1 || remaining)
# must carry
remaining ||= l # carry l if not already carrying
else
if remaining
roman << l + remaining
remaining = nil end
roman << l * num_l
end
end
digit_vals.each {|l,m,n| build_rev[l,m,n]}

roman.reverse
end

# puts(old_roman_numeral(1999))
59 changes: 56 additions & 3 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
def dictionary_sort arr
# your code here
end

=begin
def sort some_array
recursive_sort some_array, []
end


def recursive_sort unsorted_array, sorted_array
sorted_array = unsorted_array.sort { |x, y| x <=> y }
end

puts sort ["meems", "meems", "freems", "frrops", "zeeps", "haha"]
=end




# The well-known quicksort algorithm.
=begin
def sort arr
return arr if arr.length <= 1

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

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

end
sort(less) + [middle] + sort(more)
end
p(sort(['blablabla', 'car', 'Alpha', 'zebra']))
=end

def super_sort unsorted_array
capitalized = unsorted_array.select {|x| x.capitalize == x}
uncapitalized = unsorted_array.select {|x| x.capitalize != x}
capitalized.sort! {|x, y| x <=> y }
uncapitalized.sort! {|x, y| x <=> y }
puts capitalized
puts
puts
puts uncapitalized
=begin
if x == x.capitalize
capitals << x
puts capitals
=end

end

super_sort ["baaa", "Baa", 'car', 'Car']
129 changes: 128 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,130 @@
def english_number number
# your code here
if number < 0 # No negative numbers.
return 'Please enter a number that isn\'t negative.'
end
if number == 0
return 'zero'
end

# No more special cases! No more returns!

num_string = '' # This is the string we will return.

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" is how much of the number
# we still have left to write out.
# "write" is the part we are
# writing out right now.
# write and left...get it? :)
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 # How many zillions left?
left = left - write*zil_base # Subtract off those zillions.

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

if left > 0
# So we don't write 'two billionfifty-one'...
num_string = num_string + ' '
end
end
end

write = left/10 # How many tens left?
left = left - write*10 # Subtract off those tens.

if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of
# "twelve", we have to make a special exception
# for these.
num_string = num_string + teenagers[left-1]
# The "-1" is because teenagers[3] is
# 'fourteen', not 'thirteen'.

# Since we took care of the digit in the
# ones place already, we have nothing left to write.
left = 0
else
num_string = num_string + tens_place[write-1]
# The "-1" is because tens_place[3] is
# 'forty', not 'thirty'.
end

if left > 0
# So we don't write 'sixtyfour'...
num_string = num_string + '-'
end
end

write = left # How many ones left to write out?
left = 0 # Subtract off those ones.

if write > 0
num_string = num_string + ones_place[write-1]
# The "-1" is because ones_place[3] is
# 'four', not 'three'.
end

# Now we just return "num_string"...
num_string
end

# puts english_number( 0)
# puts english_number( 9)
# puts english_number( 10)
# puts english_number( 11)
# puts english_number( 17)
# puts english_number( 32)
# puts english_number( 88)
# puts english_number( 99)
# puts english_number(100)
# puts english_number(101)
# puts english_number(234)
# puts english_number(3211)
# puts english_number(999999)
# puts english_number(1000000000000)
# puts english_number(109238745102938560129834709285360238475982374561034)
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_relative 'english_number'

@number = 100

while @number > 0
if @number > 1
puts ((english_number @number) + ' bottles of beer on the wall, ' +
(english_number @number) + ' Bottles of beer!')
puts ('Take one off the wall and pass it around or smth, ' +
(english_number (@number - 1)) + ' Bottles of beer on the wall!')
elsif @number == 1
puts "One bottle of beer on the wall."
end
@number = @number - 1
end
17 changes: 15 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
def shuffle arr
# your code here
end
random = Random.new.rand(1..3)
operand = arr.pop
c = 100
while c > 0
if random > 2
operand.prepend arr
else
operand << arr
end
c = c - 1
end
return arr
end

shuffle(['hi', 'bye', 'yes', 'no'])
3 changes: 0 additions & 3 deletions ch10-nothing-new/sort.rb

This file was deleted.

7 changes: 7 additions & 0 deletions ch10-nothing-new/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
arr = ['Cayman']

if arr.pop.capitalize == arr.pop
puts "It's capitalized"
else
puts "it isn't"
end
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 '/users/michaelcalvey/Desktop/Music/'

pic_names = Dir['F:/**/*.jpg']
puts "What would you like to call this batch?"

batch_name = gets.chomp

puts
print "Downloading #{pic_names.length} files: "

pic_number = 1

pic_names.each do |name|
print '.'

if File.exists?("#{batch_name}#{pic_number}.jpg") == true
puts "File already exists, skipping..."
exit
else
new_name = if pic_number < 10
"#{batch_name}#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
end
File.rename name, new_name
pic_number = pic_number + 1
end
puts
Loading