Save string in C preprocessor - preprocessor

We have following macros:
#define START(x) [...]
#define FOO(x, a, b, c) [...]
#define BAR(x, a, b, c) [...]
#define END(x) [...]
We have piece of code:
START(foobar)
FOO (foobar, 1, 2, 3)
FOO (foobar, 0.1, 0.2, 0.3)
BAR (foobar, 2, 3, 4)
END (foobar)
Can we do something to let START store foobar somewhere? To xform previous into:
START(foobar)
FOO (1, 2, 3)
FOO (0.1, 0.2, 0.3)
BAR (2, 3, 4)
END ()
(something like #define FOO(x) #define VAR x)

What about:
definition:
#define START() [...VAR...]
#define FOO(a, b, c) [...VAR...]
#define BAR(a, b, c) [...VAR...]
#define END() [...VAR...]
usage:
#define VAR foobar
START()
FOO (1, 2, 3)
FOO (0.1, 0.2, 0.3)
BAR (2, 3, 4)
END ()
#undef VAR

Related

Is there a way in Dart using the list map method to build a list containing two (or more) items for each source list item?

In Python, this code realizes the objective:
intLst = [1, 2, 3]
f1 = lambda x: x
f2 = lambda x: x * 10
newLst = [f(x) for x in intLst for f in [f1, f2]]
print(newLst) # [1, 10, 2, 20, 3, 30]
but in Dart I was not able to do the same using an anonymous function passed to the map() List method.
You can achieve the same thing using collection for, which allows you to do the same type of things you can do with a list comprehension in python.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = [
for (var x in intLst)
for (var f in [f1, f2]) f(x),
];
print(newLst); // [1, 10, 2, 20, 3, 30]
}
The alternative would be to use expand rather than map. expand is the same as what some other languages call flatMap.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = intLst.expand((v) => [f1(v), f2(v)]).toList();
print(newLst); // [1, 10, 2, 20, 3, 30]
}
Here's another way to obtain the same result without using functions:
void main() {
List<int> intList = [1, 2, 3];
List<int> newList = [
for (var x in intList) ...[x, x * 10, x * x],
];
print(newList); // [1, 10, 1, 2, 20, 4, 3, 30, 9]
}

Newer versions of Z3 no longer keep/print all function instances

Notice in the new versions of Z3, the model only prints the arguments to the function that have a value other than the default. Is there a way to return to the old method of receiving all instances, including those that map to the default value?
Code:
import z3
config_init = z3.Function('config_init', z3.IntSort(), z3.IntSort(), z3.IntSort(), z3.IntSort())
def fooY(W, X, Y, Z):
return config_init(W, X, Y) == Z
s = z3.Solver()
s.add(fooY(1, 2, 3, 4))
s.add(fooY(2, 3, 4, 5))
s.add(fooY(1, 2, 8, 4))
s.add(fooY(2, 3, 9, 5))
print("Z3 Version", z3.get_version())
if s.check() == z3.sat:
mod = s.model()
print(mod)
else:
print('failed')
print(s.unsat_core())
Old Z3 Output
('Z3 Version', (4L, 5L, 1L, 0L))
[config_init = [(1, 2, 3) -> 4,
(2, 3, 4) -> 5,
(1, 2, 8) -> 4,
(2, 3, 9) -> 5,
else -> 4]]
New Z3 Output
Z3 Version (4, 8, 7, 0)
[config_init = [(2, 3, 4) -> 5, (2, 3, 9) -> 5, else -> 4]]

How to convert binary code to a number in Dart

Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.
Testing: [0, 0, 0, 1] ==> 1
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 0, 1] ==> 5
Testing: [1, 0, 0, 1] ==> 9
Testing: [0, 0, 1, 0] ==> 2
Testing: [0, 1, 1, 0] ==> 6
Testing: [1, 1, 1, 1] ==> 15
Testing: [1, 0, 1, 1] ==> 11
With Prime numbers I solved everything code from below
int binaryArrayToNumber(List<int> arr) {
return
(arr[0] * 8) +
(arr[1] * 4) +
(arr[2] * 2) +
(arr[3] * 1);
}
How do I make this automatic for larger numbers
A traditional approach would be to iterate over the binary digits and to perform bitshifts and ORs:
int binaryArrayToNumber(List<int> digits) {
var result = 0;
for (var digit in digits) {
result <<= 1;
result |= digit;
}
return result;
}
A more concise, less error-prone (but less efficient) approach would be to convert the list of digits to a String and then parse it:
int binaryArrayToNumber(List<int> digits) =>
int.parse(digits.join(""), radix: 2);
It's good to learn the protocols for the core data types, like List.fold.
int binaryArrayToNumber(List<int> digits) =>
digits.fold(0,(prev, cur) => 2*prev + cur);

Ruby Intro to Parallel Assignments

a = [1, 2, 3, 4]
b, c = 99, *a → b == 99, c == 1
b, *c = 99, *a → b == 99, c == [1, 2, 3, 4]
Can someone please throughly explained why in Ruby the asterisk makes the code return what it returns? I understand that the if an lvalue has an asterisk, it assigns rvalues to that lvalues. However, why does '*a' make 'c' return only the '1' value in the array and why does '*a' and '*c' cancel each other out?
In both cases, 99, *a on the right-hand side expands into the array [99, 1, 2, 3, 4]
In
b, c = 99, *a
b and c become the first two values of the array, with the rest of the array discarded.
In
b, *c = 99, *a
b becomes the first value from the array and c is assigned the rest (because of the splat on the left-hand side).
The 99, *a on the right-hand side is an example of where the square brackets around an array are optional in an assignment.
A simpler example:
a = 1, 2, 3 → a == [1, 2, 3]
Or a more explicit version of your example:
example = [99, *a] → example == [99, 1, 2, 3, 4]

How to get a list of integers from a given set of possible integers in z3?

Minimal example is the following: Given a set of possible integers [1, 2, 3] create an arbitrary list of size 5 using z3py. Duplicates are allowed.
The expected result is something like [1, 1, 1, 1, 1] or [3, 1, 2, 2, 3], etc.
How to tackle this problem and how to implement 'choosing'? Finally, I would like to find all solutions which can be done by adding additional constraints as explained in link. Any help will be very appreciated.
The following should work:
from z3 import *
def choose(elts, acceptable):
s = Solver()
s.add(And([Or([x == v for v in acceptable]) for x in Ints(elts)]))
models = []
while s.check() == sat:
m = s.model ()
if not m:
break
models.append(m)
block = Not(And([v() == m[v] for v in m]))
s.add(block)
return models
print choose('a b c d e', [1, 2, 3])

Resources