Datastax DSE 5.0.x broken behavior on detecting existing vertex by Text() property value when a search index exists on that field - datastax

Hi I made this failing Test - I have set the mode to be Production and allow_scans to true. I have also set a materialized index and a search index on property value:
schema.propertyKey("value").Text().create()
schema.vertexLabel("Number").properties("value").create()
schema.vertexLabel('Number').index('byValue').materialized().by('value').add()
schema.vertexLabel('Number').index('search').search().by('va‌​lue').asString().add‌​()
I am using DSE 5.0.7 with
<dependency>
<groupId>com.datastax.dse</groupId>
<artifactId>dse-java-driver-graph</artifactId>
<version>1.2.3</version>
</dependency>
public class DatastaxTransactionTest {
private GraphTraversalSource g;
public GraphTraversalSource getGraph() {
DseCluster dseCluster = DseCluster.builder()
.addContactPoint("192.168.1.24")
.build();
DseSession dseSession = dseCluster.connect();
return DseGraph.traversal(dseSession, new GraphOptions().setGraphName("test"));
}
#Before
public void setUp() {
g = getGraph();
g.V().drop().iterate();
}
#After
public void clean() {
g.V().drop().iterate();
}
#Test
public void testTransactions() {
GraphTraversalSource g = getGraph();
g.V().drop().iterate();
for (int i=0; i<100; i++) {
boolean exists = g.V().has("Number", "value", "32").count().next().intValue() > 0;
// this works: boolean exists = g.V().count().next().intValue() > 0;
if (!exists) {
g.addV("Number").property("value","32").next();
report();
}
}
Assert.assertEquals(1, g.V().count().next().intValue()); //this is 100
Assert.assertEquals(1, g.V().has("Number", "value", "32").count().next().intValue()); // this is 0!!
}
public void report() {
int vCount = g.V().count().next().intValue();
int numberCount = g.V().hasLabel("Number").count().next().intValue();
int number32Count = g.V().has("Number", "value", "32").count().next().intValue();
System.out.println("added Number (Total Vs: "+vCount+ ", Number Vs: "+numberCount + ", Number 32 Vs: "+number32Count );
}
}
This is trying to insert a Number vertex only if it doesn't exist.. existence over pure count of vertices works fine but doesn't work through indices.. I had huge problems on some of my tests that they were passing only through debugging but not run.. It is as if the indices are not bound to transaction but are eventually updated sometime later.. if that is the case it is a very bad behavior..
Here is the output:
added Number (Total Vs: 1, Number Vs: 1, Number 32 Vs: 0
added Number (Total Vs: 2, Number Vs: 2, Number 32 Vs: 0
added Number (Total Vs: 3, Number Vs: 3, Number 32 Vs: 0
added Number (Total Vs: 4, Number Vs: 4, Number 32 Vs: 0
added Number (Total Vs: 5, Number Vs: 5, Number 32 Vs: 0
added Number (Total Vs: 6, Number Vs: 6, Number 32 Vs: 0
[...]
added Number (Total Vs: 97, Number Vs: 97, Number 32 Vs: 0
added Number (Total Vs: 98, Number Vs: 98, Number 32 Vs: 0
added Number (Total Vs: 99, Number Vs: 99, Number 32 Vs: 0
added Number (Total Vs: 100, Number Vs: 100, Number 32 Vs: 0
java.lang.AssertionError:
Expected :1
Actual :100
<Click to see difference>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:631)
at com.xxxx.DatastaxTransactionTest.testTransactions(DatastaxTransactionTest.java:53)
[...]
Thanks!

Related

Supply different families of priors as a parameter in the bugs/stan model

This is the classic eight school example in Bayesian data analysis by Andrew Gelman. Please see the stan file and R code below. I use a cauchy prior with paratmer A for the hyperparamter tau in the stan file. I am trying to supply the R function "school" with different priors not within cauchy family, for example, uniform(0,1000) prior, so that I do not have to create different stans file for the new priors. Is this possible within stan or bugs?
schools.stan:
`
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
real<lower=0> A;
}
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
}
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
}
model {
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
tau ~ cauchy(0,A);
}
`
`
school <- function(A=100){
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
A=A)
fit <- stan(file = "schools.stan", data = schools_dat,iter = 20)
print(fit)
}
school()
`
I tried the following but have no idea how to change the stan file correspondingly.
`
school <- function(prior="dunif(0,1000"){
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
prior=prior)
fit <- stan(file = "schools.stan", data = schools_dat,iter = 20)
print(fit)
}
school()
`
It's possible to pre-specify more than one distribution in the Stan code, and then specify which distribution you want in the input data. Stan isn't really intended to be used this way, but it can be done!
Here's an example. I've added a new data variable, tau_prior; it's an integer that specifies which prior you want to use for tau. 1 = Cauchy, 2 = uniform, 3 = exponential. In addition, for each type of prior, there's a data variable that sets a hyperparameter. (Hyperparameters for the distributions that aren't chosen have no effect.)
data {
int<lower=0> J; // number of schools
real y[J]; // estimated treatment effects
real<lower=0> sigma[J]; // standard error of effect estimates
int<lower=1,upper=3> tau_prior;
real<lower=0> cauchy_sigma;
real<lower=0> uniform_beta;
real<lower=0> exponential_beta;
}
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
}
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
}
model {
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
if(tau_prior == 1) {
tau ~ cauchy(0, cauchy_sigma);
} else if(tau_prior == 2) {
tau ~ uniform(0, uniform_beta);
} else if(tau_prior == 3) {
tau ~ exponential(exponential_beta);
}
}
I've also modified the R function so that it provides default values for each hyperparameter, on a scale similar to the one you've used already.
school <- function(tau_prior = 1,
cauchy_sigma = 100,
uniform_beta = 1000,
exponential_beta = 0.01) {
schools_dat <- list(J = 8,
y = c(28, 8, -3, 7, -1, 1, 18, 12),
sigma = c(15, 10, 16, 11, 9, 11, 10, 18),
tau_prior = tau_prior,
cauchy_sigma = cauchy_sigma,
uniform_beta = uniform_beta,
exponential_beta = exponential_beta)
fit <- stan(file = "schools.stan", data = schools_dat, iter = 20)
print(fit)
}
# The default: use a Cauchy prior with scale 100.
school()
# Use a uniform prior with the default upper limit (1000).
school(tau_prior = 2)
# Use an exponential prior with a non-default rate (1).
school(tau_prior = 3, exponential_beta = 1)

Groovy not returning a list of lists after for loop

I am learning Groovy and I am trying to return a list of lists but when I do my for loop in counter() function, it automatically returns just giving me the first iteration and doesn't continue with the rest of the words.
I found the issue is in the for loop of counter(), it looks like Groovy shares the i variable in the loops. Coming from Python each for loop holds its own variable i. Is there something like this in Groovy?
lista = ["apple","banana","orange","melon","watermelon"]
def copaa(a_list_of_things){
lista_to_return = []
for (i = 0; i < a_list_of_things.size(); i++) {
lis = counter(a_list_of_things[i])
lista_to_return.add(lis)
}
return lista_to_return
}
def counter(word){
list_of_times = []
//return "bla"
for (i = 0; i < word.length(); i++) {
list_of_times.add(i)
}
return list_of_times
}
ls = copaa(lista)
println(ls)
Avoid global scope:
prefix the i variable declarations with the implicit type def (actually Object) or an appropriate explicit type (e.g. int or Integer) to make the scope local to the loop. Otherwise these variables are placed (as a single one i) in the bindings of the script (practically it's treated as a global variable).
Modify the relevant lines of your code like this:
// with def...
for (def i = 0; i < a_list_of_things.size(); i++) {
// ...
for (def i = 0; i < word.length(); i++) {
// ...OR with an explicit type (e.g. int) the scope is limited
// to the for loop as expected
for (int i = 0; i < a_list_of_things.size(); i++) {
// ...
for (int i = 0; i < word.length(); i++) {
Output
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
The Groovy Way
To gives you some extra hints I reimplemented your algorithm using some of the cool features groovy provides (collect, closure, numeric ranges):
wordList = ["apple","watermelon"]
// collect process each word (the implicit variable it) and returns a new list
// each field of the new list is a range from 0 till it.size() (not included)
outList = wordList.collect { (0 ..< it.size()).toArray() }
assert outList == [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]

TWIN PRIMES BETWEEN 2 VALUES wrong results

I've been working on this program to count how many twin primes between two values and it's been specified that twin primes come in the (6n-1, 6n+1) format, with the exception of (3, 5). My code seems to work fine, but it keeps giving me the wrong result....1 less couple of twin primes than i should get. Between 1 and 40, we should have 5 twin primes, but I'm always getting 4. é
What am I doing wrong? Am I not taking into account (3, 5)?
Here's my code:
#include <stdio.h>
int prime (int num) {
int div;
if (num == 2) return 1;
if (num % 2 == 0) return 0;
div = 3;
while (div*div <= num && num%div != 0)
div = div + 2;
if (num%div == 0)
return 0;
else
return 1;
}
int main(void) {
int low, high, i, count, n, m;
printf("Please enter the values for the lower and upper limits of the interval\n");
scanf("%d%d", &low, &high);
printf("THIS IS THE LOW %d\n AND THIS IS THE HIGH %d\n", low, high);
i = low;
count = 0;
while (6*i-1>=low && 6*i+1<=high) {
n = 6*i-1;
m = 6*i+1;
if (prime(n) && prime(m)) ++count;
i = i + 1;
}
printf("Number of twin primes is %d\n", count);
return 0;
}
Your program misses (3 5) because 3 is not trapped as a prime number, and because 4 is not a multiple of 6. Rather than the main loop stepping by (effectively) 6, this answer steps by 1.
#include <stdio.h>
int prime (int num) {
int div;
if (num == 1) return 0; // excluded 1
if (num == 2 || num == 3) return 1; // included 3 too
if (num % 2 == 0) return 0;
div = 3;
while (div*div <= num) {
if (num % div == 0) // moved to within loop
return 0;
div += 2;
}
return 1;
}
int main(void) {
int low, high, i, count, n, m;
printf("Please enter the values for the lower and upper limits of the interval\n");
scanf("%d%d", &low, &high);
printf("THIS IS THE LOW %d\n AND THIS IS THE HIGH %d\n", low, high);
count = 0;
for (i=low; i<=high; i++) {
n = i-1;
m = i+1;
if (prime(n) && prime(m)) {
printf ("%2d %2d\n", n, m);
++count;
}
}
printf("Number of twin primes is %d\n", count);
return 0;
}
Program output
1
40
THIS IS THE LOW 1
AND THIS IS THE HIGH 40
3 5
5 7
11 13
17 19
29 31
Number of twin primes is 5
Next run:
3
10
THIS IS THE LOW 3
AND THIS IS THE HIGH 10
3 5
5 7
Number of twin primes is 2
https://primes.utm.edu/lists/small/100ktwins.txt
The five twin primes under forty are (3,5), (5,7), (11,13), (17,19), (29,31) so if you know that your code isn't counting (3,5) then it is working correctly, counting (5,7), (11,13), (17,19), and (29,31).
A possible fix would be to add an if-statement which adds 1 to "count" if the starting number is less than 4. I'm not really that used to reading C syntax so I had trouble getting my head around your formulas, sorry.
edit: since comments don't format code snippets:
i = low;
count = 0;
if (low <= 3 && high >= 3){
count ++; // accounts for (3,5) twin primes if the range includes 3
}
You have a problem in your prime function, this is the output of your prime function for the first ten prime evaluations
for(i=1;i<=10;i++) printf("%d\t%d",i,prime(i));
1 1
2 1
3 0
4 0
5 1
6 0
7 1
8 0
Note the prime() function from Weather Vane, you should include 3 as prime (and exclude 1).
From [1], twin primes are the ones that have a prime gap of two, differing by two from another prime.
Examples are (3,5) , (5,7), (11,13). The format (6n-1,6n+1) is true but for (3,5) as you stated. Your program runs almost ok since it shows the number of twin primes that are in the interval AND follows the rule mentioned above. This doesn't include (3,5). You can make a kind of exception (like if low<=3 add 1 to total count), or use another algorithm to count twin primes (like verify if i is prime, then count distance from i to next prime, if distance=2 then they are twin primes)
[1] http://en.wikipedia.org/wiki/Twin_prime

Check if a number is divisible by 3

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any idea how to do it?
The current answers all focus on decimal digits, when applying the "add all digits and see if that divides by 3". That trick actually works in hex as well; e.g. 0x12 can be divided by 3 because 0x1 + 0x2 = 0x3. And "converting" to hex is a lot easier than converting to decimal.
Pseudo-code:
int reduce(int i) {
if (i > 0x10)
return reduce((i >> 4) + (i & 0x0F)); // Reduces 0x102 to 0x12 to 0x3.
else
return i; // Done.
}
bool isDiv3(int i) {
i = reduce(i);
return i==0 || i==3 || i==6 || i==9 || i==0xC || i == 0xF;
}
[edit]
Inspired by R, a faster version (O log log N):
int reduce(unsigned i) {
if (i >= 6)
return reduce((i >> 2) + (i & 0x03));
else
return i; // Done.
}
bool isDiv3(unsigned i) {
// Do a few big shifts first before recursing.
i = (i >> 16) + (i & 0xFFFF);
i = (i >> 8) + (i & 0xFF);
i = (i >> 4) + (i & 0xF);
// Because of additive overflow, it's possible that i > 0x10 here. No big deal.
i = reduce(i);
return i==0 || i==3;
}
Subtract 3 until you either
a) hit 0 - number was divisible by 3
b) get a number less than 0 - number wasn't divisible
-- edited version to fix noted problems
while n > 0:
n -= 3
while n < 0:
n += 3
return n == 0
Split the number into digits. Add the digits together. Repeat until you have only one digit left. If that digit is 3, 6, or 9, the number is divisible by 3. (And don't forget to handle 0 as a special case).
While the technique of converting to a string and then adding the decimal digits together is elegant, it either requires division or is inefficient in the conversion-to-a-string step. Is there a way to apply the idea directly to a binary number, without first converting to a string of decimal digits?
It turns out, there is:
Given a binary number, the sum of its odd bits minus the sum of its even bits is divisible by 3 iff the original number was divisible by 3.
As an example: take the number 3726, which is divisible by 3. In binary, this is 111010001110. So we take the odd digits, starting from the right and moving left, which are [1, 1, 0, 1, 1, 1]; the sum of these is 5. The even bits are [0, 1, 0, 0, 0, 1]; the sum of these is 2. 5 - 2 = 3, from which we can conclude that the original number is divisible by 3.
A number divisible by 3, iirc has a characteristic that the sum of its digit is divisible by 3. For example,
12 -> 1 + 2 = 3
144 -> 1 + 4 + 4 = 9
The interview question essentially asks you to come up with (or have already known) the divisibility rule shorthand with 3 as the divisor.
One of the divisibility rule for 3 is as follows:
Take any number and add together each digit in the number. Then take that sum and determine if it is divisible by 3 (repeating the same procedure as necessary). If the final number is divisible by 3, then the original number is divisible by 3.
Example:
16,499,205,854,376
=> 1+6+4+9+9+2+0+5+8+5+4+3+7+6 sums to 69
=> 6 + 9 = 15 => 1 + 5 = 6, which is clearly divisible by 3.
See also
Wikipedia/Divisibility rule - has many rules for many divisors
Given a number x.
Convert x to a string. Parse the string character by character. Convert each parsed character to a number (using atoi()) and add up all these numbers into a new number y.
Repeat the process until your final resultant number is one digit long. If that one digit is either 3,6 or 9, the origional number x is divisible by 3.
My solution in Java only works for 32-bit unsigned ints.
static boolean isDivisibleBy3(int n) {
int x = n;
x = (x >>> 16) + (x & 0xffff); // max 0x0001fffe
x = (x >>> 8) + (x & 0x00ff); // max 0x02fd
x = (x >>> 4) + (x & 0x000f); // max 0x003d (for 0x02ef)
x = (x >>> 4) + (x & 0x000f); // max 0x0011 (for 0x002f)
return ((011111111111 >> x) & 1) != 0;
}
It first reduces the number down to a number less than 32. The last step checks for divisibility by shifting the mask the appropriate number of times to the right.
You didn't tag this C, but since you mentioned atoi, I'm going to give a C solution:
int isdiv3(int x)
{
div_t d = div(x, 3);
return !d.rem;
}
bool isDiv3(unsigned int n)
{
unsigned int n_div_3 =
n * (unsigned int) 0xaaaaaaab;
return (n_div_3 < 0x55555556);//<=>n_div_3 <= 0x55555555
/*
because 3 * 0xaaaaaaab == 0x200000001 and
(uint32_t) 0x200000001 == 1
*/
}
bool isDiv5(unsigned int n)
{
unsigned int n_div_5 =
i * (unsigned int) 0xcccccccd;
return (n_div_5 < 0x33333334);//<=>n_div_5 <= 0x33333333
/*
because 5 * 0xcccccccd == 0x4 0000 0001 and
(uint32_t) 0x400000001 == 1
*/
}
Following the same rule, to obtain the result of divisibility test by 'n', we can :
multiply the number by 0x1 0000 0000 - (1/n)*0xFFFFFFFF
compare to (1/n) * 0xFFFFFFFF
The counterpart is that for some values, the test won't be able to return a correct result for all the 32bit numbers you want to test, for example, with divisibility by 7 :
we got 0x100000000- (1/n)*0xFFFFFFFF = 0xDB6DB6DC
and 7 * 0xDB6DB6DC = 0x6 0000 0004,
We will only test one quarter of the values, but we can certainly avoid that with substractions.
Other examples :
11 * 0xE8BA2E8C = A0000 0004, one quarter of the values
17 * 0xF0F0F0F1 = 10 0000 0000 1
comparing to 0xF0F0F0F
Every values !
Etc., we can even test every numbers by combining natural numbers between them.
A number is divisible by 3 if all the digits in the number when added gives a result 3, 6 or 9. For example 3693 is divisible by 3 as 3+6+9+3 = 21 and 2+1=3 and 3 is divisible by 3.
inline bool divisible3(uint32_t x) //inline is not a must, because latest compilers always optimize it as inline.
{
//1431655765 = (2^32 - 1) / 3
//2863311531 = (2^32) - 1431655765
return x * 2863311531u <= 1431655765u;
}
On some compilers this is even faster then regular way: x % 3. Read more here.
well a number is divisible by 3 if all the sum of digits of the number are divisible by 3. so you could get each digit as a substring of the input number and then add them up. you then would repeat this process until there is only a single digit result.
if this is 3, 6 or 9 the number is divisable by 3.
Here is a pseudo-algol i came up with .
Let us follow binary progress of multiples of 3
000 011
000 110
001 001
001 100
001 111
010 010
010 101
011 000
011 011
011 110
100 001
100 100
100 111
101 010
101 101
just have a remark that, for a binary multiple of 3 x=abcdef in following couples abc=(000,011),(001,100),(010,101) cde doest change , hence, my proposed algorithm:
divisible(x):
y = x&7
z = x>>3
if number_of_bits(z)<4
if z=000 or 011 or 110 , return (y==000 or 011 or 110) end
if z=001 or 100 or 111 , return (y==001 or 100 or 111) end
if z=010 or 101 , return (y==010 or 101) end
end
if divisible(z) , return (y==000 or 011 or 110) end
if divisible(z-1) , return (y==001 or 100 or 111) end
if divisible(z-2) , return (y==010 or 101) end
end
C# Solution for checking if a number is divisible by 3
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int num = 33;
bool flag = false;
while (true)
{
num = num - 7;
if (num == 0)
{
flag = true;
break;
}
else if (num < 0)
{
break;
}
else
{
flag = false;
}
}
if (flag)
Console.WriteLine("Divisible by 3");
else
Console.WriteLine("Not Divisible by 3");
Console.ReadLine();
}
}
}
Here is your optimized solution that every one should know.................
Source: http://www.geeksforgeeks.org/archives/511
#include<stdio.h>
int isMultiple(int n)
{
int o_count = 0;
int e_count = 0;
if(n < 0)
n = -n;
if(n == 0)
return 1;
if(n == 1)
return 0;
while(n)
{
if(n & 1)
o_count++;
n = n>>1;
if(n & 1)
e_count++;
n = n>>1;
}
return isMultiple(abs(o_count - e_count));
}
int main()
{
int num = 23;
if (isMultiple(num))
printf("multiple of 3");
else
printf(" not multiple of 3");
return 0;
}

arc4random except some numbers

How can you disallow some nubers from being chosen with the arc4random function?
Current code:
int random = (arc4random() % 92);
(numbers from 0 to 92)
I want it do disallow the following numbers: 31, 70, 91, 92
First, you'll need to change
% 92
to
% 93
to get numbers from 0..92
I'd do something like this
int random;
do {
random = arc4random() % 93;
}
while ( random == 31 || random == 70 || random == 91 || random == 92 );
If you are going to disallow numbers 91 and 92, why bother including them in your mod?
Expanding on the previous answer:
int random;
do {
random = arc4random() % 91;
}
while ( random == 31 || random == 70 );
Simple, keep asking for numbers:
get a new random number
while the new number is one of the disallowed ones:
get a new random number
return the random number
Pseudo code, but you should get the idea.
Solution for Swift:
func randomValue(except: Int) -> Int {
var rand: Int = 0;
repeat {
rand = Int(arc4random_uniform(3) + 1)
}
while(rand == except)
return Int(rand)
}