patternpythonMinor
Basic Scheduler, codingame 'Super Computer' Challenge
Viewed 0 times
schedulerchallengesupercomputercodingamebasic
Problem
I have built a program that handles the basic scheduling requests for CodinGame's
Specifications (taken from CodinGame)
The Goal
In the Computer2000 data center, you are responsible for planning the usage of a supercomputer for scientists. Therefore you've decided to organize things a bit by planning everybody’s tasks. The logic is simple: the higher the number of calculations which can be performed, the more people you can satisfy.
Rules
Scientists give you the starting day of their calculation and the number of consecutive days they need to reserve the calculator.
Example
In this example, it’s not possible to carry out all the calculations because the periods for B and C overlap. 3 calculations maximum can be carried out: A, D and C.
Input
Line 1: The number N of calculations.
The N following lines: on each line, the starting day J and the duration D of reservation, separated by a blank space.
Output
The maximum number of calculations that can be carried out.
Example
Input:
Output:
Thing's I am looking for in this review
A couple more things
-
Which calculations removed (duped out) is irrelevant, I only care about # possible calculations
-
Regenerating of the list for
-
The tests written really weren't meant to be comprehensive, just something to get by for big mistakes. My real testing was done on CodinGame.
-
This code doesn't entirely pass, it says I need more optimizations to pass it's
Super Computer challenge. Specifications (taken from CodinGame)
The Goal
In the Computer2000 data center, you are responsible for planning the usage of a supercomputer for scientists. Therefore you've decided to organize things a bit by planning everybody’s tasks. The logic is simple: the higher the number of calculations which can be performed, the more people you can satisfy.
Rules
Scientists give you the starting day of their calculation and the number of consecutive days they need to reserve the calculator.
Example
Calculation Starting Day Duration
A 2 5
B 9 7
C 15 6
D 9 3
Calculation A starts on day 2 and ends on day 6
Calculation B starts on day 9 and ends on day 15
Calculation C starts on day 15 and ends on day 20
Calculation D starts on day 9 and ends on day 11
In this example, it’s not possible to carry out all the calculations because the periods for B and C overlap. 3 calculations maximum can be carried out: A, D and C.
Input
Line 1: The number N of calculations.
The N following lines: on each line, the starting day J and the duration D of reservation, separated by a blank space.
Output
The maximum number of calculations that can be carried out.
Example
Input:
4
2 5
9 7
15 6
9 3Output:
3Thing's I am looking for in this review
- Optimizations (memory/speed)
- Bugs
A couple more things
-
Which calculations removed (duped out) is irrelevant, I only care about # possible calculations
-
Regenerating of the list for
requested_days may be slow, but having that list stored on the object becomes a memory issue. -
The tests written really weren't meant to be comprehensive, just something to get by for big mistakes. My real testing was done on CodinGame.
-
This code doesn't entirely pass, it says I need more optimizations to pass it's
Solution
Your
Testing for
But that's highly dependent on the data, it would be best to test if this works any better for you, and then apply it if it helps.
Secondly, since you're iterating over a list just to get a boolean result from a simple test. You have a perfect opportunity to use the
This returns a boolean that's
Calling
conflicts_with in the Calculation class could be more Pythonic. First of all, there's no real reason to set a temporary requested_days value, when you can just iterate over it directly:def conflicts_with(self, iterable):
"""
An attempt to reduce iterations for cross checking two Calculation dates
"""
for x in iterable:
if x in self.requested_days:
return True
return FalseTesting for
value in iterable is quite slow for a list. Sets are faster at running that test. It might be quicker to create a set like this:requested = set(self.requested_days)
for x in iterable:
if x in requested:But that's highly dependent on the data, it would be best to test if this works any better for you, and then apply it if it helps.
Secondly, since you're iterating over a list just to get a boolean result from a simple test. You have a perfect opportunity to use the
any function. It takes what's known as a generator expression. Generators are like a for loop collapsed into a single expression. In your case, it's easy to collapse down like this:any(item in self.requested_days for item in iterable)This returns a boolean that's
True if item in self.requested_days is True for any of the values of item for item in iterable. This matches your original case exactly, and is optimised to run faster than constructing a loop in Python.Calling
list() in find_conflicts to create an empty list is strange syntax, people usually just use []. If you wanted to match your set() call, then it's fine but just be aware that you don't need to do it for list although you do for set since {} will default to an empty dictionary, not a set.Code Snippets
def conflicts_with(self, iterable):
"""
An attempt to reduce iterations for cross checking two Calculation dates
"""
for x in iterable:
if x in self.requested_days:
return True
return Falserequested = set(self.requested_days)
for x in iterable:
if x in requested:any(item in self.requested_days for item in iterable)Context
StackExchange Code Review Q#117122, answer score: 2
Revisions (0)
No revisions yet.