Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Good stuff #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions bankclasses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
class Bank
def initialize(bank_name)
# Expects a string
@bank_name = bank_name
@bank_database = Hash.new
end

def set_bank_customer_funds(bank_customer, funds)
# Expects a BankCustomer object, and an integer or float
@bank_database[bank_customer] = funds
end

def get_bank_customer_funds(bank_customer)
# Expects a BankCustomer object
@bank_database[bank_customer]
end

def withdraw(customer, amount)
# Expects a BankCustomer object, and an integer or float

return false if amount > @bank_database[customer.customer_identity]

@bank_database[customer.customer_identity] = @bank_database[customer.customer_identity] - amount
true
end

end

class CreditCard
def initialize(customer_identity)
# Expects a BankCustomer object
@card_owner = customer_identity
end

def get_card_owner
@card_owner
end

end

class BankCustomer
def initialize(customer_identity, initial_funds, bank_name)
# Expects a string, integer or float, and a Bank object
@customer_identity = customer_identity
@bank_name = bank_name
register_new_customer(initial_funds)
end

def customer_identity
@customer_identity
end

def bank_name
@bank_name
end

def card
@card
end

def register_new_customer(funds)
bank_name.set_bank_customer_funds(@customer_identity, funds)
end

def register_new_credit_card
@card = CreditCard.new(self)
end

def deposit(amount)
bank_name.set_bank_customer_funds(@customer_identity, get_bank_customer_funds(@customer_identity) + amount)
puts "You deposited $#{amount} into your account, you now have $#{bank_customer[@customer_identity]}"
end
end

class Store
@products = { 'Cheesecake' => 12, 'Hair Dryer' => 52, 'Purple Object' => 600, 'Mask' => 25, 'Horn' => 125 }

def self.display_products
@products.each do | k, v |
puts "#{k}: $#{v}"
end
nil
end

def self.purchase_item(credit_card, item)
# Expects a CreditCard object, and a valid item from the hash keys of @products
if @products[item].nil?
puts "No such item."
return nil
end

if credit_card.get_card_owner.bank_name.withdraw(credit_card.get_card_owner, @products[item])
puts "Transaction successful, have fun with your new #{item}!"
else
puts "Card denied!"
end
end
end

# Tests

bank = Bank.new('bank')
customer = BankCustomer.new('customer', 5000, bank)
33 changes: 19 additions & 14 deletions http_requests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ answer the following...

1. GET /movies
Fill this in with what the page will look like in the browser:

Title: A New Hope, Year: 1977
Title: The Empire Strikes Back, Year: 1980
Title: Return of the Jedi, Year: 1983


2. GET /movies/1
Fill this in with what the page will look like in the browser:

Title: A New Hope, Year: 1977


3. POST /movies with params {:movie => {:title => "The Phantom Menace", :year => 1998}}
Expand All @@ -35,13 +37,13 @@ Fill in what the database should look like after a post with parameters
================================================
| id | Title | Year |
================================================
| | | |
| 1 | A New Hope | 1977 |
------------------------------------------------
| | | |
| 2 | The Empire Strikes Back | 1980 |
------------------------------------------------
| | | |
| 3 | Return of the Jedi | 1983 |
------------------------------------------------
| | | |
| 4 | The Phantom Menace | 1998 |
------------------------------------------------

4. PUT /movies/4 {:movie => {:title => "The Phantom Menace", :year => 1999}}
Expand All @@ -50,28 +52,31 @@ Fill in what the database should look like after the PUT with the parameters
================================================
| id | Title | Year |
================================================
| | | |
| 1 | A New Hope | 1977 |
------------------------------------------------
| | | |
| 2 | The Empire Strikes Back | 1980 |
------------------------------------------------
| | | |
| 3 | Return of the Jedi | 1983 |
------------------------------------------------
| | | |
| 4 | The Phantom Menace | 1999 |
------------------------------------------------

5. GET /movies
Fill in what the page should look like in the browser

Title: A New Hope, Year: 1977
Title: The Empire Strikes Back, Year: 1980
Title: Return of the Jedi, Year: 1983
Title: The Phantom Menace, Year: 1999

