For code using intervals to measure “distance” between values, possibly as input, output, or in method calls.
25
votes
4answers
86k views
Simple Example of an Iterable and an Iterator in Java
I was a TA for a first-year Java programming course this year. As a very simple example of iterables/iterators, I wrote the following code for the students. I am curious if any styling or other ...
15
votes
2answers
911 views
Calculate the intersection between two intervals on the same circle
I want to calculate the ratio of intersection between two intervals; the length of the intersection divided by the length of the shorter interval.
If two intervals do not intersect, the ratio is 0. ...
11
votes
6answers
5k views
Merging Overlapping Intervals
A while back I was asked the following question in a phone interview, and to be honest it stumped me. After many long nights of rolling the problem around in my head I think I came up with a decent ...
11
votes
4answers
3k views
Generate sequence in Linq
I want to generate a sequence using Linq that goes from 10 to 100 with a step size of 10. The sequence also must contain a custom value (in the correct order).
This is what I have now, and I'm ...
11
votes
2answers
1k views
CodeEval's SkyScrapers challenge
This is a solution to CodeEval's SkyScrapers challenge.
You are given a list of triples \$(l, h, r)\$. Each triple represents an axis-aligned rectangle, with top-left corner at \$(l, h)\$ and bottom-...
10
votes
2answers
1k views
Using a Pythonesque range() generator function with the Java foreach loop
Now that we have the nice new foreach loop in Java, the old-style loop looks ugly be comparison.
I like the way Python has a ...
10
votes
1answer
283 views
Tweets per second, using linked list
This is a TalentBuddy Challenge
Your task is to
write a function that prints to the standard output (stdout) the highest number of tweets recorded between each second in the array and the past <...
10
votes
1answer
3k views
Converting a range of integers from a string to an IEnumerable
Goal
Convert the following into an IEnumerable of integers accounting for the x-y ranges:
...
9
votes
2answers
436 views
Finding the busiest time-frame in a parking lot
The text file containing cars' arrival and departure time will upload stuff from the parking lot.
Text will be like this:
...
9
votes
1answer
293 views
Date range validation
The following code is ran when the user presses a button to generate a log file based on the date selected. The DatePicker has a restrictive selected date range of ...
8
votes
4answers
2k views
Calculating range of ArrayLists
I have written this method which calculates the range (max - min) of an ArrayList. I have two for loops each for a different <...
8
votes
2answers
235 views
Parsing a list of single numbers and number ranges
I have input of a string containing a single number (like: \$3\$) or a range (like: \$1-5\$). Sample input, all together, looks like: "1-5,3,15-16", and sample ...
8
votes
2answers
281 views
Container for list of ranges
I made a C++ container of an ordered list of integers, that, in order to save space, I save as ranges:
For example, the list \$ \{1,2,3,5,6,7,8,9,20\} \$ is saved in memory as \$ \{(1,3), (5,9), (20,...
8
votes
1answer
294 views
consecutive_find function for returning a consecutive range of elements
consecutive_find() is a function that returns the beginning of a range that contains strictly N consecutive elements. It was part of a SO question that I answered.
...
7
votes
4answers
547 views
Adding intervals to an interval store
Write a program to do addition of intervals to an interval store.
An interval is represented by an array of two elements - the lower and
the upper bound (you can assume integers). Assume that ...
7
votes
3answers
7k views
Interval search tree
An interval is a data structure that represents a range (start & end, from & to, or min & max, etc.). An Interval Tree stores these intervals in a sorted tree structure that makes ...
7
votes
1answer
121 views
Onion Run Festival (or the unexpected anagram for Union of Intervals)
Original question: Union of intervals
In the original code, the Interval model class implements Comparable so that an input of <...
7
votes
1answer
284 views
Counting the overlapping intervals in the union of two sets
I had to recently implement a code to count the union of all intervals (if there are overlapping intervals) for an interview. I was provided with the following function stub:
...
7
votes
1answer
510 views
C++ template range
From a previous question I got an answer that included some template magic (that to be blunt was mind-boggling (as I could not understand it)).
So I have been trying to achieve the same results (...
6
votes
4answers
5k views
Finding overlapping time intervals
You have two lists with meetings scheduling (start time, end time). Meetings in single list don't intersect. Find all intersecting meetings across the two lists.
Is my complexity \$O(N M)\$? If I use ...
6
votes
2answers
3k views
Determine if an int is within range
Can I somehow make this a bit cleaner using Math.[Something], without making a method for it?
...
6
votes
2answers
9k views
Find intersections of overlapping intervals
This code finds the intersections of all overlapping intervals.
Example: if [0-20], [15-40], and ...
6
votes
4answers
5k views
Grouping consecutive numbers into ranges in Python 3.2
The following is a function that I wrote to display page numbers as they appear in books.
If you enter the list [1,2,3,6,7,10], for example, it would return:
<...
6
votes
3answers
254 views
5
votes
5answers
732 views
Representing the opening and closing time for a business
I have an OpenClose class which just represents the hours of operation of a business by the opening and closing time. It takes the opening and closing times as arguments to its constructor, and each ...
5
votes
3answers
4k views
Vertical Histogram
I've been been working on a vertical histogram that prints an asterisk in place of a number in a certain range (say 1-10... and so on). My code is working as required.
Is there a better, simpler or ...
5
votes
2answers
300 views
VSTO Write Dictionary To Range Extension Method
I was hoping to receive some criticism on this block of code. I wrote it to write the contents of a dictionary to excel range in VSTO. It works well. I am interested to see how people could improve it....
5
votes
3answers
1k views
Inserting interval into a collection
The task is fairly simple. I want to create a collection of intervals and implement an Add method which would insert new intervals to the collection and merge ...
5
votes
3answers
456 views
Model interval inclusiveness in C#
This is my first attempt to model a day's time interval and the fact that the start and end are either inclusive or not:
...
5
votes
2answers
1k views
Reverse sublists of a linked list
This code reverses each consecutive n elements of a linked list, and for spare nodes, leaves them as they are.
For example, the linked list
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
for an interval of 3 ...
5
votes
4answers
2k views
Aggregate array values into ranges
In five minutes I made a pretty ugly looking function. Can you help before I have to commit the code into history?
Requirements:
I would like a function that takes an array of numbers, and ...
5
votes
1answer
186 views
A Range object for Java that partially implements `List`
I'm writing a neural net which uses a genetic algorithm to adjust the weights. To represent the possible range of "genes" for the GA, I'm passing in a list of bases. Unfortunately, I realized after ...
5
votes
1answer
481 views
Combine a set of “ranges” to find least number of ranges
Given a set of ranges with lower bound and upper bound, I need to combine these ranges to produce minimum number of ranges which will cover all the values in the original set and nothing else.
...
5
votes
1answer
2k views
Segment tree implementation
I'm learning segment trees and their implementation. For the purpose, I tried solving the problem on CodeChef.
Full code here
My tree implementation is as follows:
...
5
votes
1answer
119 views
Calculate attendance compliance based on attendance record
This is part of a larger working system which I'm now trying to clean up. In the interests of making the system more test-able I'm splitting up the functions which can be split, but this 100-line ...
5
votes
1answer
70 views
Merging trips to the same location with overlapping dates
I have a SQLite database that only contains the holidays for 1 user.
I have a new Trip object (newTrip) that has a ...
5
votes
1answer
148 views
Store CMTimeRange into Core Data
I wanted to store CMTimeRange structures inside managed objects, so I've worked out the following:
...
5
votes
2answers
154 views
Basic Scheduler, codingame 'Super Computer' Challenge
I have built a program that handles the basic scheduling requests for CodinGame's Super Computer challenge.
Specifications (taken from CodinGame)
The Goal
In ...
5
votes
1answer
965 views
Orthogonal range search for a static kd tree
I'm trying to optimise my implementation of a static kd tree to perform orthogonal range searches in C++.
My problem: The code performs slowly even for a small number of queries when the number of ...
4
votes
4answers
152 views
Subdividing intervals that contain the largest error values
I'm trying to accomplish the following:
Start out with 1 interval on the real line. This interval is processed in some function and out comes an array of errors (size < 10 usually). The maximum ...
4
votes
3answers
862 views
Mapping uint to value within min/max set
I have a method that transforms any input uint into a [min,max] set. I am looking for improvements on the function, specifically ...
4
votes
3answers
545 views
Tweets per second
This challenge is from Talent Buddy:
Tweets per second
Japan Castle in the Sky airing broke a Twitter
record on August 3, 2013. At one point during the classic anime movie
showing, people ...
4
votes
2answers
116 views
Representing and Parsing an Open or Closed Range
I would like to represent a numeric range in C#. Either open-ended, such as "up to 35" or "100 on up" or closed-ended, such as "34 to 65". I'd like to represent the open end with NULL. Further, I'd ...
4
votes
1answer
52 views
Average interval between dates with random blanks - follow-up
Original question:
Average interval between dates with random blanks
I have a spreadsheet with order date data:
I need to find the average interval in days between each order date. I have to both ...
4
votes
1answer
81 views
Average interval between dates with random blanks
I have a spreadsheet with order date data:
I need to find the average interval in days between each order date. I have to both find a way to get past the blank cells in the row, and also take into ...
4
votes
3answers
2k views
Getting a value from an Excel sheet column
The code below gets value from a column in an Excel sheet. The values I get are B1, B2, B3, B4...., B100, ...Bn. All I need to do is strip out the char 'B' and convert the numeric string to integer ...
4
votes
1answer
281 views
Union of intervals
I am trying to solve for the problem with a bunch of intervals:
(1,2),(2,5),(4,7),(9,11)
I find their union, which in the above given case would be:
...
4
votes
1answer
279 views
Data Stream as Disjoint intervals
I was asked the following question:
Given a data stream input of non-negative integers a1, a2, ..., an,
..., summarize the numbers seen so far as a list of disjoint
intervals.
For example, ...
3
votes
2answers
4k views
Consolidate list of ranges that overlap
I wanted to implemented an algorithm in Python 2.4 (so the odd construction for the conditional assignment) where given a list of ranges, the function returns another list with all ranges that overlap ...
3
votes
1answer
49 views
Searching the number with the biggest amount of odd divisors in an interval
I wrote this program for school (first year in college) and it works, but it's too slow. I wondered if anyone could help me optimize this a bit, because I've been trying and trying, but it just won't ...