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 ...
- 12.2k
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 ...
- 54.6k
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 ...
- 711
10
votes
Merging Overlapping Intervals
This problem is well suited to an iterable method, rather than one that generates a list:
...
- 101
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 ...
- 11.5k
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 ...
- 67.6k
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 ...
- 18.4k
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 ...
- 134
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
...
- 4,221
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 ...
- 13.8k
7
votes
Accepted
Onion Run Festival (or the unexpected anagram for Union of Intervals)
Interval
Your interval class is neat, well, structured in general. The hashCode/equals/compareTo contracts all look accurate. I like the Java-8-like use of the ...
- 97.1k
7
votes
VSTO Write Dictionary To Range Extension Method
You have a lot of unnecessary vertical spacing which should be removed to improve readability.
Based on the naming guidelines input parameter should be named using ...
- 50.1k
7
votes
Accepted
A Range object for Java that partially implements `List`
A lot (most) of the methods from the interface are currently throwing an UnsupportedOperationException, either because the operation doesn't make sense (adding to the range),
Start with an ...
- 13.5k
7
votes
Parsing a list of single numbers and number ranges
Since the whole exercise is a string-transformation problem, I suggest performing it using a regex substitution.
...
- 143k
7
votes
Accepted
Merging an overlapping collection of intervals
Some style issues:
Space before {
Spaces around ->
Space after ,
No space before ...
- 8,508
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() --> ...
- 853
7
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 ...
- 21.4k
7
votes
Accepted
Collect subsequences of consecutive integers
Using yield is often simpler than building up a list to return. Here it would reduce
...
- 24k
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 ...
- 2,317
6
votes
Accepted
Formatting a date range, with shortening if the interval falls entirely within a calendar month
This is a self-contained problem, with well-defined inputs and outputs. To be maintainable, the code should reside in a function.
You have three distinct cases, and maybe more:
Start and end dates ...
- 143k
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 ...
- 301
6
votes
Accepted
Parsing a list of single numbers and number ranges
Disclaimer: I'm not a Python programmer!
Your code isn't that bad. It's readable enough for me to understand it. B+ for readability!
Currently, you have this function:
...
- 6,066
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 ...
- 18.4k
6
votes
Flipping coins performance
Since Codechef seems to support numpy, I would propose a solution like this:
...
- 40.7k
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)...
- 711
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 ...
- 49.4k
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 ...
- 40.7k
6
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 ...
- 5,707
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 × 20
c++ × 17
programming-challenge × 15
python-3.x × 13
interview-questions × 11
javascript × 10
iterator × 7
php × 6
array × 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