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. Embarrassing code admission of the day (or why C.S. is good for you)

Embarrassing code admission of the day (or why C.S. is good for you)

Scheduled Pinned Locked Moved The Lounge
questioncsharpandroidcomdesign
63 Posts 29 Posters 0 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.
  • E Offline
    E Offline
    Ennis Ray Lynch Jr
    wrote on last edited by
    #1

    Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

    //init the list and fill it
    List fakeList = new List();
    //Find the subtle bug
    while (fakeList.Count > 0) {
    double temp = fakeList[0];
    //..do something
    fakeList.RemoveAt(0);
    }

    Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

    K _ M X P 22 Replies Last reply
    0
    • E Ennis Ray Lynch Jr

      Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

      //init the list and fill it
      List fakeList = new List();
      //Find the subtle bug
      while (fakeList.Count > 0) {
      double temp = fakeList[0];
      //..do something
      fakeList.RemoveAt(0);
      }

      Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      Ignoring the fact that nothing is added to the List, this is better of as a Queue<double>? [Edit] The while loop should also throw, as the list is being changed in it. This was just rubbish - see next post.

      Sort of a cross between Lawrence of Arabia and Dilbert.[^]
      -Or-
      A Dead ringer for Kate Winslett[^]

      D 1 Reply Last reply
      0
      • K Keith Barrow

        Ignoring the fact that nothing is added to the List, this is better of as a Queue<double>? [Edit] The while loop should also throw, as the list is being changed in it. This was just rubbish - see next post.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]
        -Or-
        A Dead ringer for Kate Winslett[^]

        D Offline
        D Offline
        Dario Solera
        wrote on last edited by
        #3

        No, it's not a foreach.

        If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe, but not a personality. [Charlie Brooker] ScrewTurn Wiki, Software Localization Tools & Services and My Blog

        K 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

          //init the list and fill it
          List fakeList = new List();
          //Find the subtle bug
          while (fakeList.Count > 0) {
          double temp = fakeList[0];
          //..do something
          fakeList.RemoveAt(0);
          }

          Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          _ Offline
          _ Offline
          _Zorro_
          wrote on last edited by
          #4

          I guess you were expecting a queue or a stack maybe?

          1 Reply Last reply
          0
          • D Dario Solera

            No, it's not a foreach.

            If you truly believe you need to pick a mobile phone that "says something" about your personality, don't bother. You don't have a personality. A mental illness, maybe, but not a personality. [Charlie Brooker] ScrewTurn Wiki, Software Localization Tools & Services and My Blog

            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            Quite right!

            Sort of a cross between Lawrence of Arabia and Dilbert.[^]
            -Or-
            A Dead ringer for Kate Winslett[^]

            1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

              //init the list and fill it
              List fakeList = new List();
              //Find the subtle bug
              while (fakeList.Count > 0) {
              double temp = fakeList[0];
              //..do something
              fakeList.RemoveAt(0);
              }

              Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

              Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

              M Offline
              M Offline
              Mladen Jankovic
              wrote on last edited by
              #6

              fakeList[0] fakeList[0] fakeList[0] fakeList[0] Indexer on a list? [edit] For those who are asking - I'm suggesting that it might be a problem since accessing Nth element in the list has O(n) complexity.

              _ P G 4 Replies Last reply
              0
              • M Mladen Jankovic

                fakeList[0] fakeList[0] fakeList[0] fakeList[0] Indexer on a list? [edit] For those who are asking - I'm suggesting that it might be a problem since accessing Nth element in the list has O(n) complexity.

                _ Offline
                _ Offline
                _Zorro_
                wrote on last edited by
                #7

                What's wrong with that?

                M 1 Reply Last reply
                0
                • M Mladen Jankovic

                  fakeList[0] fakeList[0] fakeList[0] fakeList[0] Indexer on a list? [edit] For those who are asking - I'm suggesting that it might be a problem since accessing Nth element in the list has O(n) complexity.

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  It's allowed. How else would you get the value at position i?

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  M 1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                    //init the list and fill it
                    List fakeList = new List();
                    //Find the subtle bug
                    while (fakeList.Count > 0) {
                    double temp = fakeList[0];
                    //..do something
                    fakeList.RemoveAt(0);
                    }

                    Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                    X Offline
                    X Offline
                    Xiangyang Liu
                    wrote on last edited by
                    #9

                    Cannot find any bug.

                    My Younger Son & His "PET"

                    P 1 Reply Last reply
                    0
                    • _ _Zorro_

                      What's wrong with that?

                      M Offline
                      M Offline
                      Mladen Jankovic
                      wrote on last edited by
                      #10

                      O(n)?

                      _ 1 Reply Last reply
                      0
                      • E Ennis Ray Lynch Jr

                        Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                        //init the list and fill it
                        List fakeList = new List();
                        //Find the subtle bug
                        while (fakeList.Count > 0) {
                        double temp = fakeList[0];
                        //..do something
                        fakeList.RemoveAt(0);
                        }

                        Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #11

                        Took me a moment or two to spot that. Couldn't really see it until I thought it through. Good catch - how did you find it? For others - what happens when you remove at 0? How is this handled in terms of resizing when you remove from the start of the list. As a comparison, remove from the last position instead (ok, it's not the same logical code, but it shows timings).

                        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                        _ M E M F 5 Replies Last reply
                        0
                        • M Mladen Jankovic

                          fakeList[0] fakeList[0] fakeList[0] fakeList[0] Indexer on a list? [edit] For those who are asking - I'm suggesting that it might be a problem since accessing Nth element in the list has O(n) complexity.

                          G Offline
                          G Offline
                          GParkings
                          wrote on last edited by
                          #12

                          yes. list being the simplest data type in which an order is applied to a set of data Though, judging by your rep scores on here you are more likely to know what you are talking about than I am, so ... am i missing something here, do we work in different languages with different concepts of 'list'? should we be using

                          list.ElementAt(0)

                          instead?

                          Pedis ex oris Quidquid latine dictum sit, altum sonatur

                          1 Reply Last reply
                          0
                          • P Pete OHanlon

                            It's allowed. How else would you get the value at position i?

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                            M Offline
                            M Offline
                            Mladen Jankovic
                            wrote on last edited by
                            #13

                            Yes it is, But also it has O(n) complexity.

                            J D 2 Replies Last reply
                            0
                            • X Xiangyang Liu

                              Cannot find any bug.

                              My Younger Son & His "PET"

                              P Offline
                              P Offline
                              Pete OHanlon
                              wrote on last edited by
                              #14

                              It's not a bug per se, it's an efficiency thing.

                              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                              1 Reply Last reply
                              0
                              • E Ennis Ray Lynch Jr

                                Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                //init the list and fill it
                                List fakeList = new List();
                                //Find the subtle bug
                                while (fakeList.Count > 0) {
                                double temp = fakeList[0];
                                //..do something
                                fakeList.RemoveAt(0);
                                }

                                Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                X Offline
                                X Offline
                                Xiangyang Liu
                                wrote on last edited by
                                #15

                                Ennis Ray Lynch, Jr. wrote:

                                Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                But that is not called a bug, is it?

                                My Younger Son & His "PET"

                                J T 2 Replies Last reply
                                0
                                • M Mladen Jankovic

                                  fakeList[0] fakeList[0] fakeList[0] fakeList[0] Indexer on a list? [edit] For those who are asking - I'm suggesting that it might be a problem since accessing Nth element in the list has O(n) complexity.

                                  P Offline
                                  P Offline
                                  Pete OHanlon
                                  wrote on last edited by
                                  #16

                                  Didn't think that deserved a 1. Corrected. As a hint, the answer has to do with which side of the list you remove the element from. How is it handled re, resizing?

                                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                  1 Reply Last reply
                                  0
                                  • E Ennis Ray Lynch Jr

                                    Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                    //init the list and fill it
                                    List fakeList = new List();
                                    //Find the subtle bug
                                    while (fakeList.Count > 0) {
                                    double temp = fakeList[0];
                                    //..do something
                                    fakeList.RemoveAt(0);
                                    }

                                    Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                    K Offline
                                    K Offline
                                    Keith Barrow
                                    wrote on last edited by
                                    #17

                                    Ah. Now you see, that is another example as to why programming is hard.

                                    Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                                    -Or-
                                    A Dead ringer for Kate Winslett[^]

                                    P R 2 Replies Last reply
                                    0
                                    • M Mladen Jankovic

                                      O(n)?

                                      _ Offline
                                      _ Offline
                                      _Zorro_
                                      wrote on last edited by
                                      #18

                                      Oh, what would be a better approach? ElementAt? I thought it would be the same...

                                      G 1 Reply Last reply
                                      0
                                      • E Ennis Ray Lynch Jr

                                        Pretend the overall logic is entirely sound. The bug below is very subtle and is not a logic bug but a design bug, to make it harder, pretend the overall logic is correct. What is the bug?

                                        //init the list and fill it
                                        List fakeList = new List();
                                        //Find the subtle bug
                                        while (fakeList.Count > 0) {
                                        double temp = fakeList[0];
                                        //..do something
                                        fakeList.RemoveAt(0);
                                        }

                                        Hint: Ok, if it is too hard. Remember what a List is in C# and then remember the specifics of that data structure from intro to programming. Edit: The data structure is correct, and the logic is technically correct but wrong. Another Hint: Run it with a populated list of 100,000 elements and check the timing. There is a particular feature of this data structure that happens with this particular code that one small change would avoid.

                                        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                                        C Offline
                                        C Offline
                                        Claude Martel Olivier
                                        wrote on last edited by
                                        #19

                                        Not sure if it's intended or not but you're going to delete the list by deleting the first item over and over?

                                        P 1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          Took me a moment or two to spot that. Couldn't really see it until I thought it through. Good catch - how did you find it? For others - what happens when you remove at 0? How is this handled in terms of resizing when you remove from the start of the list. As a comparison, remove from the last position instead (ok, it's not the same logical code, but it shows timings).

                                          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                                          _ Offline
                                          _ Offline
                                          _Zorro_
                                          wrote on last edited by
                                          #20

                                          Pete O'Hanlon wrote:

                                          what happens when you remove at 0? How is this handled in terms of resizing when you remove from the start of the list

                                          I see it now, thank's!

                                          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