Skip to content

learn to program #520

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 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
ce7eac8
ch09 ask.rb complete
shellop Jun 9, 2017
0ac22c6
ch09 roman_numerals.rb
shellop Jun 9, 2017
3544e78
ch9 roman numerals programs
shellop Jun 9, 2017
6e3b3a3
ch9 roman numerals programs
shellop Jun 9, 2017
a0108cf
ch9 roman numerals programs
shellop Jun 9, 2017
7208366
ch9 programs edited
shellop Jun 9, 2017
69ef343
ch10 english number
shellop Jun 9, 2017
505e8c0
ch13 orange_tree
shellop Jun 9, 2017
0b47e77
updated roman numerals to return string
shellop Jun 9, 2017
2cefdb8
updated roman numerals to return string
shellop Jun 9, 2017
ba1f007
ch9 programs debugged
shellop Jun 9, 2017
094b666
ch10 dictionary_sort
shellop Jun 9, 2017
8c14c84
ch10 dictionary_sort
shellop Jun 9, 2017
cb1215b
updated ch9 with lines at end of files
shellop Jun 9, 2017
f7ab622
debugging dictionary, added shuffle
shellop Jun 10, 2017
55c5394
shuffle
shellop Jun 11, 2017
b7d966e
english number bottles of beer
shellop Jun 11, 2017
bda4937
ch10
shellop Jun 11, 2017
0aa6998
english number
shellop Jun 11, 2017
6376141
english number
shellop Jun 11, 2017
70746f4
shuffle and english number
shellop Jun 11, 2017
986fd19
playlist
shellop Jun 12, 2017
cd1da98
roman to int
shellop Jun 12, 2017
18d864e
roman to int
shellop Jun 12, 2017
162226b
roman to int
shellop Jun 12, 2017
802de11
extend integer
shellop Jun 12, 2017
2c05842
extend integer
shellop Jun 12, 2017
1d93ae5
extend integer
shellop Jun 12, 2017
0565b12
orange tree
shellop Jun 12, 2017
a317b46
return organge tree info
shellop Jun 12, 2017
bb6ccb1
return organge tree info
shellop Jun 12, 2017
fee3e4e
organge tree
shellop Jun 12, 2017
2f66737
organge tree
shellop Jun 12, 2017
52ce5fa
orange
shellop Jun 12, 2017
a5cd5a9
orange
shellop Jun 12, 2017
bd5329f
orange
shellop Jun 12, 2017
da32618
orange
shellop Jun 12, 2017
6648ad3
orange
shellop Jun 13, 2017
dfe2fc7
orange
shellop Jun 13, 2017
64dbd80
orange
shellop Jun 13, 2017
184f533
clock
shellop Jun 13, 2017
bb70b6d
prog log
shellop Jun 13, 2017
36f68db
better logger
shellop Jun 13, 2017
98b0075
ch14
shellop Jun 13, 2017
fae4953
ch14
shellop Jun 13, 2017
0638ccd
ch14
shellop Jun 13, 2017
3501077
seconds
shellop Jun 14, 2017
69344aa
seconds
shellop Jun 14, 2017
26f3471
ch12
shellop Jun 14, 2017
2140882
ch13
shellop Jun 14, 2017
ef70c75
bottles
shellop Jun 14, 2017
9de0778
pics
shellop Jun 14, 2017
e129329
ch11
shellop Jun 14, 2017
d80ffbe
ch11
shellop Jun 14, 2017
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
13 changes: 12 additions & 1 deletion ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
def ask question
# your code here
# 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
end
end
end
16 changes: 15 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,17 @@
def old_roman_numeral num
# your code here
# your code here
num = num.to_i
(puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000))

numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"I" }
roman = ""

numerals.keys.each do |k|
n = num/k
if n > 0
roman += (numerals[k]*n)
num = num % k
end
end
return roman
end
17 changes: 16 additions & 1 deletion ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
def roman_numeral num
# your code here
# your code here
num = num.to_i
(puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000))

numerals = {1000=>"M", 500=>"D", 400=>"CD", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 9=>"IX", 5=>"V", 4=>"IV", 1=>"I" }
roman = ""

numerals.keys.each do |k|
n = num/k
if n > 0
roman += numerals[k]*n
num = num % k
end
end

return roman
end
19 changes: 19 additions & 0 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
return recursive_sort(arr, [])
end

def recursive_sort(unsorted_array, sorted_array)
return sorted_array if unsorted_array.empty?
#l = unsorted_array.length-1
smallest = unsorted_array.pop
unsorted2 = []
unsorted_array.each do |check|
if check.downcase < smallest.downcase
unsorted2 << smallest
smallest = check
else
unsorted2 << check
end
end
sorted_array << smallest

recursive_sort(unsorted2, sorted_array)
end
122 changes: 121 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,123 @@
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!

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

