@serra only a unit test suite. If you buy into the whole concept it will cause you to create that kind of code. Don't forget the refactor step.
@pry@niu.moe I invite you to participate #toyprogrammingchallenge
@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.
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 awesome. try to imclude #toyprogrammingchallenge in posts about these
@Absinthe That sounds very fair. I'll submit am example using a `deque` later today!
Finished answer (Python)
@masterofthetiger@theres.life @zingbretsen
That works. Simpler and much more elegant looking code than my craziness. I am not sure about the relative efficiency between yours and mine, I think you might have more multiplications but you also solve the side case of "don't use division"
@zingbretsen personally, I don't care. The "freebies" are ones I have found that were given as job interview exercises (so far).
Personally, I am working on buffing up my personal python skills, so I am trying to import as little as possible. So far I have needed only the random import for setting up data.
But you need to decide what you want to get out of these challenges. If importing libraries works for you I don't see anything much wrong with it. Unless the challenge is about the functionality that you want to import. For example. if the challenge was to write a bubblesort, obviously importing someone else's bubblesort library would be pretty much cheating.
Okay, here is another freebie :) I will put in a real one before the end of the weekend.
Basically, you are given a list from which you need to create a new list where each element is the product of all the "OTHER" elements.
I found a few interesting corner cases.
I challenge you to give it a try!
Read the challenge here:
https://git.qoto.org/Absinthe/productnot/blob/master/README.md
My attempt is checked into that repo as well.
I just found an error in my implementation, I wonder if you might want to test yours.
Here is the issue. Let's say your list of numbers is [2, 5, 8, 3, 7, 9] and your K=4 So, in my case I take K-2 to get 2 and search for it in the list and find it. But it is not a second 2 just the only 2. I believe I would return True, and it should be false. No one ever said the numbers in the table were unique, so in theory there could be 2 2's or just one.
I will be able to fix one way that I did it, but I am not sure I will still be able to pull off the 1 line solution :)
@namark that's pretty cool. But without understanding all the predicates and such it was hard to follow what the code was doing. But it seemed that when you did A + B = K that it didn't know anything about the list, so when it went through the member call, I was wondering what it might be trying to test
@namark maybe i will try it from sources this weekend.
@namark I really do wish I could run the code :(
I usually try not to involve I/O in the problems as that can just add an unnecessary layer of complexity to most things.
So, let me ask, the line that says
A + B = K ... does that generate a whole bunch of things like (1 + K-1) (2 + K-2) (3 + K-3) and so on?
If that is the case, what does it feel like if your list of numbers are large? Like 1,234,567,890?
Here's another freebie!! I signed up for a programming problem of the day. I got my first one today, rated "easy".
Here's the description of the problem.
https://git.qoto.org/Absinthe/sum-of-number-in-list/blob/master/README.md
My code to solve it, though not clean and delicious, is in that same repo, but certainly give it a crack before peeking. :)
I get you. Remember that was a freebie :) I do like to frame them in interesting story lines if possible. However, it is a base algorithm that one should be able to pull out of one's head. Not sure a great reason to write one, other than the exercise itself. Watching something you knock out in 10 minutes be able to sort the heck out of stuff is kind of cool. And as you print out the passes it is cool to see the values bubble through.
@freemo if that is too easy, then start with them in randomized order. :)
Here's one I found for you :) I was looking through some and it seemed like it might be too much to ask for a fresh beginner, but I will be giving it a try, though I haven't quite yet.
The number 1 2 3 4 5 6 7 8 9 can have either the operator "+" or "-" placed between them, as well if no operator is placed then the adjoining numbers go together such as 1 2 would become "12" or 1 2 3 would become "123" and so forth. The goal is to find the combinations that would make the line calculate up to 100 exactly.
Here is an example: 12 + 3 - 4 + 5 + 67 + 8 + 9 = 100
There are more to be found.
The green faerie