6 7 □ 9 7 □ 9 5 □ 9 9 □ 6 0 □ 5 0 □ 5 5 □ 3 7 □ 7 0 □ 6 1
We can fill the squares above with + and/or − , how many distinct results could we get?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Wow, that is some clever use of
itertools
Can you briefly explain what the code is doing? This seems an advance use of Python.
Log in to reply
I think the most worth-explaining part in my code is the last line, so I will only explain this line.
Let's tear it into parts:
At the last part there is a
product(*b)
.
Referring to line 4,
b
is a two-layer array:
[[1],[1,-1],...,[1,-1]]
.
Each
1
,
−
1
represents a
+
or
−
filled into the box.
The first is not
[1,-1]
because we cannot fill a
−
in front of
6
7
.
The
*
'flattens' the array and so
product(*b)
means
product([1],[1,-1],...,[1,-1]
.
product
is a itertool producing an iterator which gives all elements of the Cartesian product of its argument, for example
(1,1,-1,-1,1,1,1,-1,-1,1)
.
So the
i
fed into the
map(mul,a,i)
are tuples of length
1
0
with
1
s and
−
1
s.
map(func,a,b)
will take one from
a
, one from
b
, apply
func
on them and put it into the
map
object until the iterator is exhausted.
mul
is the multiplication function.
So for example,
map(mul,[67,97,95,99,60,50,55,37,70,61],(1,1,-1,-1,1,1,1,-1,-1,1))
will produce a
map
object (which is an iterator) containing
(67,97,-95,-99,60,50,55,-37,-70,61)
.
sum
takes an iterator and return its sum (duh), in this case,
6
7
+
9
7
−
9
5
−
9
9
+
6
0
+
5
0
+
5
5
−
3
7
−
7
0
+
6
1
=
8
9
now
sum(map(mul,a,i))for i in product(*b)
is a iterator with all possible results (with repetition) in it.
set(iterator)
turns the iterator into a set, which does not have repetition.
len
, when applied to a
set
, measures the size of the
set
, which is the number of all possible results without repetition, which is the answer we want ;)
Binary manipulation and use of python's
exec
function.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Ruby
A = %w(67 97 95 99 60 50 55 37 70 61).map{|l|l.to_i}
$a = []
def recAdd(l, sum)
if l == 10
$a << sum
return
end
recAdd(l + 1, sum + A[l])
recAdd(l + 1, sum - A[l])
end
recAdd(1, 67)
$a.uniq!
p $a.length
We iterate through every binary string of length 9. If bit i is 1, then the i -th column is + , otherwise − . How can we generate all binary string of length 9? Simply loop through every integers from 0 to 2 9 − 1 , which binary representation are 0 0 0 0 0 0 0 0 0 and 1 1 1 1 1 1 1 1 1 respectively.
set
is used to avoid duplicates.
Here is my code in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Relevant: bit manipulation hacks
What's wrong with my code?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
I was using the same approach as you, that is makin 1=+1 and 0=-1 and then multiplying it with the numbers.
Log in to reply
Please cleanup your code and briefly describe what the code is doing. It's hard for us to view a code without any comments.
Problem Loading...
Note Loading...
Set Loading...
Here is my code in python:
The answer is 337.