Computer Science Canada FeedBack Please. |
Author: | TokenHerbz [ Wed Dec 08, 2010 7:26 pm ] | ||
Post subject: | FeedBack Please. | ||
Being Very new to Ruby I'm unsure of the best ways to go about using certain loops and when i should compact code, etc etc.. I'm going to upload a picture which contains a challenge I've done, along with my code for you guys to give me feed back on. Any tips or helpfulness would be wonderful, I want to learn the correct way and books/etc only take me so far, skilled programmers opinions are another valued source of learning. First: the CHALLENGE: http://i.imgur.com/ieyDP.jpg Second: The CODE (please excuse the really long named variables, I got carried away!)
|
Author: | Insectoid [ Wed Dec 08, 2010 9:04 pm ] | ||||||
Post subject: | RE:FeedBack Please. | ||||||
Your break_down_number method can be reduced a fair bit.
I may have a syntax error or two, I didn't bother running this. Whenever you have something like
It's (in my opinion) better to use the ? : structure, ie
|
Author: | jcollins1991 [ Wed Dec 08, 2010 9:15 pm ] |
Post subject: | Re: FeedBack Please. |
I'd suggest tying to write it with a while loop, I'm not sure how efficient Ruby is with tail recursion. |
Author: | Tony [ Wed Dec 08, 2010 9:26 pm ] | ||||
Post subject: | RE:FeedBack Please. | ||||
can also be written as
|
Author: | wtd [ Wed Dec 08, 2010 11:47 pm ] | ||
Post subject: | RE:FeedBack Please. | ||
Why not implement this as a method on the Fixnum class?
|
Author: | Tony [ Thu Dec 09, 2010 12:22 am ] | ||||
Post subject: | RE:FeedBack Please. | ||||
Now that I have some more time to look over this: - there is no need to initialize variables - the typical convention for internal variables is _name instead of name_ - return doesn't need brackets for expressions - the question states that the input would be valid, so the check isn't required, but if you must -- checking against ranges works too
- from the performance standpoint, you are evaluating [break_down_number(x,0)] twice (once in a conditional and once more to actually save the value). Also, the same x will always produce the same sequence, so a lot of work can be shortcircuited with dynamic programming. My solution (because reading code is essential for writing code):
|
Author: | TokenHerbz [ Thu Dec 09, 2010 6:45 am ] | ||
Post subject: | Re: FeedBack Please. | ||
Thank you everyone for the feed back. Insectoid : I like how to minimize those If statements and will be doing that more. I wonder though, does it work with elsif too? or should I use a case? Or case is best suited for MULTIPLE ELSEIF's only? What I've read on case is that it's really helpful or best used in really really large if structures but I'm sure it can be used for small ones to. But it doesn't say what the Ideal Situation would be to use them is... While learning ruby and coding I want to learn the correct way so I don't have poor programming habits later. wtd : I've not even read on the ruby class's yet, but I will take a look in due time. I feel I have way to much of the ruby basics to learn still. thanks tho. I'm curious though, Do you add that to the actual FIXNUM class?... for future use in all programs? Tony : Awesomeness. I'll be going threw my ruby reads because I have no idea how some of that code works. 1 quick example would be
|
Author: | Insectoid [ Thu Dec 09, 2010 10:46 am ] |
Post subject: | RE:FeedBack Please. |
That conditional structure only supports binary conditionals. It's basically, <condition> ? <run if condition is true> : <run if false> So, If/else structures only. I generally don't use case unless there's 3 or more unrelated values or if I need to run a different function based on input. An example of this is a basic calculater- there isn't any efficient way (I think) to assign *, /, + and - characters to their operations without a case or large, unwieldy if. Also, I'm not quite sure but I think Ruby evaluates zero as false and anything else as true, so instead of <var> == 0 you can just do !<var>. |
Author: | jcollins1991 [ Thu Dec 09, 2010 11:38 am ] |
Post subject: | Re: RE:FeedBack Please. |
Insectoid @ Thu Dec 09, 2010 10:46 am wrote: That conditional structure only supports binary conditionals.
It's basically, <condition> ? <run if condition is true> : <run if false> So, If/else structures only. I generally don't use case unless there's 3 or more unrelated values or if I need to run a different function based on input. An example of this is a basic calculater- there isn't any efficient way (I think) to assign *, /, + and - characters to their operations without a case or large, unwieldy if. Also, I'm not quite sure but I think Ruby evaluates zero as false and anything else as true, so instead of <var> == 0 you can just do !<var>. The only false things in Ruby are 'nil' and 'false' EDIT: Though, you can just do 0.zero? to get the falseness of 0 in other languages |
Author: | wtd [ Thu Dec 09, 2010 11:49 am ] |
Post subject: | RE:FeedBack Please. |
Yes. Zero is a perfectly cromulent number, and in no way false. |
Author: | Tony [ Thu Dec 09, 2010 1:51 pm ] |
Post subject: | RE:FeedBack Please. |
results is a Hash -- http://ruby-doc.org/core/classes/Hash.html it stores all the values that have already been calculated, to be used for future computations. This idea is the core of Dynamic Programming (bad naming, it's more of "programming while having a table of stored values"). Try to see what's stored inside after each call to max_cycle_for. {1 => 1} is the base base. We know that the max cycle for n=1 is 1, per definition, so we initialize to this pair so that recursive loops terminate. |
Author: | wtd [ Thu Dec 09, 2010 1:52 pm ] |
Post subject: | RE:FeedBack Please. |
Dynamic programming is about doing as little work as possible by remembering where you've been before. |
Author: | TokenHerbz [ Sat Dec 11, 2010 2:15 pm ] | ||
Post subject: | Re: FeedBack Please. | ||
Another Programming Challenge Needing Feedback! : I feel there's improvements, But I'll let you, the experts decide. Here is the challenge: http://i.imgur.com/SZ8Dh.jpg However, I went ahead and changed a few things myself: I made it so you can adjust the grid size inside the code, And adjust the MINE PERCENTAGE for that grid. The mines are also Randomly generated. Please, be very critical and show me how how sloppy this is I put if's inside the loops and the bottom if's i couldn't figure out how to compact that any way. I gave it a lot of thought and this is the best i could come up with.
|
Author: | Tony [ Sat Dec 11, 2010 4:44 pm ] | ||||||||||
Post subject: | RE:FeedBack Please. | ||||||||||
you don't need |mines| if you're not going to use it.
Also, strictly speaking, there is a chance of filling the same location with a mine twice. Meaning that at 100% MINE_PERCENTAGE, there could still be empty spaces.
sometimes it's easier to read do-end blocks
One trick to avoid such checks is to have a buffer around your array, so that you could safely go out of bounds. (Another would be to treat edges in a special way and use those as a buffer to safely work with everything else). |
Author: | TokenHerbz [ Sat Dec 11, 2010 5:21 pm ] |
Post subject: | RE:FeedBack Please. |
so have the array go from -1 to 5. 0,1,2,3,4 being the used array elements. -1, 5 being NIL? i'll try it out, i like that idea. |
Author: | Tony [ Sat Dec 11, 2010 7:02 pm ] |
Post subject: | RE:FeedBack Please. |
if you set the buffer to "." then everything should work well, and you wouldn't need a special case for nils. Btw, index of -1 refers to the last element in the array. Quote: >> [1,2,3][-1] => 3 |
Author: | Tony [ Sat Dec 11, 2010 8:09 pm ] | ||
Post subject: | RE:FeedBack Please. | ||
and..
|