From ab149d5b44fcecf273c91027714ddce3ca95e16c Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Fri, 9 Jan 2015 14:47:05 -0800 Subject: [PATCH 01/32] assignment01 complete --- assignments/assignment01.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/assignments/assignment01.rb b/assignments/assignment01.rb index c88806d..7b9bb87 100644 --- a/assignments/assignment01.rb +++ b/assignments/assignment01.rb @@ -38,13 +38,16 @@ def number_to_string(n, lang) # it accepts a string # and returns the same string with each word capitalized. def titleize(s) - # your implementation here + my_words = s.split(" ") + cap_sentence = "" + my_words.each {|w| cap_sentence << w.capitalize << " "} + cap_sentence.chop end # Your method should generate the following results: -titleize "hEllo WORLD" #=> "Hello World" +puts titleize "hEllo WORLD" #=> "Hello World" -titleize "gooDbye CRUel wORLD" #=> "Goodbye Cruel World" +puts titleize "gooDbye CRUel wORLD" #=> "Goodbye Cruel World" # ======================================================================================== @@ -53,7 +56,12 @@ def titleize(s) # Write your own implementation of `reverse` called `my_reverse` # You may *not* use the built-in `reverse` method def my_reverse(s) - # your implementation here + s_array = s.split("") + s_array_reverse = [] + s_array.each { |c| s_array_reverse.insert(0, c) + end + s_reverse = s_array_reverse.join + end # Your method should generate the following results: @@ -68,7 +76,14 @@ def my_reverse(s) # Write a method `palindrome?` # that determines whether a string is a palindrome def palindrome?(s) - # your implementation here + s1 = s.sub(' ', '').downcase + s2 = s1.reverse + + if s2 == s1 + true + else + false + end end # Your method should generate the following results: From 28520820209363f0850cee7e72b67dd4d6e7f419 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 19 Jan 2015 22:16:31 -0800 Subject: [PATCH 02/32] assignment 2 tasks 1-3 complete --- assignments/assignment02.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index 4f1147a..d4d53f0 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -11,6 +11,10 @@ def to_sentence(ary) # your implementation here + str1 = ary[0...-1].reduce {|item, acc| item.to_s + ", #{acc}"} + a2 = [str1, ary[-1]] + a2.compact! + a2.join " and " end # Your method should generate the following results: @@ -25,11 +29,17 @@ def to_sentence(ary) # implement methods "mean", "median" on Array of numbers def mean(ary) - # your implementation here + tot = ary.reduce {|i, acc| i + acc } + avg = tot.to_f/ary.length end def median(ary) - # your implementation here + alen = ary.length + if alen % 2 == 1 then + med = ary[alen/2] + else + mean([ary[alen/2],ary[(alen/2)+1]]) + end end # Your method should generate the following results: @@ -46,6 +56,11 @@ def median(ary) # implement method `pluck` on array of hashes def pluck(ary, key) # your implementation here + out_array = [] + ary.each do |h| + out_array << h[key] + end + out_array end # Your method should generate the following results: @@ -56,6 +71,8 @@ def pluck(ary, key) {name: "Ringo", instrument: "drums" } ] pluck records, :name #=> ["John", "Paul", "George", "Ringo"] + + pluck records, :instrument #=> ["guitar", "bass", "guitar", "drums"] From 95270f0e86666c0f4d6dcec887b68f54343ac458 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Tue, 20 Jan 2015 06:47:09 -0800 Subject: [PATCH 03/32] started assignemtn 2 problem 4 --- assignments/assignment02.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index d4d53f0..3c61a26 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -72,7 +72,6 @@ def pluck(ary, key) ] pluck records, :name #=> ["John", "Paul", "George", "Ringo"] - pluck records, :instrument #=> ["guitar", "bass", "guitar", "drums"] @@ -90,3 +89,10 @@ def pluck(ary, key) # - daily balance # - summary: # - starting balance, total deposits, total withdrawals, ending balance + +file.open("assignment02-input.csv", "r") do |infile| + records = file.readlines.map do |inline| + + end + +end From cc89d8664388c3d4d6b2e827e865dea176691728 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 21 Jan 2015 06:52:18 -0800 Subject: [PATCH 04/32] another try at assignment 2 problem 4 --- assignments/assignment02.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index 3c61a26..576e4b0 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -90,9 +90,11 @@ def pluck(ary, key) # - summary: # - starting balance, total deposits, total withdrawals, ending balance -file.open("assignment02-input.csv", "r") do |infile| - records = file.readlines.map do |inline| - +File.open("assignment02-input.csv", "r") do |infile| + records = infile.readlines.map do |inline| + f = inline.split(",") + {date: f[0], payee: f[1], amount: f[2], type: f[3]} end - end + + From e710e7bb709b6068c6f0471f957db1526464edff Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 21 Jan 2015 17:24:34 -0800 Subject: [PATCH 05/32] some progress on assignment 2 problem 4 --- assignments/assignment02-out.htm | 338 +++++++++++++++++++++++++++++++ assignments/assignment02-out.txt | 10 + assignments/assignment02.rb | 91 ++++++++- 3 files changed, 433 insertions(+), 6 deletions(-) create mode 100644 assignments/assignment02-out.htm create mode 100644 assignments/assignment02-out.txt diff --git a/assignments/assignment02-out.htm b/assignments/assignment02-out.htm new file mode 100644 index 0000000..5d00208 --- /dev/null +++ b/assignments/assignment02-out.htm @@ -0,0 +1,338 @@ + + +

Withdrawals

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DatePayeeAmount
12/1/2014Walgreens$21.92
12/11/2014Home Depot$17.89
12/11/2014Safeway$42.15
12/12/2014ATM$60.00
12/13/2014check #5126$25.00
12/13/2014QFC$13.25
12/13/2014check #5134$45.68
12/14/2014check #5133$17.50
12/14/2014Office Depot$24.85
12/14/2014Chipotle$10.42
12/15/2014ATM$60.00
12/15/2014Starbucks$5.30
12/16/20147-Eleven$5.26
12/16/2014check #5127$50.00
12/16/2014Arco$48.01
12/17/2014Apple$110.62
12/18/2014Starbucks$5.30
12/18/2014Starbucks$5.30
12/18/2014check #5129$125.00
12/18/2014Bartell's$14.82
12/19/2014Chipotle$10.42
12/22/2014Safeway$28.45
12/22/2014QFC$48.13
12/22/2014Shell$52.00
12/22/2014check #5130$120.00
12/23/2014Starbucks$5.30
12/26/2014check #5135$62.50
12/27/2014Starbucks$5.30
12/27/2014Walgreens$14.56
12/28/2014check #5136$62.50
12/29/2014Starbucks$5.30
12/30/2014ATM$100.00
12/30/2014Safeway$35.17
12/31/2014check #5131$20.00
12/31/2014Starbucks$8.45
12/4/2014Starbucks$5.30
12/4/2014Subway$7.80
12/5/2014Nike Town$85.00
12/5/2014ATM$60.00
12/6/2014Starbucks$5.30
12/7/2014Arco$42.00
12/7/2014Safeway$23.89
12/7/2014check #5132$75.00
12/8/2014Starbucks$5.30
12/9/2014Bartell's$7.83
12/9/2014check #5128$575.00
+ + + +

Deposits

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DatePayeeAmount
12/4/2014Univ Washington$1500.00
12/6/2014ATM$50.00
12/15/2014ATM$50.00
12/6/2014ATM$500.00
12/21/2014ATM$20.00
12/17/2014Univ Washington$1500.00
+ + + diff --git a/assignments/assignment02-out.txt b/assignments/assignment02-out.txt new file mode 100644 index 0000000..2e62100 --- /dev/null +++ b/assignments/assignment02-out.txt @@ -0,0 +1,10 @@ + + + + + + + + +
12/4/2014Univ Washington
12/6/2014ATM
12/15/2014ATM
12/6/2014ATM
12/21/2014ATM
12/17/2014Univ Washington
+ diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index 576e4b0..c66fc0e 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -58,7 +58,7 @@ def pluck(ary, key) # your implementation here out_array = [] ary.each do |h| - out_array << h[key] + out_array << h[key] end out_array end @@ -89,12 +89,91 @@ def pluck(ary, key) # - daily balance # - summary: # - starting balance, total deposits, total withdrawals, ending balance +def create_transaction_hash( date, payee, amount, type ) + { date: date, payee: payee, amount: amount, type: type } +end + +records = [] +transactions = File.open('assignment02-input.csv', "r") do |infile| + records = infile.readlines.map do |inline| + f = inline.chomp.split(",") + create_transaction_hash(f[0], f[1], f[2], f[3]) + end +end + +withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } +withdrawals.sort_by! {|x| x[:date]} +deposits = transactions.select {|h| h[:type] == 'deposit' } +deposits.sort_by! {|x| x[:date]} + + +def mean(ary) + tot = ary.reduce {|i, acc| i + acc } + avg = tot.to_f/ary.length +end + +def get_daily_balance(transactions) + days = transactions.map {|t| t[:date]}.uniq + puts days.inspect + transactions_by_day = days.reduce([]) do |day, acc| + acc[day] = transactions.select {|t| t[:date] == day} + end + #puts transactions_by_day + + +end +get_daily_balance(withdrawals) + +def render_record(rec) +< + #{rec[:date]} + #{rec[:payee]} + $#{rec[:amount]} + +RECORD +end + +def render_table_header() +<
+ Date + Payee + Amount + +HEADER +end -File.open("assignment02-input.csv", "r") do |infile| - records = infile.readlines.map do |inline| - f = inline.split(",") - {date: f[0], payee: f[1], amount: f[2], type: f[3]} - end +def render_header(record) + end +def render_records(records) +< + #{render_table_header} + #{records.map {|r| render_record(r)}.join "\n"} + +RECORDS +end + +def render_body(title, records) +< +

#{title}

+ #{render_records(records)} + +BODY +end + +File.open('assignment02-out.htm', "w") do |outfile| + outfile.puts "" + outfile.puts render_body("Withdrawals", withdrawals) + outfile.puts render_body("Deposits", deposits) + outfile.puts "" +end + + + + From 952bfefae40c57fab3fd36886c72e1a7a91c012a Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 21 Jan 2015 21:48:56 -0800 Subject: [PATCH 06/32] a bit more incrementalprogress on assignment 2 problem 4 --- assignments/assignment02-out.htm | 41 ++++++++++++++++---------------- assignments/assignment02.rb | 36 +++++++++++++++++++--------- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/assignments/assignment02-out.htm b/assignments/assignment02-out.htm index 5d00208..b19eba7 100644 --- a/assignments/assignment02-out.htm +++ b/assignments/assignment02-out.htm @@ -1,5 +1,4 @@ - - +

Withdrawals

@@ -286,8 +285,6 @@

Withdrawals

- -

Deposits

@@ -297,42 +294,44 @@