6. DELETE /movies/4
Fill in what the database should look like after the above request

================================================
| id | Title | Year |
================================================
| | | |
| 1 | A New Hope | 1977 |
------------------------------------------------
| | | |
| 2 | The Empire Strikes Back | 1980 |
------------------------------------------------
| | | |
| 3 | Return of the Jedi | 1983 |
------------------------------------------------
66 changes: 62 additions & 4 deletions interview_responses.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
You can write your responses here!

1.
1.
If you can only pull fruit out randomly, then it is the number of buckets * the number of times you iterate over them until one of the buckets produces a different fruit. Then you would have found the bucket with two types of fruit, and aleady know the contents of the other two. If you can select to attempt to take out a certain type of fruit, for example, I want to go to bucket A and try to pull out an apple, and either fail or succeed, then you can determine the contents of the buckets in 5 draws ( or less, depending on luck ). Check the three buckets for apples, when you fail to find an apple in one bucket, search the two other buckets for oranges. The bucket that has an orange in it is the apple + orange bucket, the first bucket is the orange bucket, and the remaining bucket is the apple bucket.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is a little simpler than that 😉

If I have the three buckets, and I know that the labels are wrong, I should be able to get it in (at most) two tries. I'm going to use apples and oranges (I can't remember the example off the top of my head). Try thinking about it this way:

1 try:
Case: Pull an apple out of a bucket labeled 'apples'.

2 tries:
Case: Pull an orange out of a bucket labeled 'apples'.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I wasn't thinking about the wrong labels correlating to anything. If the labels can only be 'apples', 'oranges', or 'both apples and oranges', ( not labeled 'broccoli' for example ) and they are definitely wrong, then it should only take one or two tries. Taking an apple out of the 'apple' box means that it is the both box, and that the other boxes are 'apples' -> 'oranges', 'oranges' -> 'apples'. If you draw an orange out of the apple box, then you must draw out of the orange box. If you get an orange, it is the both box, if you get an apple, then the first box is the both box.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, you got it! This was an actual interview question I got, and I only got it when they reminded me that it was 'important that the labels are initially wrong.'


2.
2.
reversing_string = ('a'..'z').to_a
reversed_string = ''
reversing_string.each do | k |
reversed_string = k + reversed_string
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I like the use of enumerables here. One thing I'd like to point out, though, is that to_a is a rails extension for the String class. How would you do that if you knew that you didn't have the to_a functionality for a string?


3.
3.

With Recursion:

summing_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]]

$passed_sum = 0
def recursive_sum_funct(sum_array)
for element in sum_array do
if element.kind_of?(Array)
recursive_sum_funct(element)
else
$passed_sum += element
end
end
end

puts recursive_sum_funct(summing_array)
puts $passed_sum

Question here -> With the puts statement for the function, I get back each number seperately that I wish to add. Ideally, instead of using a global variable, I would like to use the numbers that each recursive function is returning, and just add those. Is there a way to add the results outside of the function? I.E., if I wanted to say, do
"final_sum = recursive_sum_funct(summing_array)" and somehow have that add together all the recursive results. Is that possible? Or is it just the exact same as using a global variable since what I'm trying to add is each recursive function return, which is the number anyways?

Or, more simply with helper methods:
summing_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]]
summing_array.flatten.inject{|sum, x| sum + x}

Without Recursion:

sum = 0
summable_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]]

formatted_array = summable_array.join(" ").split(" ").each do | element |
sum += element.to_i
end

puts sum


4.

Just going to construct this as arrays, considering that you don't yet know what's in the jars, do this. Take one pill from each jar, to put on the scale, and record the order. Treating each jar as an array with each pill being it's own array containing an integer representing the weight. I.E.
mystery_jar4 = [[10], [10], [10], etc...]

scale = 0
scale += mystery_jar1.pop, mystery_jar2.pop, mystery_jar3.pop, mystery_jar4.pop, etc...

Take the measurement

scale.index(9)

The index shows which jar the contaminated pill came from.

Outside of a programming construct, not sure how the scale measurement relationship makes sense. Is it only fair to measure the sum of any collection of materials on the scale?

4.