Does a piece have to be in a particular key?

Question: There have been many questions on this site relating to keys.
This one asks what 'rule' is there that says a piece actually has to be in a particular key.
Obviously most are, and will have the corresponding signature for that key, albeit with modulations that quite often happen, bringing the inevitable accidentals.
But - is there a necessity, which a lot of questioners seem to feel there is, for any piece to be 'in this key'?
It's to an extent a rhetorical question, as I think the key signature is just there to be of help, and remind players of which set of notes will be most prevalent in this piece. But minor stuff, and modal stuff, will use a key signature which won't give too much of a clue, but also, why is there all the compunction to find/know what key a piece is in?
Original tag was a little misleading - sorry!
Comment: I'm not sure what you're really asking. Why people want to know the "key" of non-major/minor system music (rather than just saying the tonic.) Why do most major/minor key works start/end in the same key, or something else?
Comment: Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackexchange.com/rooms/138513/discussion-on-question-by-tim-does-a-piece-have-to-be-in-a-particular-key).
Answer: Two broad-level points:
1: Music theory doesn't proscribe, it describes. As you know, whenever we talk about "rules," there are no "music theory police" to arrest you if you break them. In the big picture, music theory "rules" are like scientific principles. An apple doesn't fall to earth because Isaac Newton made up a rule that it should; rather, science observes and describes what it sees happening. Similarly, music theory is our way of observing and explaining what we see happening in actual practice. A good mantra is "theory follows practice." When practices mutate, theoretical frameworks mutate to explain and codify them.
And yes, when teaching these principles, pedagogical traditions often frame them as "rules" (don't write parallel fifths), but it's important to remember that these rules are bounded within unspoken contexts. It's like saying, in a discussion of soccer, "Don't hit the ball with your hands," and the unspoken context is "because if you do you'll be playing volleyball."

As others have pointed out, the notion of being "in a key" is part of an unspoken context limiting the discussion to the Western tonal tradition, and it ignores vast swaths of human music-making. Medieval music, Western post-tonal music, contemporary non-classical genres, and the musics of many traditional cultures don't fit easily within discussions of being "in a key."

And finally, to address part of the question: "why is there all the compunction to find/know what key a piece is in?": Well, one could think of a few answers: It might be just because the questioner is used to these assumed contexts, and hasn't been introduced to ideas like modal mixture or polytonality. But to take a more generous view, we can benefit in a Schenkerian kind of way by asking ourselves the kind of questions that always power music theory: "Looking at this great big complicated tangle of music, how can I explain it? How can I boil it down to big, overarching principles, instead of just shrugging and saying 'Well, it's a bit of this and a bit of that'? How can I identify a solid core to it, and then explain the outlying or contradictory bits in light of that central narrative?"
Comment: Peter's right: regardless of how good or bad the parallel 5ths and octaves sound, the examiners *will* mark them down. I also get nasty comments on my Musescore scores if I use parallel 5ths or octaves in Baroque(-style) music. In practice, there *is* a "music theory police".
Comment: Music theory both proscribes (parallel fifths, for example) and prescribes (cadential formulas, for example).  Of course, it also describes, as you say.  The relationship between theory and composition isn't unidirectional.
Comment: @phoog I have to disagree. Parallel fifths aren't _forbidden_ by 'music theory', they're not often used in Baroque and Classical music simply because they don't sound good most of the time. But composers who knew how to use them have done so. Students learning harmonisation are discouraged from using them, and hidebound examiners will mark them down without listening to whether they actually sound good.
Comment: At any rate natural sciences like physics aren't a good metaphor for music theory, because physics can't possibly be influenced by anything we humans do – the world just is what it is, and all we can do is discover phenomenona that are already there. On the other hand, music theory clearly does have at least some influence on what phenomenona composers put into their works. A better analogy would be _engineering principles_.
Comment: @phoog And I'd still quibble about "big picture" vs "small picture": In the small picture, Fux et alia talk in terms of "rules," but the unspoken context is "do it this way [if you want to be doing it within this context that has been established]." Play soccer this way, if you want to be playing soccer. Problems arise when people observe what is in fact volleyball—or maybe Calvinball—and wonder why it's breaking the rules of soccer. To pursue the sports metaphor, as new practices and technologies arise, the "rules" adapt to address them (steroids, "tech" swimsuits, triple-twisting backflips).
Comment: @Peter if you have read a counterpoint book from the baroque or classical period or earlier that does not forbid parallel fifths or octaves, I would be interested to know its name.  I would also be interested to know which examples you have in mind of baroque or classical pieces that use parallel fifths or octaves.  It's all well and good (@ Andy) to say that music theory is descriptive rather than proscriptive, but that flies in the face of centuries of actual practice by music theoreticians.
Comment: @phoog OK, theorists can make up rules, and it's the composer's job either to follow them or to decide not to. I was a practising musician, and I have to admit that I cannot bring to mind any examples, but remember reading (OK, anecdotal now) that there are examples in JS Bach which sound fine. Another possibility is the 2nd bar of Mozart's 'Kegelstatt' trio if you play the apoggiatura on the beat - B flat-E flat moving to A flat-D.  If no-one broke the rules we'd still be in the baroque era! Look at the stories about now-respected composers' student days.
Answer: Sometimes it's useful to think of a piece of music being in a particular key, sometimes it isn't.
Around the turn of the 20th century, the 'classical' musical world (well some of it) decided they'd had enough of 'keys' and tried other forms of structure ('12-tone' music etc.)
In the second half of the 20th century, the jazz guys - previously pretty well committed to 'ii-V-I' harmony - discovered 'modes', which in that context largely meant keys that lacked the sharpened leading note that enabled tritones, dominant 7ths and all that 'functional' stuff.  (Blues were a step down that path, but you generally still pretty firmly knew what key you were in with a blues.)
That's a lot of stuff to throw into the mixing pot of today's music, whether a full-blown movie score or a guitarist-songwriter deciding what chord shapes to play!    Plenty of music is still tonally unambiguous.   A whole lot more floats between major and relative minor (C major/A minor) and uses the 'natural minor' scale (no G# in A minor), making it hard to pin down just which note (C or A) is the tonic at any given point.  Not a lot of 'ii-V-I' going on!   Musicians who have studied 'modes' may stick in one - Dorian, Mixolydian, Phrygian... - for a whole piece. Others may roam more freely.
Students crave order.  They want to know 'why does this work?'   Well, very often now, even in a simple pop song, the 'why' may have little to do with functional harmony - chords with a tendency to resolve towards a tonic.   The connection between two chords may be nothing more than having a note in common.
It's also worth pointing out that a whole lot of music doesn't fit into a 'chord sequence' at all.   And that includes modern 'popular' music.
Comment: @Tim   Coo! So I have!  Do I get a hat?
Comment: No, that's only a winter fetish.
Comment: If you mean that the concept of key requires leading tones or something, then that's not the de-facto actual modern meaning of the word. Nowadays key really only means a center pitch and usually the third above it. The style of organization of pitches around that center pitch can be modal, but the mode's root would be marked as the key. Why has this change of meaning happened? Maybe it's because non-common-practice harmonic styles have become such... common practice nowadays, and there has been no central authority to introduce a better term that would have left the old meaning of "key" alone.
Comment: Congratulations, by the way, in getting to 2nd place in upvotes!
Answer: Trivially, no
Unless you have a very narrow definition of "piece" (of music), there are many, many existing pieces that cannot reasonably be described as being in any key.
Comment: One gets the impression that many questions here are posed by people with an almost complete lack of exposure to music of the 20th century, pop & film excepted.
Answer: I deleted my first answer, because it concentrated on the wrong question.

is there a necessity, which a lot of questioners seem to feel there is, for any piece to be 'in this key'?

Key means a harmonic center pitch and a certain interval structure around that center where the harmony would feel to be in a resting position and not leaning off balance. A sense of harmonic center and balance is created by organizing notes in a certain way. If we create a piece by randomly placing notes in pitch and time dimensions, it is highly unlikely that the result can be perceived as being in a key. Even if we make the time dimension non-random, the pitches still need to be organized in a very particular way to create the feeling of a center.
The concept of key is a tool for operating in musical situations, for reasoning about and dealing with notes and chords. It is an organizational scheme, for keeping things under control. If you have many voices and notes without a coherent idea about their relationships, it will quickly become an unmanageable chaos. Use a key as your guide!
The tonal center that music creates - if it creates a tonal center - is partly in the ear of the listener, it is subjective. How a certain person feels about tonality and a possible harmonic center, depends on the person, and it cannot be objectively calculated with certainty. A possible center and the organization of notes around it can also change and move many times during a piece, or it can be ambiguous or disappear completely. "Now you see it ... now you don't."
So when "is" a piece in a key? It "is" in a key when people find it useful for some purpose to say that it is in a key, or to see it as being in a key. Key is an organizational concept. Just like in football it is possible to organize the players' roles in different ways, it is possible to organize musical notes in many different ways, and only some them are based on the concept of a key.
What do you want to do? Play the piece mechanically? It may be enough to be able to read, you don't need to "understand" what you're playing, or to know what the composer or arranger thought when writing the notes.
Do you want to create variations of the tune, or play something over it? Then it is good if you can identify a possible key, or keys, for the passages you intend to manipulate. Did the composer think about a key when composing? Maybe, maybe not. If you cannot identify a key, it is possible that the composer had some other kind of scheme in mind. If you want to stay in the same or compatible style and feeling, you have to figure out what the scheme is.
A friend of mine once asked for help with an electronic tune he had made. It sounded weird to me - kind of tonal, but then again not. After playing over it I figured out the problem or unusual feature about the piece - the bass line created a feeling of F minor being a center, but other instruments seemed to center on G minor. Interesting! For playing that piece on the piano, it would be useful to think about not just a key, but two keys. F minor for the left hand and G minor for the right hand. I adviced my friend to practice playing tunes by ear, and to find chords and keys by ear, because it was obviously his intention to have the instruments have the same center, but he lacked the organizational tools and skills to keep his notes under control and in the desired formation. The concept of key would have helped him a lot!
Comment: Thank you so much for going to the trouble of generating another answer - it's good that some contributors do more than merely provide comments - and this one is another slant on what the question is *supposed* to be about. I appreciate your time and effort. +1
Comment: @Tim No problem! I'm learning to talk about music-making. Writing makes me organize my thoughts, and I learn a little bit about how other people think as well.
Comment: *It "is" in a key when people find it useful for some purpose to say that it is* —Mm, I like that. That's a nice, pragmatic, *sociological* way of explaining the behaviors of those explaining the music. Similar to my favorite answer to "what is music and what isn't" (music is that which anyone perceives as or construes as music).
Answer: Usually music is written in a key, but there are of course songs that break these "rules", and lack a tone, or a key. The definition of Atonality (according to Dictionary.com) "is the absence of key or tonal center.", or according to Encyclopedia Britannica "the absence of functional harmony as a primary structural element."
Britannica also notes that "Schoenberg's song cycle Pierrot Lunaire (1912) and Alban Berg's opera Wozzeck (1925) are typical examples of atonal works." So a piece of music doesn't necessary have to be in a key to be music.
But why do people use keys? According to StudyBass,

In music a key is the major or minor scale around which a piece of
music revolves. A song in a major key is based on a major scale. A
song in a minor key is based on a minor scale.

This website further explains what keys are and how they are used in music
Many songs are written with not just one key (or limited to just sharps, or just flats). For example, some songs that I see switch from C major to E♭ major to F# major. That is totally possible in music too. Most of the time, switching between keys makes the music seem smoother, or more refined, in a sense.
Also, about finding/knowing what key a piece is in, Guitar Gear Finder states:

Finding the key of a song is an important way to better understand the
music you play. A song's key can tell you a lot about how the song was
written as well as what chords and notes you're likely to play in it.

Personally, I couldn't care less. I don't usually need to find the key of a song, because it doesn't really help me (though it might help others) at all.
Comment: On the occasions I'm in the house band, a key is very helpful, as it means I can play along from the start rather than wait to find out. As long as they tell me the true key!
Comment: This answer seems to confuse key and key signature, which are not the same thing.
Comment: @Tim That's true. I don't usually perform, and I've never been to a band before, so I haven't had much real experience with using a key!
Comment: @piiperiReinstateMonica Thanks so much for your feedback, I did my best to clear the confusion.
Comment: It would further improve the answer, if you could really separate the concept of key signature from it. The question is valid regardless of staff notation. If we assume that nobody in the world reads or writes staff notation, and so nobody has a need for accidentals or key signatures. Let's say everyone uses a numerical chromatic equal-temperament note-naming scheme with numbers 0-11, and accidentals do not exist at all. Or everybody plays six-string guitars and they just think in terms of fret numbers. People still want to use keys i.e. tonal centers and "tonic chords". Why?
Comment: @piiperiReinstateMonica Thanks so much for the detailed feedback. I've fixed it now. Is it better?
Answer: All tonal music has a key. Not all music is tonal.
All musical works that are tonal are in either a major or minor key. This includes all works in the baroque style, the classical style and romantic style, i.e. everything from Bach to Wagner (this is referred to as the common practice period).
Furthermore, if you believe Heinrich Schenker, in order to be a "master work," the piece must begin and end in the same key.
That being said, there are many types of music that are not tonal.

Music prior to the baroque period was generally modal, i.e. using the diatonic scale but not restricted to major and minor, which are just two of several possible modes.
Music in the late romantic period and beyond tended toward a form of "dissonant tonalism" which so embellished the tonal structure that the key might not be discernable.
Some twentieth-century music is purposefully atonal, i.e. going out of the way to avoid having a key. The epitome of this is serial music, which employs all 12 tones equally, and purposefully avoids cadence or other structures that might imply a key or tonal center (example).
Non-western music often uses a completely different system, such as Arabic maqam, which doesn't even use the same scales as the west. Obviously this type of music cannot have a key in the traditional sense.
Some types of music don't even seem to have a scale. For example Ligeti Lux Aetera.
Bitonal and polytonal music have 2 or more simultaneous keys (example).

Popular music today is a mix. Some would call it post-tonal. Some of it is tonal, some of it is not, and much of it borrows heavily from tonality but breaks many of the rules or integrates different styles (example). It would be a mistake to say that every piece must have a key, but keys are often used by composers because tonal music has a such a rich language and is familiar to so many listeners. Also, musicians are generally pretty good at playing it, because they practice scales and arpeggios which are meant essentially to prepare you to play tonal music. But there is no inherent reason why music has to be tonal or have a key. Much of it isn't, and doesn't.
Comment: I've heard a fair few pieces/musical themes (pretty much all of them from video games) where each section (or, at least, nearly every section) of it is tonal but I either cannot satisfactorily assign the entire piece to a single key or the single key label I give it looks *really* shaky - e.g. "VS. Star Dream" from *Kirby: Planet Robobot* (ambiguous -> C minor -> F# minor -> B minor -> D minor -> G# minor -> C# minor -> E minor), "The Azure Arbitrator" from *The Legend of Heroes: Trails to Azure* (D minor -> E minor -> C# minor -> D minor -> E minor -> C# minor).
Answer: The history of music provides you the answer: no, they don't have to be in a key as we observe in Schoenberg's beautiful atonal songs. But in tonal music -most songs are tonal- yes there is a tendency to be in a key. Some say the need to hear the music around a central tonic is an innate function, some say this is because of our past musical experiences/exposures. The important thing to notice is a piece can move to several keys, this is the nature of music, but there is a 'sense of key'. Please note that most people memorise and repeat the melodies which are in a key and are consisting of mostly stepwise motion more easily than the ones which are not in a key and contains many skips and leaps.
Answer: Back in the day, with Baroque and Classical(-era) music, pieces were in certain keys, and their individual keys were part of their names. Before the advent of well temperaments, some keys were much rarer than others due to producing awful wolf intervals with the then-established tunings (e.g. just intonation, quarter-comma meantone) and the old standards for A notes (e.g. the oft-rumoured A415, the A465 apparently used in Baroque-era North German organs according to sources like https://en.wikipedia.org/wiki/North_German_baroque_organ_in_%C3%96rgryte_Nya_Kyrka). Taking advantage of well temperaments, The Well-Tempered Clavier contains pieces in all 24 major and minor keys. At the very least, there was an interest (however rare) back then in purposefully composing music in every key you can get your hands on, and this requires caring about which key your piece is in.
Despite Baroque and Classical-era music commonly switching keys in their middles, and even not ending in the keys they began in (this is most common with minor-key sonata-allegros that end in the tonic major), they were still titled with their keys or at least the overall keys of the multi-movement works they belong in. Plenty of examples are still reasonably popular today: the infamous Toccata and Fugue in D Minor, BWV 565; Beethoven's Symphony No. 5 in C Minor; Mozart's Piano Sonata No. 16 in C Major, K. 545 "Sonata facile"; Elgar's Pomp and Circumstance March No. 1 in D Major; the list goes on...
This older interest in labelling music with their singular keys persists to the present day, even if it may have lost some of its value, and even in cases where half of a piece is not in any one key (so any key label is at least half incorrect). For example, IMSLP labels Sousa's "The Stars and Stripes Forever" as being in E flat major in https://imslp.org/wiki/The_Stars_and_Stripes_Forever_(Sousa%2C_John_Philip) even though, not only does this Sousa march end in A flat major instead, more than half of it is in A flat major. In fact, both Sousa marches and ragtime pieces commonly end in the subdominant keys of the keys they begin in (one such ragtime piece is Joplin's "Elite Syncopations").
Perhaps one of the most compelling reasons today to know what (overall) key a piece is in is that some keys are easier to play on certain instruments or with entire ensemble types than others. For example, despite Shostakovich's Festive Overture, Op. 96 being in A major overall, two prominent concert band arrangements of it change its key to keys concert band instrument players (e.g. B flat clarinet, E flat alto saxophone) find easier to read due to having fewer accidentals and simpler key signatures: the Donald Hunsberger arrangement in A flat major (according to https://en-academic.com/dic.nsf/enwiki/4504206, heard in https://www.halleonard.com/product/127014/festive-overture-op-96) and the Donald Patterson arrangement in B flat major (according to https://www.marineband.marines.mil/Portals/175/Docs/Audio/Educational_Series/feste/feste.pdf, heard in

). In fact, the US Marine Band Feste PDF for the Patterson arrangement outright says that A major is "not nearly as friendly to most band instruments" and B flat major ensures that "technical passages are more easily negotiated by all the instruments".
Comment: I already wrote an answer to the question you're addressing here, but I deleted the answer, because that's not what Tim intended his original question to be about. ;) The real question is, do pieces have to have keys. Or since that's such a trivial question - of course not, there is atonal or keyless music, as we all know - maybe the more important question is, why do so many people act as if writing and playing in keys was the only possibility, or why do they insist on trying to find a key for anything and everything.
Comment: @piiperiReinstateMonica - that's about the top and tail of the question! Glad *someone* understood me...
Comment: @piiperiReinstateMonica - Yeah, this answer actually leans towards answering "why do they insist on trying to find a key for anything and everything". I actually found a fairly nasty anecdotal example earlier this week for why a certain musician might want to know the key of everything they play (spoiler: they played the music in more difficult keys out of tune).
Answer: It is not strictly necessary. You do have music with a loose adherence to keys. The adherence to keys gives a structure to music. A Tonal foundation, but that does not mean you cannot have music without a tonal foundation.
It all just gets a bit loose if you are not in a specific key, although loose does not mean bad. It is just one of many effects you can employ. It is not about what is wrong and what is correct it is about what approach best fits the song and the emotions you are trying to convey.
There are also just pure practical considerations. A harp abhors sharps. You pluck the strings on a harp towards your body, hence flats are easy but having to go away from your body and think about sharps is hard.
Choosing keys in Piano music is important as to how the fingering of the key you play affects the composition. Your fingers are unfortunately capable of certain things.
If you are in a band you have to ask yourself what exactly is the singer capable of. Singers have specific ranges and keys that fall comfortable in their voices. Playing loosely according to a key can have an influence on them, you have to consider that.
In closing, I'm a firm believer that standard music theory is standard for a reason. Yes, it may be interesting as an academic pursuit to discuss the ideas about certain re-imaginings of what is standard, for the most part, for people who make music for public consumption. People who create with the idea of selling for a profit. Standard harmony is simply just a better bet for getting your stuff out there.
Comment: "You pluck the strings on a harp towards your body, hence flats are easy but having to go away from your body and think about sharps is hard."   Eh what?   That's like saying 'you play all notes on a piano by pressing the keys DOWN therefore...' well, therefore what?
Harps are fine with sharps.  They have to think a bit what to do if you write them a double sharp or double flat though!
Here's a harpist doing the 'pedal dance'.  Impressive, but not outlandishly so.  Think what a church organist does with their feet in a Bach fugue.  https://www.youtube.com/watch?v=bMYjCRbxyX8


