Tag Info

New answers tagged sorting

0

I think, I would hold on to your decorator pattern because it is more reusable than the selection with anonymous objects. public class DistancedAgent : Agent { public DistancedAgent(Agent source, double distance) { AgentId = source.AgentId; Latitude = source.Latitude; Longitude = source.Longitude; Distance = distance; ...

7

var agents = db.GetAgents(); if(agents == null || agents.Count == 0) { Console.WriteLine("No Results"); } You should exit your application/code block when you detect an invalid state. Here even if "No Results" is printed, the app will still run into a NullReferenceException or IndexOutOfRangeException in the next few steps. Also, talking of ...

4

I'd say you have the following options: Keep that new class if you think it's relevant in your business. Here, it should be important to know if the logic of calculate the distance should be on that web service (I assume that 'ws' variable means that) or within your model (i.e., the 'Agent' class). Use an anonymous class if you think you won't pass that ...

1

You're ignoring the Date-part of the timestamps; it doesn't sound like that's on purpose. (Also, the year is missing altogether, which should make us quite nervous.) Also, let's use explicit datetime utilities and regexes. import datetime import re timestamp_regex = re.compile("[^:]+:\d\d:\d\d") def convert_time(logline): stamp = timestamp_regex....

0

Since lists are passed by reference, the two lists that are passed as arguments will be half-empty after the function returns. a = [1, 2, 4] b = [3, 5] merge_lists(a, b) print(a) # is empty now but shouldn't print(b) # only contains 5 now Therefore you should not use list.pop at all but instead iterate over the lists via indexes, since these don't modify ...

4

Python's style guide says to use lower_snake_case for variable names. You can use a turnery to assign the desired list to a variable to remove the duplicate code. L1.pop(0) runs in $O(n)$ time, making your code $O(n^2)$. You can fix this by using collections.deque. import collections def merge_lists(list_1, list_2): list_1 = collections.deque(...

1

It would make more sense to store the distance with the point so that you don't have to calculate it each time. class CoordinatePoint implements Comparable<CoordinatePoint> { int[] coordinates; int squaredDistance; private CoordinatePoint(int[] coordinates, int squaredDistance) { this.coordinates = coordinates; this....

1

This task sounds as if it came directly from an advertisement for the Java 8 streams API: Sort the points by their distance Take the minimum k points This boils down to the following code: static int[][] kClosest(int[][] points, int k) { return Arrays.stream(points) .sorted(Comparator.comparing((int[] point) -> point[0] * point[0] + point[1]...

-1

alist= int (input (" enter the values of the list")) #list formed will have the keyboard values n= len(alist) # length of the list for r in range( n-1): for c in range(0, n-r-1): if alist[c] > alist [ c+1]: alist [ c] , alist [ c+1] =alist [ c+1] , alist [c] #toswap print( " sorted list",...

1

Reduce the inner loops iteration count. The problem is the inner loop is looping over too many items. The result is that you spend too much time processing data you know is irrelevant. As the function should exit if it detects a position has made over 2 bribes, you need only have the inner loop check positions down 2 from the item you are checking, and not ...

Top 50 recent answers are included