IgorBrown31
BAN USERpublic class Limiter2 implements RateLimit {
int qps;
int numQueriesAtCurrentSecond = 0;
long lastUpdateTime = -1;
@Override
public void setQPS(int qps) {
if (qps < 1 || qps > 1000000) {
throw new RuntimeException();
}
this.qps = qps;
}
@Override
public boolean allowThisRequest() {
long currentTime = System.currentTimeMillis();
if (lastUpdateTime == -1) { // first request
lastUpdateTime = currentTime;
numQueriesAtCurrentSecond++;
return true;
} else {
long difference = currentTime - lastUpdateTime;
if (hasSecondPassed(difference)) {
numQueriesAtCurrentSecond = 0;
numQueriesAtCurrentSecond++;
return true;
} else {
if (canAccept1MoreQuery()) {
numQueriesAtCurrentSecond++;
return true;
} else {
// too many queries per second
return false;
}
}
}
}
public boolean canAccept1MoreQuery() {
return numQueriesAtCurrentSecond + 1 <= qps;
}
public static boolean hasSecondPassed(long differenceMillis) {
return differenceMillis >= 1000;
}
}
How about this one? Will it work?
- IgorBrown31 October 30, 2014public class Limiter implements RateLimit {
long time = -1;
int qps;
long qps_millis;
@Override
public boolean allowThisRequest() {
long currentTime = System.currentTimeMillis();
if (time == -1) {
time = currentTime;
return true;
} else {
long result = currentTime - time;
if (result < qps_millis) {
return false;
} else {
time = currentTime;
return true;
}
}
}
@Override
public void setQPS(int qps) {
if (qps < 1 || qps > 1000000) {
throw new RuntimeException();
}
this.qps = qps;
qps_millis = qps * 1000;
}
}
And perhaps synchronize allowRequest()
- IgorBrown31 October 30, 2014Fixed, thanks. Slightly (<- strikethrough) cumbersome code, but it should work now.
- IgorBrown31 October 29, 2014static int numLoops;
static int numIterations;
static int[] loops;
static Map<Integer, Integer> results = new HashMap<Integer, Integer>();
public static void writeHistogram(int numDices, int numSides) {
numLoops = numDices;
numIterations = numSides;
loops = new int[numLoops];
loop(0);
System.out.println();
for (Map.Entry<Integer, Integer> entry : results.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
static void loop(int currentLoop) {
if (currentLoop == numLoops) {
printLoop();
return;
}
for (int counter = 1; counter <= numIterations; counter++) {
loops[currentLoop] = counter;
loop(currentLoop + 1);
}
}
static void printLoop() {
int currentSubtotal = 0;
for (int i = 0; i < numLoops; i++) {
System.out.printf("%d, ", loops[i]);
currentSubtotal += loops[i];
}
System.out.println(" -> " + currentSubtotal);
if (!results.containsKey(currentSubtotal)) {
results.put(currentSubtotal, 1);
} else {
int oldValue = results.get(currentSubtotal);
oldValue++;
results.put(currentSubtotal, oldValue);
}
}
public static void main(String[] args) {
writeHistogram(3, 6);
}
static String[] dictionary = {"world", "hello", "super", "hell"};
public static boolean isConcatenation(String key) {
StringBuilder currentString = new StringBuilder();
for (int i = 0; i < key.length(); i++) {
currentString.append(key.charAt(i));
int dictIndex = 0;
boolean found = false;
while (!found && dictIndex < dictionary.length) {
if (dictionary[dictIndex].equals(currentString.toString())) {
found = true;
}
dictIndex++;
}
if (found) {
String restOfTheWord = key.substring(i + 1);
int dictIndex_2 = 0;
while (dictIndex_2 < dictionary.length) {
if (restOfTheWord.equals(dictionary[dictIndex_2])) {
return true;
}
dictIndex_2++;
}
}
}
return false;
}
public static void printNumAnagrams(String[] array) {
char[][] chars = new char[array.length][];
for (int i = 0; i < array.length; i++) {
char[] currentChars = array[i].toCharArray();
Arrays.sort(currentChars);
chars[i] = currentChars;
}
int[] numAnagrams = new int[array.length];
for (int i = 0; i < chars.length - 1; i++) {
ArrayList<Integer> indeces = new ArrayList<Integer>();
int numRepetitions = 1;
for (int j = i + 1; j < chars.length; j++) {
if (Arrays.equals(chars[i], chars[j])) {
System.out.println();
numRepetitions++;
if (!indeces.contains(Integer.valueOf(i))) {
indeces.add(i);
}
if (!indeces.contains(Integer.valueOf(j))) {
indeces.add(j);
}
}
}
if (numRepetitions != 1) {
for (int integ : indeces) {
numAnagrams[integ] = numRepetitions;
}
}
indeces.clear();
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + ": ");
int count = numAnagrams[i];
if (count != 0) {
System.out.print(count);
} else {
System.out.print("1");
}
System.out.println();
}
}
public static void printAllPossible(List<String> list1, List<String> list2, List<String> list3) {
for (String list1Element : list1) {
for (String list2Element : list2) {
for (String list3Element : list3) {
System.out.println(list1Element + " " + list2Element + " " + list3Element);
}
}
}
}
Another way is to use contains(100 - integerI) right after you've got your first number, but in this case you'll have duplicate results that are to be filtered as well as it will result true even if {50}. So i guess its best to stick to this one, since it's less verbose and more clear
public static void get100(List<Integer> integers) {
for (int i = 0; i < integers.size() - 1; i++) {
int integerI = integers.get(i);
for (int j = i + 1; j < integers.size(); j++) {
int integerJ = integers.get(j);
if (integerI + integerJ == 100) {
System.out.println(integerI + ", " + integerJ);
}
}
}
}
//somewhat wild
static String givenLine;
public static void findSubSequence(String line) {
givenLine = line;
char char1;
char char2;
HashSet<String> possibleCombinations = new HashSet<String>();
for (int i = 0; i < line.length() - 1; i++) {
char1 = line.charAt(i);
for (int j = 0; j < line.length(); j++) {
char2 = line.charAt(j);
String combination = char1 + "" + char2;
if (char1 != char2 && !possibleCombinations.contains(combination)) {
System.out.println(char1 + ", " + char2 + " - " + repeats(char1, char2));
possibleCombinations.add(char1 + "" + char2);
}
}
}
}
public static boolean repeats(char char1, char char2) {
ArrayList<Integer> char1Occurences = new ArrayList<Integer>();
ArrayList<Integer> char2Occurences = new ArrayList<Integer>();
int indexOf1char = givenLine.indexOf(char1);
while (indexOf1char != -1) {
char1Occurences.add(indexOf1char);
indexOf1char = givenLine.indexOf(char1, indexOf1char + 1);
}
int indexOf2char = givenLine.indexOf(char2);
while (indexOf2char != -1) {
char2Occurences.add(indexOf2char);
indexOf2char = givenLine.indexOf(char2, indexOf2char + 1);
}
// System.out.println(char1Occurences);
// System.out.println(char2Occurences);
if (char1Occurences.size() < 2 || char2Occurences.size() < 2) {
return false;
}
int startIndex = 0;
ArrayList<Integer> bothIndeces = new ArrayList<Integer>();
while (startIndex < char1Occurences.size()) {
Integer char1Index = char1Occurences.remove(startIndex);
Integer char2Index = char2Occurences.remove(startIndex);
bothIndeces.add(char1Index);
bothIndeces.add(char2Index);
}
return isSorted(bothIndeces);
}
public static boolean isSorted(ArrayList<Integer> a) {
for (int i = 0; i < a.size() - 1; i++) {
if (a.get(i) > a.get(i + 1)) {
return false;
}
}
return true;
}
public static void printOutput(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream);
List<Long> numbers = new ArrayList<Long>();
while (scanner.hasNext()) {
numbers.add(scanner.nextLong());
}
int numNumbers = numbers.size();
if (numNumbers == 0 || numNumbers == 1) {
//
} else {
for (int i = 1; i < numNumbers; i++) {
System.out.print(numbers.get(i) - numbers.get(i - 1) + ",");
}
}
}
- IgorBrown31 October 16, 2014public static void printOutput(InputStream inputStream) {
Scanner scanner = new Scanner(inputStream);
List<Long> numbers = new ArrayList<Long>();
while (scanner.hasNext()) {
numbers.add(scanner.nextLong());
}
int numNumbers = numbers.size();
if (numNumbers == 0 || numNumbers == 1) {
//
} else {
for (int i = 1; i < numNumbers; i++) {
System.out.print(numbers.get(i) - numbers.get(i - 1) + ",");
}
}
}
public static void stepping(int start, int end) {
ArrayList<Integer> integers = new ArrayList<Integer>();
for (int i = start; i <= end; i++) {
integers.clear();
integers = breakIntegerIntoNumbers(i, integers);
int arrayIndex = 0;
boolean isStepping = true;
while (isStepping && arrayIndex < integers.size()) {
if (arrayIndex != integers.size() - 1) {// not last ele
if (Math.abs(integers.get(arrayIndex) - integers.get(arrayIndex + 1)) != 1) {
isStepping = false;
}
}
arrayIndex++;
}
if (isStepping) {
System.out.println("stepping"+i);
}else{
// System.out.println("not stepping -" +i);
}
}
}
public static ArrayList<Integer> breakIntegerIntoNumbers(int number, ArrayList<Integer> emptyList) {
if (number < 10) {
emptyList.add(number);
return emptyList;
} else {
breakIntegerIntoNumbers(number / 10, emptyList);
emptyList.add(number % 10);
return emptyList;
}
}
public static String getRunLength(String line) {
LinkedHashSet<Character> characters = new LinkedHashSet<Character>();
for (int i = 0; i < line.length(); i++) {
characters.add(line.charAt(i));
}
StringBuilder result = new StringBuilder();
for (Character character : characters) {
int numChars = 0;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == character) {
numChars++;
}
}
result.append(character);
if (numChars != 1) {
result.append(Integer.toString(numChars));
}
}
return result.toString();
public static boolean isPalindrome(String str) {
int start = 0;
int end = str.length() - 1;
while (start < end) {
if (str.charAt(start) != str.charAt(end)) {
return false;
}
start++;
end--;
}
return true;
}
- IgorBrown31 October 14, 2014
The person under trial said. You'll sentence me to three years in prison." Now if this were true, then the judge would have to sentencing him to two years (which would make the man's statement false). However, if the judge were to sentencing him to three years, then the man's statement would be true and he would have to sentencing him to two years! The man's statement had thus created a 'catch 22' situation...so rather than contradict himself, the judge set the man free!!!
- IgorBrown31 October 31, 2014yahoo answers qid=20100304164205AAr5Jyk