Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Friday Programming Quiz [modified]

Friday Programming Quiz [modified]

Scheduled Pinned Locked Moved The Lounge
delphihtmldatabasedebuggingxml
44 Posts 17 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Rama Krishna Vavilala

    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 -> ESOP

    In 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

    S Offline
    S Offline
    Shog9 0
    wrote on last edited by
    #24

    Don't feel like firing up the compiler, but felt like being old-school...

    /*
    * colName points to column name, cannot be NULL
    * output points to buffer suitable for holding display name, cannot be NULL
    * maxOutputLen is the maximum number of characters that can be stored in output,
    * excluding the inevitable NULL terminator. output must be maxOutputLen+1 chars in length
    */
    char* DisplayNameFromColumnName(const char* colName, char* output, int maxOutputLen)
    {
    int inPos = 0;
    int outPos = 0;
    while ( colName[inPos] && outPos < maxOutputLen)
    {
    if ( isupper(colName[inPos]) && colName[inPos+1] && !isupper(colName[inPos+1]) )
    {
    output[outPos++] = ' ';
    if ( outPos == maxOutputLen )
    break;
    }
    output[outPos++] = colName[inPos++];
    }
    output[outPos] = '\0';

    return output;
    }

    1 Reply Last reply
    0
    • R Rama Krishna Vavilala

      I agree that there is no 100% fool proof way esp. for cases like IUseHTMLALot or XMLValue. But this is for fun.


      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

      M Offline
      M Offline
      Matt Gerrans
      wrote on last edited by
      #25

      IUseHTMLALot is problematic, but XMLValue seems okay. If you have any sequence of caps followed by lowercase, then you break before the last cap, right?

      Matt Gerrans

      1 Reply Last reply
      0
      • R Rama Krishna Vavilala

        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

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #26

        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.

        1 Reply Last reply
        0
        • Steve EcholsS Steve Echols

          Seeing all these solutions reminds me I really need to learn regex. :^)


          - S 50 cups of coffee and you know it's on!

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #27

          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.

          Steve EcholsS 1 Reply Last reply
          0
          • R Rama Krishna Vavilala

            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 -> ESOP

            In 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

            S Offline
            S Offline
            Shog9 0
            wrote on last edited by
            #28

            Rama Krishna Vavilala wrote:

            Removed XMLValue -> XML Value

            Bah! I could've been the second respondent if it weren't for that! Now i have to settle for 5th, 10th, 11th...

            1 Reply Last reply
            0
            • S Shog9 0

              Christian Graus wrote:

              IUseHTMLALot

              I hate those. It may be incorrect, but i'll still write it as IUseHtmlALot.

              1 Offline
              1 Offline
              123 0
              wrote on last edited by
              #29

              Shog9 wrote:

              i'll still write it as IUseHtmlALot.

              I use HTML very little. Is that a problem?

              1 Reply Last reply
              0
              • S Shog9 0

                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.

                Steve EcholsS Offline
                Steve EcholsS Offline
                Steve Echols
                wrote on last edited by
                #30

                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!

                • S
                  50 cups of coffee and you know it's on!
                  Code, follow, or get out of the way.
                P 1 Reply Last reply
                0
                • R Rama Krishna Vavilala

                  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 -> ESOP

                  In 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

                  M Offline
                  M Offline
                  Marc Clifton
                  wrote on last edited by
                  #31

                  return lookupUserFriendlyName[columnName];

                  ;P Marc

                  Thyme In The Country

                  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

                  P 1 Reply Last reply
                  0
                  • R Rama Krishna Vavilala

                    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

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #32

                    Microsoft doesn't get to decide on the proper capitalization of the technologies created by others.

                    1 Reply Last reply
                    0
                    • Steve EcholsS Steve Echols

                      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!

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #33

                      Too many dialects.

                      1 Reply Last reply
                      0
                      • M Marc Clifton

                        return lookupUserFriendlyName[columnName];

                        ;P Marc

                        Thyme In The Country

                        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

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #34

                        DoWhatImThinking ( Data )

                        1 Reply Last reply
                        0
                        • K Kacee Giger

                          Does this handle the BodyHTML -> Body HTML case--I believe your solution would give "Body H T M L"?

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #35

                          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

                          M 1 Reply Last reply
                          0
                          • M Matt Gerrans

                            Well, all we need to do is just compile his solution with the Plain English compiler and try it out! Grande?

                            Matt Gerrans

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #36

                            :laugh:!

                            Dave Kreskowiak Microsoft MVP - Visual Basic

                            1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              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 -> ESOP

                              In 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

                              T Offline
                              T Offline
                              Tomas Petricek
                              wrote on last edited by
                              #37

                              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..)

                              N 1 Reply Last reply
                              0
                              • T Tomas Petricek

                                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..)

                                N Offline
                                N Offline
                                Nemanja Trifunovic
                                wrote on last edited by
                                #38

                                Tomas Petricek wrote:

                                My F# solution

                                That's OCaml, right? Can't you use pattern matching?


                                Programming Blog utf8-cpp

                                T S 2 Replies Last reply
                                0
                                • N Nemanja Trifunovic

                                  Tomas Petricek wrote:

                                  My F# solution

                                  That's OCaml, right? Can't you use pattern matching?


                                  Programming Blog utf8-cpp

                                  T Offline
                                  T Offline
                                  Tomas Petricek
                                  wrote on last edited by
                                  #39

                                  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..)

                                  S 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    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

                                    M Offline
                                    M Offline
                                    Matt Gerrans
                                    wrote on last edited by
                                    #40

                                    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

                                    D 1 Reply Last reply
                                    0
                                    • M 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

                                      D Offline
                                      D Offline
                                      Dave Kreskowiak
                                      wrote on last edited by
                                      #41

                                      Done! I rewrote it in Plain Portugese, though. Brazil is such an ignored market!

                                      Dave Kreskowiak Microsoft MVP - Visual Basic

                                      1 Reply Last reply
                                      0
                                      • N Nemanja Trifunovic

                                        Tomas Petricek wrote:

                                        My F# solution

                                        That's OCaml, right? Can't you use pattern matching?


                                        Programming Blog utf8-cpp

                                        S Offline
                                        S Offline
                                        Stuart Dootson
                                        wrote on last edited by
                                        #42

                                        Yes - see my Haskell solution :cool:

                                        1 Reply Last reply
                                        0
                                        • R Rama Krishna Vavilala

                                          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 -> ESOP

                                          In 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

                                          S Offline
                                          S Offline
                                          Stuart Dootson
                                          wrote on last edited by
                                          #43

                                          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.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups