I found a corner case on the find sum in list that I overlooked. If the list is [1,2,5] and the number is 4 I will return true because I am looking through the whole list for K-element and will of course find element. I was so happy to get a single line that worked, I am wondering if I can fix it and still keep it one line?
https://git.qoto.org/Absinthe/sum-of-number-in-list
I know count may work better than any, or some other logic to handle the special case of K-element == element
@Absinthe #toyprogrammingchallenge Still not at a computer, but I think this would work (and be slightly more efficient):
print(any(the_num - item in the_list[n+1:] for n, item in enumerate(the_list)))
i.e., only check elements to the right of the current element
Was also considering list.remove(item) to remove the item under consideration, but that operates in place (without returning the modified list) and I think the slicing solution is clearer. Also, it's generally not good to modify a list that you're looping over.
Does the line above make sense?
@Absinthe exactly, if you already visited an item, you don't need to check it again.
List slicing is very useful. It is inclusive of the first index and exclusive of the second index. e.g.,
[0,1,2,3,4][1:3] would return [1,2]
Leaving off the first index will start you at the beginning of the list.
Leaving off the second index will include everything through the end of the list.
@zingbretsen I like the list slicing it just isn't a first class tool for me yet. Sure if I use it a few more times it will stick
@zingbretsen it is the direction I was trying to get to, because I don't think I need to check any one I have already visited a second time. Those list semantics with the : throw me, I am still getting to know them, but that feels right, I will try it when I get home.