How can I solve this the right way?

Piggy-backing on @appylpye’s suggestion, we also have the built-in, enumerate()

>>> def remove_all_before(items, border):
	for i, x in enumerate(items):
		if x == border: return items[i:]
	return items[:]

>>> remove_all_before([], 0)
[]
>>> remove_all_before([7, 7, 7, 7, 7, 7, 7, 7, 7], 7)
[7, 7, 7, 7, 7, 7, 7, 7, 7]
>>> remove_all_before([range(7)] + [7], 7)
[7]
>>> 

Edited May 19 to fix errors

2 Likes

Thanks, @mtf, and all.

On a bright note, this challenge has provided us with an impetus to delve into various issues and features concerning algorithms, Python, and even Haskell. I think it’s because the problem throws us a curve ball in that when the border is not found, we need to return the full list rather than an empty one. @ionatan alluded to this previously.

3 Likes

You’re giving it too much credit.
We create verbs and nouns to combine together into increasingly more complicated things, but this is several such units combined together in a way that is too specific to be widely useful where the two individual parts would be more useful on their own, like a button that both calls a taxi and prepares a bath. As two separate buttons those would be very useful.

Implementation-wise it’s a trivial special case for the sake of it, something that would be more interesting if you could find a way to get both “cases” to converge to the same code path.

And, I especially dislike that because combining universal things is what makes programming fun and powerful, while having specific stuff like this is a dead end.

Implementation-wise, the “right thing” is achieved by asking python’s built-ins to search for that thing, and that turns out to have an ugly api. Doing anything other than that will be doing something slightly worse because python code can’t compete with builtins… Which I suppose is a common problem when comparing approaches in python, it’s not about how to solve it it’s about how to get a builtin to solve it. So maybe that should be ignored, and instead express it in whatever way is nice using python. This is a big part of why C and go and … things like that, are probably nicer educational languages, they make all things equal.

1 Like

We’ll need to invoke a different sports analogy …

curve ball --> deflated football ?

This post was edited on May 19, 2020 to remove what might be a copyrighted image related to Wikipedia: Deflategate.

3 Likes