Show newer

Good morning and here comes the latest challenge.

git.qoto.org/Absinthe/onetonin

The numbers 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

Would like maybe something other than eval().

re: A Python solution 

@billstclair @zingbretsen I have access to gcl but it looks like I need more to make this function

@billstclair @freemo

I thought I sent this one to @freemo privately. :) Live and learn. This is going to be the one for this week.

I am going to try to run this, but just walking the code is showing
me more Lisp than I currently know. Although, I would have expected the Lisp to look shorter than my python .....
It does work with gcl from my command line :)

re: A Python solution 

@billstclair @zingbretsen Hey bill please remember to include hashtag in your responses so people can find them by searching for it.

Can I run this in gcl or emacs or do I need more code to have the list and output?

A Python solution 

@zingbretsen am pretty sure there is no technical reason not to use division, just to make the problem more complicated. I am getting ready to put up a new one. But feel free to keep playing with this one. I got a new one today, but it seems a little "specific" because they give a python code starting point, and want you to serialize and deserialize a binary tree. I will likely not put that one up as a freebie. Especially since I want to get the new real on up.

A Python solution 

@zingbretsen This works...

print("One Liner no division")
print([reduce(lambda a, b: a *b,
[ y for x, y in enumerate(random_list) if x != idx])
for idx, _ in enumerate(random_list)])

It's a one liner, except I am sticking to 80 columns so I split it a few times.. still calling it a one liner :)

A Python solution 

@zingbretsen What about this... as a one liner:

[reduce(lambda a, b: a *b, [ y for x, y in enumerate(list) if x != idx]) for idx, _ in enumerate(list)]

A Python solution 

@zingbretsen right... bbiab, errand time

A Python solution 

@zingbretsen The only problem I have with all of these no-division solutions is that you have to do the multiplication in all cases. I wonder if there is a mathematical trick to somehow solve without having to calculate the whole product each time, like the division solution. @freemo ... any ideas?

@zingbretsen yeah, I looked it up. Works nice, perhaps I will try it with the list comprehension that I suggested.

A Python solution 

@zingbretsen Okay, this is less of a criticism as it is a question of understanding:
You used the deque to rotate and slice the list for the multiplication, Could you have also used list comprehension something like this:

current_index = the one we are working on
[ value for idx, value in enumerate(list) if idx != current_idx]

I know you wanted to use the deque

A Python solution 

@zingbretsen now you are stretching me. I have never seen rotate() but I can intuit what it does. I also have not used lamda's. But what is happening in the reduce() call? I assume the lamda takes 2 values how is that acting on the list? Or is the lamda being passed into the reduce call ... time to read about reduce :)

A Python solution 

@Absinthe Here is my no-division solution:

```
#!/usr/bin/env python
from collections import deque
from functools import reduce
from random import randint

def get_product_and_rotate(d):
"""Gets product of all but the first items in the queue"""
prod = reduce(lambda a, b: a * b, list(d)[1:])
d.rotate(-1) # Rotates left so that the next
return prod

if __name__ == "__main__":
n_items = randint(2, 10)
input_list = [randint(0, 9) for _ in range(n_items)]
print(input_list)
product_queue = deque(input_list)
print([get_product_and_rotate(product_queue) for _ in input_list])
```

Uses `reduce` for the multiplication, and uses a `deque` (double-ended queue) to rotate the list. I've read that using a deque is actually more efficient than just taking slices out of the list.

@namark I have not had a chance to try to compile mercury yet. I don't do much with procedural languages. I guess there is no time like the present.

@namark Thanks! It still doesn't feel intuitive yet. If I came across such a line I would have to work it out to see what it did. But hey, it does work and with all the corner cases I could think of.

@pry@niu.moe use whatever language you like.

@pry@niu.moe
If you click the hashtag it should take you to all conversations about it.

Basically I am proposing small programming problems/puzzles. People can try them or evaluate the work of others' attempts. Or however you feel you will get something out of it?

Here should be a link to the beginning:

qoto.org/@Absinthe/10280565958

Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.