Most Popular

1500 questions
4170
votes
38 answers

How can I pair socks from a pile efficiently?

Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. This requires iterating over n/2…
amit
  • 175,853
  • 27
  • 231
  • 333
4123
votes
13 answers

grep: show lines surrounding each match

How do I grep and show the preceding and following 5 lines surrounding each matched line?
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
4093
votes
42 answers

What is the !! (not not) operator in JavaScript?

I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does? The context in which I saw this was, this.vertical = vertical !== undefined ?…
Hexagon Theory
  • 43,627
  • 5
  • 26
  • 30
4089
votes
73 answers

How can I validate an email address using a regular expression?

Over the years I have slowly developed a regular expression that validates most email addresses correctly, assuming they don't use an IP address as the server part. I use it in several PHP programs, and it works most of the time. However, from time…
acrosman
  • 12,814
  • 10
  • 39
  • 55
4080
votes
33 answers

Is there a CSS parent selector?

How do I select the
  • element that is a direct parent of the anchor element? As an example, my CSS would be something like this: li < a.active { property: value; } Obviously there are ways of doing this with JavaScript, but I'm hoping that…
  • jcuenod
    • 55,835
    • 14
    • 65
    • 102
    4073
    votes
    73 answers

    How do I generate random integers within a specific range in Java?

    How do I generate a random int value in a specific range? The following methods have bugs related to integer overflow: randomNum = minimum + (int)(Math.random() * maximum); // Bug: `randomNum` can be bigger than `maximum`. Random rn = new…
    user42155
    • 48,965
    • 27
    • 59
    • 60
    4073
    votes
    42 answers

    Create ArrayList from array

    Given an array of type Element[]: Element[] array = {new Element(1), new Element(2), new Element(3)}; How do I convert this array into an object of type ArrayList? ArrayList arrayList = ???;
    Ron Tuffin
    • 53,859
    • 24
    • 66
    • 78
    4063
    votes
    91 answers

    How to round to at most 2 decimal places, if necessary

    I'd like to round at most two decimal places, but only if necessary. Input: 10 1.7777777 9.1 Output: 10 1.78 9.1 How can I do this in JavaScript?
    stinkycheeseman
    • 43,437
    • 7
    • 30
    • 49
    4048
    votes
    60 answers

    Sort array of objects by string property value

    I have an array of JavaScript objects: var objs = [ { first_nom: 'Lazslo', last_nom: 'Jamf' }, { first_nom: 'Pig', last_nom: 'Bodine' }, { first_nom: 'Pirate', last_nom: 'Prentice' } ]; How can I sort them by the value of…
    Tyrone Slothrop
    • 40,787
    • 3
    • 17
    • 8
    3997
    votes
    137 answers

    Message 'src refspec master does not match any' when pushing commits in Git

    I clone my repository with: git clone ssh://xxxxx/xx.git But after I change some files and add and commit them, I want to push them to the server: git add xxx.php git commit -m "TEST" git push origin master But the error I get back is: error: src…
    sinoohe
    • 40,334
    • 3
    • 19
    • 16
    3990
    votes
    23 answers

    Make an existing Git branch track a remote branch?

    I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch? I know I can just edit the .git/config file, but it seems there should be an easier way.
    Pat Notz
    • 208,672
    • 30
    • 90
    • 92
    3990
    votes
    12 answers

    Proper use cases for Android UserManager.isUserAGoat()?

    I was looking at the new APIs introduced in Android 4.2. While looking at the UserManager class I came across the following method: public boolean isUserAGoat() Used to determine whether the user making this call is subject to…
    Ovidiu Latcu
    • 71,607
    • 15
    • 76
    • 84
    3976
    votes
    33 answers

    Checking if a key exists in a JavaScript object?

    How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
    Adam Ernst
    • 52,440
    • 18
    • 59
    • 71
    3965
    votes
    46 answers

    Loop through an array in JavaScript

    In Java, you can use a for loop to traverse objects in an array as follows: String[] myStringArray = {"Hello", "World"}; for (String s : myStringArray) { // Do something } Can I do the same in JavaScript?
    Mark Szymanski
    • 56,590
    • 24
    • 70
    • 87
    3945
    votes
    33 answers

    How to iterate over rows in a DataFrame in Pandas

    I have a pandas dataframe, df: c1 c2 0 10 100 1 11 110 2 12 120 How do I iterate over the rows of this dataframe? For every row, I want to access its elements (values in cells) by the name of the columns. For example: for row in…
    Roman
    • 124,451
    • 167
    • 349
    • 456