4 Public Domain, WTFNMFPL Public Licence
6 from pprint
import pformat
as pfmt
8 from pyscipopt
import Model
29 sdic = {0:
"even", 1:
"odd"}
34 Prints if a value is even/odd/neither per each value in a example list
36 This example is made for newcomers and motivated by:
37 - modulus is unsupported for pyscipopt.scip.Variable and int
38 - variables are non-integer by default
39 Based on this: #172#issuecomment-394644046
42 number: value which parity is checked
45 sval: 1 if number is odd, 0 if number is even, -1 if neither
51 assert number == int(round(number))
60 x = m.addVar(
"x", vtype=
"I", lb=
None, ub=
None)
61 n = m.addVar(
"n", vtype=
"I", lb=
None)
62 s = m.addVar(
"s", vtype=
"B")
68 m.addCons(x == number)
71 m.addCons(s == x - 2 * n)
75 assert m.getStatus() ==
"optimal"
76 boolmod = m.getVal(s) == m.getVal(x) % 2
79 print(
"%*s: %d" % (fmtlen, v, m.getVal(v)))
80 print(
"%*d%%2 == %d?" % (fmtlen, m.getVal(x), m.getVal(s)))
81 print(
"%*s" % (fmtlen, boolmod))
86 print(
"%*d is %s" % (fmtlen, xval, sstr))
87 except (AssertionError, TypeError):
88 print(
"%*s is neither even nor odd!" % (fmtlen, number.__repr__()))
96 if __name__ ==
"__main__":
98 If positional arguments are given:
99 the parity check is performed on each of them
101 the parity check is performed on each of the default example values
104 from ast
import literal_eval
as leval
109 values = sys.argv[1:]
112 values = example_values
114 fmtlen = max([len(fmt)
for fmt
in pfmt(values, width=1).split(
'\n')])
118 except (ValueError, SyntaxError):