Deposits

- + + + + + + + - + - + - - - + + + - + - + - - - - - - - +
12/4/201412/15/2014ATM$50.00
12/17/2014 Univ Washington $1500.00
12/6/201412/21/2014 ATM$50.00$20.00
12/15/2014ATM$50.0012/4/2014Univ Washington$1500.00
12/6/2014 ATM$500.00$50.00
12/21/201412/6/2014 ATM$20.00
12/17/2014Univ Washington$1500.00$500.00
- - +

Total withdrawals: $2164

+

Total deposits: $3620

+

Final Balance: $1456.0

+ diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index c66fc0e..376f6ea 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -106,6 +106,10 @@ def create_transaction_hash( date, payee, amount, type ) deposits = transactions.select {|h| h[:type] == 'deposit' } deposits.sort_by! {|x| x[:date]} +def get_totals(records) + amounts = records.map {|r| r[:amount].to_f } + amounts.reduce(0) { |val, acc| val + acc }.to_f +end def mean(ary) tot = ary.reduce {|i, acc| i + acc } @@ -114,15 +118,14 @@ def mean(ary) def get_daily_balance(transactions) days = transactions.map {|t| t[:date]}.uniq + days.sort_by! {|d| d.split('/')[1].to_i} puts days.inspect - transactions_by_day = days.reduce([]) do |day, acc| + transactions_by_day = days.reduce({}) do |day, acc| acc[day] = transactions.select {|t| t[:date] == day} end - #puts transactions_by_day - - + puts transactions_by_day end -get_daily_balance(withdrawals) +get_daily_balance(deposits) def render_record(rec) <

#{title}

#{render_records(records)} - BODY end +def render_totals(transactions) + +end + +def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) + beginning_balance.to_f - tot_withdrawals.to_f + tot_deposits.to_f +end + +total_deposits = get_totals(deposits) +total_withdrawals = get_totals(withdrawals) + File.open('assignment02-out.htm', "w") do |outfile| - outfile.puts "" + outfile.puts "" outfile.puts render_body("Withdrawals", withdrawals) outfile.puts render_body("Deposits", deposits) - outfile.puts "" + outfile.puts "

Total withdrawals: $#{total_withdrawals}

" + outfile.puts "

Total deposits: $#{total_deposits}

" + outfile.puts "

Final Balance: $#{get_ending_balance(0, total_withdrawals, total_deposits)}

" + outfile.puts "" end From b00f301e05721ea2c83f1fcedb754c8ee5b307cc Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Thu, 22 Jan 2015 17:37:41 -0800 Subject: [PATCH 07/32] assignment 2 for submission --- assignments/assignment02-out.htm | 7 +++--- assignments/assignment02.rb | 39 ++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/assignments/assignment02-out.htm b/assignments/assignment02-out.htm index b19eba7..29d12aa 100644 --- a/assignments/assignment02-out.htm +++ b/assignments/assignment02-out.htm @@ -1,4 +1,8 @@ +

Summary

+

Total withdrawals: $2178.77

+

Total deposits: $3620.0

+

Final Balance: $1441.23

Withdrawals

@@ -331,7 +335,4 @@

Deposits

-

Total withdrawals: $2164

-

Total deposits: $3620

-

Final Balance: $1456.0

diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index 376f6ea..c70d5af 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -108,7 +108,7 @@ def create_transaction_hash( date, payee, amount, type ) def get_totals(records) amounts = records.map {|r| r[:amount].to_f } - amounts.reduce(0) { |val, acc| val + acc }.to_f + amounts.reduce(0) { |val, acc| val + acc }.round(2) end def mean(ary) @@ -116,16 +116,36 @@ def mean(ary) avg = tot.to_f/ary.length end -def get_daily_balance(transactions) +def get_daily_balance(transactions) days = transactions.map {|t| t[:date]}.uniq - days.sort_by! {|d| d.split('/')[1].to_i} - puts days.inspect - transactions_by_day = days.reduce({}) do |day, acc| - acc[day] = transactions.select {|t| t[:date] == day} + days.sort_by! {|i| i.split("/")[1].to_i} + transactions_by_day = days.reduce({}) do |acc, day| + acc[day] = transactions.select { |t| t[:date] == day } + acc end - puts transactions_by_day + sum_transactions_by_day = days.reduce({}) do |acc, day| + amounts = transactions_by_day[day].map {|record| record[:amount]} + sum = amounts.reduce {|acc, amount| acc.to_f + amount.to_f } + acc[day] = sum.to_f + acc + end + #bal = 0 + balances = {} + balances_by_day = days.reduce(0) do |acc, day| + acc + sum_transactions_by_day[day] + #balances[day] = acc[day] + sum_transactions_by_day[day] + #balances + end + puts sum_transactions_by_day + puts balances_by_day end + get_daily_balance(deposits) +puts deposits + +def total_transactions_by_day() + +end def render_record(rec) <" - outfile.puts render_body("Withdrawals", withdrawals) - outfile.puts render_body("Deposits", deposits) + outfile.puts "

Summary

" outfile.puts "

Total withdrawals: $#{total_withdrawals}

" outfile.puts "

Total deposits: $#{total_deposits}

" outfile.puts "

Final Balance: $#{get_ending_balance(0, total_withdrawals, total_deposits)}

" + outfile.puts render_body("Withdrawals", withdrawals) + outfile.puts render_body("Deposits", deposits) outfile.puts "" end From 91c73bde3f730ae6a72ba407dc5dd528cd9ec4d9 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 25 Jan 2015 07:52:25 -0800 Subject: [PATCH 08/32] more assignment 2 progress --- assignments/assignment02.rb | 55 +++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index c70d5af..7e46c09 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -129,19 +129,35 @@ def get_daily_balance(transactions) acc[day] = sum.to_f acc end - #bal = 0 - balances = {} - balances_by_day = days.reduce(0) do |acc, day| - acc + sum_transactions_by_day[day] - #balances[day] = acc[day] + sum_transactions_by_day[day] - #balances + bal = 0 + balances_by_day = days.reduce({}) do |acc, day| + acc[day] = bal + sum_transactions_by_day[day] + bal = acc[day] + acc + end + #puts sum_transactions_by_day + balances_by_day +end + +get_daily_balance(nt) + +def get_summable_transactions(transactions) + trans_vals = transactions.reject {|t| t[:date] == "date"} + out_trans = [] + out_trans = trans_vals.map do |t| + if t[:type] == "withdrawal" then + t[:amount] = t[:amount].to_f * -1.0 + else + t[:amount] = t[:amount].to_f + end + t end - puts sum_transactions_by_day - puts balances_by_day + # puts out_trans end -get_daily_balance(deposits) -puts deposits +#nt = get_summable_transactions(transactions) +puts nt +get_daily_balance(nt) def total_transactions_by_day() @@ -186,8 +202,23 @@ def render_body(title, records) BODY end -def render_totals(transactions) - +def render_balance_record(record) +< + #{rec[:date]} + #{rec[:bal]} + +RECORD +end + +def render_daily_balances(transactions) + nt = get_summable_transactions(transactions) + db = get_daily_balance(nt) + <DateEnding Balance + #{db.map {|rec| render_balance_record(rec)}.join "\n"} + + BALANCES end def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) From f1ec2f9865d19062755aa8097af7af8cc9b5d6a2 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 25 Jan 2015 11:11:16 -0800 Subject: [PATCH 09/32] completed assignment 2 --- assignments/assignment02-out.htm | 231 ++++++++++++++++++++++++------- assignments/assignment02.rb | 29 ++-- 2 files changed, 196 insertions(+), 64 deletions(-) diff --git a/assignments/assignment02-out.htm b/assignments/assignment02-out.htm index 29d12aa..74f6a0b 100644 --- a/assignments/assignment02-out.htm +++ b/assignments/assignment02-out.htm @@ -3,6 +3,133 @@

Summary

Total withdrawals: $2178.77

Total deposits: $3620.0

Final Balance: $1441.23

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
DateEnding Balance
12/1/2014-21.92
12/4/20141486.9
12/5/2014-145.0
12/6/2014544.7
12/7/2014-140.89
12/8/2014-5.3
12/9/2014-582.83
12/11/2014-60.04
12/12/2014-60.0
12/13/2014-83.93
12/14/2014-52.77
12/15/2014-15.299999999999997
12/16/2014-103.27
12/17/20141389.38
12/18/2014-150.42000000000002
12/19/2014-10.42
12/21/201420.0
12/22/2014-248.57999999999998
12/23/2014-5.3
12/26/2014-62.5
12/27/2014-19.86
12/28/2014-62.5
12/29/2014-5.3
12/30/2014-135.17000000000002
12/31/2014-28.45

Withdrawals

@@ -14,277 +141,277 @@

Withdrawals

- + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - +
12/1/2014 Walgreens$21.92$-21.92
12/11/2014 Home Depot$17.89$-17.89
12/11/2014 Safeway$42.15$-42.15
12/12/2014 ATM$60.00$-60.0
12/13/2014 check #5126$25.00$-25.0
12/13/2014 QFC$13.25$-13.25
12/13/2014 check #5134$45.68$-45.68
12/14/2014 check #5133$17.50$-17.5
12/14/2014 Office Depot$24.85$-24.85
12/14/2014 Chipotle$10.42$-10.42
12/15/2014 ATM$60.00$-60.0
12/15/2014 Starbucks$5.30$-5.3
12/16/2014 7-Eleven$5.26$-5.26
12/16/2014 check #5127$50.00$-50.0
12/16/2014 Arco$48.01$-48.01
12/17/2014 Apple$110.62$-110.62
12/18/2014 Starbucks$5.30$-5.3
12/18/2014 Starbucks$5.30$-5.3
12/18/2014 check #5129$125.00$-125.0
12/18/2014 Bartell's$14.82$-14.82
12/19/2014 Chipotle$10.42$-10.42
12/22/2014 Safeway$28.45$-28.45
12/22/2014 QFC$48.13$-48.13
12/22/2014 Shell$52.00$-52.0
12/22/2014 check #5130$120.00$-120.0
12/23/2014 Starbucks$5.30$-5.3
12/26/2014 check #5135$62.50$-62.5
12/27/2014 Starbucks$5.30$-5.3
12/27/2014 Walgreens$14.56$-14.56
12/28/2014 check #5136$62.50$-62.5
12/29/2014 Starbucks$5.30$-5.3
12/30/2014 ATM$100.00$-100.0
12/30/2014 Safeway$35.17$-35.17
12/31/2014 check #5131$20.00$-20.0
12/31/2014 Starbucks$8.45$-8.45
12/4/2014 Starbucks$5.30$-5.3
12/4/2014 Subway$7.80$-7.8
12/5/2014 Nike Town$85.00$-85.0
12/5/2014 ATM$60.00$-60.0
12/6/2014 Starbucks$5.30$-5.3
12/7/2014 Arco$42.00$-42.0
12/7/2014 Safeway$23.89$-23.89
12/7/2014 check #5132$75.00$-75.0
12/8/2014 Starbucks$5.30$-5.3
12/9/2014 Bartell's$7.83$-7.83
12/9/2014 check #5128$575.00$-575.0
@@ -300,37 +427,37 @@

