Skip to content

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.

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