0

How can i get the freq of consecutive numbers in a list in Netlogo?

For example, if my list is:

list = [1 1 1 0 0 1 1 0 1 1 0 0 0 ]

Then the output should look as follows:

output = [3 2 2 1 2 3]
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • What have you tried so far and what is its output. – Dragonthoughts Jul 03 '18 at 11:00
  • 1
    Please note that *StackOverflow* is not a code writing website. Please provide a minimal code of what you have tried so far, details [here](https://stackoverflow.com/help/mcve), so that we may help/ assist you. – Zac Jul 03 '18 at 11:12
  • thanku for your reply but i am not getting any idea how to start the code therefor i asked ! –  Jul 03 '18 at 11:33
  • In a situation like this where you don't have any idea how to even get started, a good approach is to make the problem simpler; solve that simpler problem; and then try to extend the solution to work for the full problem. (You might do multiple rounds of this, starting with a *very* simple problem, then gradually get more complex.) If at any point you get truly stuck, you'll have a question to ask that isn't just "write my code for me" — you can show the code for your last successful solution and explain the difficulty you had in generalizing it any further. – Seth Tisue Jul 09 '18 at 23:35

2 Answers2

3

I have decided that this is a job for recursion:

to-report count-consecutive-items [ xs ]
  report count-consecutive-items-loop [] nobody xs
end

to-report count-consecutive-items-loop [ counts-so-far last-item remaining-items ]
  report ifelse-value (empty? remaining-items) [
    ; no more items to count, return the counts as they are
    counts-so-far
  ] [
    (count-consecutive-items-loop
      (ifelse-value (first remaining-items = last-item) [
        ; this is the same item as the last,
        ifelse-value (empty? counts-so-far) [
          ; if our list of counts is empty, start a new one
          [1]
        ] [
          ; add one to the current count and keep going
          replace-item (length counts-so-far - 1) counts-so-far (1 + last counts-so-far)          
        ]
      ] [
        ; this is an item we haven't seen before: start a new count
        lput 1 counts-so-far
      ])
      (first remaining-items)
      (but-first remaining-items)
    )
  ]
end

to test
  let result count-consecutive-items [1 1 1 0 0 1 1 0 1 1 0 0 0]
  print result
  print result = [3 2 2 1 2 3]
end

I'm sure that someone else can come up with a nice imperative version that will be much easier to understand than this, but you can consider this as a pedagogical exercise: if you manage to understand this code, it will help you on your way to NetLogo enlightenment.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Nicolas Payette
  • 14,847
  • 1
  • 27
  • 37
2
to-report countRuns [#lst]
  if 0 = length #lst [report #lst]
  let val first #lst
  let _ct 1
  let cts []
  foreach butfirst #lst [? ->
    ifelse ? = val [
      set _ct (1 + _ct)
    ][
      set cts lput _ct cts
       set val ?
     set _ct 1
    ]
  ]
  report lput _ct cts
end
Alan
  • 9,410
  • 15
  • 20