Programming Puzzle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julie

    #76
    Re: Programming Puzzle

    Jerry Coffin wrote:[color=blue][color=green]
    > > Q7 Remove duplicates in array[/color]
    >
    > You can't really "remove" an element from an array, so this is poorly
    > defined. If it was a C++ vector (for example) std::sort and
    > std::unique would render it trivial, as would inserting the elements
    > into an std::set, and then copying them back out. Doing it quickly
    > while retaining the original order is a little more challenging.[/color]

    How can you _not_ remove an element from an array?

    Here is a trivial case:

    size_t count = 2;
    int * array = (int *)malloc(sizeof (int) * count);
    array[0] = 42;
    array[1] = 9000;
    array = (int *)realloc(array , sizeof(int) * (--count));

    Are you saying that an element hasn't been removed?

    Comment

    • Howard

      #77
      Re: Programming Puzzle


      "Julie" <julie@nospam.c om> wrote in message
      news:40E0431B.D 172B8FF@nospam. com...[color=blue]
      > Jerry Coffin wrote:[color=green][color=darkred]
      > > > Q7 Remove duplicates in array[/color]
      > >
      > > You can't really "remove" an element from an array, so this is poorly
      > > defined. If it was a C++ vector (for example) std::sort and
      > > std::unique would render it trivial, as would inserting the elements
      > > into an std::set, and then copying them back out. Doing it quickly
      > > while retaining the original order is a little more challenging.[/color]
      >
      > How can you _not_ remove an element from an array?
      >
      > Here is a trivial case:
      >
      > size_t count = 2;
      > int * array = (int *)malloc(sizeof (int) * count);
      > array[0] = 42;
      > array[1] = 9000;
      > array = (int *)realloc(array , sizeof(int) * (--count));
      >
      > Are you saying that an element hasn't been removed?[/color]

      Well, that's not *really* removing an element from the array, it's simply
      reallocating data storage. How would your example look if you wanted to
      remove an element from other than the last position? You'd have to write
      code to shift the other elements to the front of the array, not just resize
      the memory allocation.

      -Howard




      Comment

      • Julie

        #78
        Re: Programming Puzzle

        Howard wrote:[color=blue]
        >
        > "Julie" <julie@nospam.c om> wrote in message
        > news:40E0431B.D 172B8FF@nospam. com...[color=green]
        > > Jerry Coffin wrote:[color=darkred]
        > > > > Q7 Remove duplicates in array
        > > >
        > > > You can't really "remove" an element from an array, so this is poorly
        > > > defined. If it was a C++ vector (for example) std::sort and
        > > > std::unique would render it trivial, as would inserting the elements
        > > > into an std::set, and then copying them back out. Doing it quickly
        > > > while retaining the original order is a little more challenging.[/color]
        > >
        > > How can you _not_ remove an element from an array?
        > >
        > > Here is a trivial case:
        > >
        > > size_t count = 2;
        > > int * array = (int *)malloc(sizeof (int) * count);
        > > array[0] = 42;
        > > array[1] = 9000;
        > > array = (int *)realloc(array , sizeof(int) * (--count));
        > >
        > > Are you saying that an element hasn't been removed?[/color]
        >
        > Well, that's not *really* removing an element from the array, it's simply
        > reallocating data storage.[/color]

        Well, if that isn't removing, then I don't know what is... Reallocation is
        merely an implementation detail, net result is that an element has been
        removed.
        [color=blue]
        > How would your example look if you wanted to
        > remove an element from other than the last position? You'd have to write
        > code to shift the other elements to the front of the array, not just resize
        > the memory allocation.[/color]

        Exactly. I started w/ the trivial case of removing the last element. Removing
        any other position would merely involve calling memmove or similar to shift the
        contents down and then reallocating.

        Still sounds like removing an element from an array to me...

        Comment

        • Howard

          #79
          Re: Swapping Bullshit


          "Julie" <julie@nospam.c om> wrote in message
          news:40E040AA.E 6AC3743@nospam. com...[color=blue][color=green]
          > >[color=darkred]
          > > >Not one, but *two* ways to do it have been
          > > >shown in this thread. Of course it will break down if those variables
          > > >happen to share the same memory location, which can be the case if[/color][/color][/color]
          using[color=blue][color=green][color=darkred]
          > > >pointers and indirecting through them.[/color][/color]
          >
          > Please describe (in code) a situation where two variables share the same[/color]
          memory[color=blue]
          > location.
          >[/color]

          Just like described, using pointers. (References can also be used.)

          void pswap( int* px, int* py )
          {
          *px = *px ^ ^py;
          *py = *py ^ *px;
          *px = *px ^ *py;
          }

          ....calling code:...
          int x = 3;
          int* px = &x;
          int* py = &x;
          pswap( px, py );


          -Howard


          Comment

          • Branimir Maksimovic

            #80
            Re: Programming Puzzle

            jsfromynr@sanch arnet.in (Jatinder) wrote in message news:<22b2a6b6. 0406261016.510e 693b@posting.go ogle.com>...
            [color=blue]
            > Q10 Write a program whose printed output is an exact copy of the
            > source. Needless to say, merely echoing the actual source file is not
            > allowed.[/color]

            #include <cstdio>
            using namespace std;int main(){const char* s="#include <cstdio>%cusi ng
            namespace std;int main(){const char*
            s=%c%s%c;printf (s,10,34,s,34,1 0);}%c";printf( s,10,34,s,34,10 );}

            Greetings, Bane.

            Comment

            • Dan Pop

              #81
              Re: Swapping Bullshit

              In <40E040AA.E6AC3 743@nospam.com> Julie <julie@nospam.c om> writes:
              [color=blue][color=green]
              >> In <cbnaoq$omp$1@o ravannahka.hels inki.fi> Joona I Palaste <palaste@cc.hel sinki.fi> writes:
              >>[color=darkred]
              >> >Not one, but *two* ways to do it have been
              >> >shown in this thread. Of course it will break down if those variables
              >> >happen to share the same memory location, which can be the case if using[/color][/color][/color]
              ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^[color=blue][color=green][color=darkred]
              >> >pointers and indirecting through them.[/color][/color][/color]
              ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^[color=blue]
              >
              >Please describe (in code) a situation where two variables share the same memory
              >location.[/color]

              Since you seem to be unable to understand plain English:

              #include <stdio.h>

              void swap(int *p, int *q) { ... }

              int main()
              {
              int i = 10;
              swap(&i, &i);
              printf("%d\n", i);
              return 0;
              }

              Try this code for different implementations of the swap function, using
              a temp var and using in-place swapping. Compare the results.

              This example is trivial, but the situation can realistically arise in more
              complex algorithms.

              Dan
              --
              Dan Pop
              DESY Zeuthen, RZ group
              Email: Dan.Pop@ifh.de

              Comment

              • Julie

                #82
                Re: Swapping

                Howard wrote:[color=blue]
                >
                > "Julie" <julie@nospam.c om> wrote in message
                > news:40E040AA.E 6AC3743@nospam. com...[color=green][color=darkred]
                > > >
                > > > >Not one, but *two* ways to do it have been
                > > > >shown in this thread. Of course it will break down if those variables
                > > > >happen to share the same memory location, which can be the case if[/color][/color]
                > using[color=green][color=darkred]
                > > > >pointers and indirecting through them.[/color]
                > >
                > > Please describe (in code) a situation where two variables share the same[/color]
                > memory[color=green]
                > > location.
                > >[/color]
                >
                > Just like described, using pointers. (References can also be used.)
                >
                > void pswap( int* px, int* py )
                > {
                > *px = *px ^ ^py;
                > *py = *py ^ *px;
                > *px = *px ^ *py;
                > }
                >
                > ...calling code:...
                > int x = 3;
                > int* px = &x;
                > int* py = &x;
                > pswap( px, py );
                >
                > -Howard[/color]

                Yes, but the two variables are pointers, and they do not share the same memory
                location -- they may *point* to the same location.

                So, I still haven't seen two variables that share the same memory location. I
                think that you can probably do it w/ placement new (C++ only!), but using (C++)
                references or pointers, you can't have two variables that share the same memory
                location.

                Comment

                • Howard

                  #83
                  Re: Swapping


                  "Julie" <julie@nospam.c om> wrote in message
                  news:40E05ABD.A 75B2DAD@nospam. com...
                  [color=blue]
                  > Yes, but the two variables are pointers, and they do not share the same[/color]
                  memory[color=blue]
                  > location -- they may *point* to the same location.
                  >
                  > So, I still haven't seen two variables that share the same memory[/color]
                  location. I[color=blue]
                  > think that you can probably do it w/ placement new (C++ only!), but using[/color]
                  (C++)[color=blue]
                  > references or pointers, you can't have two variables that share the same[/color]
                  memory[color=blue]
                  > location.[/color]

                  Geez, give me a break! What I've shown exhibits *exactly* the kind of
                  problem that can happen when trying to swap two integers using the xor
                  technique. Just because someone used terminology that suggested the two
                  variables themselves had the same memory location, surely you knew what was
                  meant! The problem is when both memory locations are the same, which can
                  happen if using pointers or references. That's all that was meant, not that
                  there were two *different* variables occupying the *same* memory.
                  -Howard



                  Comment

                  • Christopher Benson-Manica

                    #84
                    Re: Programming Puzzle

                    In comp.lang.c Joona I Palaste <palaste@cc.hel sinki.fi> wrote:
                    [color=blue]
                    > job. That's all there was to it. No tests, no panel hearings, just a few
                    > hours of talk.[/color]

                    I bet they saw your postings to comp.lang.c ;)

                    --
                    Christopher Benson-Manica | I *should* know what I'm talking about - if I
                    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                    Comment

                    • Julie

                      #85
                      Re: Swapping Bullshit

                      Dan Pop wrote:[color=blue]
                      >
                      > In <40E040AA.E6AC3 743@nospam.com> Julie <julie@nospam.c om> writes:
                      >[color=green][color=darkred]
                      > >> In <cbnaoq$omp$1@o ravannahka.hels inki.fi> Joona I Palaste <palaste@cc.hel sinki.fi> writes:
                      > >>
                      > >> >Not one, but *two* ways to do it have been
                      > >> >shown in this thread. Of course it will break down if those variables
                      > >> >happen to share the same memory location, which can be the case if using[/color][/color]
                      > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^[color=green][color=darkred]
                      > >> >pointers and indirecting through them.[/color][/color]
                      > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^[color=green]
                      > >
                      > >Please describe (in code) a situation where two variables share the same memory
                      > >location.[/color]
                      >
                      > Since you seem to be unable to understand plain English:
                      >
                      > #include <stdio.h>
                      >
                      > void swap(int *p, int *q) { ... }
                      >
                      > int main()
                      > {
                      > int i = 10;
                      > swap(&i, &i);
                      > printf("%d\n", i);
                      > return 0;
                      > }
                      >
                      > Try this code for different implementations of the swap function, using
                      > a temp var and using in-place swapping. Compare the results.
                      >
                      > This example is trivial, but the situation can realistically arise in more
                      > complex algorithms.[/color]

                      No, I do not understand your version of plain English.

                      Just because you have two pointers that _point_ to the same address, this
                      doesn't mean that they (the variables here which are still the **pointers**)
                      share the same memory location.

                      Your definition of swap doesn't take to variables, it takes two addresses.
                      Even in your example in main, you are passing in the same pointer.

                      Show me a case of two separate variables that share the same memory location,
                      without using placement new. You can even use your version of 'plain English'.

                      Here is the point in my version of 'plain English': you can't have two
                      variables that share the same memory address (excluding placement new). So, if
                      the precondition for swap is that it operates on two variables, then it will
                      always work provided the precondition is met.

                      Comment

                      • Tak-Shing Chan

                        #86
                        Re: Swapping Bullshit

                        On 28 Jun 2004, Dan Pop wrote:
                        [color=blue][color=green]
                        >>JKop <NULL@null.null > scribbled the following
                        >>on comp.lang.c:[color=darkred]
                        >>> Am I the only person here that thinks it's complete bullshit to think you
                        >>> can swap the values of two variables without a temporary variable?! It[/color][/color][/color]
                        ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^[color=blue][color=green][color=darkred]
                        >>> [snip][/color][/color]
                        > And the only good reason I can imagine is the failure to allocate memory
                        > for a temporary object (some objects can be greater than others, some[/color]
                        ^^^^^^^^^^^^^^^ ^^^

                        Your answer is completely irrelevant. ;-)

                        Tak-Shing

                        Comment

                        • Howard

                          #87
                          Re: Swapping Bullshit


                          "Julie" <julie@nospam.c om> wrote in message
                          news:40E06200.F C41D3DF@nospam. com...[color=blue]
                          > Here is the point in my version of 'plain English': you can't have two
                          > variables that share the same memory address (excluding placement new).[/color]
                          So, if[color=blue]
                          > the precondition for swap is that it operates on two variables, then it[/color]
                          will[color=blue]
                          > always work provided the precondition is met.[/color]

                          So who ever sid the procondition was that you were swapping two non-pointer,
                          non-reference variables? If you're writing the swap as a function, you have
                          to use references or pointers, or else you'll only be swapping local copies.
                          That's what makes this an important consideration, because those pointer or
                          references parameters could be referring to the same memory location. The
                          swap function need to contain a check to handle that specific case.

                          -Howard





                          Comment

                          • Ioannis Vranos

                            #88
                            Re: Programming Puzzle

                            Julie wrote:
                            [color=blue]
                            > How can you _not_ remove an element from an array?
                            >
                            > Here is a trivial case:[/color]


                            Since you use the malloc() family and this is cross posted to clc, I
                            assume you use C.


                            [color=blue]
                            >
                            > size_t count = 2;
                            > int * array = (int *)malloc(sizeof (int) * count);[/color]


                            In C this casting is not needed.


                            [color=blue]
                            > array[0] = 42;
                            > array[1] = 9000;
                            > array = (int *)realloc(array , sizeof(int) * (--count));[/color]


                            In C this casting is not needed.






                            Regards,

                            Ioannis Vranos

                            Comment

                            • Tak-Shing Chan

                              #89
                              Re: Swapping Bullshit

                              On Mon, 28 Jun 2004, Tak-Shing Chan wrote:
                              [color=blue]
                              > On 28 Jun 2004, Dan Pop wrote:
                              >[color=green][color=darkred]
                              > >>JKop <NULL@null.null > scribbled the following
                              > >>on comp.lang.c:
                              > >>> Am I the only person here that thinks it's complete bullshit to think you
                              > >>> can swap the values of two variables without a temporary variable?! It[/color][/color]
                              > ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^[color=green][color=darkred]
                              > >>> [snip][/color]
                              > > And the only good reason I can imagine is the failure to allocate memory
                              > > for a temporary object (some objects can be greater than others, some[/color]
                              > ^^^^^^^^^^^^^^^ ^^^
                              >
                              > Your answer is completely irrelevant. ;-)[/color]

                              To comp.lang.c++: sorry I didn't notice the cross-post at
                              the beginning. You can safely ignore my out-of-context reply.
                              What my post really amounts to is a (rather subtle) critique of
                              Dan Pop's lack of clarity in the replied-to post. [It took me
                              three to four readings to get his logic!]

                              Tak-Shing

                              Comment

                              • Tak-Shing Chan

                                #90
                                Re: Programming Puzzle

                                On Mon, 28 Jun 2004, Christopher Benson-Manica wrote:
                                [color=blue]
                                > In comp.lang.c Joona I Palaste <palaste@cc.hel sinki.fi> wrote:
                                >[color=green]
                                >> job. That's all there was to it. No tests, no panel hearings, just a few
                                >> hours of talk.[/color]
                                >
                                > I bet they saw your postings to comp.lang.c ;)[/color]

                                I thought he is a Java programmer? ;-)

                                Tak-Shing

                                Comment

                                Working...