midterm part 2 submission

parents
require './to_do_list'
todo = ToDoList.new
puts "================ \n"
todo.add_task("Clean room")
todo.add_task("Submit MIS21 HW")
todo.add_task("Study for MIS21 midterms")
todo.add_task("Study for Stat midterms")
todo.add_task("Submit advisement form")
todo.add_task("Consult for final project")
puts "================ \n"
todo.view_pending_tasks
puts "================ \n"
todo.accomplish_task(0)
todo.accomplish_task(2)
todo.accomplish_task(2)
puts "================ \n"
todo.view_accomplished_tasks
\ No newline at end of file
class ToDoList
def initialize
@pendingTasks = []
@accomplishedTasks = []
end
def add_task(task_name)
@pendingTasks.push(task_name)
puts "#{task_name} has been added to your task list"
end
def view_pending_tasks
puts 'Pending tasks:'
@pendingTasks.each do |task|
i = @pendingTasks.index(task)
i += 1
puts "(#{i}) #{task}"
end
end
def accomplish_task(index)
temp = @pendingTasks[index]
@pendingTasks.delete_at(index)
@accomplishedTasks.push(temp)
puts "#{temp} has been marked as accomplished"
end
def view_accomplished_tasks
puts 'Accomplished tasks:'
@accomplishedTasks.each do |task|
i = @accomplishedTasks.index(task)
i += 1
puts "(#{i}) #{task}"
end
end
end
\ No newline at end of file
require 'date'
class Budget
attr_accessor :balance
def initialize(initial_balance)
@balance = initial_balance
@expenses = {}
@income = {}
puts "Initial balance: %.2f" % [@balance]
end
def add_expense(item, amount, date)
date = Date.parse(date)
str = date.strftime('%B %e %Y')
str1 = date.strftime('%B')
if @expenses.keys.include?(str1) == false
@expenses[str1] = amount
else
x = @expenses[str1]
x += amount
@expenses[str1] = x
end
@balance -= amount
puts "#{item}: An amount of %.2f was deducted from the balance on #{str}" % [amount]
end
def add_income(item, amount, date)
date = Date.parse(date)
str = date.strftime('%B %e %Y')
str1 = date.strftime('%B')
if @income.keys.include?(str1) == false
@income[str1] = amount
else
x = @income[str1]
x += amount
@expenses[str1] = x
end
@balance += amount
puts "#{item}: An amount of %.2f was added to the balance on #{str}" % [amount]
end
def monthly_report(month, year)
date = Date.parse("#{year}0#{month}01")
str = date.strftime('%B %Y')
str1 = date.strftime('%B')
puts '===================================='
puts "MONTHLY REPORT: #{str}"
puts "Total Expense: %.2f" % @expenses[str1]
puts "Total Income: %.2f" % @income[str1]
puts '===================================='
end
def current
puts "Balance: %.2f" % [@balance]
end
end
\ No newline at end of file
require './budget'
budget = Budget.new(50000.00)
budget.add_expense("Petron", 5000.00, "2017-06-15")
budget.add_income("Work", 6000.00, "2017-06-26")
budget.add_expense("National Bookstore", 2500.00, "2017-06-27")
budget.add_expense("Gonzaga", 200.00, "2017-06-27")
budget.add_income("Freelance", 10000.00, "2017-07-10")
budget.current
budget.monthly_report(6,2017)
budget.add_expense("Nintendo Switch", 19000.00, "2017-07-15")
budget.add_expense("Samsung S8 Edge", 39300.00, "2017-07-20")
budget.monthly_report(7,2017)
budget.current
\ No newline at end of file
@distances = []
def classify_with_knn(data, k, unknown_data_point)
data.each do |data|
a = data
b = unknown_data_point
label2 = a[:label]
value = distance(a, b)
temp = {label: label2, distance: value}
@distances.push(temp)
end
sorted = @distances.sort_by{ |x| x[:distance]}
dog = 0
cat = 0
i = 0
while i < k do
temp2 = sorted[i]
if temp2[:label] == 'cat'
cat += 1
else
dog += 1
end
i += 1
end
result = []
if cat > dog
result.push('cat', cat)
return result
else
result.push('dog', dog)
return result
end
end
def distance (a, b)
temp = a[:features]
temp2 = b[:features]
output = 0
i = 0
temp.each do |v1|
v2 = temp2[i]
output += (v1 - v2)**2
i += 1
end
return output
end
\ No newline at end of file
require './knn'
collection = [
{label: 'cat', features: [12, 1, 1] },
{label: 'cat', features: [13, 3, 1] },
{label: 'cat', features: [15, 1, 1] },
{label: 'dog', features: [3, 12, 2] },
{label: 'dog', features: [4, 11, 2] },
{label: 'dog', features: [1, 11, 2] },
{label: 'cat', features: [11, 4, 1] },
{label: 'cat', features: [13, 2, 1] },
{label: 'dog', features: [3, 13, 2] },
{label: 'dog', features: [7, 12, 2] }
]
unknown_data_point = { label: nil, features: [4, 2, 1] }
result = classify_with_knn(collection, 5, unknown_data_point)
puts "Result: #{result[0]} with #{result[1]} neighbors"
\ No newline at end of file
require './person'
names = ['christian', 'noel', 'jc']
people = []
names.each do |name|
name = Person.new(name)
people.push(name)
end
p1 = people.sample
p2 = people.sample
while p2 == p1
p2 = people.sample
end
while true
puts "Who's likely to succeed? Press any key to exit."
puts "1 for #{p1.name}"
puts "2 for #{p2.name}"
choice = gets.chomp
if choice == '1'
Person.update_success_points(p1, p2)
temp = p2
while (p2 == p1 || p2 == temp)
p2 = people.sample
end
elsif choice == '2'
Person.update_success_points(p2, p1)
temp = p1
while (p1 == p2 || p1 == temp)
p1 = people.sample
end
else
puts 'Exiting...'
sorted = people.sort_by {|person| person.success_points}
sorted.reverse_each do |person|
if (person.success_points >= 0 )
puts "#{person.name} #{person.success_points}"
else
puts "#{person.name} 0.0"
end
end
break
end
end
\ No newline at end of file
class Person
attr_accessor :name, :success_points
def initialize(name)
@name = name
@success_points = 0.0
end
def to_s
puts @name
end
def expected_score(another_person)
rB = another_person.success_points
rA = @success_points
x = (rB-rA)/400
eA = 1/(1 + 10**x)
return eA
end
def new_success_points(score, another_person)
rA = @success_points
eA = expected_score(another_person)
sA = score
result = rA + 32 * (sA - eA)
return result
end
def self.update_success_points(winner, loser)
points1 = winner.new_success_points(1, loser)
points2 = loser.new_success_points(0, winner)
winner.success_points += points1
loser.success_points += points2
end
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment