Friday Programming Quiz [modified]
-
Christian Graus wrote:
IUseHTMLALot
Yes! But this is a fun Quiz ignore those issues.
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
I can't. If I do a thing I want to do it right (or at least handle all the known problems). How about fields "PriceAtCompUSA" and "IsOwnedByPaulMcCartney" Plus, breaking the field names will make it difficult to parse the resultant file. It's just not worth the effort. Well, unless I'm getting paid.
-
Seeing all these solutions reminds me I really need to learn regex. :^)
- S 50 cups of coffee and you know it's on!
-
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
-
Christian Graus wrote:
IUseHTMLALot
I hate those. It may be incorrect, but i'll still write it as IUseHtmlALot.
-
Eh, it's just another hammer. And the truth of it is, code with too many regexps in it can be nearly unmaintainable. Great for code that won't last long or that should be replaced rather than tweaked... not so good for code intended to grow and mature.
Yes! Now I can take the weekend off! :) I guess it would be good to know it, in case I ever see it out in the wild though. Or, I guess I could just look it up when I need it.
- S 50 cups of coffee and you know it's on!
-
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
return lookupUserFriendlyName[columnName];
;P Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith -
BTW: Probably that is why the .NET naming guidelines state that any acronym > 2 letters should not be all capitalized.
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
Microsoft doesn't get to decide on the proper capitalization of the technologies created by others.
-
Yes! Now I can take the weekend off! :) I guess it would be good to know it, in case I ever see it out in the wild though. Or, I guess I could just look it up when I need it.
- S 50 cups of coffee and you know it's on!
Too many dialects.
-
return lookupUserFriendlyName[columnName];
;P Marc
People are just notoriously impossible. --DavidCrow
There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh SmithDoWhatImThinking ( Data )
-
Does this handle the BodyHTML -> Body HTML case--I believe your solution would give "Body H T M L"?
Nope. It should only do it if the previous character was lower case. 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.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
Well, all we need to do is just compile his solution with the Plain English compiler and try it out! Grande?
Matt Gerrans
:laugh:!
Dave Kreskowiak Microsoft MVP - Visual Basic
-
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
My F# solution is following :->
open Array;;
open System;;
open System.Text;;let DisplayNameFromColumnName (str:string) =
let l = str.Length in
let nb = fun n -> ( (if (n = 0) then 'x' else str.[n-1]),
(str.[n]), (if (n = l-1) then 'X' else str.[n+1]) ) in
let sb = new StringBuilder() in
let ap (c:char) = ignore(sb.Append(c)) in
let up c = Char.IsUpper(c) in
let lo c = Char.IsLower(c) in
str.ToCharArray() |> iteri ( fun n _ -> let (p,c,n) = (nb n) in
if ((lo(p) && up(c)) || (up(c) && lo(n))) then ap(' '); ap(c) );
(sb.ToString()).Trim();;If works on the "XMLValue" example too...
Tomas Petricek, C# MVP
Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..) -
My F# solution is following :->
open Array;;
open System;;
open System.Text;;let DisplayNameFromColumnName (str:string) =
let l = str.Length in
let nb = fun n -> ( (if (n = 0) then 'x' else str.[n-1]),
(str.[n]), (if (n = l-1) then 'X' else str.[n+1]) ) in
let sb = new StringBuilder() in
let ap (c:char) = ignore(sb.Append(c)) in
let up c = Char.IsUpper(c) in
let lo c = Char.IsLower(c) in
str.ToCharArray() |> iteri ( fun n _ -> let (p,c,n) = (nb n) in
if ((lo(p) && up(c)) || (up(c) && lo(n))) then ap(' '); ap(c) );
(sb.ToString()).Trim();;If works on the "XMLValue" example too...
Tomas Petricek, C# MVP
Tomasp.net | My Photos | My Blog (C# 3, LINQ, F# etc..)Tomas Petricek wrote:
My F# solution
That's OCaml, right? Can't you use pattern matching?
-
Tomas Petricek wrote:
My F# solution
That's OCaml, right? Can't you use pattern matching?
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..) -
Nope. It should only do it if the previous character was lower case. 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.
Dave Kreskowiak Microsoft MVP - Visual Basic
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
-
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...