Skip to content

Instantly share code, notes, and snippets.

@christophlingg
Last active January 12, 2017 16:12
Show Gist options
  • Save christophlingg/30ec61b9ef91c6657148 to your computer and use it in GitHub Desktop.
Save christophlingg/30ec61b9ef91c6657148 to your computer and use it in GitHub Desktop.
ab test
private static boolean isInAGroup(@Nonnull String sessionId, int base) {
int sum = 0;
for(int i : sessionId.toCharArray()) {
sum += i;
}
sum = sum / base; // integer division is like Math.floor(sum / base)
return sum % 2 == 0;
}
def is_in_a_group(username, base=1):
"""
determines if user takes part in ab test
:param username: username of authenticated request
:param base: identifier of the ab group
:return: true if user is in a group
"""
num = sum(bytearray(username, "UTF-8"))
num = num // base
num %= 2
return num == 0
CREATE OR REPLACE FUNCTION in_a_group(username TEXT, base INT)
RETURNS BOOL AS $$
DECLARE s INT := 0;
c CHAR;
t BOOL;
BEGIN
FOREACH c IN ARRAY regexp_split_to_array(username, '')
LOOP
s := s + ascii(c);
END LOOP;
s := s / base;
s := s % 2;
t := s = 0;
RETURN t;
END
$$
LANGUAGE plpgsql;
usernames = ["daniel", "christoph", "tobias", "marius", "daniela", "barbara", "hans"]
for base in [1, 3]:
print("\n# use base={} ...".format(base))
for u in usernames:
is_in = is_in_a_group(u, base)
print("# {}: {}".format(u, is_in))
# use base=1 ...
# daniel: False
# christoph: True
# tobias: True
# marius: False
# daniela: True
# barbara: False
# hans: True
# use base=3 ...
# daniel: False
# christoph: True
# tobias: True
# marius: False
# daniela: False
# barbara: True
# hans: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment