[BCLUG] About Array wrapping with modulus

BCLUG admin at bclug.ca
Sun Sep 3 19:39:14 EDT 2023


Steve Litt wrote on 2023-09-03 12:09:

> Hi all,

Hi Steve,

Welcome to the list!


> About Array wrapping with modulus.
> 
> Nice! Until now, I've usually used a linked list that wraps around, 
> which is waaaaay overkill compared to a modulus.

I wish I could claim credit for "inventing" the modulus technique - it's
a beauty.


> I think I have an idea for an improvement to the solution at 
> https://bclug.ca/counter-wrapping-with-modulus/  . My thought is
> that hard coding the ID into each array element is redundant, with
> the disadvantage that if you decide to add, delete or reorder, you
> need to also change the IDs.

Thanks for the feedback.

The `id` was supplied with the problem parameters, probably can be
expected to come from a database on an API somewhere.



> Thanks for this. I just now very strongly recorded your array
> modulus wrapping in my head, so I'll never again be tempted to use a
> linked list, unless there's some other reason for using a linked
> list.

Nice.  I encountered a similar-ish problem a week later and someone else
suggested I use "my famous modulus technique" since I was drawing a bit
of a blank.

So, I guess I also need to strongly commit it to memory, 'cause my
memory failed me the second time.  "Duh."



I'm planning to add a couple more write-ups to that site. One that I'm
inordinately proud of - a "debounce" function that I spent a *long* time
considering and just compounding my confusion 'til it came time to
implement in a mock interview, where I talked through it, wrote up the
code, and got it to work on the *1st* submission attempt!


> Given a function fn and a time in milliseconds t, return a debounced
> version of that function.
> 
> A debounced function is a function whose execution is delayed by t
> milliseconds and whose execution is cancelled if it is called again
> within that window of time. The debounced function should also
> receive the passed parameters.


So, debounce() takes 2 parameters: a function and a delay, and it 
returns a function that the user calls.

Seen in things like typing into a search field, where the search happens 
on each keystroke, except if the user types fast, batch the keystrokes 
such that when, say, a 100ms delay in typing happens, *then* a search 
query executes.

https://leetcode.com/problems/debounce/




Next up was a complete failure on my part, to be recorded for posterity.

 > Valid Parentheses:
 >
 > Given a string s containing just the characters '(', ')', '{', '}',
 > '[' and ']', determine if the input string is valid.
 >
 > An input string is valid if:
 >
 >    Open brackets must be closed by the same type of brackets.
 >
 >    Open brackets must be closed in the correct order.
 >
 >    Every close bracket has a corresponding open bracket of the same type.

First attempt was with a beautifully-crafted regular expression. Which 
failed, as it didn't test nesting.

Then some complex mess of code trying to iterate and test nesting.

Then recursion.

Then recursion with a sliding "start" point, to avoid too much 
recursion. Mostly worked, but yuck.


Finally, a "stack" method (the *obvious* solution), which still failed 
when zero closing brackets existed in input, plus I missed a couple nice 
code style choices.

https://leetcode.com/problems/valid-parentheses/



Fun with code, even when failing!


rb


More information about the Discuss mailing list