Maximizing dimensions of a can with minimum cost but no given relation of cost

Question: A cylinder has a given volume of 1 cubic meter. The cost of constructing top and bottom of the cyl. is twice the cost of constructing the sides. What are the dimensions of the most economical can?
I have to find the maxima for dimensions depending on cost, but since there is no relation between the cost and the area, I am lost here. It isn't working if I take cost to be x/meter square.
Ans. given : radius $= \sqrt[3]{\frac1{4\pi}}$ m, height $= \sqrt[3]{\frac{16}{\pi}}$ m
Comment: Substituting from the constraint, I'll get function C in one variable, and then I'll minimize it, thus finding the variable (whether R or H).
Then, I can find the other dimension from V too. I hope I'm right?Thank you so much!
Comment: Welcome to MSE. It will be more likely that you will get an answer if you show us that you made an effort.
Comment: Follow the given hint and show your work here, we'll check to it. Good work!
Comment: @Dee Yes exactly! You should always try to put down the equations you have at disposal and see whether or not you have sufficient information to determine a solution. Don't stop for a single doubt. Bye
Answer: HINT
From the givens, we have the following set up

top and bottom area $A_1=2\pi R^2$
lateral area $A_2=2\pi RH$
total cost to minimize $C=2kA_1+kA_2=4k\pi R^2+2k\pi RH=2k\pi(2R^2+RH)$