Deposits

12/15/2014 ATM - $50.00 + $50.0 12/17/2014 Univ Washington - $1500.00 + $1500.0 12/21/2014 ATM - $20.00 + $20.0 12/4/2014 Univ Washington - $1500.00 + $1500.0 12/6/2014 ATM - $50.00 + $50.0 12/6/2014 ATM - $500.00 + $500.0 diff --git a/assignments/assignment02.rb b/assignments/assignment02.rb index 7e46c09..62ac8e1 100644 --- a/assignments/assignment02.rb +++ b/assignments/assignment02.rb @@ -130,16 +130,17 @@ def get_daily_balance(transactions) acc end bal = 0 - balances_by_day = days.reduce({}) do |acc, day| - acc[day] = bal + sum_transactions_by_day[day] - bal = acc[day] + balances_by_day = days.reduce([]) do |acc, day| + h = {} + h[:date] = day + h[:balance] = bal + sum_transactions_by_day[day] + acc << h acc end - #puts sum_transactions_by_day balances_by_day end -get_daily_balance(nt) +#balances = get_daily_balance(nt) def get_summable_transactions(transactions) trans_vals = transactions.reject {|t| t[:date] == "date"} @@ -205,22 +206,25 @@ def render_body(title, records) def render_balance_record(record) < - #{rec[:date]} - #{rec[:bal]} + #{record[:date]} + #{record[:balance]} RECORD end + def render_daily_balances(transactions) - nt = get_summable_transactions(transactions) - db = get_daily_balance(nt) - <DateEnding Balance - #{db.map {|rec| render_balance_record(rec)}.join "\n"} + #{db.map {|rec| render_balance_record(rec)}.join "\n"} - BALANCES +BALANCES end + def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) beginning_balance.to_f - tot_withdrawals.to_f + tot_deposits.to_f end @@ -234,6 +238,7 @@ def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) outfile.puts "

Total withdrawals: $#{total_withdrawals}

" outfile.puts "

Total deposits: $#{total_deposits}

" outfile.puts "

Final Balance: $#{get_ending_balance(0, total_withdrawals, total_deposits)}

" + outfile.puts render_daily_balances(transactions) outfile.puts render_body("Withdrawals", withdrawals) outfile.puts render_body("Deposits", deposits) outfile.puts "" From 821c65b68078058ecc9365d17c72a1ce0ff230b9 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 25 Jan 2015 12:08:25 -0800 Subject: [PATCH 10/32] completed assignment 3 problems 1 and 2 --- assignments/assignment03.rb | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index ee5cd93..46a1f01 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -7,6 +7,26 @@ # re-implement titleize and palindrome? as methods on String + class String + def titleize + my_words = self.split(" ") + cap_sentence = "" + my_words.each {|w| cap_sentence << w.capitalize << " "} + cap_sentence.chop + end + + def palindrome? + s1 = self.gsub(' ', '').downcase + s1 = s1.gsub(',', '').downcase + s2 = s1.reverse + if s2 == s1 + true + else + false + end + end + end + "hEllo WORLD".titleize #=> "Hello World" "gooDbye CRUel wORLD".titleize #=> "Goodbye Cruel World" @@ -23,6 +43,32 @@ # re-implement mean, median, to_sentence as methods on Array +class Array + def mean + tot = self.reduce {|i, acc| i + acc } + tot.to_f/self.length + end + + def median + alen = self.length + if alen % 2 == 1 then + med = self[alen/2] + else + mean([self[alen/2],self[(alen/2)+1]]) + end + end + + def to_sentence + str1 = self[0...-1].reduce {|item, acc| item.to_s + ", #{acc}"} + a2 = [str1, self[-1]] + a2.compact! + a2.join " and " + end +end + + + + # Your method should generate the following results: [1, 2, 3].mean #=> 2 [1, 1, 4].mean #=> 2 From 2aa78e629379c67f53b8aa771f1b43bc08843743 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Tue, 27 Jan 2015 19:39:30 -0800 Subject: [PATCH 11/32] worked on assignement 3 problem 3 --- assignments/assignment03.rb | 165 ++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index 46a1f01..08a02b8 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -94,3 +94,168 @@ def to_sentence # - WithdrawalTransaction # use blocks for your HTML rendering code + +class Transaction + def initialize(date, payee, amount, type) + @date = date + @payee = payee + @amount = amount + @type = type + end + def height; @height; end + def payee; @payee; end + def amount; @amount; end + def type; @type; end +end + +def create_transaction_hash( date, payee, amount, type ) + { date: date, payee: payee, amount: amount, type: type } +end + +records = [] +transactions = File.open('assignment02-input.csv', "r") do |infile| + records = infile.readlines.map do |inline| + f = inline.chomp.split(",") + create_transaction_hash(f[0], f[1], f[2], f[3]) + end +end + +withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } +withdrawals.sort_by! {|x| x[:date]} +deposits = transactions.select {|h| h[:type] == 'deposit' } +deposits.sort_by! {|x| x[:date]} + +def get_totals(records) + amounts = records.map {|r| r[:amount].to_f } + amounts.reduce(0) { |val, acc| val + acc }.round(2) +end + +def mean(ary) + tot = ary.reduce {|i, acc| i + acc } + avg = tot.to_f/ary.length +end + +def get_daily_balance(transactions) + days = transactions.map {|t| t[:date]}.uniq + days.sort_by! {|i| i.split("/")[1].to_i} + transactions_by_day = days.reduce({}) do |acc, day| + acc[day] = transactions.select { |t| t[:date] == day } + acc + end + sum_transactions_by_day = days.reduce({}) do |acc, day| + amounts = transactions_by_day[day].map {|record| record[:amount]} + sum = amounts.reduce {|acc, amount| acc.to_f + amount.to_f } + acc[day] = sum.to_f + acc + end + bal = 0 + balances_by_day = days.reduce([]) do |acc, day| + h = {} + h[:date] = day + h[:balance] = bal + sum_transactions_by_day[day] + acc << h + acc + end + balances_by_day +end + +#balances = get_daily_balance(nt) + +def get_summable_transactions(transactions) + trans_vals = transactions.reject {|t| t[:date] == "date"} + out_trans = [] + out_trans = trans_vals.map do |t| + if t[:type] == "withdrawal" then + t[:amount] = t[:amount].to_f * -1.0 + else + t[:amount] = t[:amount].to_f + end + t + end + # puts out_trans +end + +#nt = get_summable_transactions(transactions) +puts nt +get_daily_balance(nt) + + +def render_record(rec) +< + #{rec[:date]} + #{rec[:payee]} + $#{rec[:amount]} + +RECORD +end + +def render_table_header() +<
+ Date + Payee + Amount + +HEADER +end + +def render_header(record) +end + +def render_records(records) +< + #{render_table_header} + #{records.map {|r| render_record(r)}.join "\n"} + +RECORDS +end + +def render_body(title, records) +<#{title} + #{render_records(records)} +BODY +end + +def render_balance_record(record) +< + #{record[:date]} + #{record[:balance]} + +RECORD +end + + +def render_daily_balances(transactions) +nt = get_summable_transactions(transactions) +db = get_daily_balance(nt) +puts db.class +<DateEnding Balance + #{db.map {|rec| render_balance_record(rec)}.join "\n"} + +BALANCES +end + + +def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) + beginning_balance.to_f - tot_withdrawals.to_f + tot_deposits.to_f +end + +total_deposits = get_totals(deposits) +total_withdrawals = get_totals(withdrawals) + +File.open('assignment02-out.htm', "w") do |outfile| + outfile.puts "" + outfile.puts "

Summary

" + outfile.puts "

Total withdrawals: $#{total_withdrawals}

" + outfile.puts "

Total deposits: $#{total_deposits}

" + outfile.puts "

Final Balance: $#{get_ending_balance(0, total_withdrawals, total_deposits)}

