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

Good stuff #1

wants to merge 3 commits into from

Conversation

chrisvans
Copy link

The classes relationship was an excellent exercise to tool around and figure out how ruby works. I feel like this alone should definitely be a prerequisite to the rails introduction.

@@ -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.'

@chrisvans
Copy link
Author

Interesting! I used to_a in irb without requiring anything, and in a .rb file with requiring anything, so I assumed it was just ruby. How do you tell when Rails extensions are being used in irb? Is there a way to disable it for testing?

In any case, using ('a'..'z').to_a was simply for the convenience of making a large ordered string->array. Taking a string like 'cheesecake' assigned to reversing_string, I would do reversing_string.split("") to get each character into an array.

@deanxorr
Copy link
Member

Okay, so I tried it.

('a'..'z').to_a

will work because (...) is a Range object, which has to_a defined. Give the same thing a try with hello.to_a and it will raise a NoMethodFound error or something.

The alternative to splitting the whole string is using the .chars method and converting that to an array, which is what I was suggesting.

@chrisvans
Copy link
Author

Ah, I see. I messed around with enumerables a bit using .collect, .cycle, etc. So this turns a string into an enumerable, which I can then use, say .collect on without ever turning it into an array.
reversed_string = ""
reversed_string = 'reverseme'.chars.collect { |a| a + reversed_string }
Sounds more efficient. Is it the same as taking the length of the full string, iterating over the range(length), and using the index of the range to grab each letter and put it into a new string? As far as memory usage goes, I mean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants