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

Week 2: learn_to_program repo #502

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
33 changes: 31 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
def ask question
# your code here
end
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

puts 'Hello and thank you blah blah'
ask 'Do you like eating tacos?'
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 drinking horchata?'
ask 'Do you like eating flautas?'

puts
puts 'DEBRIEFING:'
puts 'Thank you for...'
puts
puts wets_bed
18 changes: 16 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,17 @@
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 )
roman
end

puts old_roman_numeral 17
puts old_roman_numeral 1567
puts old_roman_numeral 222
puts old_roman_numeral 865
puts old_roman_numeral 4000
53 changes: 51 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
def roman_numeral num
# your code here
end
thousands_to_convert = (num / 1000)
hundreds_to_convert = (num % 1000 / 100)
tens_to_convert = (num % 100 / 10)
units_to_convert = num % 10

if thousands_to_convert <= 3
converted_thous = "M" * thousands_to_convert
end

if hundreds_to_convert <= 3
converted_huns = "C" * hundreds_to_convert
elsif hundreds_to_convert == 4
converted_huns = 'C' + 'D'
elsif hundreds_to_convert == 5
converted_huns = 'D'
elsif hundreds_to_convert > 5 && hundreds_to_convert < 9
converted_huns = 'D' + "C" * (hundreds_to_convert - 5)
elsif hundreds_to_convert == 9
converted_huns = 'C' + "M"
end

if tens_to_convert <= 3
converted_tens = "X" * tens_to_convert
elsif tens_to_convert == 4
converted_tens = 'X' + 'L'
elsif tens_to_convert == 5
converted_tens = 'L'
elsif tens_to_convert > 5 && tens_to_convert < 9
converted_tens = 'L' + "X" * (tens_to_convert - 5)
elsif tens_to_convert == 9
converted_tens = 'X' + "C"
end

if units_to_convert <= 3
converted_units = "I" * units_to_convert
elsif units_to_convert == 4
converted_units = 'I' + 'V'
elsif units_to_convert == 5
converted_units = 'V'
elsif units_to_convert > 5 && units_to_convert < 9
converted_units = 'V' + "I" * (units_to_convert - 5)
elsif units_to_convert == 9
converted_units = 'I' + "X"
end

converted_num = converted_thous + converted_huns + converted_tens + converted_units
puts converted_num
converted_num
end
roman_numeral 119
roman_numeral 101
37 changes: 34 additions & 3 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
def dictionary_sort arr
# your code here
end
def dictionary_sort some_array
recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array
final_array = []
sorting_hash = Hash.new 0
lookup_value = 0
sorting_array = []
unsorted_array.each do |x|
sorting_hash[x.downcase] = x
end
unsorted_array.each do |x|
sorting_array.push(x.downcase)
end
while sorting_array.length > 0
smallest_word = sorting_array.pop
sorting_array.each do |x|
if x == smallest_word
sorted_array.push(x)
elsif x < smallest_word
sorting_array.push(smallest_word)
smallest_word = x
end
end
sorting_array.delete(smallest_word)
sorted_array.push(smallest_word)
end
sorted_array.each do |x|
final_array.push(sorting_hash[x])
end
puts final_array
return final_array
end
72 changes: 71 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
def english_number number
# your code here
if number < 0
return 'Please enter a numer that isnt negative'
end
if number == 0
return 'zero'
end
num_string = ''
ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
left = number

write = left / 1000000000
left = left - write*1000000000
if write > 0
billions = english_number write
num_string = num_string + billions + ' billion'
if left > 0
num_string = num_string + ' '
end
end

write = left / 1000000
left = left - write*1000000
if write > 0
millions = english_number write
num_string = num_string + millions + ' million'
if left > 0
num_string = num_string + ' '
end
end

write = left / 1000
left = left - write*1000
if write > 0
thousands = english_number write
num_string = num_string + thousands + ' thousand'
if left > 0
num_string = num_string + ' '
end
end
write = left / 100
left = left - write*100
if write > 0
hundreds = english_number write
num_string = num_string + hundreds + ' hundred'
if left > 0
num_string = num_string + ' '
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

puts english_number 5436000000
99 changes: 98 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,98 @@
# your code here
def english_number number
if number < 0
return 'Please enter a numer that isnt negative'
end
if number == 0
return 'zero'
end
num_string = ''
ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
left = number

write = left / 1000000000
left = left - write*1000000000
if write > 0
billions = english_number write
num_string = num_string + billions + ' billion'
if left > 0
num_string = num_string + ' '
end
end

write = left / 1000000
left = left - write*1000000
if write > 0
millions = english_number write
num_string = num_string + millions + ' million'
if left > 0
num_string = num_string + ' '
end
end

write = left / 1000
left = left - write*1000
if write > 0
thousands = english_number write
num_string = num_string + thousands + ' thousand'
if left > 0
num_string = num_string + ' '
end
end
write = left / 100
left = left - write*100
if write > 0
hundreds = english_number write
num_string = num_string + hundreds + ' hundred'
if left > 0
num_string = num_string + ' and '
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

def ninety_nine_bottles start_num


while start_num > 0
start_num_english = english_number start_num
start_num_caps = start_num_english.capitalize
next_num = start_num - 1
next_num_english = english_number next_num

if start_num == 1
puts "#{start_num_caps} bottle of beer on the wall, #{start_num_english}"
puts "bottle of beer. Take one down, pass it around, #{next_num_english}"
puts "bottles of beer on the wall."
puts
else
puts "#{start_num_caps} bottles of beer on the wall, #{start_num_english}"
puts "bottles of beer. Take it down, pass it around, #{next_num_english}"
puts "bottles of beer on the wall."
puts
end
start_num = start_num - 1
next_num = next_num - 1
end
end

ninety_nine_bottles 10
13 changes: 11 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
def shuffle arr
# your code here
end
shuffled_arr = []
while arr.length > 0
shuffle_entry = rand(arr.length)
entry_to_push = arr[shuffle_entry]
shuffled_arr.push(entry_to_push)
arr.delete_at(shuffle_entry)
end
arr = shuffled_arr
puts arr
arr
end
28 changes: 25 additions & 3 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
def sort arr
# your code here
end
def sort some_array
recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array

if unsorted_array.length <= 0
return sorted_array
puts sorted_array
end
smallest_word = unsorted_array.pop
still_unsorted = []
unsorted_array.each do |x|
if x < smallest_word
still_unsorted.push(smallest_word)
smallest_word = x
else
still_unsorted.push(x)
end
end
sorted_array.push(smallest_word)
recursive_sort still_unsorted, sorted_array
end

puts (sort(["fucking", "hell", "bollocks"]))
Loading