11
2
My family has an e-commerce business. On our own site, we force people to choose their state from a dropdown menu when they enter their address, but through some other channels we use, customers can enter anything they want into the box.
My mom loves the invoice templates I made for her, which are automagically generated. But because they're so pretty and balanced, she can't stand it when people WRITE OUT the names of their states, or worse, write something like "new jersey." She says it ruins the look.
My dad likes code to be lightweight. So rather than using a switch-case block, he wants a leaner solution.
So the challenge is to make a short function that takes the possible inputs and returns a two letter abbreviation (capitalized, for Mom). We're going to make a (faulty) assumption that our users can spell and always put a space in the name (where needed) or pass in the correct abbreviation. The scope is the 50 US states.
- New York
- new york
- NY
- ny
are all acceptable inputs for New York, and should output NY.
If something like New Yrok is passed in, the function can return the original value.
You can use any common language. This is a popularity contest, so the one with the most votes at the end of a week wins. I assume that this will favor novelty and utility.
EDIT: The description is story fluff, but I was working a similar project and thought that there must be a more interesting way to do it. I can do the project myself (already did) but I thought this was a good place for a more interesting challenge. By "Any common language" I was excluding custom languages/libraries designed for this challenge - I was trying to look for novel methods, rather than free code help. I figure everyone has done this at some point, but it would be fun to do it in an unusual way. I find that the most interesting projects are the ones where you tackle everyday tasks in new and interesting ways - that's why this is a popularity contest rather than golf.
I'm disappointed it is closed. It looks like everybody managed to get an answer in except me... – Jerry Jeremiah – 2015-08-09T23:26:22.927
Not sure if +1 for your mom's thoughts on aesthetics or -1 for your dad's thoughts on short code. – Ingo Bürk – 2014-10-03T18:32:29.157
Would
nyoutputNY? – hmatt1 – 2014-10-03T18:37:03.207Yes, it should. I'll make that edit. – Josiah – 2014-10-03T18:38:37.357
14I'm unsure why this is a popularity contest instead of code golf (especially since the name includes 'golf' and your dad favors short code). – Geobits – 2014-10-03T18:47:55.227
I agree with Geobits, this would work better as a code-golf I think. – James Williams – 2014-10-03T18:52:45.857
@Geobits: For actual production code, you would want the code to be lean but reasonable, not golfed... unless you want to maintain code that looks like something an electric cat coughed up (
X⌿⍨Y^.=⍨X↑⍨(⊃⍴X),⍴Y)? – Claudiu – 2014-10-03T19:26:26.2375@Claudiu True, but this site isn't intended for production code... – Geobits – 2014-10-03T19:27:41.400
@Geobits: Isn't it? "My family has an e-commerce business. On our own site [...] My dad likes code to be lightweight. So rather than using a switch-case block, he wants a leaner solution." It sounds like he plans to use this code on the e-commerce site. – Claudiu – 2014-10-03T20:39:46.180
3@Claudiu I honestly assumed that was "story fluff" of the sort that usually goes with these challenges. Either way, when I said "this site..." I meant PP&CG, as most code here is explicitly not intended for use in production. Honestly, if he's looking for actual code to use on his site, it would be more ethical to do it himself or contract it out ;) – Geobits – 2014-10-03T20:52:43.297
1@Geobits I thought it was fluff as well. Based on his profile, it sounds like OP is actually looking for code since his parents have an online toy store. I deleted my answer and voting to close as off-topic. This isn't a code challenge, its free contracting. – hmatt1 – 2014-10-03T21:18:33.603
1It sounds like OP is asking us to write code for him, but trying to frame it as a code-challenge. – hmatt1 – 2014-10-03T21:19:15.940
8@chilemagic
you can use any code... so OP will rewrite his site to use your APL/CJAM/GolfScript solution? It's a challenge based on a true story. I vote up – edc65 – 2014-10-03T21:23:49.4304It's a pretty trivial task, why would OP go to all the effort of typing up a question when it would be easier just to code it himself? Either way, I enjoyed giving it a go. – James Williams – 2014-10-03T21:47:53.700
@edc65 IMO, There's a big difference between what you quoted ("you can use any code") and what the OP actually said ("you can use any common language"). – Geobits – 2014-10-03T23:10:52.063
@Geobits good point, I honestly misread. On the other hand, as you said, that's not the place for production code. I'm here to have fun. – edc65 – 2014-10-03T23:22:43.903
3Grossly unsuitable winning criterion – None – 2014-10-04T14:54:09.337