A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

New answers tagged

2

The basic approach doesn't look bad. You can optimize by working more with arrays that won't require any offset-handling. For example: You can combine 'Zero' and the arrays OneToTen and TenToNineteen to one direct-initialized array. The array Ten could also have two empty values in the first two entries, making 'Twenty' available at index 2. The Thousands,...


3

It's often best to write this kind of function along with its tests. Let's start with a simple test: import doctest; def englishify(number): """Format NUMBER into standard English form. NUMBER must be in range [0..999999] >>> englishify(0) 'Zero' """ return 'Zero' if __name__ == '__main__': doctest.testmod() Now, ...


3

Bug in Solution3 (using heapq) The items being put on the heap need to be fully sortable. Tuples are compared element by element. If the first element of two tuples compare equal, then the next element of the two tuples are compared. In your solution, the second element of the tuple is a ListNode. But no methods for comparing ListNodes have been defined....


0

int start = 0 int end = 0 int total = 0; int target = 8; while(end < arr.length){ total += arr[end]; // 1 3 1 4 = 9 /* Adding this part is enough as well */ if(total == target){ return true; } /* Adding this part is enough as well */ while(total>=target){ total-=arr[start]; // 8 start++; if(total == target){...


6

I am %100 sure that this is the correct answer, I'm not so sure... I dumped your code into an online compiler*. I made very minor adjustments, moving target to a parameter and adding semicolons which aren't a big deal since you probably didn't have a compiler when writing it. I did not modify your code beyond that and treated it like a black-box. import ...


2

I don't know Java, but I still found your challenge interesting! So I translated your code to Python and tried to beat it: def your_search(seq, target): start, end, total = 0,0,0 while (end < len(seq)): total += seq[end] while (total>=target): total-=seq[start] start+=1 if (total == target):...


0

Thank you, everyone, who participated in this! New code is on my GitHub. After rewriting code by your advises project looks like this: Project Structure: ST - SingleThread, MT - MultiThread, just for testing and showing the result to you, on Git is uploaded clean version. Main Class: package ee.raintree.test.numbers; import java.io.File; import java.io....


0

A more readable solution using java8 stream: int sum = input.chars() .map(c -> c == '(' ? 1 : c == ')' ? -1 : 0) .sum(); return sum == 0;


3

Did you measure run time before implementing the multi threaded random number generator and analyser? I'm betting that combining the files takes a lot more time than you gain from concurrency (IO is slow). This would be premature optimization and a red flag. The main method should not contain any logic other than parsing arguments to a format understood by ...


5

Before handing the company any code you wrote, you should ask for clarification of the task. Write a JAVA program that will: The correct spelling is Java, not JAVA. Generate a file with random numeric (range from 1 to 2^64 − 1 integers) data. The grammar is slightly wrong here. They should have written "with random integers in the range from 1 ...


3

The difference in runtime of 4ms is very small compared to normal system variance due to scheduling and whatever background tasks are running. I don't believe that indicates a performance benefit right vs left. And the only difference between the two approaches (as far as I can see) is which order you iterate the string. Algorithmically speaking going right ...


Top 50 recent answers are included