# Function for recognize quantity of unique combinations browsing

• posted
1

Hello pythonista community. Can someone adwise me formula for recognize quantity of unique combinations when you count to some max digit with sign "-" (minus)? Answer can't be under zero.

Example1:
sign = '-'
max = 2
2-2
2-1
1-1

• we have only three unique combinations.

Example2:
max = 2
1+1

• we have only one unique example.

I already have formula for two signs "+-": unic_col=max**2

But i need formula for '+' and '-'
And maybe someone know for:
+-×
+-/
+-×/

So, i need function like that:

def uniques_col(operators, max):
if operators == '+-':
return max**2
elif operators == '+':
return .....
elif operators == '-':
return .....
elif operators == '+-×':
return .....
......

• posted
1

@bennr01, just read an article on functional programming in Python, so one step further:

``````import itertools
import operator

OPERATORS = {
"-": operator.sub,
"*": operator.mul,
"×": operator.mul,
}

def uniques_col(operators, max_value):
return filter(
lambda c: 0 <= OPERATORS[c[1]](c[0], c[2]) <= max_value,
itertools.product(
range(1, max_value+1),
operators,
range(1, max_value+1)
)
)

print(list(uniques_col('+-', 2)))``````

• posted
0

@lyubomyr83, not an answer to your question, but maybe you can use these brute-force versions to check the formulas you come up with?

• posted
1

@bennr01, with one more fancy function:

``````import itertools

def uniques_col(operators, max_value):
possibles = lambda: range(1, max_value+1)
return filter(
lambda c: 0 <= eval(''.join(map(str, c))) <= max_value,
itertools.product(possibles(), operators, possibles())
)

print(list(uniques_col('+-', 2)))``````

• posted
0

@cvp, thank you all for help!!!
With your function i receive right col for '-', but not for '+':

def uniques_col(operators, max):
n = 0
if operators == '+':
for i in range(1,max):
for j in range(i,max-i+1):
n += 1
print(i,operators,j,'=',i+j)
elif operators == '-':
for i in range(1,max+1):
for j in range(1,i+1):
n += 1
print(i,operators,j,'=',i-j)
return n

while True:
op = input('operator\n')
max = int(input('max\n'))
print(uniques_col(op,max))

operator-
max4
1 - 1 = 0
2 - 1 = 1
2 - 2 = 0
3 - 1 = 2
3 - 2 = 1
3 - 3 = 0
4 - 1 = 3
4 - 2 = 2
4 - 3 = 1
4 - 4 = 0
10
operator+
max4
1 + 1 = 2
1 + 2 = 3
1 + 3 = 4
2 + 2 = 4
4

Where is 1+3 and 2+1, so for '+' i need receive 6 unique examples.

• posted
0

@lyubomyr83 said:

Where is 1+3 and 2+1,

Ok, I did believe that 1+2 is the same as 2+1, thus I did not generate it.
As I told you, I was not sure to correctly understand 😀

Thus it is better to use the other script (of @bennr01 and @mikael ) because it tries all combinations.

• posted
0

@lyubomyr83

Or Change into

``````    if operators == '+':
for i in range(1,max):
for j in range(1,max-i+1):
``````

• posted
0

i think you would want to check 1 to max for both numbers (consider /, max/max=1)

• posted
0

@JonB if you do that, you have to check and skip cases where i+j>max
With « my » formula, no check is needed

• posted
0

• posted
0

@ccc I'm always positively surprised by the number of libraries in Python

• posted
1

``````from itertools import product

def uniques_col(ops, maximum):
number_range = range(1, maximum+1)
numbers = list(map(str, number_range))
return filter(
lambda c: 0 <= eval(''.join(c)) <= maximum,
product(numbers, ops, numbers)
)

uniques = uniques_col('+-/', 2)

print(*[
f"{i+1:4}: {''.join(c)}"
for i, c in enumerate(uniques)],
sep='\n'
)``````

• posted
1

numbers = [str(i + 1) for i in range(maximum)]

Read “What’s new in Python 3” for a discussion on avoiding map(), reduce(), filter().

• posted
0

@ccc, you take the prize for conciseness, and readability is not bad either.

I was debating the value of separating the range of numbers (problem domain issue) and the conversion to strings (a technical implementation detail).

• posted
0

@mikael and @ccc I think that I'll stop to believe I can program in Python 😢

• posted
0

@ccc said:

What’s new in Python 3

For clarity, What’s new in Python 3 does not advice against using `map` and `filter` as such, but against using `list(map(...))` when a list comprehension can be used instead.

Thus, as already said, your amendment makes a lot of sense, but it does not automatically follow that we would change the filter into a comprehension, if we want to leave it up to the user of the function to decide whether to ”collapse” the iterator or not.

• posted
0

@cvp, ha ha. There’s a huge difference and swiftly diminishing returns between ”getting to results” and ”getting to perfect”. While this kind of noodling is fun (for me), your recent track record of real results speaks for itself.

• posted
0

@mikael you're too kind 😳

• posted
0

@cvp thank's a lot. It working for '+', '-' and '+-':

``````def uniques_col(operators, max):
col = 0
if operators == '+':
for i in range(1,max):
for j in range(1,max-i+1):
col += 1
elif operators == '-':
for i in range(1,max+1):
for j in range(1,i+1):
col += 1
elif operators == '+-':
col = max**2

return col

while True:
op = input('operator\n')
max = int(input('max\n'))
print(uniques_col(op,max))
``````
``````And maybe you know uniques col for:
+-×
+-/
+-×/
/×
``````

• posted
0

@lyubomyr83, this code:

``````from itertools import product

def uniques_col(ops, maximum):
numbers = [str(i+1) for i in range(maximum)]
return filter(
lambda c: 0 <= eval(''.join(c)) <= maximum,
product(numbers, ops, numbers)
)

ops_list = ['+-*', '+-/', '+-/*', '/*']
to_limit = 20

for ops in ops_list:
print('-'*20)
print('ops', ops)
for maximum in range(1, to_limit+1):
print(maximum, len(list(
uniques_col(ops, maximum))))
``````

... gives series up to 20 for each set of operations. For `+-/`, I can deduct that the formula for possibilities is `2*max*max`. For the others, not as easy, need to dig out a maths book or two.

• posted
0

@lyubomyr83 please try this and tell us if non integer result is allowed for division

``````def uniques_col(operators, max):
col = 0
if len(operators) > 1:
for op in operators:
col += uniques_col(op, max)
return col
if operators == '+':
for i in range(1,max):
for j in range(1,max-i+1):
print(i,operators,j,i+j)
col += 1
elif operators == '-':
for i in range(1,max+1):
for j in range(1,i+1):
print(i,operators,j,i-j)
col += 1
elif operators == 'x':
for i in range(1,max+1):
for j in range(1,1+int(max/i)):
print(i,operators,j,i*j)
col += 1

return col

while True:
op = input('operator\n')
max = int(input('max\n'))
``````

Internal error.

Oops! Looks like something went wrong!