Find the biggest and the deepest

Where's the biggest item in a list?

We are given a list of integers and are to find the index of the first occurrence of the largest number in the list.

We are going to have to look at every number in the list. It therefore seems best to make a single pass through the list, keeping track of the largest value found so far, and its index. And that's the solution I have submitted.

I thought about solutions involving sorting the list, but that's overkill for such a simple requirement.

What's the deepest path leading to all the members of a list?

We are given a list of absolute Linux file paths and asked to determine the deepest path to the directory that contains all of them.

Let's start with the first item in the list - say /a/b/c/d/xx. We can immediately see that the longest possible answer is /a/b/c/d/, because any longer one would not be common to the first item.

So let's go down the list looking to see if they all start with /a/b/c/d/. As soon as we find one that doesn't, lets start again, stripping the deepest directory from our initial guess, which in our case leaves us with /a/b/c/.

Eventually we'll find a path that is common to all the items in the list, though that may simply be /.

So this week's tasks are relatively easy and solved in a few lines of code, but both bear thinking about.



Comments

Popular posts from this blog

Illuminating the Ulam

How to find palindromes quickly

Numbers in words and Cardano triplets