Check if two sequences contain the same number in xpath - xpath

Im looking for a xpath function that compares two sequences and checks if the any of the values in sequence one is contained in sequence two.
Example:
Seq1: 5, 1, 8, 0 and seq2: 9, 4, 3 should be false
Seq1: 5, 7, 8 and seq2: 0, 2, 5 should give true

XPath 2.0 has sequences and comparison of sequences with = that does just what you ask for so doing $seq1 = $seq2 is all you need in XPath 2.0.
XPath 1.0 does not have sequences so you will need to explain in more detail how your input looks if you are asking about XPath 1.0.

http://sources.redhat.com/ml/xsl-list/2000-08/msg00355.html
eg count($set1[count(.|$set2)=count($set2)])>0

Related

Storing a specific set of numbers in ruby

How do you generate some sort of checksum of an array of 5 numbers that would distinguish a set of numbers from another?
For example:
[ 1, 2, 3, 4, 5] has the same checksum as [ 2, 3, 4, 5, 1]
I want to generate millions of 5 digit combinations and compare them against a predetermined set of numbers. I want to be able to checksum the ones I generate, and then compare them against a bank of numbers I've already generated.
Let me explain:
I create an array of numbers as an array
I generate 6 numbers in an array using Rand()
I compare the generated numbers to the array I created, exit if they match
If the numbers don't match, create a hash that I can compare future arrays to. The arrangement of the numbers inside of the array do not matter.
I thought about using an md5sum, but then if the elements change inside, then the md5 would be the same.
I could just store the arrays in memory, but I'm trying to minimize the amount of numbers I store in memory
[1,2,3,4,5].sort.hash #=> 1777030444607087813
[2,3,4,5,1].sort.hash #=> 1777030444607087813
should make the sets distinguishable.
Also should be a more memory-friendly solution because
5.size #=> 8
1777030444607087813.size #=> 8
The problem with hashes is that you always need to worry about collisions. Here is a way to make sure each value is unique, (and it's even O(N))
require 'prime'
pr = Prime.take(10)
[ 1, 2, 3, 4, 5].map{|x| pr[x]}.reduce(&:*)
=> 15015
[ 2, 3, 4, 5, 1].map{|x| pr[x]}.reduce(&:*)
=> 15015

Using for loop along with range

So I am working on an assignment in python where the goal is the user enters a number and then it displays the number with a general label (apples in this case), and then counts down from that number down to 1, with the label attached to each integer. What I have so far is :
x=int(input("How many apples?"))
for x in [range(x,0,-1)]:
print(list(x),"apples")
So am looking for if user enters 10 that it would count down:
10 apples
9 apples
8 apples
etc
But I am getting [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] apples
Been trying to arrange things different but can't seem to get it. Thanks in advance
Here's your problem. The code list(x) is making a list, which in this case you don't need. Just do this:
x=int(input("How many apples?"))
for x in range(0, x, -1):
print(x+" apples")
If you need anything else in Python, let me know- I've been using Python for 6 years.

SWI-Prolog list manipulation

As part of a program I'm writing I need to make sure a variable does not equal any number that is the result of multiplying 2 numbers in a given list. For example: I've got a list Primes = [2, 3, 5, 7, 11] and I need to make sure that X does not equal any two of those numbers multiplied together such as 6 (2*3) or 55 (5*11) etc...
The code I have is as follows:
list(Numbers):-
Numbers = [X, Y, Sum],
between(3,6,Y),
between(3,6,X),
Primes = [2, 3, 5, 7, 11],
Sum is X+Y,
(Code i need help with)
The above code wiill type out results of [3,3,6], [4,3,7], [5,3,8] and so on. Now what I want is to be able to identify when sum is equal to a prime * prime and exclude that from the results. Something like Sum \= prime * prime. However, I don't know how to loop through the elements in Prime in order to multiply two elements together and then do that for all element in the list.
Hope this makes sense; im not great at explaining things.
Thanks in advance.
This is inefficient, but easy to code:
...
forall((nth(I,Primes,X),nth(J,Primes,Y),J>I), Sum =\= X*Y).
I think you could use that loop to initialize a list of precomputed factors, then use memberchk/2.
In SWI-Prolog use nth1/3 instead of nth/3

Sum of cells with several possibilities

I'm programming a Killer Sudoku Solver in Ruby and I try to take human strategies and put them into code. I have implemented about 10 strategies but I have a problem on this one.
In killer sudoku, we have "zones" of cells and we know the sum of these cells and we know possibilities for each cell.
Example :
Cell 1 can be 1, 3, 4 or 9
Cell 2 can be 2, 4 or 5
Cell 3 can be 3, 4 or 9
The sum of all cells must be 12
I want my program to try all possibilities to eliminate possibilities. For instance, here, cell 1 can't be 9 because you can't make 3 by adding two numbers possible in cells 2 and 3.
So I want that for any number of cells, it removes the ones that are impossible by trying them and seeing it doesn't work.
How can I get this working ?
There's multiple ways to approach the general problem of game solving, and emulating human strategies is not always the best way. That said, here's how you can solve your question:
1st way, brute-forcy
Basically, we want to try all possibilities of the combinations of the cells, and pick the ones that have the correct sum.
cell_1 = [1,3,4,9]
cell_2 = [2,4,5]
cell_3 = [3,4,9]
all_valid_combinations = cell_1.product(cell_2,cell_3).select {|combo| combo.sum == 12}
# => [[1, 2, 9], [3, 5, 4], [4, 4, 4], [4, 5, 3]]
#.sum isn't a built-in function, it's just used here for convenience
to pare this down to individual cells, you could do:
cell_1 = all_valid_combinations.map {|combo| combo[0]}.uniq
# => [1, 3, 4]
cell_2 = all_valid_combinations.map {|combo| combo[1]}.uniq
# => [2, 5, 4]
. . .
if you don't have a huge large set of cells, this way is easier to code. it can get a bit inefficienct though. For small problems, this is the way I'd use.
2nd way, backtracking search
Another well known technique takes the problem from the other approach. Basically, for each cell, ask "Can this cell be this number, given the other cells?"
so, starting with cell 1, can the number be 1? to check, we see if cells 2 and 3 can sum to 11. (12-1)
* can cell 2 have the value 2? to check, can cell 3 sum to 9 (11-1)
and so on. In very large cases, where you could have many many valid combinations, this will be slightly faster, as you can return 'true' on the first time you find a valid number for a cell. Some people find recursive algorithms a bit harder to grok, though, so your mileage may vary.

Searching through arrays in Ruby using a range

i've seen a lot of other questions touch on the subject but nothing as on topic as to provide an answer for my particular problem. Is there a way to search an array and return values within a given range...
for clarity I have one array = [0,5,12]
I would like to compare array to another array (array2) using a range of numbers.
Using array[0] as a starting point how would I return all values from array2 +/- 4 of array[0].
In this particular case the returned numbers from array2 will be within the range of -4 and 4.
Thanks for the help ninjas.
Build a Range that is your target ±4 and then use Enumerable#select (remember that Array includes Enumerable) and Range#include?.
For example, let us look for 11±4 in an array that contains the integers between 1 and 100 (inclusive):
a = (1..100).to_a
r = 11-4 .. 11+4
a.select { |i| r.include?(i) }
# [7, 8, 9, 10, 11, 12, 13, 14, 15]
If you don't care about preserving order in your output and you don't have any duplicates in your array you could do it this way:
a & (c-w .. c+w).to_a
Where c is the center of your interval and w is the interval's width. Using Array#& treats the arrays as sets so it will remove duplicates and is not guaranteed to preserver order.

Resources