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