with the constraint

$V=\pi R^2H=1$


Alinhar ícone com texto verticalmente

Question: Como faço para deixar o ícone alinhado com o texto?

.material-icons {
    width: 24px;
    overflow: hidden;
}
.material-icons.tiny {
    font-size: 1rem;
    width: 1rem;
    overflow: hidden;
}
.material-icons.small {
    font-size: 2rem;
    width: 2rem;
    overflow: hidden;
}
.material-icons.medium {
    font-size: 4rem;
    width: 4rem;
    overflow: hidden;
}
.material-icons.large {
    font-size: 6rem;
    width: 6rem;
    overflow: hidden;
}
.material-icons.extra-large {
    font-size: 10rem;
    width: 10rem;
    overflow: hidden;
}
 <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

errorOps...

Answer: Use vertical-align: middle;.

Nota: uma coisa importante, a propriedade vertical-align só funciona com elementos cujo o valor display seja inline ou inline-block, quando usar float ou block o vertical-align não terá efeito, pois ele é usado para elementos que tenham comportamento "inline".

O seu CSS deve ficar assim:

.material-icons {
    width: 24px;
    overflow: hidden;
    vertical-align: middle;
}
.material-icons.tiny {
    font-size: 1rem;
    width: 1rem;
    overflow: hidden;
}
.material-icons.small {
    font-size: 2rem;
    width: 2rem;
    overflow: hidden;
}
.material-icons.medium {
    font-size: 4rem;
    width: 4rem;
    overflow: hidden;
}
.material-icons.large {
    font-size: 6rem;
    width: 6rem;
    overflow: hidden;
}
.material-icons.extra-large {
    font-size: 10rem;
    width: 10rem;
    overflow: hidden;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

errorOps...

Comment: vlw, obrigado. era isso mesmo

Alinhar ícone com texto verticalmente


Effecient way to compare data tables

Question: I have below c# method to compare two data tables and return the mismatch records.
public DataTable GetTableDiff(DataTable dt1, DataTable dt2, string columnName)
{
    var StartTime = DateTime.Now;
    dt1.PrimaryKey = new DataColumn[] { dt1.Columns["N"] };
    dt2.PrimaryKey = new DataColumn[] { dt2.Columns["N"] };

    DataTable dtDifference = null;
    //Get the difference of two datatables
    var dr = from r in dt1.AsEnumerable()
             where !dt2.AsEnumerable().Any(r2 => r["N"].ToString().Trim().ToLower() == r2["N"].ToString().Trim().ToLower()
                 && r[columnName].ToString().Trim().ToLower() == r2[columnName].ToString().Trim().ToLower())
             select r;

    if (dr.Any())
    {
        dtDifference = dr.CopyToDataTable();
    }
    return dtDifference;
}

This code works, but it takes 1.24 minutes to compare 10,000 records in the datatable.  Any way to make this faster?
N is the primary key and columnName is the column to compare.
Thanks.
Comment: By calling `dr.Any()` and then `dr.CopyToDataTable()` on an `EnumerableRowCollection`, essentially you are running your heavy query twice. You could prevent this by materializing the query, appending the method `ToList` or `ToArray`.
Answer: First I would ask if you have tried this in a simple for/foreach loop instead and compared the performance?
At the moment you are creating a new Enumerable and then copying to a datatable.
If you use a for/foreach loop then you can compare and copy in the same iteration.
You should also look at the string comparison.  At the moment you are trimming then converting to lowercase.  This will allocate new memory for each operation for each string as strings are immutable.  So in your where statement you are basically doing this (up to) 8 times per iteration.
I would also ask if you really need Trim()?  Is it likely that one DT will have a space at the front of the string and the other not?  Or will a comparison still be true?  Don't trim strings unless really needed.
Then you should use case insensitive string comparison rather than converting ToLower.  This will be quicker. According to MS StringComparison.OrdinalIgnoreCase is better performing.
Do these and then compare performance and see how much difference you have
See also:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings
Update:
This intrigued me, so I went back and done some tests.
I generated 10,000 rows of random(ish) data in two datatables where every second row would match and executed your comparison vs a simplified for loop comparison with a String comparison like this:
  for (int i = 0; i < dt1.Rows.Count; i++)
  {
      if (dt1.Rows[i]["N"].ToString().Equals(dt2.Rows[i]["N"].ToString(), StringComparison.OrdinalIgnoreCase)
          && dt1.Rows[i][columnName].ToString().Equals(dt2.Rows[i][columnName].ToString(), StringComparison.OrdinalIgnoreCase))
      {
          dtDifference.Rows.Add(dt1.Rows[i].ItemArray);
      }
  }

Your code = 66,000ms -> 75,000ms
For loop code = 12ms -> 20ms
A significant difference!
Then I did a comparison using the for loop method but with the two different string comparison types for the string.  Using my string comparison, vs yours. But I had to test on 1 million rows for this, to get a significant difference.
This differend by between 200ms and 800ms
So it seems in this case that the string comparison is not a major factor.
So it seems that your Linq query creating the datarows is what is taking the majority of time and not the comparison of the rows themselves.
So switch to using the for loop, and all will be well in the world again!
Comment: Marked this as answer.  with this approach, am able to reduce the processing time to 45ms.  Thankyou.
Comment: very detailed.  got to learn from your answer.  let me try and get back to you.  Thanks.
Answer: If the string values in the DataTable were trimmed you could use the DataRowCollection.Find method to make quick lookups based on the primary key, in combination with the DataTable.CaseSensitive property. But since this is not the case, you can't use the built-in functionality of ADO.NET. Fortunately it's easy to accomplish the same thing using the ToLookup LINQ method, which returns a read-only, one-to-many dictionary.
public DataTable GetTableDiff(DataTable dt1, DataTable dt2, string columnName)
{
    IEqualityComparer<string> comparer = StringComparer.OrdinalIgnoreCase;

    var lookup = dt2.AsEnumerable().ToLookup(row => row["N"].ToString().Trim(),
        comparer);

    var diffList = dt1.AsEnumerable()
        .Where(r1 => !lookup[r1["N"].ToString().Trim()].Any(r2 => comparer.Equals(
            r1[columnName].ToString().Trim(), r2[columnName].ToString().Trim())))
        .ToList();

    if (diffList.Count == 0) return null;
    return diffList.CopyToDataTable();
}

Effecient way to compare data tables


foreach loop to fetch JSON

Question: I'm trying to make this json
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}

print the values as follows
['04/24/2018', '04/25/2018']

But I could not do it, any suggestions?

var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];

for(var key in datesBooking){
  console.log(datesBooking[key])
}
Comment: `datesBooking.map(x => x.date)`
Answer: You could use map method by passing a provided callback function.

var datesBooking = [
{"date":"04\/24\/2018"},
{"date":"04\/25\/2018"}
];
console.log(datesBooking.map(function(item){
  return item.date;
}));

Another approach is simply using arrow functions.
datesBooking.map(a => a.date)

Answer: If you are explicitly looking for the foreach method:

var datesBooking = [
  {"date": "04\/24\/2018"},
  {"date": "04\/25\/2018"}
];

datesBooking.forEach(function(data, index) {
  datesBooking[index] = data.date;
});

console.log(datesBooking);

The above code will rewrite over the existing array.
The same can be achieved using map

var datesBooking = [
  {"date": "04\/24\/2018"},
  {"date": "04\/25\/2018"}
];

datesBooking = datesBooking.map(x => x.date);

console.log(datesBooking);

As you can see, the map was introduced to simplify such a use of foreach.

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

foreach loop to fetch JSON


Can someone explain like I am 5 year-old about this problem from Hastie's ESL Book?

Question: I am working through Hastie's ESL book, and I am having a tough time with Question 2.3.  The question is as follows:

