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
@zingbretsen care to take a shot at a fix for my one-line solution in this one?
@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?
@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.
@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