Live data from Hacker News

Pythonic monotonic

nedbatchelder.com

31–40 of 64 posts

Re: Pythonic monotonic

#31

FWIW, here is my solution. I think it is grokable and does not rely on one knowing complex python, I think even someone without Python knowledge would understand what is going on: def order_values(value, prev): """ Compare value and prev, returns, -1, 0, 1 depending on the relative ordering. """ return int(value > prev) - int(prev > value) def monotonic_increasing(seq): """ Given a sequence of elements, detect the in…

My updated version is more like a state machine. Your function and mine give the same answers both with the manual test cases and with a cross-comparison using random inputs:

    def monotonic_direct(seq):
        prev_value = None
        prev_dir = 0  # 0 for increasing or decreasing, 1 for increasing, -1 for decreasing
        group = []
        #print("Run", seq)
        for value in seq:
            #print(prev_dir, prev_value, value)
            if not group:
                # Can only get here with the first element
                prev_value = value
                group.append(value)
                continue
            
            if prev_dir == 0:
                # Undecided order
                if prev_value 

Re: Pythonic monotonic

#32

The first case is clearer to me than the second, by quite away. Iter, next? Opaque testing of [-1] values? Adjusting a few identifier names, the first actually describes what's going on. Eg., rename `Monotonic()` to `CurrentLtLast()`

`iter` seems more approachable than `__call__` to me FWIW.

Re: Pythonic monotonic

#33
Gave it a try. I enjoy writing readable code but it's hard to judge one's own work. What do you all think?

  def monotonic(arr):
      """
      Turn an unordered list into a list of lists of monotone
      sequences. All sequences are converted to increasing.

      Start reading the list in chunks of two to decide if we're increasing or not.
      """

      if len(arr) = sub_arr[0]

      for m in gen_arr:
          if m >= sub_arr[-1] and increasing \
          or m = m) if n else ([m], None)

      new_arr.append(sorted(sub_arr))

      return new_arr
Edit: thanks for the feedback. Changing `I think you could make it even more readable by traversing the list twice and keeping track of the runs.

Re: Pythonic monotonic

#35
post #33

Gave it a try. I enjoy writing readable code but it's hard to judge one's own work. What do you all think? def monotonic(arr): """ Turn an unordered list into a list of lists of monotone sequences. All sequences are converted to increasing. Start reading the list in chunks of two to decide if we're increasing or not. """ if len(arr) = sub_arr[0] for m in gen_arr: if m >= sub_arr[-1] and increasing \ or m = m) if n el…

This is the easiest one to read, thanks for that. If the intention of posting it was an invitation for review, here's a quick one.

Making the first entry a special case is probably a good idea, but it must also take into account when the first sub_arr is also the only:

  >>> monotonic([1, 2])
  []
There's also the fact that equality is considered a valid increase both not a valid decrease. One might expect that either it is valid as both or none. This is not necessarily wrong but may give surprising results:

  >>> monotonic([2, 1, 1, 1, 1, 2])
  [[1, 2], [1, 1], [1, 1], [1, 2]]
Otherwise a readable and straightforward implementation of a somewhat underspecified problem.

Re: Pythonic monotonic

#36
post #30

Earlier quoted context omitted.

https://flinhong.github.io/images/201607/tabs-spaces.jpg

LOL. That cartoon is particularly funny because it is rather insightful about the nature of such disagreements.

My own personal take on this is that I don't care whether it's tabs vs spaces as long as it's consistent. But if you mix both (especially in Python code), then you should be dead by now.

By the way, one of the popular Python packages pyaml uses tabs (consistently though). Not a good example, but still.

Re: Pythonic monotonic

#37
post #33

Gave it a try. I enjoy writing readable code but it's hard to judge one's own work. What do you all think? def monotonic(arr): """ Turn an unordered list into a list of lists of monotone sequences. All sequences are converted to increasing. Start reading the list in chunks of two to decide if we're increasing or not. """ if len(arr) = sub_arr[0] for m in gen_arr: if m >= sub_arr[-1] and increasing \ or m = m) if n el…

Fails for

  [1, 1, 2, 3, 3, 2, 1, 1] -> [[1, 1, 2, 3, 3], [1, 2], [1, 2], [1]]

Re: Pythonic monotonic

#40
I see so many examples with comments explaining “what” - given that you have to read code to truly understand what it does, comments must be maintained in addition to code, and comments can be either or both misleading and outright incorrect, isn’t it generally considered best practice not to write huge block comments at the top of a function implementation? Given these points, those kinds of comments are really just additional text the reader needs to parse through (to make sure they aren’t missing a meaningful “why” portion of the comment.)

Without circular reasoning (“we do it this way because it’s done this way”), can anyone provide some context here? Much appreciated!

Post reply on HN