We are considering a nearest neighbor estimate at the origin, and the median distance from the origin to the closest data point is given by this equation.  I have no idea where to begin in terms of trying to derive this.
I know that most data points are closer to the boundary of the sample space, than to any other data point (curse of dimensionality), but I am having trouble translating this into the Linear Algebra / Probability sense.
Comment: You're five years old. All credit to you for wanting to understand ESL, but you'll have to wait until you're six. It's a book for big boys and girls.
Comment: A five year old might start by looking at the one-dimensional case (p = 1). And once that is in hand, take it from there.
Comment: What does the "ELI5" in the title mean?  If you want to derive that equation you will need to start with a probability model for points in the ball: what is that model?  (Please don't require your readers to refer to a book or some other site in order to understand your question.)
Comment: @whuber I agree -- Acronyms are a terrible hashing scheme.
Comment: If we are going to have ELI5 spelled out what about ESL?
Comment: @mdewey That's a good point.  There's a big fuzzy line somewhere, because some acronyms become so commonplace they no longer really need spelling out: *eg*, ANOVA, SD, DF, EDA.  Searches of [our site](http://stats.stackexchange.com/search?q=esl), and of the Web generally, suggest "ESL" might be well on its way into that group. In the present case my hope is that the OP will provide the background necessary to understand the question (it's not even clear what "$n$" stands for--dimension or sample size?), at which point it shouldn't matter much what "ESL" stands for.
Comment: I gave an answer, but definitely not the one I would give my 5 year old!
Comment: @fcop Like others, I do hope you stay with us. I'd like to underline that the OP injected a humorous touch at the outset (only an extraordinary 5 year old would understand the question; perhaps Terence Tao might have done). Some of us responded lightheartedly. We are, I trust, **laughing with the OP and absolutely not attempting to mock the OP or their question**, which would, as you imply, be utterly inappropriate. The main culprits are serious supporters of this forum and respectful of it.
Comment: @whuber It's an internet acronym that Reddit users use to mean "explain like I'm five [years old]." But usually the acronym is used to mean to explain it with minimal technical jargon, rather than literally explaining a concept to a five-year-old.
Comment: @General Thanks; I figured it was something like that.  But since we ask people to spell out technical acronyms in posts (in part because they are often ambiguous), we ought to be consistent and ask other acronyms to be expanded, too.  How is one to know that something like "ELI5" isn't *also* a specialized acronym or even an abbreviation peculiar to a particular author?
Comment: @fcop I am sorry you feel bad. There's nothing in your recent tirades that seems to allow for a constructive response, so I won't try since I don't want to offend you further. I apologize for any misunderstanding, but please just take some time to cool down before you post anything more. The various edits *made by people other than the original poster* have changed some crucial aspects of the original question. I therefore rolled this post back to the beginning so we can start over and respond to what was actually asked rather than what each individual thinks was asked.
Comment: @whuber for the kids on Reddit you could now say "[TIL](https://www.reddit.com/r/todayilearned/) what ELI5 means"!
Comment: You can kind of see that @MarkL.Stone may be onto something since your expression is (p'th power) of something that doesn't depend on $p$.
Comment: @fcop Please don't do that, I have enjoyed your contributions.
Comment: AAA (acronym Angst anecdote): IV means independent variable, except that it probably means instrumental variable if you're an econometrician, implied volatility if you work with financial data, and intravenous if you are a medic.
Comment: @whuber: The question is indeed unclear, I tried to make the question more clear, maybe like this it can be 're-opened' ?
Comment: @Tim; Consider N data points uniformly distributed in a p-dimensional unit ball centered at the origin. Show that the median distance from the origin to the closest data point is given by that expression. Is that so unclear ?
Comment: @fcop  Thank you for including that information. Now that the question is a little clearer, it leaves me wondering what the authors really intend. Normally a "uniform distribution" is understood with respect to the volume measure induced by the *Euclidean* ($L^2$) metric.  Now that an $L^p$ metric is involved, exactly how should we understand "uniform"?  Do the authors state what they mean by that?  Similarly, should we understand "unit ball" to be the $L^p$ unit ball or the $L^2$ unit ball?
Comment: @whuber: well I think you should-re-read my edit, it is about a unit ball in a p-dimensional space, no $L_p$ involved here, and N is the number of datapoints uniformly distributed in that ball.  Is that so unclear ?
Comment: @Tim, sorry, you are right, $n$ and $N$ are the same, I will edit.
Comment: @fcop Please don't be offensive; I have read this passage many times. Presuming "$d(p,*)$" refers to $L^p$ distances, one is *compelled* to wonder exactly what metric is used to define "unit ball" and "uniform."  If that's not what $d(p,*)$ means, then it needs a definition.
Comment: @whuber: sorry, I don't know why you say I  am offensive, in my edit section there is no $L_p$ at all, if you read $d(x,y)$ in one or other question, do you then presume $L_x$ distance ?, but you are right, I was not explicit about distance, but without further details one can assume that distance is 'Euclidean' no ? Especially if I ask about ''layman terms'', but it's ok, I was just trying to help, apparently that is not appreciated so I will undo the edit if you want that ?.
Comment: @whuber: maybe you should read the other comments and then conclude who is most offensive or even 'humiliating' towards a newcomer like Nick ( 116 votes and just asking something he does not understand). When I read all these stupid comments (this is offensive) I decided to try to help.
Comment: @whuber: the goal of this site is, at least in my understanding, to help people understand things, but apparently you should laugh at them ( read the comments), if that is how it works and then you say that someone who tries to help is offensive, then I will look for another hobby and you can delete my account instantantanuously !
Comment: I added a simple summary to the end (maybe OK for a gifted 10 year old?)
Comment: @whuber Flagging the previous comment FYI.
Comment: @whuber: I don't feel bad at all, in fact I am at this site just for fun. I don't know what you mean by tirades but I can guess what you mean. There also your reaction is biased as you seem to insinuate that I am causing trouble.
Comment: @fcop: It'd indeed be a shame if you were to leave. I'm sure any mis-understandings can be ironed out.
Comment: Please see the Solution 2.3 of ESL here https://yuhangzhou88.github.io/ESL_Solution/ESL-Solution/2-Overview-of-Supervised-Learning/ex2-3/
Answer: Let $r$ be distance from the origin, and let $V_0[p]$ be the volume of the unit hypersphere in $p$ dimensions. Then the volume contained in a hypersphere of radius $r$ is
$$V[r]=V_0[p]r^p$$
If we let $P=V[r]/V_0[p]$ denote the fraction of the volume contained within this hypersphere, and define $R=r^p$, then
$$P[R]=R$$
If the data points are uniformly distributed within the unit ball, then for $0\leq R\leq 1$ the above formula is a cumulative distribution function (CDF) for $R$. This is equivalent to a uniform probability density for $R$ over the unit interval, i.e. $p[R]= P'[R] =1$. So, as hinted by Mark Stone in the comments, we can reduce the $p$ dimensional case to an equivalent 1D problem.
Now if we have a single point $R$, then by definition of a CDF we have $\Pr[R\leq \rho]=P[\rho]$ and $\Pr[R\geq \rho]=1-P[\rho]$. If $R_{\min}$ is the smallest value out of $n$ points, and the points are all independent, then the CDF for is given by
$$\Pr[R_{\min}\geq \rho]=\Pr[R\geq \rho]^n=(1-\rho)^n$$
(this is a standard result of univariate extreme value theory).
By definition of the median, we have
$$\frac{1}{2}=\Pr[(R_{\min})_{\mathrm{med}}\geq R]=(1-R)^n$$
which we can rewrite as
$$(1-d^p)^n=\frac{1}{2}$$
which is equivalent to the desired result.
EDIT: Attempt at "ELI5"-style answer, in three parts.

For the 1D case with a single point, the distance is uniformly distributed over $[0,1]$, so the median will be $\frac{1}{2}$.
In 1D, the distribution for the minimum over $n$ points is the first case to the $n$-th power.
In $p$ dimensions, the distance $r$ is not uniformly distributed, but $r^p$ is.
Comment: Ha ha, I gave the comment that a 5 year old might start by looking at the p = 1 case.  I thought about adding a comment that a 4 year old might not only start with the p = 1 case, but also n = 1. But I figured I'd let the 5 year old figure that out.
Comment: Note that when I answered the question, it was after it had been clarified by @fcop to read: "Consider N data points uniformly distributed in a p-dimensional unit ball centered at the origin. Show that the median distance from the origin to the closest data point is given by ...". So a unit-ball with respect to the $L_2$ norm in $p$ dimensional space. After this the question was rolled back to the original, which differs and is not so clear. (See comment chain under original question.)


Windows store app development - Windows 2012

Question: I would like to develop Windows Store App using Windows Server 2012. Is it possible?
During installing VS 2013 some hint appeared: "windows store app development requires windows 8.1".
Answer: Reading the MSDN entry it mentions the following

You can't create an app by using Windows 7. In addition, developer licenses aren't available for Windows Server 2012, so you can't develop a Windows Store app on that operating system.

So no, you can not develop Store apps using Server 2012.
Answer: Yes, you can't develop Windows Store App using Windows Server 2012, because developer licenses aren't available for Windows Server 2012.
You can read more about it on MSDN: Develop apps by using Visual Studio 2013


Computation of hypergeometric function in R

Question: I'm having tremendous difficulty evaluating $_2F_1(a,b;c;z)$ with the hypergeo package in R. In my case, values of $a$, $b$, $c$ are always positive real numbers. Even so, the hypergeometric function is incredibly sensitive to their values. I am not looking for extreme precision; I can use Excel to get a rough estimation of the Guass hypergeometric that is fine for my purposes.
Any suggestions for an implementation in R that will give a fast, error-free, if not super accurate Gaussian hypergeometric computation of positive real numbers with a wide range of values?
Edit: it seems there is far more code for this in MATLAB than R. Any thoughts as to why?
Comment: i would have thought a good approach would be to find the largest term in the hypergeometric sum and truncate around this term.  this way you can factor out the largest term and work with terms less than 1 in the sum.  you could also use laplace method on the integral representation (suitably parameterised to avoid boundary issues).
Answer: Unless you need to evaluate the Gauss hypergeometric function for complex values of the parameters or the variable, it is better to use Robin Hankin's gsl package.
Based on my experience I also recommend to only evaluate the Gauss hypergeometric function for a value of the variable lying in $[0,1]$, and use a transformation formula for values in $]-\infty, 0]$.
library(gsl)
Gauss2F1 <- function(a,b,c,x){
    if(x>=0 & x<1){
        hyperg_2F1(a,b,c,x)
    }else{
            hyperg_2F1(c-a,b,c,1-1/(1-x))/(1-x)^b
        }
}

Comment: Thanks for a simple, clean solution to a not so simple problem! There isn't a lot of documentation on gauss hypergeometric functions for R so this approach is valuable.
Comment: @benrolls I have experienced the Gauss hypergeometric function with the hypergeo package, the gsl package, another method implemented by myself, and Mathematica too. I guarantee that the solution I give above is very performant, I have never find a bug when using it.
Comment: Stéphane's formula above is great, but I've noticed that it produces NaNs when $c-a$ is negative with large absolute value. Use of another transformation formula leads to:
    library(gsl)
Answer: @Stéphane Laurent's's formula above is great. I've noticed that it sometimes produces NaNs when a, b, c are large and z is negative – I haven't been able to pin the precise conditions down. In these cases we can use another hypergeometric transformation starting from Stéphane's alternative expression. It leads to this alternative formula:
library(gsl)
Gauss2F1b <- function(a,b,c,x){
    if(x>=0 & x<1){
        hyperg_2F1(a,b,c,x)
    }else{
        hyperg_2F1(a,c-b,c,1-1/(1-x))/(1-x)^a
    }
}

For example:
> Gauss2F1(80.2,50.1,61.3,-1)
[1] NaN
>
> Gauss2F1b(80.2,50.1,61.3,-1)
[1] 5.498597e-20
>
>
> Gauss2F1(80.2,50.1,61.3,-3)
[1] NaN
> Gauss2F1b(80.2,50.1,61.3,-3)
[1] 5.343807e-38
>
>
> Gauss2F1(80.2,50.1,61.3,-0.4)
[1] NaN
> Gauss2F1b(80.2,50.1,61.3,-0.4)
[1] 3.322785e-10

all three agreeing with Mathematica's Hypergeometric2F1. This formula seems well behaved also for smaller a, b, c. Note that there are cases in which this formula gives NaN and Stéphane's doesn't, though. Best to check case by case.


Loop Copy/Paste every Nth row into new sheet?

Question: I am a complete VBA newbie and am not proficient enough to decipher some solutions I've seen in the web. I'm hoping a guru can stamp out a quick VBA loop and I can play around with it.
I'm simply trying to:
Copy every 3 rows from Sheet1. Paste 3 rows into new sheet (Sheet2).
Copy next 3 rows from Sheet1, and paste into another new sheet (Sheet 3).

I want to loop this function until the rows are blank.
I've recorded VBA of this function and it runs only once. I need it to loop till the rows are blank.
    Sub countrySplit()
    '
        Sheets("vs Target").Select
        Range("A5:N5, A6:N8").Select
        Selection.Copy
        Sheets.Add After:=ActiveSheet
        ActiveSheet.Select
        Range("A1").Select
        ActiveSheet.Paste
        ActiveSheet.Name = ActiveSheet.Range("$A$2")

End Sub

Comment: a) `Range("A5:N5, A6:N8")` is actually `Range("A5:N8")` which is four rows. b) Are there rows to skip? or are you just trying to copy 3 rows then the next 3 rows, etc?
Comment: @Jeeped If I were a betting man, A5:N5 are the headers and rows 6-8 are the 3 rows of data. That would be a normal excel setup right? I mean we all see plenty of sheets that don't start at row 1.
Answer: @JackC ... Here is how I would do it... there are many ways of course but give this one a try if you want.  Just edit it per the notes in the code to adjust for your workbook if you do not then not all your data will copy to the new sheets..
Sub loopTest()
Dim hdr As Range 'header range
Dim dta As Range 'data range
Dim cl As Integer 'copy line
Dim ns As Excel.Worksheet

