# Tag Info

49

For the record, this is Project Euler problem 75. 1. Admonition You write: Please note that I am not looking for a different calculating method (I am sure there is one, but, for me, Project Euler is about finding your own methods. Using yours would, to me, be cheating.) Abandon this attitude! A key part of programming — just as important as coding — ...

22

1. Review The large bounty suggests that getting a review of this code is important. But there are some other things you could have done to make reviewing this code more productive: Give us some more context. What is the purpose of this code? What do these rotated rectangles represent? Tell us how the data computed in main relates to your problem. Is this ...

19

A triangle has, by definition, three sides. I find it therefore weird to take a single sides argument, which could be of any size. This opens you up to obscure bugs, such as these ones, which are not covered in your tests: >>> is_triangle([1,2,3,4]) True # ? >>> is_triangle([1, 1]) True # ??? >>> is_triangle([float('nan')]) ...

18

You have common code, which moreover has applications beyond this one, so should you not pull it out into a function? Then you can reduce overlap to def overlap(r1, r2): '''Overlapping rectangles overlap both horizontally & vertically ''' return range_overlap(r1.left, r1.right, r2.left, r2.right) and range_overlap(r1.bottom, r1.top, r2....

15

First of all, let's define the problem precisely. According to your comment, this is the behavior you want: You are correct in believing I am expecting it to be relative to 12 o'clock, and for it to move clockwise. However, your code doesn't actually behave that way. Your angle() function, as written, computes the orientation of point relative to this, ...

15

Comments Honestly, too many of them. Object-orientation I don't see why vector components are made private. A client can freely and independently modify them via Set method. There's no internal state to maintain, no invariant to protect. I recommend to make them public and eliminate Set(), X(), Y(), Z() methods altogether. Math I am quite surprised by the ...

15

C++14 Its 2014 most modern compilers now support C++14 so you should use it. This code is still very C++03. For this class this simply means adding move semantics (and nothrow on swap). To add move semantics you need to add a move constructor and move assignment operator. vect(vect&& other); vect& operator=(vect&& other); Potentially ...

15

Here are a few of the high- and low-level ideas: too many comments. Remember, comments tend to age and outdate, they need to be maintained as the code changes. And, if they are over-used, they hurt readability, not improve it. See more at Coding Without Comments convert some of the comments preceding the functions and methods to proper documentation strings ...

13

1. Introduction Thanks for running the profiler. As you can see from the output, most of the runtime is being spent in your containing_tet function. The first thing to say is that you have made this question unnecessarily difficult for us because your functions have no documentation. We have to read and reverse-engineer your code to try to figure out what ...

13

General C++ advice There's a few general suggestions here that are for the most part independent of your algorithms. using namespace std using namespace std; This is usually a code smell, generally speaking the only time you would really want to do this is when you are making some small throwaway program for testing a concept or making an example (or ...

13

In computer geometry, always use vectors if possible! Code gets more complicated if you try to work with Cartesian co-ordinates $(x, y)$ or with line equations $y = mx + b$. Here, for example, you have special cases for horizontal lines, $m = 0$, and vertical lines, $m = \infty$. So let's try to program this, sticking to vectors throughout. ...

12

Firstly, even though your commenting style may help you read your code, it is definitely overly verbose in places. Comments like: // Default Constructor ... Vector() are obvious, and more experienced C++ programmers would likely find it bothersome. In regards to inline functions: I use inline functions because I heard that the C++ compiler, while ...

12

Data types Why not use a simple point struct instead of std::pair<int, int>? It doesn't have to do anything, just a simple one would suffice: struct point { int x = 0; int y = 0; }; This would increase readability and generally might make the code more bug resistent. Remember, std::pair<int, int> can represent many different things: ...

11

I want to know how I could make the method look prettier, and have better 'coding practices' though avoid single letter variable if they aren't iterator variables. If you or Sam the maintainer needs to come back to this method in a few months, neither you nor Sam will grasp at first glance what the variables are about. avoid shortening of method names. ...

11

Any two rectangles can fit into a third rectangle (without overlapping with each other) only under 2 cases: As mentioned in the question, consider: x,y -> length and height of first rectangle into which below two rectangles must fit. l1,h1 -> length/width and height of second rectangle. l2,h2 -> length and height of third rectangle. Note: You could use ...

11

This is an addendum to Oscar's answer. Just doing some math. Let t = 0 be 12 o'clock, when all hands are coinciding and the area is 0. We need to calculate the area as a function of t. Let s, m, h be the angles of second, minute and hour with respect to the trigonometric reference. Then s=\frac{\pi}2-\frac{\pi}{30}t\\ m=\frac{\pi}2-\frac{\pi}{1800}t\\ h=\...

10

I would simply apply a logic transformation. Here's your original, verbatim: def overlap(r1,r2): '''Overlapping rectangles overlap both horizontally & vertically ''' hoverlaps = True voverlaps = True if (r1.left > r2.right) or (r1.right < r2.left): hoverlaps = False if (r1.top < r2.bottom) or (r1.bottom > r2....

10

Naming Your "vector" is not really like std::vector, but vect sounds like it. It would be more intuitive to rename it some thing different, for example vectorstats, or just vstats, or something. The m and s variables are terrible. size I don't really see the point of the s variable. Why do you want to count the size yourself? Why not use m.size()? That ...

10

Generation of vertices You could use itertools.product to generate vertices. You can replace: for i in range(0,2**n): temp = list(bin(i)) temp.remove("b") temp.remove("0") while len(temp) < n: temp.insert(0,"0") vertices.insert(0,temp) with: import itertools vertices = list(itertools.product('10', repeat=n)) Import math ...

10

Well, you are manually testing every point $(X_i, Y_j)$ to see where it falls. You shouldn't, really. Remember the sage advice: If in doubt, sort. A better algorithm goes: Read all X-coordinates. Change them to squared distance from center-line. Sort them. Read an Y-coordinate. Calculate the squared-distance from center-line for X-coordinates leading to ...

10

Use types consistently I prefer double over Double, but it doesn't really matter which you use as long as it's consistent. Mixing them, though, means unnecessary cognitive load. Naming ...(y.X - x.X); ...(y.Y - x.Y); In my opinion, x and y are about the worst possible names for a type which has members called X and Y. If you insist on one-...

9

The cross product of two vectors is perpendicular to both vectors, unless both vectors are parallel. So you could simply take the cross product of your first vector with (1, 0, 0), unless it is parallel to (1, 0, 0), in which case you could use (0, 1, 0). If you use lists rather than dedicated classes with attributes and are willing to use numpy, this gets ...

9

Proposed solution Okay BoardPosition should be an immutable data structure like this: public class BoardPosition { public final int x; public final int y; public BoardPosition(int pX, int pY) { y = pY; x = pX; } } You should never specify which specific implementation of List you want but ...

9

Try another algorithm Instead of considering triangles, you could simply consider lines starting from a point. For each point, compute the distance between this point and every other point; you will get a number of distances. Now, for every distance from a point, count the number $n$ of equal distances, and add $n*(n-1)/2$ to the number of isoceles ...

9

I don't like any of these answers. In particular, the accepted answer is simply wrong. Let's start by critiquing the interface: List<Point> OrderByDistance(List<Point> pointList) The contract is: the list must contain at least one element, the list is destroyed (!!!) by the method, the first element is special, and the result is a mutable ...

8

Some ideas: When comparing points, you can use the fact that (,) is lexicographically ordered: comparePoints :: Point2D -> Point2D -> Ordering comparePoints a b = compare (y a, x a) (y b, x b) Vector2D and it's accessors. It's structurally equivalent to Point2D but I want to typecheck its usage. Hence I use newtype and not type, but it makes me ...

8

Hmm. The lack of comments make it slightly non-obvious what is happening. The inverted conditions not iszero(…) don't make it any easier to understand. The same set of these conditions occurs two times, which is a bit confusing. I also don't know where the iszero(…) function is from, and will replace it by … == 0 in the following. Now a bit of math first. ...

8

Jaime beat me to it! Anyway, here's an optimised version that uses the same idea and doesn't call numpy.cross: def perpendicular_vector(v): if iszero(v.x) and iszero(v.y): if iszero(v.z): # v is Vector(0, 0, 0) raise ValueError('zero vector') # v is Vector(0, 0, v.z) return Vector(0, 1, 0) return ...

8

Don't need to typedef struct in C++ like you did in C. struct are already in the normal namespace (not their own). Also its nice to indent the code to make it nice to read. struct Bridges { int number; double x1; double y1; double x2; double y2; int degree; bool deleted; }; Don't know what a Row is here. typedef vector<Rows&...

Only top voted, non community-wiki answers of a minimum length are eligible