• Post Reply Bookmark Topic Watch Topic
  • New Topic

Way to write without using indicator variable  RSS feed

 
Greenhorn
Posts: 23
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I learned this technique from a TA in college, it is called "using indicator variable" and the name pretty much says the essence of this technique.

the program below is coding bat exercise.i solved it

the question itself:
Return the sum of the numbers in the array, except ignore
sections of numbers starting with a 6 and extending to
the next 7 (every 6 will be followed by at least one 7).
Return 0 for no numbers.






Question:
1.is there other technique to avoid using "indicator variable" trick?
2.I suspect it is not "design patterns " that explains these little tricks. but I need to know all those little things.

what is the part of software development that describes these tricks/techniques to newbies? how is it called?
3. what books are there that give you list of those little techniques? or maybe tutorials? anything would be great! recommendations?

P.S. i would not be able to solve this problem another way , if i did not know indicator variable thing! So i want to know other ways of solving this problem!
 
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have to recognize a 6,...,6,7 subsequence in a sequence then you will essentially need to maintain some internal state.
Whether you call it "indicator variable" or not.

Formally you could avoid using an explicit "indicator variable" but I do not see much point in it.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erjan Kenjegalee wrote:1.is there other technique to avoid using "indicator variable" trick?

Erjan,

1. Please don't write in all bold. It comes across as shouting!!! and it's not very pleasant to read.

2. The answer to your question is: Yes there is.
Have a look at this:however, simplest is usually best, so I wouldn't advise using it unless you have a very good reason.

It's also not the only way of doing it. See if you can think of another.

Winston
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A nice approach.

However how does this suggested solution work on a series where there are a lot of 6's but no trailing 7?
skipTo7 bumps i up to the final value, thus eliminating the 6's from the sum, even though there is no trailing 7.

Or I am missing something.



 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And never use == false or == true. They are poor style and error‑prone. What if you write = by mistake?
Winston has shown a better way to write that test.
Saying “See if you can think of another” is probably intended to imply there is another way to do it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Or I am missing something.

Well, Erjan's post did say that every 6 will be followed by at least one 7. I suppose you might need to clarify what to do if it isn't.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!