Set hdr = Excel.Worksheets(1).Range("A1:C1") 'set this range for what ever range your headers are on
cl = 2 'set this value for what ever row your data starts on
Do While Excel.Worksheets(1).Range(Cells(cl, 1), Cells(cl, 1)).Value <> "" 'this stops the loop when there are no more records
Set dta = Excel.Worksheets(1).Range(Cells(cl, 1), Cells(cl + 2, 3)) 'this sets the data range change the number 3 to how ever many columns there are in your dataset
Set ns = Excel.Worksheets.Add(, ActiveSheet) 'this sets the new sheet to the ns (new sheet = ns ) variable
hdr.Copy
ns.Range("A1").PasteSpecial xlPasteAll
dta.Copy
ns.Range("A2").PasteSpecial xlPasteAll
cl = cl + 3
Loop

End Sub

Comment: Wait! I got it! Added one line right before the Loop command: "Sheets("vs Target").Select. vs Target is the source data I'm pulling info from. I think it works beautifully! Thanks Gambill!
Comment: ...Thank you for the help so far. However, I'm getting error on this line of code: Do While Excel.Worksheets(1).Range(Cells(cl, 1), Cells(cl, 1)).Value <> "" ...I'll tinker on my end, but hopefully your additional insight can help.
Comment: Gambil "Run-Time Error '1004': Application-defined or object-defined error.

Code is able to copy+paste header + 3 rows of data to new sheet once, but won't continue the loop.


balls and bins when maxload $\geq 3$

Question: There are $m$ balls and $n$ bins.
Let the number of balls that the i-th bin contains equals $X_i$, and $Y=\text{max} X_i$.
I know that when $m=\Theta(\sqrt{n})$, with high probability $Y\geq 2$. The proof is as following.

$$Pr(Y\geq 2)=1-Pr(Y=1)=1-(1-\frac{1}{n})(1-\frac{2}{n})\cdots (1-\frac{m-1}{n})$$
apply  $1-x\leq e^{-x}$,
we can get
$$Pr(Y\geq 2)\geq 1-e^{-\dfrac{1}{n}}e^{-\dfrac{2}{n}}\cdots e^{-\dfrac{m-1}{n}}=1-e^{-\frac{m(m-1)}{2n}}$$
let $m=4\sqrt{n}+1$, we can get $$Pr(Y\geq 2)\geq 1-e^{-8}$$
similarly, we can apply $1-x\geq e^{-2x}$ to get the lower bound.

My question: how to prove when $m=\Theta(n^{\frac{2}{3}})$, with high probability $Y\geq 3$ (this is from a lecture without proof).
My try:
$Pr(Y\geq 3)=1-Pr(Y=1)-Pr(Y=2)$, but I don't know how to calculate $Pr(Y=2)$.
Comment: I thought that "with high probability" means the probability tends to $1$. But I see now that this isn't the case here, but rather $m=\Theta(n^{\frac{2}{3}})$ is like an edge case: for lower exponents the probability always goes to $0$ and for higher always to $1$. If I understood correctly.
Comment: @ploosu2 When $m=\Theta(n^{2/3})$, we could only change the coefficient to make sure the probability tends to 1. I can't understand what's your meanning "lower exponents" and "higher exponents". I think the exponents is fixed since $m=\Theta(n^{2/3})$.
Answer: Number the balls from $1$ to $m$. For any $1\le i<j<k\le m$, define the event
$$
E_{i,j,k}=\{\text{event that balls $i,j$ and $k$ are in same box}\}.
$$
Then, let $N$ be the number of these events that occur, that is the number of triples of balls which end up in the same box:
$$
N=\sum_{1\le i<j<k\le n}{\bf 1}(E_{i,j,k}).
$$
Note that $Y\ge 3$ if and only if $N>0$, so we just need a lower bound for $P(N>0)$. This is provided by the second moment inequality:
$$
P(N>0)\ge \frac{(E[N])^2}{E[N^2]}=\frac{(E[N])^2}{(E[N])^2+\operatorname{Var}N}.\tag1
$$
By linearity of expectation, we know
$$E[N]=\binom m3\frac 1{n^2}.\tag2$$
All that remains is to handle the variance. We can write
$$
\newcommand{\Var}{\operatorname{Var}}
\newcommand{\Cov}{\operatorname{Cov}}
\Var N=\sum_{1\le i<j<k\le n} \Var[{\bf 1}(E_{i,j,k})]+\sum_{}\Cov({\bf 1}(E_{i,j,k}),{\bf 1}(E_{\ell ,p,q}))
$$
The second summation ranges over all ways to choose distinct triples $\{i,j,k\}\neq\{\ell,p,q\}$ so that $i<j<k$ and $\ell<p<q$. The first summation is easy, since
$$
\sum_{1\le i<j<k\le n} \Var[{\bf 1}(E_{i,j,k})]=\binom{m}3 \frac 1{n^2}\left(1-\frac1{n^2}\right)
$$
The covariance summation is where most of the work is. Each covariance summand is equal to
$$
\Cov({\bf 1}(E_{i,j,k}),{\bf 1}(E_{\ell ,p,q}))=P(E_{i,j,k}\cap E_{\ell,p,q})-P(E_{i,j,k})P(E_{\ell,p,q})
$$
This quantity depends on the number of indices that $\{i,j,k\}$ has in common with $\{\ell,p,q\}$. Fortunately, if the indices are disjoint, then the events are independent, so the covariance is zero. The same is true if the indices have exactly one number in common (prove this). All that remains is when they have two indices in common, say $p=i$ and $q=j$. In this case, we have
$$
P(E_{i,j,k}\cap E_{i,j,\ell})-P(E_{i,j,k})P(E_{i,j,\ell})=\frac1{n^3}-\left(\frac1{n^2}\right)^2=\Theta(n^{-3})
$$
Since the number of ways to choose $\{i,j,k\}$ and $\{\ell,p,q\}$ so that they intersect in two places is $\Theta(m^4)$ (there are $\binom{m}2$ ways to choose the overlapping pair, then $(m-2)(m-3)$ ways to choose the other two numbers), the entire covariance summation is something like
$$
\sum_{}\Cov({\bf 1}(E_{i,j,k}),{\bf 1}(E_{\ell ,p,q}))=\Theta(m^4n^{-3})
$$
This is negligible in the regime where $m=\Theta(n^{2/3})$. Therefore, we get
$$
\Var N=\binom{m}3n^{-2}(1-n^{-2})+o(1)=\binom{m}3n^{-2}+o(1)\tag3
$$
where $o(1)$ is a term going to zero as $m\to\infty$.
Conclude by combining $(1),(2)$ and $(3)$.
Comment: Thank you very much, your proof is elegant and readable.
Answer:
Disclaimer: I couldn't solve it, but maybe this method can be salvaged or gives some ideas.

I assume the balls and the boxes are both distinguashable. Let's calculate
$$\mathbb{P}(Y\leq 2) = \frac{\text{number of ways to put so that } Y\leq2}{n^m}.$$
The numerator equals $m!$ times the coefficient of $x^m$ in $(1+x+\frac{1}{2}x^2)^n$. Using the multinomial theorem we can write
$$(1+x+\frac{1}{2}x^2)^n
= \sum_{i+j+k=n} {n \choose i \space j \space k}\frac{1}{2^k}x^{j+2k}
$$
To get the coefficient of $x^m$ (let's call it $c_m$), solve the equations $i+j+k=n$ and $j+2k=m$ in terms of $k$ to get
$$c_m = \sum_{k=0}^{\lfloor \frac{m}{2} \rfloor} {n \choose n-m+k, \space m-2k, \space k}\frac{1}{2^k}
$$
So
$$
\mathbb{P}(Y\leq 2) = \frac{m!c_m}{n^m}
= \sum_{k=0}^{\lfloor \frac{m}{2} \rfloor} \frac{m!n!} {n^m(n-m+k)! (m-2k)! k! 2^k}
$$
Using the Stirling bounds $e(\frac{x}{e})^x \leq x! \leq ex(\frac{x}{e})^x$ we have
$$\mathbb{P}(Y\leq 2)
\leq e^{-1-m}m^{m+1}n^{n+1-m} \sum_{k=0}^{\lfloor \frac{m}{2} \rfloor} \frac{1}{(n-m+k)^{n-m+k}(m-2k)^{m-2k}(2k)^k}
$$
Now, assuming (this isn't true!!, but I feel like it's kind of almost true) that the term in the sum is at its biggest for $k=0$, we would have (there are at most $\frac{m}{2}+1 \leq m$ terms)
$$\mathbb{P}(Y\leq 2)
\leq e^{-m - 1} m^2 n^{-m + n + 1} (n - m)^{m - n}
$$
and plugging in $m=n^{2\over 3}$ according to Wolfram alpha that tends to zero.
EDIT: We can actually solve which term is the largest in the sum by considering the function (take logarithm to make it more managable)
$$g(k) = -(n-m+k)\log(n-m+k) - (m-2k)\log(m-2k)-k\log(2k).$$
Deriving and finding root we get a second degree equation and the solution (pick minus sign to have $k$ in the region)
$$k_{\max} = \frac{n+m-\sqrt{(n+m)^2-2m^2}}{2}.$$
This is close to zero because $n$ is much bigger than $m$. This still isn't a rigorous proof but maybe it will show the way...
Not sure if this will help, but using the inequality $1-\frac{x}{2}-\frac{x^2}{2} \leq \sqrt{1-x}$ we get
$$2k_{\max} = (n+m)\left(1- \sqrt{1-\frac{2m^2}{(n+m)^2}} \right)
\leq \frac{m^2}{n+m} + \frac{2m^4}{(n+m)^3}.
$$

balls and bins when maxload $\geq 3$


Populate a detail view with (correct) txt-file

Question: I have a tableview with country names. When a cell is tapped I want it to move to a detail view with text about the country. I have a separate txt-file for each country with the same name as the title in the cell (e.g. USA.txt).
How do I display these txt-files in the detail view?
My code so far looks like this:
TableViewController.h
@property (nonatomic, strong) NSArray *Title;

TableViewController.m
- (void)viewDidLoad {
[super viewDidLoad];

_Title = @[@"Afganistan",
        @"Albania",
        @"Algeria",

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

int row = [indexPath row];

cell.TitleLabel.text = _Title[row];

return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"ShowDetails"]) {
    DetailViewController *detailviewcontroller = [segue destinationViewController];

    NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

    int row = [myIndexPath row];
    detailviewcontroller.DetailModal = @[_Title[row]];
  }
}

DetailViewController.h
@property (strong, nonatomic) IBOutlet UILabel *TitleLabel;

@property (strong, nonatomic) NSArray *DetailModal;

DetailViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

_TitleLabel.text = _DetailModal[0];

self.navigationItem.title = _DetailModal[0];

}