onesPlace = ['one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine']
tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen']
nums = {'quindecillion'=>10**48, 'quattuordecillion'=>10**45, 'tredecillion'=>10**42, 'duodecillion'=>10**39, 'undecillion'=>10**36,
'decillion'=>10**33, 'nonillion'=>10**30, 'octillion'=>10**27, 'septillion'=>10**24, 'sextillion'=>10**21,
'quintillion'=> 10**18, 'quadrillion'=> 10**15,
'trillion'=> 10**12, 'billion'=> 10**9, 'million'=> 10**6, 'thousand'=> 10**3, 'hundred'=> 10**2 }

# "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

nums.each do |name,value|
#print name
write = left/value # How many millions left to write out?
left = left - write*value # Subtract off those millions.

if write > 0
# trillions
trillions = english_number write
numString = numString + (english_number write) + " " + name.to_s

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

=begin
write = left/1000000000000 # How many millions left to write out?
left = left - write*1000000000000 # Subtract off those millions.

if write > 0
# Now here's a really sly trick:
hundreds = english_number write
numString = numString + hundreds + ' hundred'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number". Remember that "write" is (at the moment) the number of
# hundreds we have to write out. After we add "hundreds" to
# "numString", we add the string ' hundred' after it.
# So, for example, if we originally called english_number with
# 1999 (so "number" = 1999), then at this point "write" would
# be 19, and "left" would be 99. The laziest thing to do at this
# point is to have english_number write out the 'nineteen' for us,
# then we write out ' hundred', and then the rest of
# english_number writes out 'ninety-nine'.

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' '
end
end
=end
#### ------------> TENS

write = left/10 # How many tens left to write out?
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.
numString = numString + 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
numString = numString + tensPlace[write-1]
# The "-1" is because tensPlace[3] is 'forty', not 'thirty'.
end

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

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

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

# Now we just return "numString"...
return numString
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)
177 changes: 176 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,176 @@
# your code here
=begin
https://pine.fm/LearnToProgram/chap_08.html

Expand upon englishNumber. First, put in thousands.
So it should return 'one thousand' instead of 'ten hundred'
and 'ten thousand' instead of 'one hundred hundred'.
Expand upon englishNumber some more.
Now put in millions, so you get 'one million' instead of
'one thousand thousand'. Then try adding billions and trillions.
How high can you go?

=end


def weddingNumber number
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!

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

onesPlace = ['one', 'two', 'three', 'four', 'five',
'six', 'seven', 'eight', 'nine']
tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty',
'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen', 'eighteen', 'nineteen']

# "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

#### ------------> TRILLIONS

write = left/1000000000000 # How many millions left to write out?
left = left - write*1000000000000 # Subtract off those millions.

if write > 0
# trillions
trillions = weddingNumber write
numString = numString + trillions + ' trillion'

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' and '
end
end

#### ------------> BILLIONS

write = left/1000000000 # How many millions left to write out?
left = left - write*1000000000 # Subtract off those millions.

if write > 0
# billions
billions = weddingNumber write
numString = numString + billions + ' billion'

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' and '
end
end

#### ------------> MILLIONS
write = left/1000000 # How many millions left to write out?
left = left - write*1000000 # Subtract off those millions.

if write > 0
# millions
millions = weddingNumber write
numString = numString + millions + ' million'

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' and '
end
end

#### ------------> THOUSANDS

write = left/1000 # How many thousands left to write out?
left = left - write*1000 # Subtract off those thousands.

if write > 0
# for thousands
hundreds = weddingNumber write
numString = numString + hundreds + ' thousand'

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ', '
end
end

#### ------------> HUNDREDS

write = left/100 # How many hundreds left to write out?
left = left - write*100 # Subtract off those hundreds.

if write > 0
# Now here's a really sly trick:
hundreds = weddingNumber write
numString = numString + hundreds + ' hundred'
# That's called "recursion". So what did I just do?
# I told this method to call itself, but with "write" instead of
# "number". Remember that "write" is (at the moment) the number of
# hundreds we have to write out. After we add "hundreds" to
# "numString", we add the string ' hundred' after it.
# So, for example, if we originally called englishNumber with
# 1999 (so "number" = 1999), then at this point "write" would
# be 19, and "left" would be 99. The laziest thing to do at this
# point is to have englishNumber write out the 'nineteen' for us,
# then we write out ' hundred', and then the rest of
# englishNumber writes out 'ninety-nine'.

if left > 0
# So we don't write 'two hundredfifty-one'...
numString = numString + ' and '
end
end

#### ------------> TENS

write = left/10 # How many tens left to write out?
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.
numString = numString + 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
numString = numString + tensPlace[write-1]
# The "-1" is because tensPlace[3] is 'forty', not 'thirty'.
end

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

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

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

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


def ninety_nine_bottles_of_beer num
while (num > 0)
puts weddingNumber(num) + " bottles of beer on the wall, " + weddingNumber(num) + " bottles of beer."
puts "Take one down and pass it around, " + weddingNumber(num-1) + " bottles of beer on the wall."
num -= 1
end
end

ninety_nine_bottles_of_beer 99
Loading