Questions tagged [fencepost]

13 questions
308
votes
34 answers

What is the pythonic way to detect the last element in a 'for' loop?

How can I treat the last element of the input specially, when iterating with a for loop? In particular, if there is code that should only occur "between" elements (and not "after" the last one), how can I structure the code? Currently, I write code…
e.tadeu
  • 5,024
  • 2
  • 20
  • 21
3
votes
4 answers

Elegant Solutions to the Fencepost Problem (with Strings)

What I'm referring to is concatenating Strings with a certain String in the middle, such as concatenating sentences separated by a period, or parameter lists with a comma. I know you can use libraries, but sometimes these can't do what you want,…
Sean Kelleher
  • 1,952
  • 1
  • 23
  • 34
3
votes
5 answers

How could this code behave as I saw?

I have a C++ application that had a one time assertion failure that I cannot reproduce. Here is the code that failed once: unsigned int test(std::vector actionQueue) { unsigned int theLastCount = actionQueue.size() - 1; …
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
2
votes
1 answer

Fencepost conditions and portability for using of snprintf()?

Given the following code: const int size = 20; char buffer[size]; // From the Linux man page for snprintf(): // // The 'res' is the number of bytes that would be written to buffer had size been // sufficiently large excluding the terminating null…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
2
votes
2 answers

toString method for polynomials

This is a toString method for formatting the terms of a polynomial and adding them to a String. Its final output is something like "+ 2x^2 + 2x + 1" How would I go about removing the first plus sign? Thanks //toString method for returning…
user3019579
  • 71
  • 10
1
vote
1 answer

Issue with understanding fencepost handling with merge-sort

Fencepost (AKA An off-by-one error (OBOE), also commonly known as an OBOB (off-by-one bug). Given an array, I would generally iterate through index 0 to array.length() (Half-open interval). I've noticed that certain versions of merge-sort require…
worker_bee
  • 451
  • 4
  • 11
1
vote
3 answers

Why am I having a logical error with my homework?

I'm a junior student in IT. I am facing a problem with the output of the program. The idea of the program is that I should use functions to read an array of 10 elements, then get the average of the elements, then get the the max and min. I've got…
kristian
1
vote
2 answers

Intro Java Fencepost/While loops Conitional Tests

I've been trying to answer this problem all night, but I think my brain is just too fried from my midterms to answer it correctly. So the question exactly is [quoted]: Write a method highLow that accepts an integer as a parameter and returns…
alexcarll
  • 193
  • 1
  • 1
  • 9
1
vote
1 answer

Getting round the fencepost principle in a WPF ItemsControl

I've got an ItemsControl with an item DataTemplate that includes a Margin value of 0,0,0,20 to add a 20-pixel gap between each item. The problem is, when this control is scrolling, there's a spare 20px after the final item caused by this margin…
thecoop
  • 45,220
  • 19
  • 132
  • 189
0
votes
3 answers

Remove un-wanted delimiters from concatenation function google script

I am trying to concatenate two columns delimited by , and post back the results to a third column I am getting: colA colb concatenated aa bb ww ww aa bb, ww ww mm m, qq ,qq zz oo zz oo,…
xyz
  • 2,253
  • 10
  • 46
  • 68
0
votes
1 answer

why did not fill with zeros

Allocated array for 10000 bits = 1250 bytes(10000/8): mov edi, 1250 call malloc tested the pointer: cmp rax, 0 jz .error ; error handling at label down the code memory was allocated: (gdb) p/x $rax $3 = 0x6030c0 attempted to fill that allocated…
Bulat M.
  • 680
  • 9
  • 25
0
votes
1 answer

Final loop does not iterate, even though debugger shows test is true

The last for loop in this class is the culprit. Where i write the mode words to the newly created array. The for loop will not iterate a final time, even though the eclipse debugger shows the value i as being lesser than (tokens.length-2). Perhaps…
Mr. A
  • 37
  • 4
0
votes
2 answers

For or While loop skipping an input

Possible Duplicate: Scanner issue when using nextLine after nextInt I am creating a client program that needs to read both a String and an integer from my server. Depending on what integer it receives it adds some labels to the GUI. So far, my…
Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364