|
| | #1 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 586
| Programming Problem #1 So I'm sitting in SEA-TAC waiting for my flight home for Christmas and found a free internet connection which means I finally have time to do this. Note: I have no idea if this will be worthwhile, but if this helps just one person learn something about programming, I'll consider it a success. I'm going to post the problem now and then solve it for myself on my flight. Problem #1: Create a class that functions as a standard 52 card deck of cards. Methods: Constructor: Creates the deck Shuffle: Shuffles the deck (this is where beginners can stretch themselves and those more advanced can get as ridiculous as they'd like). Cut: (Edited based on input from Daerath) Cut the deck - Cuts the deck in a random place +1 Overload - Passes in index of deck cut to occur. Deal: Passes in number parameter (or just parameter for non-strongly typed languages) and deals that many cards off of the top of the deck. That's it. I figured I'd start with something simple-ish. If you've got some time, post your solutions. ![]() Last edited by Zippygoose : 01-02-2007 at 05:01 PM. |
| | |
| | #3 (permalink) |
| Registered User Join Date: Mar 2002
Posts: 805
| IMO, this isn't the best design. Whenever someone cuts a deck they generally do so at random. Add a method that has no parameters that will randomly cut the deck and then have it call the overloaded method that takes an index. This way you don't have to specify the index.
__________________ Lukas: it is, he used his own logarithms that he wrote for the shadow system in doom 3 which was simply not needed. Eomer: logarithms huh? Fuck you are an idiot. Lukas: algorithms, sorry mr english teacher Kan: lol that goes beyond misspelling thats just plain retardism Lukas |
| | |
| | #4 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 586
| Good call daerath, my shuffling method already does what you've described. Yes, I have a solution - it's on my laptop. I finished it on the plane home long after the realization that I should try to not program during my vacation and hang out with my family instead I'll post it when I get home from work. |
| | |
| | #5 (permalink) | |
| So there's this plane on a treadmill... Join Date: Jan 2005 Location: Southern California
Posts: 2,863
+2 Internets | Quote:
| |
| | |
| | #6 (permalink) | |
| Registered User Join Date: Oct 2005
Posts: 225
| Quote:
A no-parameter version of a cut function that generates a random number between 1 and 51 and passes that to a cut(int) function sounds sensible, although not required by the original spec. | |
| | |
| | #8 (permalink) | |
| Registered User Join Date: Oct 2005
Posts: 225
| Quote:
Second, cutting IRL isn't random. It's barely semi-random. I know when I cut I make a conscious decision to _try_ to cut at a specific location. I've been known to cut at 1 and 51 on purpose. Cutting at 27 is a bit more difficult to manage, but sometimes I try. If you were going to be totally anal about it you'd figure out the randomness of the actual cut compared to the intended cut (0 at 1 and 51, maybe +/- 10 at 26, with a slope between) and add a random factor to the intended cut. But given that the deck is sorted pseudo-randomly (or should be) when it's shuffled, unlike a human shuffle, the actual effect of the cut should be negligible if any. You could make cut(int) a noop and nobody'd know unless they read the code. EDIT: Actually, I've also done a zero-cut (ie accepted the deck as shuffled). Last edited by Rodyland : 01-02-2007 at 07:48 PM. | |
| | |
| | #9 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 586
| You could also pass in "top" "middle" "bottom" which would then randomly cut the deck within the passed criteria. i.e., passing "top" would cut the deck randomly between 0 and 20, middle between 21 and 32, etc. etc. I liked the idea of an overloaded method simply because it introduces the idea to people who might not be familiar with it. Edit: Posting solution soon, had to tweak cut method to account for overloads. Last edited by Zippygoose : 01-02-2007 at 10:20 PM. |
| | |
| | #10 (permalink) | |
| Registered User Join Date: Mar 2002
Posts: 805
| Quote:
Lets say you have a method known as GetAddresses. It takes no parameters and returns a typed dataset of all Addresses in a database. public Addresses GetAddresses() { // Source code to get Addresses from the database. } Some time later during development you discover that in some cases it is useful to only get the active or inactive addresses instead of just getting all of them. And yes, in many systems, you can deactivate an address. Customers can be very particular about not losing their data. The easiest way to accomplish this with the least possible risk to your existing code is to create an overloaded method of GetAddresses that takes an input parameter. In this case I'll use an enum that allows you to specify "All", "Active", or "Inactive". Pull the code out from the original GetAddresses method and place it into your new parameterized method. Modify it to recognize the input parameter. Then in the body of the original method, have it call your new parameterized method with the appropriate parameter (ex. an enum value representing All). enum RecordState { All = 1, Active, Inactive }; public Addresses GetAddresses(RecordState recState) { // Source code to get Addresses from the database. } public Addresses GetAddresses() { return GetAddresses(RecordState.All); } The benefit here is that you haven't duplicated any source code and you haven't changed any existing source code that uses GetAddresses(). You will still have to verify that the modified data access code works throughout the system, but you would have needed to do that anyway. Unless, of course, you chose to duplicate the data access code for your new method, which is inadvisable in almost every situation. If later you realized that you also needed to restrict the addresses to a particular type (in some cases) you would follow the same set of steps. enum RecordState { All = 1, Active, Inactive }; enum AddressType { All = 1, Home, Business }; public Addresses GetAddresses(RecordState recState, AddressType aType) { // Source code to get Addresses from the database. } public Addresses GetAddresses(RecordState recState) { return GetAddresses(recState, AddressType.All); } public Addresses GetAddresses() { return GetAddresses(RecordState.All, AddressType.All); } Again, I'm not changing the behavior of the existing methods, but I am adding functionality to the class. Additionally, many overloaded methods in Java, .NET, many C++ libraries, etc. are implemented in this manner. It can be incredibly useful to create methods that call overloaded versions of themselves. This lets you set default values while still using the same source code. It also lets you release a new version of a public component used by other developers without changing the signatures or behaviors of the existing methods.
__________________ Lukas: it is, he used his own logarithms that he wrote for the shadow system in doom 3 which was simply not needed. Eomer: logarithms huh? Fuck you are an idiot. Lukas: algorithms, sorry mr english teacher Kan: lol that goes beyond misspelling thats just plain retardism Lukas | |
| | |
| | #12 (permalink) |
| Math Enthusiast/Badass MC Join Date: Jun 2002 Location: Seattle
Posts: 586
| Apologies for the delay, work was crazy this week. One thing to note about my solution: My shuffle and cut methods are quite literal. I liked the idea of having a shuffle method that acted like a physical shuffle, meaning the cards become more random as the number of shuffles increase, and vice versa. So before you go blasting me for an inefficient shuffling method, I realize there are much faster computational shuffles I also opted for an enum of cut types to pass into my cut method rather than overloading the method. Class is in C# Code:
Last edited by Zippygoose : 01-07-2007 at 09:45 PM. |
| | |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
| |