Friday Programming Quiz [modified]
-
Dave Kreskowiak wrote:
But, then again, I wrote the "psuedo" in my head after looking at the problem for all of 15 seconds with nothing more than the CP post window in front of me.
Ship it!
Matt Gerrans
Done! I rewrote it in Plain Portugese, though. Brazil is such an ignored market!
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Tomas Petricek wrote:
My F# solution
That's OCaml, right? Can't you use pattern matching?
Yes - see my Haskell solution :cool:
-
I recently encountered/solved this problem and it is fairly simple. Column names in a database are named using Pascal casing, however to display it in a user friendly manner words need to be separated with spaces to generate display names. Following examples show the output for some strings.
Name Display Name
BodyHTML -> Body HTML
LastAccessedTime -> Last Accessed Time
ESOP -> ESOPIn a language of your choice implement a procedure that will convert the column names to display names.
String DisplayNameFromColumnName(String columnName) {
}-- modified at 16:56 Friday 1st December, 2006 Removed XMLValue -> XML Value
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan
As usual - a Haskell solution. I decided to use raw list processing rather than regexes, 'cause I couldn't be bothered to look up the regex library functions... It also handles single letter wored like A - try "ThereIsAColumn"
wordise [] = [] wordise [x] = [x] wordise (x:y:rest) | (isAlpha x) && (isUpper y) = x: (' ': (wordise (y:rest))) | otherwise = x: (wordise (y:rest))
-- modified at 19:53 Saturday 2nd December, 2006 OK - so I could be bothered to look up the regex functions...
wordise2 s | Just (before, _, after, [lower, upper]) <- matchRegexAll (mkRegex "([a-zA-Z])([A-Z])") s = before ++ lower ++ (' ':upper) ++ (wordise after) | otherwise = s
This uses a Haskell 98 extension called pattern guards to do pattern matching on the results of a function called on the input, rather than directly on the input. The first guard succeeds in the case of a successful regex match. The second handles a failing regex match by just returning the string.
-
Yeah, F# is based on OCaml :). As I'm thinking about the problem it could be possible to use another very interesting F# feature called active patterns[^], but I have not played with this feature very much and I'm to lazy to think about it now.. it's friday :-O
Tomas Petricek, C# MVP
Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..)Active patterns look similar to a Haskell extension called pattern guards[^]. My Haskell solution has an example of their use...