I found the code below here on StackOverflow but I don't know how to implement it.
NSString *path = [[NSBundle mainBundle]pathForResource:@"Afganistan" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

Answer: You need can take one more UILabel(say detailLabel) to display the content of .txt files.
In ViewDidLoad of DetailViewController, add below lines
NSString *path = [[NSBundle mainBundle]pathForResource:_DetailModal[0] ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
self.detailLabel.text = content
self.detailLabel.numberOflines = 0


Can't delete row in MySQL error 1451. Need to implement DELETE ON CASCADE

Question: I want to delete a record based on an id. But I can't since foreign key constraint fails...that means that maybe I need to implement DELETE on Cascade...but I don't know how that works.  Bottom Line I need to fix the error, and/or I need to implement Delete on Cascade since I searched through the site and that is the most possible solution.
This is the query:
SET SQL_SAFE_UPDATES = 0;
DELETE `rentals`, `rental_movie`
FROM `rentals`
LEFT JOIN `rental_movie`
    ON `rentals`.`idRentals` = `rental_movie`.`Rentals_idRentals`
WHERE `Movie_idMovie` = 1;

I have the Error 1451 in MySQL:
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`project`.`rental_movie`, CONSTRAINT `fk_Rental_Movie_Rentals1` FOREIGN KEY (`Rentals_idRentals`) REFERENCES `rentals` (`idRentals`) ON DELETE NO ACTION ON UPDATE NO ACTION)

This is my database:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

CREATE SCHEMA IF NOT EXISTS `project` DEFAULT CHARACTER SET utf8 ;
USE `project` ;

-- -----------------------------------------------------
-- Table `project`.`customer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`customer` ;

CREATE TABLE IF NOT EXISTS `project`.`customer` (
  `idCustomer` INT(11) NOT NULL AUTO_INCREMENT,
  `CustomerName` VARCHAR(20) NOT NULL,
  `CustomerLastName` VARCHAR(20) NOT NULL,
  `CustomerAddressl` VARCHAR(45) NOT NULL,
  `ZipCode` INT(11) NOT NULL,
  `CustomerPueblo` VARCHAR(20) NOT NULL,
  `CustomerTel` DECIMAL(10,0) NOT NULL,
  PRIMARY KEY (`idCustomer`))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8;

-- -----------------------------------------------------
-- Table `project`.`movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`movie` ;

CREATE TABLE IF NOT EXISTS `project`.`movie` (
  `idMovie` INT(11) NOT NULL AUTO_INCREMENT,
  `TitleMovie` VARCHAR(45) NOT NULL,
  `Genre` VARCHAR(45) NOT NULL,
  `ReleaseDate` VARCHAR(45) NOT NULL,
  `RunTime` TIME NOT NULL,
  `Rated` VARCHAR(10) NOT NULL,
  PRIMARY KEY (`idMovie`))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8;

-- -----------------------------------------------------
-- Table `project`.`rentals`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`rentals` ;

CREATE TABLE IF NOT EXISTS `project`.`rentals` (
  `idRentals` INT(11) NOT NULL AUTO_INCREMENT,
  `Customer_idCustomer` INT(11) NOT NULL,
  `RentedDate` DATE NOT NULL,
  `ReturnDate` DATE NULL DEFAULT NULL,
  PRIMARY KEY (`idRentals`),
  INDEX `fk_Rentals_Customer_idx` (`Customer_idCustomer` ASC),
  CONSTRAINT `fk_Rentals_Customer`
    FOREIGN KEY (`Customer_idCustomer`)
    REFERENCES `project`.`customer` (`idCustomer`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8;

-- -----------------------------------------------------
-- Table `project`.`rental_movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`rental_movie` ;

CREATE TABLE IF NOT EXISTS `project`.`rental_movie` (
  `Movie_idMovie` INT(11) NOT NULL,
  `Rentals_idRentals` INT(11) NOT NULL,
  PRIMARY KEY (`Movie_idMovie`, `Rentals_idRentals`),
  INDEX `fk_Rental_Movie_Rentals1_idx` (`Rentals_idRentals` ASC),
  CONSTRAINT `fk_Rental_Movie_Movie1`
    FOREIGN KEY (`Movie_idMovie`)
    REFERENCES `project`.`movie` (`idMovie`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Rental_Movie_Rentals1`
    FOREIGN KEY (`Rentals_idRentals`)
    REFERENCES `project`.`rentals` (`idRentals`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

-- -----------------------------------------------------
-- Table `project`.`transaction`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`transaction` ;

CREATE TABLE IF NOT EXISTS `project`.`transaction` (
  `idTransaction` INT(11) NOT NULL AUTO_INCREMENT,
  `idRentals` INT(11) NOT NULL,
  `DaysRented` INT(11) NULL DEFAULT NULL,
  `Cost` DECIMAL(10,0) NULL DEFAULT NULL,
  `TotalCost` DECIMAL(10,0) NULL DEFAULT NULL,
  PRIMARY KEY (`idTransaction`),
  INDEX `idRentals_idx` (`idRentals` ASC),
  CONSTRAINT `idRentals`
    FOREIGN KEY (`idRentals`)
    REFERENCES `project`.`rentals` (`idRentals`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8;

USE `project` ;

-- -----------------------------------------------------
-- Placeholder table for view `project`.`new_view`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project`.`new_view` (`idRentals` INT, `Customer_idCustomer` INT);

-- -----------------------------------------------------
-- Placeholder table for view `project`.`rentals_view`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `project`.`rentals_view` (`idRentals` INT, `idCustomer` INT, `CustomerName` INT, `idMovie` INT, `TitleMovie` INT, `RentedDate` INT);

-- -----------------------------------------------------
-- procedure PName
-- -----------------------------------------------------

USE `project`;
DROP procedure IF EXISTS `project`.`PName`;

DELIMITER $$
USE `project`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `PName`(cid INT)
BEGIN

  SELECT * FROM rentals;

END$$

DELIMITER ;

-- -----------------------------------------------------
-- View `project`.`new_view`
-- -----------------------------------------------------
DROP VIEW IF EXISTS `project`.`new_view` ;
DROP TABLE IF EXISTS `project`.`new_view`;
USE `project`;
CREATE  OR REPLACE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `project`.`new_view` AS select `project`.`rentals`.`idRentals` AS `idRentals`,`project`.`rentals`.`Customer_idCustomer` AS `Customer_idCustomer` from `project`.`rentals`;

-- -----------------------------------------------------
-- View `project`.`rentals_view`
-- -----------------------------------------------------
DROP VIEW IF EXISTS `project`.`rentals_view` ;
DROP TABLE IF EXISTS `project`.`rentals_view`;
USE `project`;
CREATE  OR REPLACE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `project`.`rentals_view` AS select `r`.`idRentals` AS `idRentals`,`c`.`idCustomer` AS `idCustomer`,`c`.`CustomerName` AS `CustomerName`,`m`.`idMovie` AS `idMovie`,`m`.`TitleMovie` AS `TitleMovie`,`r`.`RentedDate` AS `RentedDate` from (((`project`.`rentals` `r` join `project`.`customer` `c` on((`r`.`Customer_idCustomer` = `c`.`idCustomer`))) join `project`.`rental_movie` `rm` on((`rm`.`Rentals_idRentals` = `r`.`idRentals`))) join `project`.`movie` `m` on((`rm`.`Movie_idMovie` = `m`.`idMovie`)));

SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

Answer: Perhaps you should consider dropping the rental_movie table, and move the Movie_idMovie column onto the rentals table, since it's a 1-to-1 relationship, this table you've created will cause you grief like you've already experienced.
DROP TABLE `project`.`rental_movie`;

ALTER TABLE `project`.`rentals`
ADD COLUMN `Movie_idMovie` INT(11) NOT NULL AFTER `Customer_idCustomer`,
ADD INDEX `fk_Rentals_Movie_idx` (`Movie_idMovie` ASC);
ALTER TABLE `project`.`rentals`
ADD CONSTRAINT `fk_Rentals_Movie`
        FOREIGN KEY (`Movie_idMovie`)
        REFERENCES `project`.`movie` (`idMovie`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION;

Answer: You need a foreign key declared with the ON DELETE CASCADE option.
-- -----------------------------------------------------
-- Table `project`.`rental_movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `project`.`rental_movie` ;

CREATE TABLE IF NOT EXISTS `project`.`rental_movie` (
  `Movie_idMovie` INT(11) NOT NULL,
  `Rentals_idRentals` INT(11) NOT NULL,
  PRIMARY KEY (`Movie_idMovie`, `Rentals_idRentals`),
  INDEX `fk_Rental_Movie_Rentals1_idx` (`Rentals_idRentals` ASC),
  CONSTRAINT `fk_Rental_Movie_Movie1`
    FOREIGN KEY (`Movie_idMovie`)
    REFERENCES `project`.`movie` (`idMovie`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Rental_Movie_Rentals1`
    FOREIGN KEY (`Rentals_idRentals`)
    REFERENCES `project`.`rentals` (`idRentals`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;

Then if you remove the value in rental_movie.Rentals_idRentals, it will automatically remove the value in rentals.idRentals.
This remove will be simultaneous and atomic -- that is, no other query running concurrently will be able to see the data removed in one table and not removed in the other table.


Twilio Redirect From Webhook

Question: I've been trying to redirect incoming phone calls from Twilio via webhook to execute TwiML hosted on a website.
For testing, I've been using 'raw' GitHub to host my XML as a 'GET' request webhook.
My code is in the format below:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Dial>
        <Number>
            +447xxxxxxxxx
        </Number>
    </Dial>
</Response>

When I run this code from a TwiML bin, it redirects as I want, however when I send it through a webhook, it just says the phone number out loud.
Any help on why this is would be greatly appreciated,
Thank you.
Answer: This has to do with the MIME type the web server is returning.
You can find more details here.
Twilio understands MIME types
The webserver is returning text/plain instead of text/xml.
You can host your static TwiML in a TwiMl Bin.
Comment: Thank you for your quick response! That makes a lot of sense! Out of interest, would you know how to designate mime types on static html pages?
Comment: It is a setting handled by the server, and each server handles it differently so really difficult to say. Specific to github, I found this but haven't tried it - https://docs.github.com/en/github/working-with-github-pages/about-github-pages#mime-types-on-github-pages.

Twilio Redirect From Webhook


How can I send text from a.html to b.html

Question: It is possible that the input text is sent to the second page with the help of javascript.
I need suggestions, help how I can send the text from a.html to b.html
Cod a.HTML
<input type="text" >
<button>send</button>

Cod b.HTML

Comment: Does this answer your question? [Transfer data from one HTML file to another](https://stackoverflow.com/questions/17502071/transfer-data-from-one-html-file-to-another)
Comment: It's not the first time I've written about this. Why write the same question twice)
Answer: This is basically how forms work. You wrap your input and button in a form tag with the attributes method="get" and action="b.html". Inside of b.html you can read the submitted data from the URL.
https://www.w3schools.com/tags/tag_form.asp
Answer: You can use Get parameters. When going to b.html, set a get parameter b.html?text=SOME_TEXT
and in b.html using js to get parameter:
let url = new URL(window.loacation.href);
let text = url.searchParams.get("text");
document.getElementById('mytxt').innerText = text

Comment: Thank you very much
Answer: You could use the query part of the URL to "send" data to another page:
a.html:
<form action="b.html" method="get">
    <input type="text" name="mytext">
    <button>send</button>
</form>

b.html:

However, this has its own limits. E.g. URL's length should be under 2000 characters, and if somebody revisits the generated link, the same text will appear.
If you have access to some server-side language/configuration, you can make a POST request instead, and manipulate b.html on the server.
If you have to stick to front-end, you could also try something with the Session Storage, and a redirection using JS.
Comment: Thank you very much


Excel (GoogleSpreadsheet) : I want to match text in a column and alter other columns of the same row

Question: I am creating a spreadsheet to help with inventory and orders. The Inventory table looks like this:
Product       Number Sold
Flashlight    some number
Toolbox       some number

The Orders table looks like this:
Customer      Product      Number Sold
Alice         Flashlight       5
Bob           Flashlight       2

I am in search of the proper function to use for accumulating on the inventory list the total number sold for each product in the orders list.
Answer: Just have a look at my example. Hope this helps.


Specify which function was used on string within a map

Question: I have a map of strings that all need to be modified by one of several functions based on some conditions.
My problem is that once I return the map, I need someway to identify which function was used on that specific string. My current idea is to add an identfier at the end of each string so I can check.
Ex. In the case the function "HPV" was used : mystring = mystring + "HPV"
Then on the return end, I can check the end of the string and know which function was used.
Is this an appropriate solution or is there a more efficient way that does not involve checking and modifying every single string?
Comment: Does each method return a String? You could return a `Tuple` where the first param is the actual String to return and the second param is a unique identifier for the method.

You can then figure out what method was invoked by looking at the second param of the Tuple.
Answer: You should probably use an object that can return both the actual String and the identifier of the function that did the work on it.
If the functions that will do the work are all known at compile-time, they can be identified by an enum, otherwise just another String that holds their ID.
Here is a simple suggestion on how to do that:
<code>enum StringModifier {
  HPV, OTHER
}

record ModifiedString(
  String string,
  StringModifier modifier
) {}

main(String[] args) {
  Map<String, String> originalMap = ...
  Map<String, ModifiedString> modifications = transform(originalMap, ...)
}
</code>
This avoids allocating new Strings and "dirty-ing" them, which can give you trouble later to clean them up as you probably want to make sure you know what the actual result String should look like.


Delete items older than a week

Question: $date =  (Get-Date).AddDays(-7).ToString('dd/MM/yyyy')

Get-ChildItem  -Path c:\Filestore  | Where-Object { ($_.CreationTime).ToString('dd/MM/yyyy') -lt $date } | remove-item

I want all items in the folder c:\Filestore to be delete if 1 week old or older.
When I test out this script it doesn't seem to delete anything I'm not sure if I'm missing something.
I call on the date 7 days ago and then list all items in c:\Filestore where the date equals that of 7 days ago and delete them.
Answer: You don't want to compare strings, you want to compare dates. - so just remove the ToString() invokes:
$date =  (Get-Date).AddDays(-7)
Get-ChildItem -Path c:\Filestore | Where-Object CreationTime -lt $date | remove-item

Delete items older than a week


How to extract filenames with two file extensions?

Question: I have a folder containing multiple files which have the double extension ".nii.gz". I want to extract the filenames using os.path.basename but this function returns the filenames only with the first extension ".nii". How can I extract the full filenames with both extensions?
Answer: Why not use the good old str.split
filename = 'file.nil.gz'
li = filename.split('.')
basename = li[0]
extensions = li[1:]

print(basename)
#file
print(extensions)
#['nil', 'gz']

Answer: This function extracts the entire name, even with multiple ".":
from os import listdir
from os.path import isfile, join
mypath = 'blabla'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
print(onlyfiles)

if you don't want to see the extention, then here is the add-on:
onlyfiles = [f.split('.')[0] for f in listdir(mypath) if isfile(join(mypath, f))]
print(onlyfiles)

Answer: I think, best practice is to use pathlib for operations with file path.
Code:
from pathlib import Path

print(Path("C://filename.tar.gz").suffixes)

Result:
['.tar', '.gz']

To extract all suffixes into one string just join them:
print("".join(Path("C://filename.tar.gz").suffixes))

Comment: OP wants to extract filenames NOT suffixes
Comment: @ruslaniv, so he got an answer, what do you want from me?


Drush can't find php

Question: I am on a shared server and someone else has installed Drush previously. But as of now Drush does not work and throws the following error.
<code>[root@vps1]# drush
/root/.composer/vendor/bin/drush: line 47: which: command not found
ERROR: can't find php.
</code>
The last thing I can remember was updating PHP (mod_php) to 5.4.36 several days ago. It worked on 5.4.34, but PHP was recompiled during the update. Is this related? What do I need to do to tell drush about this 'new' php?
Comment: It seems what you need is PHP-CLI (command line interface).  Did you check if typing "php -v" works?
Comment: Well, I thought about that too. But it did not change the mode php runs in... I only updated it, so I guess that should not be it... Command line works if I type `php -r 'echo "hi";'`
Comment: `Php -v` returns: `PHP 5.4.36 (cli) (built: Dec 23 2014 18:36:53) - Copyright (c) 1997-2014 The PHP Group - Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies`
Answer:
which: command not found

I don't really use Drush,  but that looks like it's dying when it uses the "which" command to locate PHP...  Drush is falsely reporting that PHP can't be found,  but it's because the command it uses to look for it ("which") is not available.
Comment: I read that too, but I can't quite figure where that comes from...
Comment: Drush uses multiple `which` statements prior to line 47. So I doubt this is the problem.
Comment: Ok, you were right. The system showed `which` as an installed package but the command did not work. `yum reinstall which` solved the issue.

Drush can't find php


No VS Code tem como ter um Color Theme para cada Linguagem?

Question: Minha dúvida é a seguinte. Eu gostaria de tem um tema de cores mais simples e com menos cores para trabalhar com CSS, já para o HTML eu gostaria de ter um Tema com cores mais acentuadas e marcadas.
Por exemplo ao abrir um arquivo .HTML eu quero usar o tema Monokai, mas se eu abrir uma arquivo .CSS em outra aba eu quero o tema Dracula
Teria como configurar o VS Code para usar um Color Theme para cada extensão de arquivo ao mesmo tempo em abas diferentes?

OBS: antes que me questionem perguntas sobre ferramentas comuns entre programadores são dentro do escopo do site ok https://pt.stackoverflow.com/help/on-topic
Comment: Difícil essa... Estou revirando  o settings e os docs aqui  e nada...
Comment: @fernandosavio o máximo que consegui foi uma extensão... e um link de uma Issue https://github.com/Microsoft/vscode/issues/20652
Answer: Ainda não considero a opção perfeita. Mas consegui uma extensão do VS Code que faz isso!
https://marketplace.visualstudio.com/items?itemName=jsaulou.theme-by-language
Funciona assim, primeiro vc abre um arquivo com a extensão que vc quer, ou cria um arquivo novo e salva com a extensão que vc quer, tipo um .html ou .js
Então com esse arquivo aberto e a aba dele ativa na sua tela vc da um ctrl+shit+p e no prompt digita Set theme for current file language como na imagem:

Agora sempre que vc clicar em uma aba que tenha um arquivo com essa extensão aberto o seu tema vai mudar. O que eu não gostei é que ele troca o tema do VS Code como um todo, mas eu gostaria de usar as configs do tema novo só dentro do Editor Workbench
Veja como funciona: (tem um pequeno delay na troca de temas, mas acho que isso é questão de hardware...)

Não vou aceitar aminha própria resposta pq acho que no futuro terá um jeito melhor para tratar isso


trigger attribute AJAX using custom select element

Question: I'm currently theming the product page for a Drupal Commerce site. Our frontend developer has insisted we use his custom "fancy" select dropdown for the attributes, which is causing a bit of a theming headache!
I have implemented his jQuery successfully, and when a user changes the fancy select option, it changes the "selected" option of the hidden select element too.
The problem is that none of the Drupal AJAX calls to update the price  are fired when we change the selected attribute option via jQuery.
Does anyone know how I can force the onChange AJAX call for attribute options via jQuery?
Thanks in advance!
Answer: I had the same problem and I managed to find the solution.
You need to call .attr and .trigger
<code>    $('ul.choose-product-quantity li').click(function(){
      var quantity = $(this).children('.product-quantity').text();
      $('.form-item-attributes-field-product-qty input')
      .filter(function(index){
        return $(this).attr("value") == quantity;
      })
      .attr('checked', 'checked').trigger('change');
    });
</code>
See attached screenshot as well. Good luck. !
Custom attributes selector (image)
Comment: wow thank you so much! I'll give your method a proper bash ASAP!!
Comment: I'm having an issue updating one attribute select field when another is changed with jQuery - calling .attr and .trigger don't seem to help because the form doesn't update


Interpretation of correlogram

Question: What does a correlogram describe? And, in particular, how should I interpret these two correlograms? Thanks
Comment: How many lags can i include in arima if first amd fourth bubble are outside shaded area
Answer: On the y axis is the autocorrelation. The x axis tells you the lag.  So, if x=1 we are looking at the correlation of December with November, November with October, etc.  If x=2, we have a lag of 2 and we are looking at the correlation of December with October, November with September, etc.
In the first graph, there are high positive correlations that only slowly decline with increasing lags. This indicates a lot of autocorrelation and you will need to take that into account in your modeling.
In the second graph, the correlations are very low (the y axis goes from +.10 to -.10) and don't seem to have a pattern.
The gray areas are confidence bands (e.g. tell you whether the correlation is statistically significant).

Interpretation of correlogram


inserting records in Oracle SQL

Question: I have a table with following columns
USER_ID,LINK_NAME,LINK_URL,DESCRIPTION,LINK_ID.

USER_ID can have duplicate values. I need to add a specific data containing LINK_NAME,LINK_URL,DESCRIPTION,LINK_ID for those USER_ID which does not already contain it. Any solution is welcomed.
Comment: Post some sample data with desired results. Otherwise it is tough to get the solution
Answer: Here is an easy way to do this.
INSERT INTO your_table (user_id,
                    link_name,
                    link_url,
                    description,
                    link_id)
SELECT a.user_id,
      a.link_name,
      a.link_url,
      a.description,
      a.link_id
 FROM (SELECT DISTINCT user_id,
                       :1 link_name,
                       :2 link_url,
                       :3 description,
                       :4 link_id
         FROM your_table yt) a
WHERE NOT EXISTS
             (SELECT 'X'
                FROM your_table b
               WHERE     b.user_id = a.user_id
                     AND NVL (b.link_name, 'IMPOSSIBLE_VALUE') = a.link_name
                     AND NVL (b.link_url, 'IMPOSSIBLE_VALUE') = a.link_url
                     AND NVL (b.description, 'IMPOSSIBLE_VALUE') = a.description
                     AND NVL (TO_CHAR(b.link_id), 'IMPOSSIBLE_VALUE') = a.link_id)

I have assumed that link_id will be a NUMBER so in order to use NVL on this I transformed this to CHAR.
Also another assumption I have taken is that USER_ID has index. If you don't have index on this column you will have performance issues (depending on the size of the table).
In case this is a huge table, you might want to use PARALLEL hint  before DISTINCT since you must full table scan in order to  get the distinct user ids (and not make multiple inserts for a user_id).

inserting records in Oracle SQL


Null Pointer exception using Page object model with Cucumber

Question: I have used Page Object Model design pattern along with Cucumber. So for this I have created two pages named as abstractPage and loginPage but on running the script i get null pointer exception even when i have already initialize the webelements using pagefactory , please have a look at below code:
abstractPage:
public class abstractPage {

    protected WebDriver driver;
    public static loginPage lpOBJ;

    public void openBrowsernURLhit() {
        driver=new FirefoxDriver();
        driver.get("http://www.facebook.com");
        PageFactory.initElements(driver, loginPage.class);
    }
}

loginPage:
public class loginPage extends abstractPage {

    @FindBy(name = "email")
    public WebElement username;

    @FindBy(name = "pass")
    public WebElement password;

    @FindBy(id = "u_0_2")
    public WebElement loginButon;

    public void loginIntoApp() {

        String url=driver.getCurrentUrl();
        System.out.println("the url is::::::::::::::::"+url);
        username.sendKeys("testuser");
        password.sendKeys("123");
    }

    public void clicklogn() {
        loginButon.click();
    }
}

And then I have a class stepDefination from where I am calling the above methods of login class. Browser is opened fine and url is hit but when it goes inside loginintoapp() method it throws exception on first line itself.
public class SmokeTest {

    @Given("^Open Firefox and start application$")
    public void Open_Firefox_and_start_application() throws Throwable {

        abstractPage obj = new abstractPage();
        obj.openBrowsernURLhit();
    }

    @When("^I enter valid \"([^\"]*)\" and \"([^\"]*)\"$")
    public void I_enter_valid_username_and_password(String arg1, String arg2) throws Throwable {

        loginPage lpobj = new loginPage();
        lpobj.loginIntoApp();
    }

    @Then("^user should be able to login successfully$")
    public void user_should_be_able_to_login_successfully() throws Throwable {

        loginPage lpobj = new loginPage();
        lpobj.clicklogn();
    }
}

I also have a TestRunner class where i have glued my stepDefination:
@RunWith(Cucumber.class)
@CucumberOptions(
        features= "features",
        glue= {"stepDefination"},
        plugin= {"html:target/cucumber-html-report"}
        )

public class TestRunner {

}

But on running the above script I always get NullPointerException in first line ofloginIntoApp() method.I have already used Pagefactory to initialize webelements but i guess "driver" variable is not getting initialized in Login class though i have inherited asbtract class where i m instantiating the driver due to which it is throwing null pointer.Please see what I am i doing wrong here.Error is as follows:

1 Scenarios ([31m1 failed[0m)
      3 Steps ([31m1 failed[0m, [36m1 skipped[0m, [32m1 passed[0m)
      0m18.149s

java.lang.NullPointerException
    at pages.loginPage.loginIntoApp(loginPage.java:22)
    at stepDefination.SmokeTest.I_enter_valid_username_and_password(SmokeTest.java:33)
    at ?.When I enter valid "firstname.lastname@example.com" and "pass"(MyApp.feature:5)

Thanks
Answer: please add the following constructor. It may solve the issue.
public loginPage(){
  PageFactory.initElements(driver, loginPage.class);
}

you can put outside as well like,
 public void loginIntoApp() {
      String url=abstractPage.driver.getCurrentUrl();
      System.out.println("the url is::::::::::::::::"+url);
      loginPage lp=PageFactory.initElements(abstractPage.driver, loginPage.class);
      lp.username.sendKeys("testuser");
      lp.password.sendKeys("123");
   }

other way is,
public class SmokeTest extends abstractPage {

    @Given("^Open Firefox and start application$")
    public void Open_Firefox_and_start_application() throws Throwable {
            openBrowsernURLhit();
    }

    @When("^I enter valid \"([^\"]*)\" and \"([^\"]*)\"$")
    public void I_enter_valid_username_and_password(String arg1, String arg2) throws Throwable {

        loginPage lpobj = PageFactory.initElements(driver, loginPage.class);
        lpobj.loginIntoApp();
    }

    @Then("^user should be able to login successfully$")
    public void user_should_be_able_to_login_successfully() throws Throwable {

        loginPage lpobj = PageFactory.initElements(driver, loginPage.class);
        lpobj.clicklogn();
    }
}

Comment: actually it is working if return my driver from abstractpage class and accept it in the smoke test class in openBrowsernURLhit method n then use this driver variable , otherwise it doesnt work , dont knw why even after extending the class it does get the initialized driver variable
Comment: public void loginIntoApp() {

  String url=abstractPage.driver.getCurrentUrl();
  System.out.println("the url is::::::::::::::::"+url);
  PageFactory.initElements(abstractPage.driver, loginPage.class);
  username.sendKeys("testuser");
Comment: it still throw null pointer exception on line username.sendkeys("testuser");
Comment: ok it is working when i m putting pagefactory code inside constrcutor but not workng when i m putting it inside loginIntoAppmethod() as pased above , any idea why it behaves like that?
Comment: you can put outside as well like, public void loginIntoApp() { String url=abstractPage.driver.getCurrentUrl(); System.out.println("the url is::::::::::::::::"+url); loginPage lp=PageFactory.initElements(abstractPage.driver, loginPage.class); lp.username.sendKeys("testuser");
Comment: yes but putting it inside is not working which is my question
Comment: the problem here is the page factory elements should be initialized using the initElements method and it will return the object of  the page.
Comment: Instead of extending the abstract page class in login page, you can extend it with smokeTest class. so that you can initialize the page factory. updated the answer please check
Comment: Like you have mentioned : loginPage lp=PageFactory.initElements(abstractPage.driver, loginPage.class);
      lp.username.sendKeys("testuser"); it works like this but if I uselike this::  PageFactory.initElements(abstractPage.driver, loginPage.class);
      username.sendKeys("testuser"); then it thrwos null pointer , any idea why it behaves like this?
Comment: because username field is not initialized.
Comment: doesn"t it get initialized just on using pagefactory above that line?
Comment: Best practice, need to be initialized in step definition class/test class not inside the page factory class method
Answer: the simplest way to solve the problem is to make your driver object static.
 protected static WebDriver driver;

as you are using page factory, you also need to initiazlize the page object, otherwise all the the WebElement will be null
  public loginPage(){
        PageFactory.initElements(driver, this);
    }

or this, which I don't think is a good practice and people should avoid this.
public void loginIntoApp() {
String url=driver.getCurrentUrl();
System.out.println("the url is::::::::::::::::"+url);
PageFactory.initElements(driver, this);
username.sendKeys("testuser");
password.sendKeys("123");

}
Comment: I did like this bt still getting same exception:
Comment: public void loginIntoApp() {

  String url=abstractPage.driver.getCurrentUrl();
  System.out.println("the url is::::::::::::::::"+url);
  PageFactory.initElements(abstractPage.driver, loginPage.class);
  username.sendKeys("testuser");

 }
Comment: ok it is working when i m putting pagefactory code inside constrcutor but not workng when i m putting it inside loginIntoAppmethod() as pasted above , any idea why it behaves like that?
Comment: if you will look closely I have provided constructor and not a method. what you have done is put code in method. simply copy paste the code in your loginPage class and everything will work. Also, please read java before jumping into automation.
Comment: i said it works when putting it inside constructor which is fine but my question is why doesnt it work when putting it inside method?constructor part i m aware of , i have used it before inside method and it worked.


Ionic 3 component javascript not refreshing after upgrading to v3

Question: I have a problem (probably) with cache while developing an ionic 3 app. I don't know if it is connected to the lazy loading of components but I have this problem after I upgraded to v3 and enabled lazy loading... The app is in development mode and I'm running it through ionic serve... If I change something in the component's ts file the change is not reflected in the browser.. I don't have this problem with component's template (html file)... If I change something in the html template it's working fine.. Also I'm using Augury chrome extension for debugging angular 2 apps... When I click on component's source it shows me the old file...
Update:
I found out that the problem lies somewhere in the ionic serve... When I restart it I can see the changes in the browser...
Answer: It seems to be an issue of the app-scripts (github issue). As you can see in that threa, to fix it just update app-scripts to the nightly version:
npm install @ionic/app-scripts@nightly
Comment: I tried ionic serve again and it seems it's working now... I will accept this answer... thanks
Comment: I get this error when I try to update app-scripts: firstname.lastname@example.com - UNMET PEER DEPENDENCY email@example.com..
npm WARN email@example.com requires a peer of ajv@>=5.0.0 but none was installed.
Comment: If you run `ionic info` in the project folder, what do you see in the `Ionic App Scripts Version:` ?
Comment: global packages:

    @ionic/cli-utils : 1.5.0
    Ionic CLI        : 3.5.0

local packages:

    @ionic/app-scripts              : 2.0.1-201707121621
    @ionic/cli-plugin-ionic-angular : 1.3.2
    Ionic Framework                 : ionic-angular 3.5.0

System:

    Node       : v7.10.0
    OS         : Linux 4.4
    Xcode      : not installed
    ios-deploy : not installed
    ios-sim    : not installed
    npm        : 4.2.0
Comment: Perfect, you now have the latest version of the app-scripts :) (I have the same version installed on my end). Could you please check if the error still persists?


How to call function?

Question: I have this function:
$scope.PinTicketSearch = function(pinTicket) {

    if (pinTicket != null) {
        ticketService.searchTicket(pinTicket)
            .then(function(response) {
                $location.search({
                    "ticketPin": pinTicket
                });
                $scope.TicketDetail = response;
                $scope.ShowDetailsAboutTicket = true;
            });
    }
}

and i have this part of code:
 if ($location.search().ticketPin)
 {

 }

How can i call this function $scope.PinTicketSearch and pass parameters from $location.search().ticketPin. I tried with $scope.PinTicketSearch($location.search().ticketPin) but i get an error

PinTicketSearch is not a function
Comment: If it's in the same controller, then make sure `if` block goes **after** `$scope.PinTicketSearch` declaration.
Comment: Not enough info. One thing for sure, `PinTicketSearch` is not in the scope or prototype chain (parent scopes). Post more code/HTML.
Comment: You see, I told you in the first comment to post more code, then would have figured it out much faster.
Comment: In one case im calling PinTicketSearch on click but now i want to call that function if there is ticketPin
Comment: Is the code `if ($location.searhc.ticketPin){..}` in the same controller as the code `$scope.PinTicketSearch`?
Comment: `ticketPin` is an argument for the `PinTicketSearch` function you're creating. you'd have to `$location.search().PinTicketSearch()`...
Comment: @callmekatootie yes it is
Comment: @dfsq can i ask u one more question? :)
Comment: @uzhas you can ask another question using the "Ask Question" button.
Comment: @MarcB: err, what? This is not how javascript works :)
Answer: You should to use a callback, instead of your if:
$scope.PinTicketSearch = function(pinTicket, success) {
    if(pinTicket != null) {
        ticketService.searchTicket(pinTicket)
            .then(function(response) {
                $location.search({
                    "ticketPin": pinTicket
                });
                $scope.TicketDetail = response;
                $scope.ShowDetailsAboutTicket = true;
                success();
            });
    }
}

and your "if", will be:
$scope.PinTicketSearch(pinTicket, function() {
    // your original "if" body
});

How to call function?