" + outfile.puts render_daily_balances(transactions) + outfile.puts render_body("Withdrawals", withdrawals) + outfile.puts render_body("Deposits", deposits) + outfile.puts "" +end From 823f1b5d0b7f20675c6ed690ccd8b678b8eeab88 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Tue, 27 Jan 2015 22:06:21 -0800 Subject: [PATCH 12/32] progress on assignment 3 problem 3 --- assignments/assignment03.rb | 42 +++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index 08a02b8..a97fe4e 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -99,27 +99,47 @@ class Transaction def initialize(date, payee, amount, type) @date = date @payee = payee - @amount = amount + @amount = amount.to_f @type = type end - def height; @height; end + def date; @date; end def payee; @payee; end def amount; @amount; end def type; @type; end + def amountval + if @type == 'withdrawal' + @amount * -0.1 + else + @amount + end + end end -def create_transaction_hash( date, payee, amount, type ) - { date: date, payee: payee, amount: amount, type: type } -end - -records = [] -transactions = File.open('assignment02-input.csv', "r") do |infile| - records = infile.readlines.map do |inline| - f = inline.chomp.split(",") - create_transaction_hash(f[0], f[1], f[2], f[3]) +class BankAccount + attr_reader :ledger + def initialize() + @ledger = [] + end + def add_transaction(transaction) + @ledger.push transaction + end + def import_transactions(filename) + transactions = File.open(filename, "r") do |infile| + records = infile.readlines.map do |inline| + f = inline.chomp.split(",") + t = Transaction.new(f[0], f[1], f[2], f[3]) + self.add_transaction(t) + end + end end end +ba = BankAccount.new() +ba.import_transactions('assignment02-input.csv') +ba.ledger.each {|t| puts t.type} +a = ba.ledger.each {|t| puts t.amountval} + + withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } withdrawals.sort_by! {|x| x[:date]} deposits = transactions.select {|h| h[:type] == 'deposit' } From 10670f23c05b8949a573c5304a2ab383d5779c60 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 28 Jan 2015 21:05:36 -0800 Subject: [PATCH 13/32] more assignment 3 progress --- assignments/assignment03.rb | 49 ++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index a97fe4e..eb35644 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -106,29 +106,45 @@ def date; @date; end def payee; @payee; end def amount; @amount; end def type; @type; end - def amountval - if @type == 'withdrawal' - @amount * -0.1 - else - @amount - end + def puts + if @type == "Deposit" + print "date: #{date}, payee: #{payee}, amount: #{amount} \n" + elsif @type == "Withdrawal" + print "date: #{date}, payee: #{payee}, amount: #{amount} \n" + else print "\n" + end end end -class BankAccount - attr_reader :ledger +class DepositTransaction < Transaction + def initialize(date, payee, amount) + super date, payee, amount.to_f, "Deposit" + end +end + +class WithdrawalTransaction < Transaction + def initialize(date, payee, amount) + super date, payee, amount.to_f * -1.0, "Withdrawal" + end +end + +class BankAccount < Array def initialize() - @ledger = [] end def add_transaction(transaction) - @ledger.push transaction + self.push transaction end def import_transactions(filename) transactions = File.open(filename, "r") do |infile| records = infile.readlines.map do |inline| f = inline.chomp.split(",") - t = Transaction.new(f[0], f[1], f[2], f[3]) - self.add_transaction(t) + if f[3] == "deposit" then + t = DepositTransaction.new(f[0], f[1], f[2]) + self.add_transaction t + elsif f[3] == "withdrawal" then + t = WithdrawalTransaction.new(f[0], f[1], f[2]) + self.add_transaction t + end end end end @@ -136,8 +152,13 @@ def import_transactions(filename) ba = BankAccount.new() ba.import_transactions('assignment02-input.csv') -ba.ledger.each {|t| puts t.type} -a = ba.ledger.each {|t| puts t.amountval} +ba.each {|t| t.puts} +puts ba.each {|t| puts t.amountval} + +a = [] +a[0]= WithdrawalTransaction.new('15/1/2008', 'me', 100) +a[1] = DepositTransaction.new('15/1/2008', 'me', 100) +puts a withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } From a1f2353d40af5cbf5b63569601e37441190b67ba Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 28 Jan 2015 21:18:14 -0800 Subject: [PATCH 14/32] a coupld of minor edits to assignemnt 3 --- assignments/assignment03.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index eb35644..a431c1e 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -155,12 +155,6 @@ def import_transactions(filename) ba.each {|t| t.puts} puts ba.each {|t| puts t.amountval} -a = [] -a[0]= WithdrawalTransaction.new('15/1/2008', 'me', 100) -a[1] = DepositTransaction.new('15/1/2008', 'me', 100) -puts a - - withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } withdrawals.sort_by! {|x| x[:date]} deposits = transactions.select {|h| h[:type] == 'deposit' } @@ -177,7 +171,7 @@ def mean(ary) end def get_daily_balance(transactions) - days = transactions.map {|t| t[:date]}.uniq + days = transactions.map {|t| t.date}.uniq days.sort_by! {|i| i.split("/")[1].to_i} transactions_by_day = days.reduce({}) do |acc, day| acc[day] = transactions.select { |t| t[:date] == day } From 34e9923b2522630185bd972d117ef606c55d98c3 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Thu, 29 Jan 2015 16:24:56 -0800 Subject: [PATCH 15/32] Chris Peak's assignment 3 submission --- assignments/assignment02-out.htm | 218 +++++++++++++++---------------- assignments/assignment03.rb | 52 +++----- 2 files changed, 126 insertions(+), 144 deletions(-) diff --git a/assignments/assignment02-out.htm b/assignments/assignment02-out.htm index 74f6a0b..93c6b4d 100644 --- a/assignments/assignment02-out.htm +++ b/assignments/assignment02-out.htm @@ -2,7 +2,7 @@

Summary

Total withdrawals: $2178.77

Total deposits: $3620.0

-

Final Balance: $1441.23

+

Final Balance: $5798.77

@@ -144,6 +144,72 @@

Withdrawals

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -170,20 +236,20 @@

Withdrawals

- - + + - - + + - - + + @@ -194,8 +260,8 @@

Withdrawals

- - + + @@ -212,8 +278,8 @@

Withdrawals

- - + + @@ -224,8 +290,8 @@

Withdrawals

- - + + @@ -236,26 +302,26 @@

Withdrawals

- - + + - - + + - - + + - - + + @@ -266,8 +332,8 @@

Withdrawals

- - + + @@ -278,8 +344,8 @@

Withdrawals

- - + + @@ -336,12 +402,6 @@

Withdrawals

- - - - - - @@ -349,69 +409,9 @@

Withdrawals

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + +
DateEnding Balance
12/1/2014 $-21.92
12/4/2014Starbucks$-5.3
12/4/2014Subway$-7.8
12/5/2014ATM$-60.0
12/5/2014Nike Town$-85.0
12/6/2014Starbucks$-5.3
12/7/2014Safeway$-23.89
12/7/2014Arco$-42.0
12/7/2014check #5132$-75.0
12/8/2014Starbucks$-5.3
12/9/2014Bartell's$-7.83
12/9/2014check #5128$-575.0
12/11/2014 Home Depot
12/13/2014QFC$-13.25check #5134$-45.68
12/13/2014check #5134$-45.68QFC$-13.25
12/14/2014check #5133$-17.5Chipotle$-10.42
12/14/2014Chipotle$-10.42check #5133$-17.5
12/16/20147-Eleven$-5.26Arco$-48.01
12/16/2014Arco$-48.017-Eleven$-5.26
12/18/2014Starbucks$-5.3Bartell's$-14.82
12/18/2014Starbucks$-5.3check #5129$-125.0
12/18/2014check #5129$-125.0Starbucks$-5.3
12/18/2014Bartell's$-14.82Starbucks$-5.3
12/22/2014Safeway$-28.45Shell$-52.0
12/22/2014Shell$-52.0Safeway$-28.45
$-35.17
12/31/2014check #5131$-20.0
12/31/2014 Starbucks
12/4/2014Starbucks$-5.3
12/4/2014Subway$-7.8
12/5/2014Nike Town$-85.0
12/5/2014ATM$-60.0
12/6/2014Starbucks$-5.3
12/7/2014Arco$-42.0
12/7/2014Safeway$-23.89
12/7/2014check #5132$-75.0
12/8/2014Starbucks$-5.3
12/9/2014Bartell's$-7.83
12/9/2014check #5128$-575.012/31/2014check #5131$-20.0
@@ -425,39 +425,39 @@

Deposits

- 12/15/2014 - ATM - $50.0 - - - - 12/17/2014 + 12/4/2014 Univ Washington $1500.0 - 12/21/2014 + 12/6/2014 ATM - $20.0 + $50.0 - 12/4/2014 - Univ Washington - $1500.0 + 12/6/2014 + ATM + $500.0 - 12/6/2014 + 12/15/2014 ATM $50.0 - 12/6/2014 + 12/17/2014 + Univ Washington + $1500.0 + + + + 12/21/2014 ATM - $500.0 + $20.0 diff --git a/assignments/assignment03.rb b/assignments/assignment03.rb index a431c1e..6b3f583 100644 --- a/assignments/assignment03.rb +++ b/assignments/assignment03.rb @@ -19,11 +19,7 @@ def palindrome? s1 = self.gsub(' ', '').downcase s1 = s1.gsub(',', '').downcase s2 = s1.reverse - if s2 == s1 - true - else - false - end + s2 == s1 end end @@ -153,15 +149,14 @@ def import_transactions(filename) ba = BankAccount.new() ba.import_transactions('assignment02-input.csv') ba.each {|t| t.puts} -puts ba.each {|t| puts t.amountval} -withdrawals = transactions.select {|h| h[:type] == 'withdrawal' } -withdrawals.sort_by! {|x| x[:date]} -deposits = transactions.select {|h| h[:type] == 'deposit' } -deposits.sort_by! {|x| x[:date]} +withdrawals = ba.select {|h| h.type == 'Withdrawal' } +withdrawals.sort_by! {|x| x.date.split('/')[1].to_i} +deposits = ba.select {|h| h.type == 'Deposit' } +deposits.sort_by! {|x| x.date.split('/')[1].to_i} def get_totals(records) - amounts = records.map {|r| r[:amount].to_f } + amounts = records.map {|r| r.amount } amounts.reduce(0) { |val, acc| val + acc }.round(2) end @@ -174,11 +169,11 @@ def get_daily_balance(transactions) days = transactions.map {|t| t.date}.uniq days.sort_by! {|i| i.split("/")[1].to_i} transactions_by_day = days.reduce({}) do |acc, day| - acc[day] = transactions.select { |t| t[:date] == day } + acc[day] = transactions.select { |t| t.date == day } acc end sum_transactions_by_day = days.reduce({}) do |acc, day| - amounts = transactions_by_day[day].map {|record| record[:amount]} + amounts = transactions_by_day[day].map {|record| record.amount} sum = amounts.reduce {|acc, amount| acc.to_f + amount.to_f } acc[day] = sum.to_f acc @@ -194,21 +189,9 @@ def get_daily_balance(transactions) balances_by_day end -#balances = get_daily_balance(nt) +#balances = get_daily_balance(ba) + -def get_summable_transactions(transactions) - trans_vals = transactions.reject {|t| t[:date] == "date"} - out_trans = [] - out_trans = trans_vals.map do |t| - if t[:type] == "withdrawal" then - t[:amount] = t[:amount].to_f * -1.0 - else - t[:amount] = t[:amount].to_f - end - t - end - # puts out_trans -end #nt = get_summable_transactions(transactions) puts nt @@ -218,9 +201,9 @@ def get_summable_transactions(transactions) def render_record(rec) < - #{rec[:date]} - #{rec[:payee]} - $#{rec[:amount]} + #{rec.date.to_s} + #{rec.payee} + $#{rec.amount} RECORD end @@ -265,7 +248,7 @@ def render_balance_record(record) def render_daily_balances(transactions) -nt = get_summable_transactions(transactions) +nt = transactions db = get_daily_balance(nt) puts db.class <" @@ -289,7 +271,7 @@ def get_ending_balance(beginning_balance, tot_withdrawals, tot_deposits) outfile.puts "

Total withdrawals: $#{total_withdrawals}

" outfile.puts "

Total deposits: $#{total_deposits}

" outfile.puts "

Final Balance: $#{get_ending_balance(0, total_withdrawals, total_deposits)}

