How do I make sure 2 instances of my class have the same hash code? How do I ensure two different instances of a class have the same hash code? So when one of them is in a HashSet, the Contains function returns true.  Here is my code:  public class Position  {      public int row, col;      public override bool Equals(object obj)      {          return ((Position)obj).row == row &amp;&amp; ((Position)obj).col == col;      }  }   And here is how I use it:  HashSet&lt;Position&gt; hash = new HashSet&lt;Position&gt;(); Position position = new Position(3, 1); Position position2 = new Position(3, 1); hash.Add(position); Console.WriteLine(hash.Contains(position2));
Why is it important to override GetHashCode when Equals method is overridden? Given the following class  public class Foo {     public int FooId { get; set; }     public string FooName { get; set; }      public override bool Equals(object obj)     {         Foo fooItem = obj as Foo;          if (fooItem == null)          {            return false;         }          return fooItem.FooId == this.FooId;     }      public override int GetHashCode()     {         // Which is preferred?          return base.GetHashCode();          //return this.FooId.GetHashCode();     } }   I have overridden the Equals method because Foo represent a row for the Foos table.  Which is the preferred method for overriding the GetHashCode?  Why is it important to override GetHashCode?
Which is grammatically correct?  I can only do so much in this time.  or  I can do only so much in this time. I'm trying to create a command, which calls on an environment, which defines a custom column definition with one argument using \newcolumntype, and defines a second command, containing parameters for a table column.  Then, still within the first command, I begin the tabular environment and call the custom column, with the second command as its argument. I always get an undefined control sequence error, though. Any thoughts on why that might be?  I'm sure that was clear as mud, but I've cut it down to a minimal example, which follows:  \documentclass{article} \usepackage{array} \usepackage[table,dvipsnames]{xcolor} \usepackage{colortbl}  \newenvironment{tablecolor}{     \newcommand{\tlight}{\columncolor{SkyBlue!30}\color{black}}     \newcolumntype{C}[1]{&gt;{##1}c} }{}  \newcommand{\tablegoals}{ \begin{tablecolor} \begin{tabular}{l|C{\tlight}} Goal 7  &amp; \\ \end{tabular} \end{tablecolor} }  \begin{document}     \tablegoals \end{document}     Here's my second attempt. Different approach, same undefined control sequence error with \columncolor.  \documentclass{article} \usepackage{array} \usepackage[table,dvipsnames]{xcolor} \usepackage{colortbl} \usepackage{ifthen}  \newenvironment{tablecolor}{     \newcolumntype{C}[1]{&gt;{         \ifthenelse{\equal{dark}{##1}}{             \columncolor{Cyan}\color{white}             }{             \columncolor{SkyBlue!30}\color{black}         }     }c} }{ }  \newcommand{\tablegoals}[1]{     \begin{tablecolor}         \begin{tabular}{l|C{dark}}             Goal 7  &amp;   #1         \end{tabular}     \end{tablecolor} }  \begin{document}     \tablegoals{Check mark} \end{document} Send authors an inbox message if their question is closed Is $L^{p}$ space with alternate norm Banach? Visiting Russia as an Arab. Am I going to be hassled? Sometimes I want to underline some text, and it extends past the end of a line.  Why does \underline{} not automatically wrap my text for me ?    Also, how can I underline text so that it will still wrap ? How do I close a tkinter window? How do I end a Tkinter program? Let's say I have this code:  from Tkinter import *  def quit():     # code to exit  root = Tk() Button(root, text="Quit", command=quit).pack() root.mainloop()   How should I define the quit function to exit my application? collapse vs minimize The Time That 2 Masses Will Collide Due To Newtonian Gravity My friend and I have been wracking our heads with this one for the past 3 hours... We have 2 point masses, $m$ and $M$ in a perfect world separated by radius r.  Starting from rest, they both begin to accelerate towards each other. So we have the gravitational force between them as:  $$F_g ~=~ G\frac{Mm}{r^2}$$  How do we find out at what time they will collide?  What we're having trouble with is this function being a function of $r$, but I have suspected it as actually a function of $t$ due to the units of $G$ being $N(m/kg)^2$.  I've tried taking a number of integrals, which haven't really yielded anything useful.  Any guidance?  No, this is not an actual homework problem, we're just 2 math/physics/computer people who are very bored at work :) Passing a formatted NSDate $H_0(X,A) = 0 \iff A \cap X_i \neq \emptyset \forall $ path-components $X_i$ I've been doing some more exercises in , in particular the following:  Show that $H_0(X,A) = 0$ iff $A$ meets each path-component of $X$.  "$\Leftarrow$": Let $x_i \in A \cap X_i \neq \emptyset \forall $ path-components $X_i$. Then for any $x \in X_i$ there is a path $\gamma$ from $x_i$ to $x$. They therefore differ by a boundary: $\partial \gamma = x - x_i$ and therefore $\{&#160;x \} = \{ x_i \}$ in $H_0(X,A) $. Therefore $H_0(X,A) = 0$.  "$\Rightarrow$" Now for this direction I'm not so sure and I'd be glad if you could give me a hint. Let $H_0(X,A) = 0$. By definition, the relative homology group is calculated from the following sequence of chain groups:  $$ 0 \rightarrow C_0(A) \rightarrow C_0(X) \xrightarrow{\partial_1} C_0(X) / C_0(A) \xrightarrow{\partial_0} 0$$  Then $H_0(X, A) = H_0(C_0(X) / C_0(A)) = ker \partial_0 / im \partial_0 = (C_0(X) / C_0(A) ) / im \partial_1 = 0$.  There are two cases where this equality holds:  (i) $C_0(X) / C_0(A) = 0$  (ii) $im \partial_1 = C_0(X) / C_0(A)$  In case (i), $A \cap X_i \neq \emptyset$ for all $X_i$.  In case (ii) I'm not sure how to proceed. Can you give me a hint? Many thanks for your help!  Edit Or maybe I could do the second direction like this:  $$0 = H_0(X,A) = \oplus_i H_0(X_i, A) \implies  H_0(X_i, A) = 0 \forall i$$  $$ \implies A \cap X_i \neq \emptyset \forall i$$? $d_1(x_1,y_1)$ and  $d_2(x_2,y_2)$ are metric on $X$ and $d(x,y)$ is defined as: $$d(x,y)= \sqrt{d_1(x_1,y_1)^2+d_2(x_2,y_2)^2}.$$ I am trying to show this is a metric. Can you give me some clue about proving the triangle inequality for $d$ ? Thank you for your help. Replacing NAs with latest non-NA value How to normalize data to 0-1 range? I am lost in normalizing, could anyone guide me please.  I have a minimum and maximum values, say -23.89 and 7.54990767, respectively.  If I get a value of 5.6878 how can I scale this value on a scale of 0 to 1. Assume you have a flat table that stores an ordered tree hierarchy:  Id   Name         ParentId   Order  1   'Node 1'            0      10  2   'Node 1.1'          1      10  3   'Node 2'            0      20  4   'Node 1.1.1'        2      10  5   'Node 2.1'          3      10  6   'Node 1.2'          1      20   Here's a diagram, where we have [id] Name.  Root node 0 is fictional.                          [0] ROOT                           /    \                [1] Node 1          [3] Node 2               /       \                   \     [2] Node 1.1     [6] Node 1.2      [5] Node 2.1           /            [4] Node 1.1.1   What minimalistic approach would you use to output that to HTML (or text, for that matter) as a correctly ordered, correctly indented tree?   Assume further you only have basic data structures (arrays and hashmaps), no fancy objects with parent/children references, no ORM, no framework, just your two hands. The table is represented as a result set, which can be accessed randomly.   Pseudo code or plain English is okay, this is purely a conceptional question.  Bonus question: Is there a fundamentally better way to store a tree structure like this in a RDBMS?    EDITS AND ADDITIONS  To answer one commenter's ('s) question: A root node is not necessary, because it is never going to be displayed anyway. ParentId = 0 is the convention to express "these are top level". The Order column defines how nodes with the same parent are going to be sorted.  The "result set" I spoke of can be pictured as an array of hashmaps (to stay in that terminology). For my example was meant to be already there. Some answers go the extra mile and construct it first, but thats okay.  The tree can be arbitrarily deep. Each node can have N children. I did not exactly have a "millions of entries" tree in mind, though.  Don't mistake my choice of node naming ('Node 1.1.1') for something to rely on. The nodes could equally well be called 'Frank' or 'Bob', no naming structure is implied, this was merely to make it readable.  I have posted my own solution so you guys can pull it to pieces. Maximum total distance between points on a sphere What is the configuration (set of locations) of $n$ points on the surface of a sphere such that the sum of distances is maximum for $n=1,2,3,...$?  The sum of distances is measured by summing the lengths of every straight line (through the sphere) connecting every possible combination of $2$ points.  All the points are on a single sphere of radius $R$.  Here's a visualization:   Acknowledgements: Based on  Physics S.E. question. Image from . Summary table with a field that shows components of each summarized record I have a table with 2 fields the first is for community and the second for the main community that it belongs to, as in the image  I want to summarize the second field (Belongs_to) but with an extra field (multiple value field) that shows components of each main community, as in the image  Could anyone help me please? Does being granted a Schengen visa and not going have any consequences? I'm granted a one-entry Schengen visa and now I can't go. Will having an unused visa in my travel passport have any consequences like future visas refusal? Do I need to do anything special? What is the difference between find . and /home/user/* as an input to for command.? For example:  for var in $(find .) do echo "$var" done   or  for var in /home/user/* do echo "$var" done   In the first case for command breaks up the file with names containing spaces. While in the second case it does not. Why? Restore the state of std::cout after manipulating it