Let's Get Clever (2 Part Series)
We spend a lot of time trying to write good code. I don't know about you, but I get weary of hearing the (apt) admonishment "don't be clever".
Everyone needs a break. So, in celebration of all our diverse skills and knowledge, Let's Get Clever!
Determine the best card to play for a trick in spades.
Spades is a card game played partners, where in your team must capture the most Tricks without going over your Bid (the number of tricks you declared at the start of the round that you'd try to capture).
You can see all 1 to 13 cards in your hand at a time. You must play one each trick.
You must match the leading suit (the suit of the card first played in the trick) if you can. If you cannot match the suit, you may play any card.
The highest spade played wins the trick, or else the highest card in the leading suit.
Aces high. (Ace beats any other value in its suit.)
If either you or your partner takes the trick, your team gets credit for it.
Strategy tip: if you have no chance of winning the trick, or if your partner already has, you should take the occasion to get rid of bad cards.
(Spades players: yes, I am skipping over the rules about leading with spades, as it doesn't fit into this challenge.)
In short, if you can win the trick, output the weakest possible card from your hand that would allow you to do that. If you cannot win the trick (or if your partner already has), get rid of a bad card.
Cards are represented by two-character strings. The first character is the value (
K), and the second character is the suit (
- You'll receive the three cards played so far as a space-delimited string.
- Your own hand will be specified first by an integer on a line telling you how many cards you have.
- Your hand will be specified by a space-deliminted string.
QH 8D 2S 5 KD 7D 2H 9S 8S
The best card played so far is
2S, which your opponent put down. You can win the trick with a spade, and you have two:
8S. The weakest of these two that can still beat
8S, so you should play that.
9D QD 3H 5 KD 7D 2H 9S 8S
Your partner has already won the trick, as
QD is the best played so far. There's no need for you to throw away a good card, so you can take the opportunity to get rid of your weak
XS 3H 7S 5 KD 7D 2H 9S 8S
Your opponent played
XS, which is the best card so far. You have nothing to beat it, so you should (again) take the occasion to throw away your weakest card,
Assume you're going last. You can see all three of the cards played by the other players. The middle one was played by your partner.
Assume your team wants to take the trick if possible.
You'll always receive 1-13 cards.
These are NOT required, but if you achieve any of these goals, major bragging rights!
⏱️ Efficiency: Can you achieve
O(n)or better for the decision-making code? (Efficiency goal doesn't include parsing input data...as long as no part of the parsing directly informs the decision-making algorithm.)
⛙ No If-Statement: Can you write a solution that doesn't use an explicit
switchstatement? (I don't even know if this is possible. Let's find out!)
(These are the same for every Let's Be Clever challenge.)
Your clever code should accomplish the stated goal in whatever way you like. Watch out for edge cases, though.
You may use any language you like. However, I discourage use of deliberately esoteric languages. Cleverness is usually more fun in production languages.
You know all those rules about readability and maintainability? Fuhgeddaboudit. Cleverness is the goal.
Portability, memory safety, security? Bah humbug. Mel is welcome.
Efficiency is not the main goal, but if your cleverness happens to have an efficiency worth bragging about, definitely point it out!
Explain your Cleverness. Don't wait for others to ask you, although you should be prepared to answer questions. The neat side-effect of Clever code is that everyone can learn cool, weird things.
Be open to feedback. Others might be able to spot edge-cases you missed, or have ideas on how to make your Clever approach even more clever.
Preening is permitted, but don't be obnoxious about it, and be equally liberal with your complements of others. Leave false modesty in the drawer. (Imposter Syndrome sucks, so let's use this to give it a good kick in the teeth.)
Upvote Cleverness! If you really like someone's solution, be sure to give it a Like.
Always be polite and constructive. Please don't criticize anyone for not being clever enough, not understanding what you did, or asking a "stupid question". This is Cleverness on display. Be the tour guide!
Don't forget: CODE HACKERY ENCOURAGED! No holds barred.
One of the most consolidated misconceptions about programming, since the early days, is the idea that such activity is purely technical, completely exact in nature, like Math and Physics. Computation is exact, but programming is not.