Intersection of two strings/ sets - delphi

As coming from python I'm looking for something equivalent to this python code (sets) in delphi5:
>>> x = set("Hello")
>>> x
set(['H', 'e', 'l', 'o'])
>>> y = set("Hallo")
>>> y
set(['a', 'H', 'l', 'o'])
>>> x.intersection(y)
set(['H', 'l', 'o'])

var
a, b, c: set of byte;
begin
a := [1, 2, 3, 4];
b := [3, 4, 5, 6];
c := a*b; // c is the intersection of a and b, i.e., c = [3, 4]
But beware:
var
a, b, c: set of integer;
will not even compile; instead, you get the 'Sets may have at most 256 elements' error. Please see the documentation for more information on Delphi sets.
Update
Sorry, forgot to mention the 'obvious' (from the point of view of a Delphi programmer):
var
a, b, c: set of char;
begin
a := ['A', 'B', 'C', 'D'];
b := ['C', 'D', 'E', 'F'];
c := a*b; // c is the intersection of a and b, i.e., c = ['C', 'D']
But your chars will all be byte chars -- that is, forget about Unicode (Delphi 5 doesn't support Unicode, so in this case this isn't really a restriction)!

Related

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])

how to align lists of words using biopython pairwise2

When I run the script below, output is getting split into single chars. Any idea why? It looks like the second argument gets split into single chars.
I am trying to align the word sequences.
I will have many words hence cannot map them to letters only.
from Bio.Seq import Seq
from Bio.pairwise2 import format_alignment
fruits = ["orange","pear", "apple","pear","orange"]
fruits1 = ["pear","apple"]
from Bio import pairwise2
alignments = pairwise2.align.localms(fruits,fruits1,2,-1,-0.5,-0.1, gap_char=["-"])
for a in alignments:
print(format_alignment(*a))
Output:
['orange', 'r', 'a', 'e', 'p', 'e', 'l', 'p', 'p', 'a', 'pear', 'orange']
|||||||||
['-', 'r', 'a', 'e', 'p', 'e', 'l', 'p', 'p', 'a', '-', '-']
Score=4
You are passing a list to localms which expects a string or a Seq object, also gap_char should be a string not a list.
Try the following snippet:
import Bio.pairwise2 as pairwise2
fruits = ["orange", "pear", "apple", "pear", "orange"]
fruits1 = ["pear", "apple"]
for f0 in fruits:
for f1 in fruits1:
print('Aligning {0} and {1}'.format(f0, f1))
alignments = pairwise2.align.localms(f0, f1, 2, -1, -0.5, -0.1, gap_char="-")
for a in alignments:
print(pairwise2.format_alignment(*a))
Output
Aligning orange and pear
orange
|
pear--
Score=2
Aligning orange and apple
orange
|
-apple
Score=2
orange-
|
--apple
Score=2
Aligning pear and pear
pear
||||
pear
Score=8
[...]

Automate creation of large symbolic matrix

Can this matrix be generated in a less manual way? It's okay for 4 x 4, but I need something larger. Thanks
--> L : matrix([L11,L12,L13,L14],[L21,L22,L23,L24],[L31,L32,L33,L34],[L41,L42,L43,L44]);
(L) matrix(
[L11, L12, L13, L14],
[L21, L22, L23, L24],
[L31, L32, L33, L34],
[L41, L42, L43, L44]
)
Answer to the question and note the noun form for L in the concat function ('L)
L:genmatrix(lambda([i,j], concat('L,i,j)), 3, 3);
(L) matrix(
[L11, L12, L13],
[L21, L22, L23],
[L31, L32, L33]
)
For a diagonal matrix
R:genmatrix(lambda([i,j], if i=j then concat('R,i) else 0), 3, 3);
(R) matrix(
[R1, 0, 0],
[0, R2, 0],
[0, 0, R3]
)

Assign Taylor expansion to function

When I use Maxima to calculate the Taylor series:
f(x,y) := taylor((x+y)^3, [x, y], [2, 3], 2);
f(2,3); /* error: wrong number of arguments */
Basically I want to define a function as a expansion of (x+y)^3, which takes in x,y as parameter. How can I achieve this?
Try
(%i1) f(x,y) := ''(ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2))) $
(%i2) f(2, 3);
(%o2) 125
or
(%i1) define(f(x, y), ratdisrep(taylor(('x+'y)^3, ['x, 'y], [2, 3], 2)))$
(%i2) f(2, 3);
(%o2) 125

Resources