I want to split up a map into an array of maps. For example, if there is a map with 25 key/value pairs. I want an array of maps with no more than 10 elements in each map.
How would I do this in groovy?
I have a solution which I am not excited about, is there better groovy version:
static def splitMap(m, count){
if (!m) return
def keys = m.keySet().toList()
def result = []
def num = Math.ceil(m?.size() / count)
(1..num).each {
def min = (it - 1) * count
def max = it * count > keys.size() ? keys.size() - 1 : it * count - 1
result[it - 1] = [:]
keys[min..max].each {k ->
result[it - 1][k] = m[k]
}
}
result
}
m is the map. Count is the max number of elements within the map.
Adapting my answer to this question on partitioning a List, I came up with this method:
Map.metaClass.partition = { size ->
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
So if you take this map:
def origMap = [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f']
All of the following assertions pass :-)
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
Or, as a Category (to avoid having to add anything to the metaClass of Map:
class MapPartition {
static List partition( Map delegate, int size ) {
def rslt = delegate.inject( [ [:] ] ) { ret, elem ->
( ret.last() << elem ).size() >= size ? ret << [:] : ret
}
rslt.last() ? rslt : rslt[ 0..-2 ]
}
}
Then, where you need this functionality, you can simply use the Category like so:
use( MapPartition ) {
assert [ [1:'a'], [2:'b'], [3:'c'], [4:'d'], [5:'e'], [6:'f'] ] == origMap.partition( 1 )
assert [ [1:'a', 2:'b'], [3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 2 )
assert [ [1:'a', 2:'b', 3:'c'], [4:'d', 5:'e', 6:'f'] ] == origMap.partition( 3 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d'], [5:'e', 6:'f'] ] == origMap.partition( 4 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e'], [6:'f'] ] == origMap.partition( 5 )
assert [ [1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f'] ] == origMap.partition( 6 )
}
Related
I am trying to simulate what happens to vultures if they randomly stumble upon a carcass that has been poisoned by poachers. The poisoned carcass needs to be random. I also need to plot the deaths, so do i need to set up a dead/poisoned state in order to plot the deaths, do I need to code a to die section. Im not sure. TIA
to go
; repeat 10800 [
ask vultures
[
if state = "searching" [ search-carcasses ]
if state = "following" [follow-leaders-find-carcasses]
if state = "searching"
[ if random-float 1 < ( 1 / 360 )
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ] ] ]
if state != "feeding"
[ fd 0.009 ]
if state = "leader" [set time-descending time-descending + 1]
if mycarcass != 0
[ if distance mycarcass <= 0.009
[ set state "feeding"
ask mycarcass
[ set occupied? "yes" ] ] ] if state = "feeding" [
ask mycarcass
[if poisoned? = "yes"
[set state "poisoned"] ] ] if state = "poisoned" [die] ] tick ; ]
I have illustrated the parallelogram spanned by two vectors, and would like to shade in the area of that parallelogram, which I tried to do like so:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )
However, this parallelogram colors over the arrows that I have already drawn, like so:
and I'd like it to be in the background. Is there a way to introduce a new object into the scene so that it is logically behind any of the existing ones, as if I had drawn it first, so that it would look like:
You can use the set_z_index method to set the z_index property of the parallelogram to a value less than that of the arrows.
Here I have set it to a lower value than that of v1:
poly.set_z_index(v1.z_index - 1)
Alternatively you can manipulate the z_index property directly:
poly.z_index = v1.z_index - 1
Using the set_z_index method would be the cleaner solution.
Complete code:
from manim import *
import numpy as np
class DrawParallelogram( Scene ):
def construct( self ):
o = np.array( [ 0, -2, 0 ] )
p1 = np.array( [ 3, 1, 0 ] ) # u
p2 = np.array( [ 1, 3, 0 ] ) # v
op1 = o + p1
op2 = o + p2
op3 = o + p1 + p2
v1 = Arrow( start = o, end = op1, buff = 0, color = RED ) # u
v2 = Arrow( start = o, end = op2, buff = 0, color = YELLOW ) # v
v1p = Arrow( start = op2, end = op3, buff = 0, color = RED ) # u'
v2p = Arrow( start = op1, end = op3, buff = 0, color = YELLOW ) # v'
parallelogram = [ o, op1, op3, op2 ]
poly = Polygon( *parallelogram, color = PURPLE, fill_opacity = 0.5 )
# Set the z-index
poly.set_z_index(v1.z_index - 1)
self.play( AnimationGroup( Write( v1 ), Write( v2 ), Write( v1p ), Write( v2p ) ) )
self.wait( )
self.play( Write( poly ) )
I was on the site rise4fun a few weeks ago and they had a python code that converted a sudoku puzzle input file to z3. I checked again today and the file is gone, and was wondering if anyone had this code or could explain to me how to implement it. Thanks!!
# 9x9 matrix of integer variables
X = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(9) ]
for i in range(9) ]
# each cell contains a value in {1, ..., 9}
cells_c = [ And(1 <= X[i][j], X[i][j] <= 9)
for i in range(9) for j in range(9) ]
# each row contains a digit at most once
rows_c = [ Distinct(X[i]) for i in range(9) ]
# each column contains a digit at most once
cols_c = [ Distinct([ X[i][j] for i in range(9) ])
for j in range(9) ]
# each 3x3 square contains a digit at most once
sq_c = [ Distinct([ X[3*i0 + i][3*j0 + j]
for i in range(3) for j in range(3) ])
for i0 in range(3) for j0 in range(3) ]
sudoku_c = cells_c + rows_c + cols_c + sq_c
# sudoku instance, we use '0' for empty cells
instance = ((5,3,0,0,7,0,0,0,0),
(6,0,0,1,9,5,0,0,0),
(0,9,8,0,0,0,0,6,0),
(8,0,0,0,6,0,0,0,3),
(4,0,0,8,0,3,0,0,1),
(7,0,0,0,2,0,0,0,6),
(0,6,0,0,0,0,2,8,0),
(0,0,0,4,1,9,0,0,5),
(0,0,0,0,8,0,0,7,9))
instance_c = [ If(instance[i][j] == 0,
True,
X[i][j] == instance[i][j])
for i in range(9) for j in range(9) ]
s = Solver()
s.add(sudoku_c + instance_c)
if s.check() == sat:
m = s.model()
r = [ [ m.evaluate(X[i][j]) for j in range(9) ]
for i in range(9) ]
print_matrix(r)
else:
print "failed to solve"
I am trying to multiply 2 matrices of 2*2 order. One of the matrix contains an unknown parameter "k1". I want to check a satisfiable solution that for which value of k1. The product of two matrices will be equal to the third one.
Note: I dont want to convert the multiplication into a linear relation or set of equation I want to manipulate it as matrices.
Here is where I am stuck.
k1 = Int ('k1')
x = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(2) ]
for i in range(2) ]
a =((1,k1),(3,4))
b =((1,1),(1,1))
c= ((3,3),(7,7))
s = Solver()
s.add(a[0][1]>0)
s.add(a*b==c)
if s.check() == sat:
m = s.model()
r = [ [ m.evaluate(x[i][j]) for j in range(2) ]
for i in range(2) ]
print_matrix(r)
else:
print "failed to solve"
Any way Out?
One possible solution is
k1 = Int ('k1')
x = [ [ Int("x_%s_%s" % (i+1, j+1)) for j in range(2) ]
for i in range(2) ]
a =((1,k1),(3,4))
b =((1,1),(1,1))
c= ((3,3),(7,7))
s = Solver()
eq1= a[0][1]>0
eq2 =[[sum(a[i][k]*b[k][j] for k in range(2)) == c[i][j] for i in range(2) ]
for j in range(2) ]
s.add(eq1)
s.add(eq2[0][0])
s.add(eq2[0][1])
s.add(eq2[1][0])
s.add(eq2[1][1])
print s
print s.check()
m = s.model()
print m
and the corresponding output is
[k1 > 0, 1 + k1*1 == 3, True, 1 + k1*1 == 3, True]
sat
[k1 = 2]
Please run this example online here
Certain problem in transport Phenomena is solved using the following code:
T_max, T_0, S, R, k, I, k_e, L, R, E, a = Reals('T_max T_0 S R k I k_e L R E a')
k = a*k_e*T_0
I = k_e*E/L
S = (I**2)/k_e
eq = T_0 + S* R**2/(4*k)
print eq
equations = [
T_max == eq,
]
print "Temperature equations:"
print equations
problem = [
R == 2, L == 5000,
T_0 == 20 + 273,
T_max == 30 + 273, k_e == 1,
a == 2.23*10**(-8), E > 0
]
print "Problem:"
print problem
print "Solution:"
solve(equations + problem)
using this code online we obtain
This output gives the correct answer but there are two issues in the code: a) the expresion named "eq" is not fully simplified and then it is necessary to give an arbitrary value for k_e . My question is: How to simplify the expression "eq" in such way that k_e be eliminated from "eq"?
Other example: To determine the radius of a tube
Code:
def inte(n,a,b):
return (b**(n+1))/(n+1)-(a**(n+1))/(n+1)
P_0, P_1, L, R, mu, q, C = Reals('P_0 P_1 L R mu q C')
k = (P_0 - P_1)/(2*mu*L)
equations = [0 == -k*inte(1,0,R) +C,
q == 2*3.1416*(-(k/2)*inte(3,0,R) + C*inte(1,0,R))]
print "Fluid equations:"
print equations
problem = [
L == 50.02/100, mu == (4.03*10**(-5)),
P_0 == 4.829*10**5, P_1==0,
q == 2.997*10**(-3), R >0
]
print "Problem:"
print problem
print "Solution:"
solve(equations + problem)
Output:
Fluid equations:
[-((P_0 - P_1)/(2·mu·L))·(R2/2 - 0) + C = 0, q =
3927/625·
(-(((P_0 - P_1)/(2·mu·L))/2)·(R4/4 - 0) + C·(R2/2 - 0))]
Problem:
[L = 2501/5000, mu = 403/10000000, P_0 = 482900, P_1 = 0, q = 2997/1000000, R > 0]
Solution:
[R = 0.0007512843?,
q = 2997/1000000,
P_1 = 0,
P_0 = 482900,
mu = 403/10000000,
L = 2501/5000,
C = 3380.3149444289?]