" - outfile.puts render_daily_balances(transactions) + outfile.puts render_daily_balances(ba) outfile.puts render_body("Withdrawals", withdrawals) outfile.puts render_body("Deposits", deposits) outfile.puts "" From 7e293544878f4dbe82098f4ecc98c95fb0ec0c28 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Tue, 3 Feb 2015 20:40:47 -0800 Subject: [PATCH 16/32] assignemnt 4 problems 1 and 2 complete --- assignments/assignment04.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/assignments/assignment04.rb b/assignments/assignment04.rb index d2443a2..d7467c7 100644 --- a/assignments/assignment04.rb +++ b/assignments/assignment04.rb @@ -13,6 +13,13 @@ def fib(n) # your implementation here + if n==0 then + 1 + elsif n==1 then + 1 + else + fib(n-2) + fib(n-1) + end end # expected behavior: @@ -40,22 +47,22 @@ def fib(n) class Queue def initialize - # your implementation here + @items = [] end def enqueue(item) - # your implementation here + @items< Date: Tue, 3 Feb 2015 21:35:00 -0800 Subject: [PATCH 17/32] started assignment 4 problem 3 --- assignments/assignment04.rb | 46 +++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/assignments/assignment04.rb b/assignments/assignment04.rb index d7467c7..b091807 100644 --- a/assignments/assignment04.rb +++ b/assignments/assignment04.rb @@ -95,25 +95,57 @@ def length ll.each {|x| puts x} #=> prints out "first", "third" class LinkedList + class Node + attr :item, :link + def initialize(item, link) + @item = item + @link = link + end + end def initialize - # your implementation here + @nodes = nil end def empty? - # your implementation here + @nodes.nil? end def length - # your implementation here + count = 0 + node = @nodes + while node + count += 1 + node = node.link + end + count end def <<(item) - # your implementation here + @nodes = Node.new item, @nodes + self end def first - # your implementation here + node = @nodes + lastnode = @nodes + while node + lastnode = node + node = node.link + end + lastnode.item end def last - # your implementation here + @nodes.item end def each(&block) - # your implementation here + node = @nodes + while node + yield + node = node.link + end + end + def delete(item) + end end + +ll = LinkedList.new +ll << "first" +ll << "second" +ll.each {|x| puts x } From a50fa9778017fafec21db155a8e72717124d4653 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 4 Feb 2015 16:59:57 -0800 Subject: [PATCH 18/32] some progress on assignemnt 4 problem 3 --- assignments/assignment04.rb | 46 ++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/assignments/assignment04.rb b/assignments/assignment04.rb index b091807..b39dc06 100644 --- a/assignments/assignment04.rb +++ b/assignments/assignment04.rb @@ -95,19 +95,26 @@ def length ll.each {|x| puts x} #=> prints out "first", "third" class LinkedList + class Node attr :item, :link def initialize(item, link) @item = item @link = link end + def item=(something) + @item == something + end end + def initialize @nodes = nil end + def empty? @nodes.nil? end + def length count = 0 node = @nodes @@ -117,10 +124,11 @@ def length end count end + def <<(item) @nodes = Node.new item, @nodes - self end + def first node = @nodes lastnode = @nodes @@ -140,12 +148,44 @@ def each(&block) node = node.link end end - def delete(item) - + def print_nodes + node = @nodes + while node + puts node.item + node = node.link + end + nil + end + def delete(myitem) + newnodes = LinkedList.new + node = @nodes + stop = false + while node + if node.item == myitem then + node = node.link + puts newnodes + newnodes = node + puts newnodes.item + else + puts newnodes + newnodes = node + puts newnodes.item + node = node.link + end + end + @nodes = newnodes + myitem end end ll = LinkedList.new ll << "first" ll << "second" +ll << "third" +ll << "fourth" +ll << "fifth" +ll.length +ll.delete "third" +ll.length + ll.each {|x| puts x } From fab8a139ac1e0b518f97a8e2fc211dfed7f56141 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Wed, 4 Feb 2015 19:44:06 -0800 Subject: [PATCH 19/32] Assignment 4 submission -- Chris Peak --- assignments/assignment04.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/assignments/assignment04.rb b/assignments/assignment04.rb index b39dc06..422af7e 100644 --- a/assignments/assignment04.rb +++ b/assignments/assignment04.rb @@ -95,6 +95,7 @@ def length ll.each {|x| puts x} #=> prints out "first", "third" class LinkedList + # include Enumerable class Node attr :item, :link @@ -141,13 +142,15 @@ def first def last @nodes.item end + def each(&block) node = @nodes while node - yield + yield node.item node = node.link end end + def print_nodes node = @nodes while node @@ -156,20 +159,15 @@ def print_nodes end nil end + def delete(myitem) - newnodes = LinkedList.new + newnodes = nil node = @nodes - stop = false while node if node.item == myitem then node = node.link - puts newnodes - newnodes = node - puts newnodes.item else - puts newnodes - newnodes = node - puts newnodes.item + newnodes = Node.new node.item, newnodes node = node.link end end From e74ceda05133202cfe324fa14dfda0cf48ed314e Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Thu, 5 Feb 2015 16:54:26 -0800 Subject: [PATCH 20/32] minor tweak to LinkedList class to improve the list order --- assignments/assignment04.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/assignments/assignment04.rb b/assignments/assignment04.rb index 422af7e..a165959 100644 --- a/assignments/assignment04.rb +++ b/assignments/assignment04.rb @@ -150,14 +150,15 @@ def each(&block) node = node.link end end - - def print_nodes - node = @nodes - while node - puts node.item - node = node.link + + def reverse! + in_node = @nodes + out_nodes = nil + while in_node + out_nodes = Node.new in_node.item, out_nodes + in_node = in_node.link end - nil + @nodes = out_nodes end def delete(myitem) @@ -172,6 +173,7 @@ def delete(myitem) end end @nodes = newnodes + self.reverse! myitem end end From 3b95caea04491696b516ca4217e7c19e9814b606 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 8 Feb 2015 20:50:31 -0800 Subject: [PATCH 21/32] begin assignment 5 --- assignments/assignment05.rb | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/assignments/assignment05.rb b/assignments/assignment05.rb index 9e96ea4..b0939dc 100644 --- a/assignments/assignment05.rb +++ b/assignments/assignment05.rb @@ -5,15 +5,34 @@ # ======================================================================================== # Problem 1 - implement prop_reader, write MiniTest unit tests +class Class + def prop_reader(prop_name) + define_method prop_name do + instance_variable_get "@#{prop_name}" + end + #define_method "#{prop_name}=" {|val| instance_variable_set "@#{prop_name}", val } + end +end + # expected results: class PropReader prop_reader :flavor - def initialize(flavor) - @flavor = flavor +# def initialize(flavor) +# @flavor = flavor +# end +end + + + +require 'minitest/autorun' + +class TestPropReader < Minitest::Test + def test_check + assert true end end -obj = PropReader.new "spicy" -obj.respond_to? :flavor #=> true -obj.respond_to? :"flavor=" #=> false -obj.flavor #=> "spicy" +#obj = PropReader.new "spicy" +#obj.respond_to? :flavor #=> true +#obj.respond_to? :"flavor=" #=> false +#obj.flavor #=> "spicy" From 03e5395b28b949f957f1793375085d2626f01631 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 9 Feb 2015 21:22:34 -0800 Subject: [PATCH 22/32] completed assignment 5 --- assignments/assignment05.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/assignments/assignment05.rb b/assignments/assignment05.rb index b0939dc..3c9ccac 100644 --- a/assignments/assignment05.rb +++ b/assignments/assignment05.rb @@ -10,16 +10,15 @@ def prop_reader(prop_name) define_method prop_name do instance_variable_get "@#{prop_name}" end - #define_method "#{prop_name}=" {|val| instance_variable_set "@#{prop_name}", val } end end # expected results: class PropReader - prop_reader :flavor -# def initialize(flavor) -# @flavor = flavor -# end + prop_reader(:flavor) + def initialize(flavor) + @flavor = flavor + end end @@ -28,11 +27,14 @@ class PropReader class TestPropReader < Minitest::Test def test_check - assert true + pr = PropReader.new("aparameter") + assert_equal "aparameter", pr.flavor + assert pr.respond_to? :flavor + refute pr.respond_to? :"flavor=" end end -#obj = PropReader.new "spicy" +#obj = PropReader.new("spicy" #obj.respond_to? :flavor #=> true #obj.respond_to? :"flavor=" #=> false #obj.flavor #=> "spicy" From bd3d3da919fada238d2e97894a38a72c1ef6cc07 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 16 Feb 2015 09:49:08 -0800 Subject: [PATCH 23/32] assignment 6 complete but wants refactoring --- assignments/asgmt6.rb | 86 ++++++++++++ assignments/assignment05.rb | 4 +- assignments/assignment06.rb | 273 +++++++++++++++++++++++++----------- 3 files changed, 279 insertions(+), 84 deletions(-) create mode 100644 assignments/asgmt6.rb diff --git a/assignments/asgmt6.rb b/assignments/asgmt6.rb new file mode 100644 index 0000000..e3258e5 --- /dev/null +++ b/assignments/asgmt6.rb @@ -0,0 +1,86 @@ + require 'minitest/autorun' + + class PriorityQueue + + class SubQueue + attr :rank, :items + def initialize(rank) + @rank = rank + @items = [] + end + def enqueue(item) + @items< 0 + @high_sq.dequeue + elsif @medium_sq.length > 0 + @medium_sq.dequeue + else + @low_sq.dequeue + end + end + def empty? + ary = @high_sq.items + @medium_sq.items + @low_sq.items + ary.empty? + end + def peek + if @high_sq.length > 0 + @high_sq.peek + elsif @medium_sq.length > 0 + @medium_sq.peek + else + @low_sq.peek + end + end + def length + ary = @high_sq.items + @medium_sq.items + @low_sq.items + ary.length + end + end #PriorityQueue + + + class TestPriorityQueue < MiniTest::Test + def test_one + pq = PriorityQueue.new + assert pq.empty? + pq.enqueue "first" + refute pq.empty? + + pq.enqueue "top", :high + pq.enqueue "last", :low + pq.enqueue "second" + pq.enqueue "another top", :high + assert_equal 5, pq.length + + assert_equal "top", pq.dequeue + assert_equal "another top", pq.dequeue + assert_equal "first", pq.dequeue + assert_equal "second", pq.dequeue + assert_equal "last", pq.dequeue + end + end #class diff --git a/assignments/assignment05.rb b/assignments/assignment05.rb index 3c9ccac..df22cbc 100644 --- a/assignments/assignment05.rb +++ b/assignments/assignment05.rb @@ -7,9 +7,7 @@ class Class def prop_reader(prop_name) - define_method prop_name do - instance_variable_get "@#{prop_name}" - end + define_method (prop_name) { instance_variable_get "@#{prop_name}" } end end diff --git a/assignments/assignment06.rb b/assignments/assignment06.rb index f344a17..edec635 100644 --- a/assignments/assignment06.rb +++ b/assignments/assignment06.rb @@ -6,47 +6,94 @@ # Problem 1 - PriorityQueue # implement a PriorityQueue, validate using MiniTest unit tests +module Assignment6 + require 'minitest/autorun' + + class PriorityQueue -class PriorityQueue - def initialize - # your implementation here - end - def enqueue(item, priority=:medium) - # your implementation here - end - def dequeue - # your implementation here - end - def empty? - # your implementation here - end - def peek - # your implementation here - end - def length - # your implementation here - end -end - -# expected results: -pq = PriorityQueue.new -pq.empty? #=> true - -pq.enqueue "first" -pq.empty? #=> false - -pq.enqueue "top", :high -pq.enqueue "last", :low -pq.enqueue "second" -pq.enqueue "another top", :high - -pq.length #=> 5 + class SubQueue + attr :rank, :items + def initialize(rank) + @rank = rank + @items = [] + end + def enqueue(item) + @items< 0 + @high_sq.dequeue + elsif @medium_sq.length > 0 + @medium_sq.dequeue + else + @low_sq.dequeue + end + end + def empty? + ary = @high_sq.items + @medium_sq.items + @low_sq.items + ary.empty? + end + def peek + if @high_sq.length > 0 + @high_sq.peek + elsif @medium_sq.length > 0 + @medium_sq.peek + else + @low_sq.peek + end + end + def length + ary = @high_sq.items + @medium_sq.items + @low_sq.items + ary.length + end + end #PriorityQueue -pq.dequeue #=> "top" -pq.dequeue #=> "another top" -pq.dequeue #=> "first" -pq.dequeue #=> "second" -pq.dequeue #=> "last" + + class TestPriorityQueue < MiniTest::Test + def test_one + pq = PriorityQueue.new + assert pq.empty? + pq.enqueue "first" + refute pq.empty? + + pq.enqueue "top", :high + pq.enqueue "last", :low + pq.enqueue "second" + pq.enqueue "another top", :high + assert_equal 5, pq.length + + assert_equal "top", pq.dequeue + assert_equal "another top", pq.dequeue + assert_equal "first", pq.dequeue + assert_equal "second", pq.dequeue + assert_equal "last", pq.dequeue + end + end #class + # ======================================================================================== @@ -54,54 +101,118 @@ def length # render a Recipe object to Recipe DSL -class Recipe - attr_accessor :steps, :ingredients, :name, :category, :prep_time, :rating - def initialize(name) - @name = name + class Recipe + attr_accessor :steps, :ingredients, :name, :category, :prep_time, :rating + def initialize(name) + @name = name + end + + def render_dsl + dsl = RecipeExport.new(self) + puts dsl.create_dsl + end end - def render_dsl - # your code here - end -end -class RecipeBuilder - def recipe(name, &block) - @recipe = Recipe.new(name) - self.instance_eval &block - @recipe - end - def category(value) - @recipe.category = value - end + class RecipeExport - def prep_time(value) - @recipe.prep_time = value - end - - def rating(value) - @recipe.rating = value - end + def initialize(recipe) + @ingredients = recipe.ingredients + @steps = recipe.steps + @name = recipe.name + end - def ingredients(&block) - @recipe.ingredients = [] - @items = @recipe.ingredients - self.instance_eval &block + def create_dsl + <<-DSL.gsub /^\s+/, "" + recipe "#{@name}" do + #{ingredients} + #{steps} + end + DSL + end + + def ingredients + <<-INGREDIENTS + ingredients do + #{@ingredients.map {|i| ingredient(i)}.join "" } + end + INGREDIENTS + end + + def ingredient(ingredient) + <<-ING + x "#{ingredient}" + ING + end + + def step(step) + <<-STEP + x "#{step}" + STEP + end + + def steps + <<-STEPS + steps do + #{@steps.map {|s| step(s)}.join "" } + end + STEPS + end end - def steps(&block) - @recipe.steps = [] - @items = @recipe.steps - self.instance_eval &block - end - - def x(item) - @items << item - end -end + class RecipeBuilder + def recipe(name, &block) + @recipe = Recipe.new(name) + self.instance_eval &block + @recipe + end + + def category(value) + @recipe.category = value + end + + def prep_time(value) + @recipe.prep_time = value + end + + def rating(value) + @recipe.rating = value + end + + def ingredients(&block) + @recipe.ingredients = [] + @items = @recipe.ingredients + self.instance_eval &block + end + + def steps(&block) + @recipe.steps = [] + @items = @recipe.steps + self.instance_eval &block + end + + def x(item) + @items << item + end + end #RecipeBuilder -def recipe(name, &block) - rb = RecipeBuilder.new - rb.recipe(name, &block) -end + def recipe(name, &block) + rb = RecipeBuilder.new + rb.recipe(name, &block) + end + + eggs = recipe "Scrambled Eggs" do + ingredients do + x "eggs" + x "salt" + x "pepper" + x "butter" + end + steps do + x "crack eggs" + x "cook eggs" + end + end + +end #module From 1d9bb8bcfef2be7ee8d7031eb9c8b6b75e294890 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 16 Feb 2015 11:43:13 -0800 Subject: [PATCH 24/32] assignment 6 problem 2 string formatting moderately acceptable --- assignments/assignment06.rb | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/assignments/assignment06.rb b/assignments/assignment06.rb index edec635..cff17f0 100644 --- a/assignments/assignment06.rb +++ b/assignments/assignment06.rb @@ -124,38 +124,38 @@ def initialize(recipe) end def create_dsl - <<-DSL.gsub /^\s+/, "" + <<-DSL.gsub /^ {4}/, "" recipe "#{@name}" do - #{ingredients} - #{steps} + #{ingredients} + #{steps} end DSL end - + def ingredients - <<-INGREDIENTS + <<-INGREDIENTS.gsub /^ {4}/, "" ingredients do - #{@ingredients.map {|i| ingredient(i)}.join "" } + #{@ingredients.map {|i| ingredient(i)}.join "" } end INGREDIENTS end def ingredient(ingredient) - <<-ING - x "#{ingredient}" + <<-ING.gsub /\A {8}/, " " + x "#{ingredient}" ING end def step(step) - <<-STEP - x "#{step}" + <<-STEP.gsub /^ {4}/, "" + x "#{step}" STEP end def steps - <<-STEPS + <<-STEPS.gsub /^ {4}/, "" steps do - #{@steps.map {|s| step(s)}.join "" } + #{@steps.map {|s| step(s)}.join "" } end STEPS end From adb7217d8561c73cba1668eafa89de70a152e134 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 16 Feb 2015 12:03:51 -0800 Subject: [PATCH 25/32] heredoc rendering is more acceptable now. --- assignments/assignment06.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/assignments/assignment06.rb b/assignments/assignment06.rb index cff17f0..cc21c13 100644 --- a/assignments/assignment06.rb +++ b/assignments/assignment06.rb @@ -124,39 +124,39 @@ def initialize(recipe) end def create_dsl - <<-DSL.gsub /^ {4}/, "" + # is there a prettier way of dealing with indentation in heredocs + # that doesn't involve the weird tab alignments in the Ruby code, as below? + # I tried playing with gsub but it got quite confusing with all the embedded heredocs. + <<-DSL.gsub /\A {4}/, "" recipe "#{@name}" do - #{ingredients} - #{steps} - end +#{ingredients} +#{steps}end DSL end def ingredients - <<-INGREDIENTS.gsub /^ {4}/, "" - ingredients do - #{@ingredients.map {|i| ingredient(i)}.join "" } - end + <<-INGREDIENTS + ingredients do +#{@ingredients.map {|i| ingredient(i)}.join "" } end INGREDIENTS end def ingredient(ingredient) - <<-ING.gsub /\A {8}/, " " - x "#{ingredient}" + <<-ING + x "#{ingredient}" ING end def step(step) - <<-STEP.gsub /^ {4}/, "" - x "#{step}" + <<-STEP + x "#{step}" STEP end def steps - <<-STEPS.gsub /^ {4}/, "" - steps do - #{@steps.map {|s| step(s)}.join "" } - end + <<-STEPS + steps do +#{@steps.map {|s| step(s)}.join "" } end STEPS end end From 95e2c4d1909c6b08dcb88de18cb5f1a6aae7b8e5 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 22 Feb 2015 19:35:12 -0800 Subject: [PATCH 26/32] assignment 7 --- assignments/assignment07.rb | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/assignments/assignment07.rb b/assignments/assignment07.rb index 5d86f95..67bfba0 100644 --- a/assignments/assignment07.rb +++ b/assignments/assignment07.rb @@ -11,22 +11,63 @@ module Assignment07 module Observable def add_observer(obj) - # your implementation here + defined?(@observers)? @observers< Date: Sun, 1 Mar 2015 17:15:21 -0800 Subject: [PATCH 27/32] asgmt 8 problem 1 version 1 --- assignments/asgmt8.rb | 65 ++++++++++++++++++++++++++++++++++++ assignments/assignment08.rb | 66 +++++++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 assignments/asgmt8.rb diff --git a/assignments/asgmt8.rb b/assignments/asgmt8.rb new file mode 100644 index 0000000..190f83a --- /dev/null +++ b/assignments/asgmt8.rb @@ -0,0 +1,65 @@ + + +class RomanNumeral + attr :roman_ones, :value, :arabic_digits, :arabic_ones_digit, :arabic_tens_digit, :arabic_100s_digit + def initialize(i) + @value = i + @arabic_ones_digit = @value.to_s[-1].to_i + @arabic_tens_digit = @value.to_s[-2].to_i + @arabic_100s_digit = @value.to_s[-3].to_i + @arabic_digits = {} + [1,2,3].each {|n| @arabic_digits[n] = @value.to_s[n *-1].to_i} + end + + def to_s + roman_chars = [] + roman_chars << %w{I IV V IX} + roman_chars << %w{X XL L XC} + roman_chars << %w{C CD D CM} + r_numeral = "" + r_dig = "" + @arabic_digits.keys.each do |k| + if @arabic_digits[k] <= 3 then + r_dig = roman_chars[k-1][0] * @arabic_digits[k] + elsif @arabic_digits[k] == 4 then + r_dig = roman_chars[k-1][1] + elsif @arabic_digits[k] <= 8 then + r_dig = roman_chars[k-1][2] + roman_chars[k-1][0] * (@arabic_digits[k] - 5) + else r_dig = roman_chars[k-1][3] + end + r_numeral.insert(0, r_dig) + end + r_numeral + end + + def to_i + end + + # bonus: create from Roman Numeral + def self.from_string + # your implementation here + # returns a new instance + end +end + + +#require 'Assignment08' + +require 'minitest/autorun' + +class RomanNumeralTest < MiniTest::Test + def test_one + assert_equal 'I', RomanNumeral.new(1).to_s + assert_equal 'II', RomanNumeral.new(2).to_s + assert_equal 'III', RomanNumeral.new(3).to_s + assert_equal 'IV', RomanNumeral.new(4).to_s + assert_equal 'V', RomanNumeral.new(5).to_s + assert_equal 'VI', RomanNumeral.new(6).to_s + assert_equal 'VII', RomanNumeral.new(7).to_s + assert_equal 'IX', RomanNumeral.new(9).to_s + assert_equal 'X', RomanNumeral.new(10).to_s + assert_equal 'XIX', RomanNumeral.new(19).to_s + assert_equal 'XXXII', RomanNumeral.new(32).to_s + assert_equal 'LI', RomanNumeral.new(51).to_s + end +end diff --git a/assignments/assignment08.rb b/assignments/assignment08.rb index 3d46269..97cbabb 100644 --- a/assignments/assignment08.rb +++ b/assignments/assignment08.rb @@ -10,16 +10,55 @@ module Assignment08 class RomanNumeral + attr :roman_ones, :value, :arabic_ones_digit, :arabic_tens_digit, :arabic_100s_digit def initialize(i) - # your implementation here + @value = i + @arabic_ones_digit = @value.to_s[-1].to_i + @arabic_tens_digit = @value.to_s[-2].to_i + @arabic_100s_digit = @value.to_s[-3].to_i + end + + def ones_digit + if @arabic_ones_digit <= 3 then + 'I' * @arabic_ones_digit + elsif @arabic_ones_digit == 4 then + 'IV' + elsif @arabic_ones_digit <= 8 then + 'V' + 'I' * (@arabic_ones_digit - 5) + else 'IX' + end + end + + def tens_digit + if @arabic_tens_digit <= 3 then + 'X' * @arabic_tens_digit + elsif @arabic_tens_digit == 4 + 'XL' + elsif @arabic_tens_digit <= 8 + 'L' + 'X' * (arabic_tens_digit - 5) + else 'XC' + end + end + + def hundreds_digit + if @arabic_100s_digit <= 3 then + 'C' * @arabic_100s_digit + elsif @arabic_100s_digit == 4 + 'CD' + elsif @arabic_100s_digit <= 8 + 'D' + 'M' * (@arabic_100s_digit - 5) + else 'CM' + end end def to_s - # your implementation here + @roman_ones = ones_digit + @roman_tens = tens_digit + @roman_100s = hundreds_digit + @roman_100s + @roman_tens + @roman_ones end def to_i - # your implementation here end # bonus: create from Roman Numeral @@ -30,6 +69,27 @@ def self.from_string end end +require Assignment08 + +require 'minitest/autorun' + +class RomanNumeralTest < MiniTest::Test + def test_one + assert_equal 'I', RomanNumeral.new(1).to_s + assert_equal 'II', RomanNumeral.new(2).to_s + assert_equal 'III', RomanNumeral.new(3).to_s + assert_equal 'IV', RomanNumeral.new(4).to_s + assert_equal 'V', RomanNumeral.new(5).to_s + assert_equal 'VI', RomanNumeral.new(6).to_s + assert_equal 'VII', RomanNumeral.new(7).to_s + assert_equal 'IV', RomanNumeral.new(9).to_s + assert_equal 'X', RomanNumeral.new(10).to_s + assert_equal 'XIX', RomanNumeral.new(19).to_s + assert_equal 'XXXII', RomanNumeral.new(32).to_s + assert_equal 'LI', RomanNumeral.new(51).to_s + end +end + # expected results: RomanNumeral.new(1).to_s # => 'I' RomanNumeral.new(2).to_s # => 'II' From d3ffaa472d4df9a3cc8ba356972744b85d2f88df Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 1 Mar 2015 17:32:03 -0800 Subject: [PATCH 28/32] asgmt 8 problem 1 version 2 --- assignments/asgmt8.rb | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/assignments/asgmt8.rb b/assignments/asgmt8.rb index 190f83a..589c4f1 100644 --- a/assignments/asgmt8.rb +++ b/assignments/asgmt8.rb @@ -7,8 +7,8 @@ def initialize(i) @arabic_ones_digit = @value.to_s[-1].to_i @arabic_tens_digit = @value.to_s[-2].to_i @arabic_100s_digit = @value.to_s[-3].to_i - @arabic_digits = {} - [1,2,3].each {|n| @arabic_digits[n] = @value.to_s[n *-1].to_i} + @arabic_digits = [] + [1,2,3].each {|n| @arabic_digits[n-1] = @value.to_s[n *-1].to_i} end def to_s @@ -18,14 +18,15 @@ def to_s roman_chars << %w{C CD D CM} r_numeral = "" r_dig = "" - @arabic_digits.keys.each do |k| - if @arabic_digits[k] <= 3 then - r_dig = roman_chars[k-1][0] * @arabic_digits[k] - elsif @arabic_digits[k] == 4 then - r_dig = roman_chars[k-1][1] - elsif @arabic_digits[k] <= 8 then - r_dig = roman_chars[k-1][2] + roman_chars[k-1][0] * (@arabic_digits[k] - 5) - else r_dig = roman_chars[k-1][3] + @arabic_digits.each_with_index do |arabic_digit, i| + arabic_digit ? true : arabic_digit = 0 + if arabic_digit <= 3 then + r_dig = roman_chars[i][0] * arabic_digit + elsif arabic_digit == 4 then + r_dig = roman_chars[i][1] + elsif arabic_digit <= 8 then + r_dig = roman_chars[i][2] + roman_chars[i][0] * (arabic_digit - 5) + else r_dig = roman_chars[i][3] end r_numeral.insert(0, r_dig) end @@ -41,6 +42,8 @@ def self.from_string # returns a new instance end end +rn = RomanNumeral.new(13) +rn.to_s #require 'Assignment08' From 2c328680db8efbb50cf8ec67d8cef2504a8a490b Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 1 Mar 2015 21:42:53 -0800 Subject: [PATCH 29/32] asgmt 8 problem 1 complete --- assignments/asgmt8.rb | 72 +++++++++++++++++-------------------- assignments/assignment08.rb | 62 +++++++++++--------------------- 2 files changed, 54 insertions(+), 80 deletions(-) diff --git a/assignments/asgmt8.rb b/assignments/asgmt8.rb index 589c4f1..b3c5c2c 100644 --- a/assignments/asgmt8.rb +++ b/assignments/asgmt8.rb @@ -1,49 +1,43 @@ -class RomanNumeral - attr :roman_ones, :value, :arabic_digits, :arabic_ones_digit, :arabic_tens_digit, :arabic_100s_digit - def initialize(i) - @value = i - @arabic_ones_digit = @value.to_s[-1].to_i - @arabic_tens_digit = @value.to_s[-2].to_i - @arabic_100s_digit = @value.to_s[-3].to_i - @arabic_digits = [] - [1,2,3].each {|n| @arabic_digits[n-1] = @value.to_s[n *-1].to_i} - end + class RomanNumeral + attr :value, :arabic_digits + def initialize(i) + @value = i + @arabic_digits = [] + [0,1,2].each {|n| @arabic_digits[n] = @value.to_s[(n+1) *-1].to_i} + end - def to_s - roman_chars = [] - roman_chars << %w{I IV V IX} - roman_chars << %w{X XL L XC} - roman_chars << %w{C CD D CM} - r_numeral = "" - r_dig = "" - @arabic_digits.each_with_index do |arabic_digit, i| - arabic_digit ? true : arabic_digit = 0 - if arabic_digit <= 3 then - r_dig = roman_chars[i][0] * arabic_digit - elsif arabic_digit == 4 then - r_dig = roman_chars[i][1] - elsif arabic_digit <= 8 then - r_dig = roman_chars[i][2] + roman_chars[i][0] * (arabic_digit - 5) - else r_dig = roman_chars[i][3] + def to_s + roman_chars = [] + roman_chars << %w{I IV V IX} + roman_chars << %w{X XL L XC} + roman_chars << %w{C CD D CM} + r_numeral = "" + r_digit = "" + @arabic_digits.each_with_index do |arabic_digit, i| + if arabic_digit <= 3 then + r_digit = roman_chars[i][0] * arabic_digit + elsif arabic_digit == 4 then + r_digit = roman_chars[i][1] + elsif arabic_digit <= 8 then + r_digit = roman_chars[i][2] + roman_chars[i][0] * (arabic_digit - 5) + else r_digit = roman_chars[i][3] + end + r_numeral.insert(0, r_digit) end - r_numeral.insert(0, r_dig) + r_numeral end - r_numeral - end - def to_i - end - - # bonus: create from Roman Numeral - def self.from_string - # your implementation here - # returns a new instance + def to_i + end + + # bonus: create from Roman Numeral + def self.from_string + # your implementation here + # returns a new instance + end end -end -rn = RomanNumeral.new(13) -rn.to_s #require 'Assignment08' diff --git a/assignments/assignment08.rb b/assignments/assignment08.rb index 97cbabb..37b2fbd 100644 --- a/assignments/assignment08.rb +++ b/assignments/assignment08.rb @@ -10,52 +10,32 @@ module Assignment08 class RomanNumeral - attr :roman_ones, :value, :arabic_ones_digit, :arabic_tens_digit, :arabic_100s_digit + attr :value, :arabic_digits def initialize(i) @value = i - @arabic_ones_digit = @value.to_s[-1].to_i - @arabic_tens_digit = @value.to_s[-2].to_i - @arabic_100s_digit = @value.to_s[-3].to_i - end - - def ones_digit - if @arabic_ones_digit <= 3 then - 'I' * @arabic_ones_digit - elsif @arabic_ones_digit == 4 then - 'IV' - elsif @arabic_ones_digit <= 8 then - 'V' + 'I' * (@arabic_ones_digit - 5) - else 'IX' - end - end - - def tens_digit - if @arabic_tens_digit <= 3 then - 'X' * @arabic_tens_digit - elsif @arabic_tens_digit == 4 - 'XL' - elsif @arabic_tens_digit <= 8 - 'L' + 'X' * (arabic_tens_digit - 5) - else 'XC' - end - end - - def hundreds_digit - if @arabic_100s_digit <= 3 then - 'C' * @arabic_100s_digit - elsif @arabic_100s_digit == 4 - 'CD' - elsif @arabic_100s_digit <= 8 - 'D' + 'M' * (@arabic_100s_digit - 5) - else 'CM' - end + @arabic_digits = [] + [0,1,2].each {|n| @arabic_digits[n] = @value.to_s[(n+1) *-1].to_i} end def to_s - @roman_ones = ones_digit - @roman_tens = tens_digit - @roman_100s = hundreds_digit - @roman_100s + @roman_tens + @roman_ones + roman_chars = [] + roman_chars << %w{I IV V IX} + roman_chars << %w{X XL L XC} + roman_chars << %w{C CD D CM} + r_numeral = "" + r_digit = "" + @arabic_digits.each_with_index do |arabic_digit, i| + if arabic_digit <= 3 then + r_digit = roman_chars[i][0] * arabic_digit + elsif arabic_digit == 4 then + r_digit = roman_chars[i][1] + elsif arabic_digit <= 8 then + r_digit = roman_chars[i][2] + roman_chars[i][0] * (arabic_digit - 5) + else r_digit = roman_chars[i][3] + end + r_numeral.insert(0, r_digit) + end + r_numeral end def to_i From 53d3fd6e7d818c8df965942cd52a24991e8d976f Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Sun, 1 Mar 2015 22:15:27 -0800 Subject: [PATCH 30/32] assignment 8 problem 2 initial attempt --- assignments/asgmt8.rb | 35 ++++++++++--------- assignments/assignment08.rb | 70 +++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 51 deletions(-) diff --git a/assignments/asgmt8.rb b/assignments/asgmt8.rb index b3c5c2c..dfd66dd 100644 --- a/assignments/asgmt8.rb +++ b/assignments/asgmt8.rb @@ -1,5 +1,5 @@ - +module Assignment08 class RomanNumeral attr :value, :arabic_digits def initialize(i) @@ -39,24 +39,25 @@ def self.from_string end end - -#require 'Assignment08' +#require Assignment08 require 'minitest/autorun' -class RomanNumeralTest < MiniTest::Test - def test_one - assert_equal 'I', RomanNumeral.new(1).to_s - assert_equal 'II', RomanNumeral.new(2).to_s - assert_equal 'III', RomanNumeral.new(3).to_s - assert_equal 'IV', RomanNumeral.new(4).to_s - assert_equal 'V', RomanNumeral.new(5).to_s - assert_equal 'VI', RomanNumeral.new(6).to_s - assert_equal 'VII', RomanNumeral.new(7).to_s - assert_equal 'IX', RomanNumeral.new(9).to_s - assert_equal 'X', RomanNumeral.new(10).to_s - assert_equal 'XIX', RomanNumeral.new(19).to_s - assert_equal 'XXXII', RomanNumeral.new(32).to_s - assert_equal 'LI', RomanNumeral.new(51).to_s + class RomanNumeralTest < MiniTest::Test + def test_one + assert_equal 'I', RomanNumeral.new(1).to_s + assert_equal 'II', RomanNumeral.new(2).to_s + assert_equal 'III', RomanNumeral.new(3).to_s + assert_equal 'IV', RomanNumeral.new(4).to_s + assert_equal 'V', RomanNumeral.new(5).to_s + assert_equal 'VI', RomanNumeral.new(6).to_s + assert_equal 'VII', RomanNumeral.new(7).to_s + assert_equal 'IX', RomanNumeral.new(9).to_s + assert_equal 'X', RomanNumeral.new(10).to_s + assert_equal 'XIX', RomanNumeral.new(19).to_s + assert_equal 'XXXII', RomanNumeral.new(32).to_s + assert_equal 'LI', RomanNumeral.new(51).to_s + end end end + diff --git a/assignments/assignment08.rb b/assignments/assignment08.rb index 37b2fbd..87017d7 100644 --- a/assignments/assignment08.rb +++ b/assignments/assignment08.rb @@ -47,49 +47,37 @@ def self.from_string # returns a new instance end end -end -require Assignment08 +#require Assignment08 require 'minitest/autorun' -class RomanNumeralTest < MiniTest::Test - def test_one - assert_equal 'I', RomanNumeral.new(1).to_s - assert_equal 'II', RomanNumeral.new(2).to_s - assert_equal 'III', RomanNumeral.new(3).to_s - assert_equal 'IV', RomanNumeral.new(4).to_s - assert_equal 'V', RomanNumeral.new(5).to_s - assert_equal 'VI', RomanNumeral.new(6).to_s - assert_equal 'VII', RomanNumeral.new(7).to_s - assert_equal 'IV', RomanNumeral.new(9).to_s - assert_equal 'X', RomanNumeral.new(10).to_s - assert_equal 'XIX', RomanNumeral.new(19).to_s - assert_equal 'XXXII', RomanNumeral.new(32).to_s - assert_equal 'LI', RomanNumeral.new(51).to_s + class RomanNumeralTest < MiniTest::Test + def test_one + assert_equal 'I', RomanNumeral.new(1).to_s + assert_equal 'II', RomanNumeral.new(2).to_s + assert_equal 'III', RomanNumeral.new(3).to_s + assert_equal 'IV', RomanNumeral.new(4).to_s + assert_equal 'V', RomanNumeral.new(5).to_s + assert_equal 'VI', RomanNumeral.new(6).to_s + assert_equal 'VII', RomanNumeral.new(7).to_s + assert_equal 'IV', RomanNumeral.new(9).to_s + assert_equal 'X', RomanNumeral.new(10).to_s + assert_equal 'XIX', RomanNumeral.new(19).to_s + assert_equal 'XXXII', RomanNumeral.new(32).to_s + assert_equal 'LI', RomanNumeral.new(51).to_s + end end end -# expected results: -RomanNumeral.new(1).to_s # => 'I' -RomanNumeral.new(2).to_s # => 'II' -RomanNumeral.new(3).to_s # => 'III' -RomanNumeral.new(4).to_s # => 'IV' -RomanNumeral.new(5).to_s # => 'V' -RomanNumeral.new(6).to_s # => 'VI' -RomanNumeral.new(9).to_s # => 'IX' -RomanNumeral.new(10).to_s # => 'X' -RomanNumeral.new(19).to_s # => 'XIX' -RomanNumeral.new(32).to_s # => 'XXXII' -RomanNumeral.new(51).to_s # => 'LI' # bonus: -RomanNumeral.from_string('III').to_i # => 3 -RomanNumeral.from_string('IX').to_i # => 9 -RomanNumeral.from_string('IX').to_i # => 9 +#RomanNumeral.from_string('III').to_i # => 3 +#RomanNumeral.from_string('IX').to_i # => 9 +#RomanNumeral.from_string('IX').to_i # => 9 # given any arbitrary integer n: -n == RomanNumeral.from_string(RomanNumeral.new(n).to_s).to_i +#n == RomanNumeral.from_string(RomanNumeral.new(n).to_s).to_i @@ -100,12 +88,26 @@ def test_one # calculate the golden ratio up to specified precision # validate using MiniTest unit tests -module Assignment08 +module Assignment08 def golden_ratio(precision) - # your implementation here + fib1= 61305790721611591 #fib(82) + fib2=37889062373143906 #fib(81) + (fib1.to_f/fib2.to_f).round(precision) + end + +#require 'Assignment08' + require 'minitest/autorun' + + class GoldenRatioTest < MiniTest::Test + def test_one + assert_equal 1.62, golden_ratio(2) + assert_equal 1.61803, golden_ratio(5) + assert_equal 1.61803399, golden_ratio(8) + end end end + # expected results: golden_ratio(2) # => 1.62 golden_ratio(5) # => 1.61803 From 71c298ae422789f0c7a55392ca79757de6c9b393 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 2 Mar 2015 22:53:20 +0000 Subject: [PATCH 31/32] assignment 8 complete (except for bonus excercises) --- assignments/asgmt8.rb | 15 ++++++++++++--- assignments/assignment08.rb | 35 +++++++++++++++++------------------ 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/assignments/asgmt8.rb b/assignments/asgmt8.rb index dfd66dd..2166a95 100644 --- a/assignments/asgmt8.rb +++ b/assignments/asgmt8.rb @@ -1,4 +1,5 @@ + module Assignment08 class RomanNumeral attr :value, :arabic_digits @@ -30,18 +31,24 @@ def to_s end def to_i + @value end # bonus: create from Roman Numeral - def self.from_string - # your implementation here + def self.from_string(rn) # returns a new instance + rn == 'III' ? @value = 3 : @value = nil #MOREMORE method in process end end + +# bonus: +#RomanNumeral.from_string('III').to_i # => 3 +#RomanNumeral.from_string('IX').to_i # => 9 +#RomanNumeral.from_string('IX').to_i # => 9 #require Assignment08 -require 'minitest/autorun' + require 'minitest/autorun' class RomanNumeralTest < MiniTest::Test def test_one @@ -57,6 +64,8 @@ def test_one assert_equal 'XIX', RomanNumeral.new(19).to_s assert_equal 'XXXII', RomanNumeral.new(32).to_s assert_equal 'LI', RomanNumeral.new(51).to_s + assert_equal 'C', RomanNumeral.new(100).to_s + assert_equal 'CI', RomanNumeral.new(101).to_s end end end diff --git a/assignments/assignment08.rb b/assignments/assignment08.rb index 87017d7..37eb9e3 100644 --- a/assignments/assignment08.rb +++ b/assignments/assignment08.rb @@ -39,17 +39,16 @@ def to_s end def to_i + @value end # bonus: create from Roman Numeral def self.from_string - # your implementation here # returns a new instance + rn == 'III' ? @value = 3 : @value = nil #MOREMORE method in process end end -#require Assignment08 - require 'minitest/autorun' class RomanNumeralTest < MiniTest::Test @@ -61,7 +60,7 @@ def test_one assert_equal 'V', RomanNumeral.new(5).to_s assert_equal 'VI', RomanNumeral.new(6).to_s assert_equal 'VII', RomanNumeral.new(7).to_s - assert_equal 'IV', RomanNumeral.new(9).to_s + assert_equal 'IX', RomanNumeral.new(9).to_s assert_equal 'X', RomanNumeral.new(10).to_s assert_equal 'XIX', RomanNumeral.new(19).to_s assert_equal 'XXXII', RomanNumeral.new(32).to_s @@ -88,27 +87,27 @@ def test_one # calculate the golden ratio up to specified precision # validate using MiniTest unit tests -module Assignment08 - def golden_ratio(precision) - fib1= 61305790721611591 #fib(82) - fib2=37889062373143906 #fib(81) - (fib1.to_f/fib2.to_f).round(precision) +module Assignment08 + class GoldenRatioContainer + def golden_ratio(precision) + fib1= 61305790721611591 #fib(82) (a large fibonacci number) + fib2=37889062373143906 #fib(81) (another large fibonacci number) + (fib1.to_f/fib2.to_f).round(precision) + end end -#require 'Assignment08' require 'minitest/autorun' class GoldenRatioTest < MiniTest::Test + def setup + @my_gr = GoldenRatioContainer.new + end + def test_one - assert_equal 1.62, golden_ratio(2) - assert_equal 1.61803, golden_ratio(5) - assert_equal 1.61803399, golden_ratio(8) + assert_equal 1.62, @my_gr.golden_ratio(2) + assert_equal 1.61803, @my_gr.golden_ratio(5) + assert_equal 1.61803399, @my_gr.golden_ratio(8) end end end - -# expected results: -golden_ratio(2) # => 1.62 -golden_ratio(5) # => 1.61803 -golden_ratio(8) # => 1.61803399 From 5a070ba3171589485fe5decc15a087e1223b6ec7 Mon Sep 17 00:00:00 2001 From: Chris Peak Date: Mon, 2 Mar 2015 22:59:39 +0000 Subject: [PATCH 32/32] added further tests to RomanNumeralTest --- assignments/assignment08.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/assignments/assignment08.rb b/assignments/assignment08.rb index 37eb9e3..69f28ba 100644 --- a/assignments/assignment08.rb +++ b/assignments/assignment08.rb @@ -65,6 +65,8 @@ def test_one assert_equal 'XIX', RomanNumeral.new(19).to_s assert_equal 'XXXII', RomanNumeral.new(32).to_s assert_equal 'LI', RomanNumeral.new(51).to_s + assert_equal 'DCC', RomanNumeral.new(700).to_s + assert_equal 'CMXII', RomanNumeral.new(912).to_s end end end