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]]
Pythonic monotonic
41–50 of 64 posts
Re: Pythonic monotonic
#42Gave 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…
Re: Pythonic monotonic
#43Choosing either implementation as "more Pythonic" than the other feels to me like choosing tabs over spaces, or vice versa: https://www.youtube.com/watch?v=V7PLxL8jIl8 i.e., it's a matter of personal preference -- except that, as gizmo686 points out below, as of Python 3 tabs are now, officially, considered more Pythonic.
Re: Pythonic monotonic
#44Earlier quoted context omitted.
Fails for [1, 1, 2, 3, 3, 2, 1, 1] -> [[1, 1, 2, 3, 3], [1, 2], [1, 2], [1]]
Thanks. I was inconsistent in my use of >= and >. Making them consistent has fixed this edge case.
[3, 3, 2, 2, 1, 1] -> [[3, 3], [2, 2], [1, 1]]
Here's my solution https://news.ycombinator.com/item?id=28170735Re: Pythonic monotonic
#45 def mono_runs_pythonic_two(seq):
curr = []
result = []
prev = None
is_decreasing = None
for elem in seq:
if prev is not None:
if (
elem Re: Pythonic monotonic
#46 def monotonic(it):
run = []
for this in it:
if not run:
pass
elif run[0]
It's short, is a single function, doesn't rely on fancy features (unless you consider subscripting with `-1` or using `yield` fancy). It will work on arbitrary iterators, the input doesn't need to be a sequence. It passes all of eesmith's tests. It uses memory proportional to the longest run in the sequence.The trick I've applied is that we don't explicitly keep track of whether we're increasing, decreasing or undecided. Instead we check the run in progress.
Re: Pythonic monotonic
#47How now APL?
MonoRuns ← {
d ←
Run online: https://mlochbaum.github.io/BQN/try.html#code=TW9ub1J1bnMg4o...Re: Pythonic monotonic
#48Here's my solution: def monotonic(it): run = [] for this in it: if not run: pass elif run[0] It's short, is a single function, doesn't rely on fancy features (unless you consider subscripting with `-1` or using `yield` fancy). It will work on arbitrary iterators, the input doesn't need to be a sequence. It passes all of eesmith's tests. It uses memory proportional to the longest run in the sequence. The trick I've ap…
Re: Pythonic monotonic
#49Earlier quoted context omitted.
Thanks. I was inconsistent in my use of >= and >. Making them consistent has fixed this edge case.
Also fails for [3, 3, 2, 2, 1, 1] -> [[3, 3], [2, 2], [1, 1]] Here's my solution https://news.ycombinator.com/item?id=28170735
Re: Pythonic monotonic
#50How now APL?
Here's my BQN solution. It uses shift functions « and Group ⊔ (links below) where APL would use windowed reduction (e.g. 2≠/d) and partitioned enclose (r←b⊂𝕩) so it's slightly different. MonoRuns ← { d ← Run online: https://mlochbaum.github.io/BQN/try.html#code=TW9ub1J1bnMg4o... https://mlochbaum.github.io/BQN/doc/shift.html https://mlochbaum.github.io/BQN/doc/group.html
"⟨3, 2, 1⟩" generates "⟨ 2 1 3 ⟩". I expected "⟨1, 2, 3⟩".