-
Notifications
You must be signed in to change notification settings - Fork 11
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
chrisvans
wants to merge
3
commits into
RampUp:master
Choose a base branch
from
chrisvans:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−18
Open
Good stuff #1
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
2. | ||
2. | ||
reversing_string = ('a'..'z').to_a | ||
reversed_string = '' | ||
reversing_string.each do | k | | ||
reversed_string = k + reversed_string | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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'.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.'