14
votes
Accepted
Flipping coins performance
The code you have posted is straightforward and basic. Because of this, I'm going to assume you are not super-experienced with Python. I'll make some direct suggestions to go from your current code to ...
11
votes
Flipping coins performance
Thou shall not brute force.
There is no reason to maintain the entire list of coin states, much less to perform actual flips. The state is easily recoverable from the sequence of ...
11
votes
Flipping coins performance
I think the problem authors maybe intended for people to use a data structure that allows for fast queries and updates. You can make it run in \$Q\log(N)\$ time by using a tree structure.
First note ...
10
votes
Merging Overlapping Intervals
This problem is well suited to an iterable method, rather than one that generates a list:
...
10
votes
Collect subsequences of consecutive integers
@Peter Taylor has a good idea, but there is more to be improved
You should add some tests
Python is often described as Batteries included
This is a perfect example to use ...
9
votes
Checking if two time intervals overlap
The code looks well presented, and it's clear how it maps to the problem statement.
The one thing that's not well defined is how we deal with exact equality of times. I'm assuming that intervals are ...
9
votes
Accepted
Checking if two time intervals overlap
Negating the no-overlap case
You mention four separate cases where two time intervals overlap. You then reduce it to three by pointing out that one case is really the intersection of two of the ...
9
votes
Coding Challenge Solution - Good Range
Don't force a class where a function is perfectly fine.
The namespace std is not designed for wholesale importation, see "Why is ...
8
votes
Checking if two time intervals overlap
Your code uses an algorithm that directly tests for an overlap of time spans, but a simpler algorithm is to check for a non-overlap - to check for whether the time-spans are completely distinct. The ...
8
votes
How to set an interval to std::map
Quite frankly it seems that you did not do what was asked from you.
The text above assign gives you the requirements
...
8
votes
Modeling and computing intersection between continuous intervals
Review
You are constrained to float which makes your class only usable for a limited number of scenarios. I would like to be able to work with ...
7
votes
Finding the busiest time-frame in a parking lot
Naming Conventions
MSDN, General Naming Conventions
DO NOT use Hungarian notation:
[dt]Start in getPickTimeframe() --> ...
7
votes
Accepted
Collect subsequences of consecutive integers
Using yield is often simpler than building up a list to return. Here it would reduce
...
7
votes
Finding all intervals that match predicate in vector
If you want to write concise, idiomatic C++, the best way is to rely on the STL as much as possible, as a tool as well as an inspiration.
So how would this algorithm be implemented in the STL?
It ...
7
votes
Accepted
7
votes
Get consecutive integer number ranges from list of int
Your code takes a list, ensures it's sorted and distinct, then assembles and returns another list. This approach is problematic when the input is in fact a generator, in which case your code forces ...
6
votes
Converting a range of integers from a string to an IEnumerable
Ok, some comments:
Code style
While there are no definitive laws about code style, most C# programmers are putting the opening brace { on a separate line. If ...
6
votes
Accepted
Searching the number with the biggest amount of odd divisors in an interval
Bug
Your code isn't always correct:
>>> amountOddDivisorsOf(15)
3
Should be 4.
Coding style
The Python style guide recommends ...
6
votes
Flipping coins performance
Since Codechef seems to support numpy, I would propose a solution like this:
...
6
votes
Flipping coins performance
It looks like certain numpy operations give a very large constant factor speed benefit, as Graipher demonstrated in his answer. Graipher's solution is \$O(QN)\$ but is competitive with the \$O(Q\log(N)...
6
votes
6
votes
Accepted
Find the intervals which have a non-empty intersection with a given interval
1. Review
There is no docstring. What does the function find_intersections do? The text in the post is pretty clear, so you could use this as the basis of a ...
6
votes
Accepted
Counting integers that are within intervals
If you sort your values, you can then make an iterator on the sorted list, forward it to the lower limit, count until the first value is reached that is larger than the upper limit and discard all ...
6
votes
Accepted
Get consecutive integer number ranges from list of int
Your code is really complicated. First, your code should be abstracted a little more. It is not specific to feature IDs, therefore the terminology should not use these words. The same algorithm can be ...
5
votes
Accepted
"Can a list of meetings all be scheduled in a conference room?"
Like knut suggested, a simple solution would be to sort the meetings by start time and for each check if it conflicts with the next.
E.g. given a list of meetings:
...
5
votes
Accepted
Counting the overlapping intervals in the union of two sets
Bugs
I tried this test input:
int[] a = {1, 10, 30, 60 };
int[] b = {2, 20, 40, 70 };
and your program returned 3. ...
5
votes
Checking overlap ranges between two prices
I am adding my comments at first glance. This is not a final review as this code needs lots of refactoring.
Delete commented code
Never keep commented code; it adds distraction while reading by ...
5
votes
Accepted
Representing and Parsing an Open or Closed Range
I would check the result of range
range = _RemoveSpaces.Replace(range, string.Empty);
against string.Empty because that is ...
5
votes
Accepted
Add dash between more than two consecutive numbers and comma between other numbers in array
Sorry but no comments (yet) on the LINQ.
Your first conditional has a logic error. You should be checking if orderedArray is null before you check if its length ...
5
votes
Find the intervals which have a non-empty intersection with a given interval
return value
In case there is no overlap, it would be cleaner to either return a sentinel value (None for example), or raise an exception, and don't trigger any ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
interval × 156python × 40
datetime × 32
java × 30
c# × 29
algorithm × 24
performance × 21
c++ × 17
programming-challenge × 15
python-3.x × 13
interview-questions × 11
javascript × 10
array × 7
iterator × 7
php × 6
c++11 × 6
object-oriented × 5
sorting × 5
excel × 5
time-limit-exceeded × 5
validation × 5
linq × 5
integer × 5
vba × 4
tree × 4