I know there should be a simple solution to this but I can't seem to figure it out. Suppose I have a query which returns something like:
xs:untypedAtomic("A"),
xs:untypedAtomic("B"),
xs:untypedAtomic("C")
and I have another one which returns something like:
xs:untypedAtomic("B"),
xs:untypedAtomic("B"),
xs:untypedAtomic("B"),
xs:untypedAtomic("A"),
xs:untypedAtomic("C"),
xs:untypedAtomic("A")
How do I get the number of occurences for each letter in the second table?
Use:
for $s in $vMySeq
return
($s, count(index-of($vSeq, $s)))
where $vMySeq is the result of the first query and $vSeq is the result of the second query.
A complete example:
let $vMySeq := ('A', 'B', 'C'),
$vSeq := ('B', 'B', 'B', 'A', 'C', 'A')
return
for $s in $vMySeq
return
($s, count(index-of($vSeq, $s)))
The result is:
A 2 B 3 C 1
I propose two variants, one containing a group by clause, that happens to show the wanted characteristics for the non-grouping variable $cntr (see http://www.w3.org/TR/xquery-30/#id-group-by for details on how grouping is performed):
let $seq := ("B", "B","B","A","C","A")
let $cntr := 1
for $it in $seq
group by $it
return <el>{
attribute num {count($cntr)},
$it
}</el>
My second, maybe more obvious, variant would be:
let $seq := ("B", "B","B","A","C","A")
for $v in distinct-values($seq)
return <el>{
attribute num {count($seq[. = $v])},
$v
}</el>
As my answer has been a little imprecise, here comes a minor correction inspired by Dimitre Novatchev answer.
Instead of using:
for $v in distinct-values($seq)
you may as well use
for $v in ("A", "B", "C") (: this is sequence 1 as stated in the question:)
which more closely resembles the questions two sequences, given that this very sequence contains distinct values only.
Related
im making a program and i need to combine a lot of variables, most of them strings but i have some int, by doing this
name = "#{variable1}#{variable2}"
name2 = "#{variable2}#{variable1}"
it´s a simple example with just two variables but thats the idea, what im trying to make. i am doing all the possibilities one by one, even when is more than two variables but there are many combinations. Is there an easy way to do it or i have to do it one by one?Also, do i need to write the quotation marks separately or that way is fine?
Is this what you had in mind?
variable1 = "cat"
variable2 = 9
variable3 = "lives"
arr = [variable1, variable2, variable3]
#=> ["cat", 9, "lives"]
arr.join
#=> "cat9lives"
Put all or some of these variables into an array which will produce the combinations easier.
s1 = 'a'
s2 = 'b'
s3 = 'c'
n = 8
[s1, s2, s3, n].combination(3).map(&:join)
=> ["abc", "ab8", "ac8", "bc8"]
Above example assumes that you will pick any of 3 variables from the array and calculate the combinations. You may want to adjust that number to meet your needs.
The whole idea of programming is not doing all possibilities one by one. "there are many combinations": they look like permutations to me. If that is the case:
var1 = "aa"
var2 = "bb"
var3 = 2
res = [var1, var2, var3].permutation.map{|perm| perm.join}
p res #=> ["aabb2", "aa2bb", "bbaa2", "bb2aa", "2aabb", "2bbaa"]
I realize this is very basic, but I could not work this out from Prolog tutorials, so I hope someone here could help me solve my problem.
I have a term which is true if one of several conditions applies:
answer -->
Var,
a([Att1],[Var1]),
a([Att2],[Var2]),
a([Att3],[Var3]),
{
[one, two, three] = [Att1, Att2, Att3] -> Var1 = Var; % first condition
[one, three, two] = [Att1, Att2, Att3] -> Var1 = Var; % second condition
[two, one, three] = [Att1, Att2, Att3] -> Var2 = Var; % third condition
[three, one, two] = [Att1, Att2, Att3] -> Var2 = Var; % fourth condition
}
All Attributes and Variables come with a fixed value, and I want to offer "answer", if any of the conditions in the "{}" section are fulfilled - but for some reason, it doesn't work. The thing is, if I just check for one of the conditions, say, the first, it works as expected. But I don't want to copy/paste the rule 4 times because I didn't get a logical "or" to work properly.
Just to put it into words, in case I coded something completely different, the first condition is meant to say: check if Att1 is equal to one and Att2 is equal to two and Att3 is equal to three. If that is the case, also confirm that Var1 is equal to the value in Var. If not, check if any of the other conditions can be resolved.
edit: Turns out I just had a ';' too much in the code.
In an xquery expression, I have obtained a set of values within a for-expression, and one value is in a separate variable.
Now, I want to subtract the single value from first value of the list, and then subtract consecutive members of the list from each other-- and in the resulting set of difference values, I want to obtain the min/max values...
The query upto now looks like this--
let $value1:= 1998
let $rows_citations:=
$doc//div[#id="patent_citations"]
/div[#id="patent_citations_v"]
/table[#class="rel_patent"]
/tbody/tr[1]
/following-sibling::tr
for $pos in $rows_citations/position()
let $date2_c := customfn:dateconverter1($rows_citations[$pos]/td[3])
Now the subtraction I want is between first value of date2_c and value 1, and after that between consecutive members of date2_c... And from the resulting list I want the min/max values... How do I go about doing this?
I am esp. confused about creating a new list variable that stores all the differences, esp. when we are already inside a for loop, and are iterating over each value of a list (via variable date2_c)
I. This XQuery 3.0 query (which is also a pure XPath 3.0 expression):
let $pVal := 1,
$vList := (2,4,7,11,16),
$vList2 := ($pVal, subsequence($vList, 1, count($vList)-1)),
$vSubtactedList :=
map-pairs(function($m as xs:integer, $n as xs:integer) as xs:integer
{
$m - $n
},
$vList,
$vList2
)
return
(min($vSubtactedList), max($vSubtactedList))
produces the wanted result the minimum and maximum values from the list of subtractions:
1 5
II. XQuery 1.0 solution:
let $pVal := 1,
$vList := (2,4,7,11,16),
$vList2 := ($pVal, subsequence($vList, 1, count($vList)-1)),
$vSubtactedList :=
for $i in 1 to count($vList)
return
$vList[$i] - $vList2[$i]
return
(min($vSubtactedList), max($vSubtactedList))
This again produces the same correct result:
1 5
I don't know how to make array from range 11AA to 99ZZ. Is it possible to do it in Ruby?
I want this array:
['11AA', '11AB', '11AC',..., '99ZX', '99ZY', '99ZZ']
Try the following:
('11AA'..'99ZZ').to_a
The pseudo code would be:
for i '1' to '9'
for j '1' to '9'
for k 'A' to 'Z'
for l 'A' to 'Z'
insert_in_array(i+j+k+l);
of course the values are stored as strings, with double "" not''.This is a pseudo code, it can be applied in any programming language.
I have a list like this:
a a . a
b . . a
a . a .
c . a .
or in list format
[['a', 'a', '.', 'a'],
['b', '.', '.', 'a'],
['a', '.', 'a', '.'],
['c', '.', 'a', '.']]
and I want to merge it into [['a','b','c'],['a'],['a'],['a']], or
a,b,c a a a
so that when two consecutive rows share the same letter at any of the four columns, their elements will be merged non-redundantly. I can do it by pairwise comparisons, but wonder if there are formal algorithms to do this?
Thanks.
You didn't specify the language but you can create a HashMap / HashTable for each column and populate it with the column values. (Your key and value will be the same thing.) Populating a HashMap means you cannot have duplicate keys so you will end uo with a list of unique values in each collection. Then pull out the values from each hashMap into an array, or arrays. If the periods in your sample data are actually periods you will have to ignore them as you loop through the array otherwise you will get them as output.
Take a look at Python dictionaries.
the pseudo code for this solution (Python will look similar ;-)
Create a list of dictionaries (list length = # of columns)
Loop over columns
Loop over rows
Insert data into appropriate dictionary
*
Loop over list of dictionaries
Loop over dictionary values
Create new set of arrays with unique values