How to block Webform spam based on keyword strings?

Question: Short Version: I would like to block Webform submissions that contain strings e.g. mail.ru in the webform textarea field.
Recently I am seeing a lot of fake webform submissions. I am already using honeypot contrib module and I have added quite a number of IP addresses to the server firewall. However there is still some spam coming through. For now I don't want use Captcha. Luckily those spammers always using recurring pattern e.g. same domain name.
How I can automatically block submission that contain certain strings? Using the rules + webform_rules module and Regex doesn't allow me to choose the Webform email field as data selector. selected-webform:0 doesn't look valid. What's the right way to block webform spam?
{ "rules_blocking_fake_registration" : {
"LABEL" : "Blocking fake registration",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "webform_rules" ],
"ON" : { "webform_rules_submit" : [] },
"IF" : [
  { "node_is_of_type" : {
      "node" : [ "node" ],
      "type" : { "value" : { "contactform" : "contactform" } }
    }
  },
  { "text_matches" : {
      "text" : [ "selected-webform:0" ],
      "match" : "plusgmail.ru",
      "operation" : "regex"
    }
  }
],
"DO" : [
  { "block_ip" : [] },
  { "mail" : {
      "to" : "email@example.com",
      "subject" : "[site:name]: Blocking user [data:e_mail-value]",
      "message" : "E-Mail Adresse: [data:e_mail-value]",
      "language" : [ "site:current-user:language" ]
    }
   }
  ]
 }
}

I really appreciate any comments and help.
Answer: Try Webform Validation module. It adds validation options for webforms. One of these extra options is "words blacklist", where you can specify the list of strings that should not appear in your field. The spammers will get validation error and will not be able to submit the form.
Comment: Kiril, thank you. This looks like pretty much what I was looking for. However I don't want to show spammer a validation error. Next step, spammer simply try out various combination until the submission is valid. Thus, I would like to auto block them.
Comment: Your are welcome. 1. This module lets you specify the error message, so you can avoid giving clues to the spammers. 2. They come to your site to put specific messages with specific links, so if you forbid these links they would have to give up.
Comment: And, of course, there's no D8 version... sigh
Answer: Part 1 - Getting access to Webform data
Below is something about using Webform data in Rules, which may help you to get this to work (assuming your question is about D7) ...
You have access to all labels and all values of any field of a webform, ie:

[data:MachineFieldName-title] contains the Label of a field with machine name MachineFieldName.
[data:MachineFieldName-value] contains the Value of a field with machine name MachineFieldName

So try to use similar tokens. For way more details on this, and a prototype rule about this, refer to the answer to "How to create a node from a Webform using rules?":

the 1st attempt relates to using "Entity has field" (which doesn't work for Webform)
the 2nd attempt explains what to do in case of Webform.

With the above in mind, and looking at something I see in your Rules Actions (not Rules Conditions ...), it seems that [data:e_mail-value] might be pretty close to what you're looking for (assuming it's the field with machine name e_mail that you want to validate via a Rules Condition, since you didn't mention the machine name of the Webform field to be validated).
Part 2 - Validating Webform data
Everything described in Part 1 is a good start, but not sufficient to achieve your goal (= validate what is contained  "in the Webform textarea field", as in your question).
However, you can use a technique similar to what is described in my answer to "How to do a data comparison to submitted webform data when using the Rules module?". For short:

Add an extra Rules Action to Add a variable, with a value assigned to it equal to your [data:e_mail-value-raw].
Add whatever validation you want, probably also something like a text comparison to check if the newly added variable value contains mail.ru.

Be aware: as per the Rules Event you are using in the rule contained in your question, the Webform has been submitted (and I'm not aware of what seems like a more appropriate event like "Before submitting a Webform"). So therefor your existing Rules Action to block the IP address is a good start (together with the eMail to the admin). But know that the form is submitted already.
Comment: Yes, as you can see my question is related to D7. The machine name is correct [data:e_mail-value]. I followed your answer, Unfortunately I am unable to define [data:e_mail-value] as data selector. php evolution is required. Looks like there's additional custom code required?
Comment: Thanks. Unfortunately I can't share screenshots in the comments. Creating a new condition with > Text comparison > Data selector > [data:e_mail-value] and COMPARISON OPERATION regular expression fails with "Data selector [data:e_mail-value] for parameter text is invalid."
Comment: Pierre: That sounds great. I am kind of surprised that no one really wants to get rid of webform spam. Various captcha contrib module are not a solution. I prefer using rules to customised the workflow. Basically rules ships with all tools that are necessary to auto block ips and to get a instant mail notification. I just the perfect swiss-knife.
Comment: @howdytom ***I cannot agree more with what you wrote in your prior comment.***. But guess what ... I think I solved your "puzzle" (and I must say "I" like my own, Rules based, solution). Merci for the challenge!
Comment: Thank you so much for sharing and your time! Based on your solution I was able to create multiple conditions which includes a kind of blacklist with Regex text comparison. Finally the spammer ip address is automatically blocked and I am getting a mail notification. Excellent! Unfortunately Webform Rules doesn't provide Event "Before" a webform has been submitted. So, the first spam message still gets through. It's a pity. Custom code is still required though.
Comment: @howdytom merci for the feedback (and bounty, etc). Let's consider this as "as good as it gets", after all "the webform has been submitted", right? The good news is that, right away, appropriate remedies are taken for such spammers. Maybe somebody should post a new (followup) question like "*How to further improve the solution explained in this answer?*".
Answer: To Avoid spam words on Webform. Try this module (Drupal 8 & 9) https://www.drupal.org/project/webform_spam_words
We can specify the spam keywords, field and error message. It's a simple and lightweight module
Comment: Thanks for sharing. It is worth noticing, that Pierre's solution goes one step further. I was looking for a way to automatically block spammers ip address based on specific keywords. I will take a look at webform_soam_words module.


Bayes optimal decision for logistic regression: Self-study exercise

Question: We want to find the Bayes optimal decision for logistic regression. That means that the goal is to find the actions, which minimize our expected loss (also often called expected cost or risk). Here are the the characteristics:

$ \begin{equation}
   \text{Conditional distribution $P(y|x)$}  =
    \begin{cases}
      \sigma (\mathbf{w}^T\mathbf{x}), & \text{if}\ y=1 \\
      1-\sigma (\mathbf{w}^T\mathbf{x}), & \text{if y= -1}
    \end{cases}
  \end{equation}$
Action set = $\{-1,1,D\}$
$ \begin{equation}
   \text{Cost-function $C(y,a)$} =
    \begin{cases}
     (\mathbf{1}_{y \neq a}), & \text{if a $\in$ {1,-1}}\  \\
     c < 0.5, & \text{if a = D}
    \end{cases}
  \end{equation}$

Where $\sigma$ is the Sigmoid-function used in the context of logistic regression. D means that "we don't know". The cost function is written down as follows in our solutions:
$(*) \mathbb{E}[C(a,y)|x] = \int C(y,a)P(y|x)dy = \sum_y (\mathbb{1}[y \neq a]\mathbb{1}[a \neq D]+c*\mathbb{1}[D=a])P(y|x)$
My question
The way (*) is written, it reads to me as:
$ (\mathbb{1}[y_1 \neq a]\mathbb{1}[a \neq D]P(y|x)+c*\mathbb{1}[D=a]P(y|x) +
(\mathbb{1}[y_{-1} \neq a]\mathbb{1}[a \neq D]P(y|x)+c*\mathbb{1}[D=a]P(y|x)$
So we are summing up the cost for D twice - is this not wrong?
Comment: It's quite different. What is called the conditional distribution here is a model assumption which will lead to the posterior distribution.
Comment: No it's a model assumption. You assume the data are conditionally Binomial with $y|x \sim Bin(n, \sigma(w^Tx))$, I suppose.
Comment: This seems to be a strange statement. The posterior is $p(w|y,x)$ and its not estimated but simulated from or approximated
Comment: Ah I think I have seen this before. However, imho the posterior probability is defined as the probability of the parameters given the data. See here: https://en.wikipedia.org/wiki/Posterior_probability
Comment: Are you sure this is the posterior distribution? It seems the posterior would more look like $p(w|y,x)$
Comment: I am sure, here the source https://las.inf.ethz.ch/courses/lis-s17/hw/hw5_sol.pdf
Comment: Where does it say that? It says conditional distribution as far as I can see
Comment: You are right, I corrected it...I use(d) to think about P(y|x) as the posterior class probability - what would be the difference?
Comment: So it's like an estimator of the posterior (intuitively)?
Comment: Hm, why then is it said that "with logistic regression, we estimate directly the *posterior*"  (in comparison to e.g. generative models)?
Comment: See e.g. here (section log regression, but also seen often in stats books): https://books.google.ch/books?id=ysucBAAAQBAJ&pg=PA644&lpg=PA644&dq=logistic+regression+estimating+directly+the+posterior&source=bl&ots=BzqlZjXEWx&sig=QvQ39EllmOXIyjZgU7pQO8XqodA&hl=en&sa=X&ved=0ahUKEwitmPr84L3VAhWBalAKHZ8ZA4IQ6AEIWTAH#v=onepage&q=logistic%20regression%20estimating%20directly%20the%20posterior&f=false
Comment: just fyi you can write $1-\sigma(w^Tx)=\sigma(-w^Tx)$. This means the conditional can be written as $P(y|x)=\sigma(yw^Tx)$
Answer: I think it is likely to be less confusing if you first write out the posterior expected loss for each action separately, rather than as one complicated function. It is actually quite simple.
$$E[C(+1,y)|x]=P(y=-1|x)$$
$$E[C(-1,y)|x]=P(y=+1|x)$$
$$E[C(D,y)|x]=c$$
The expected cost for "don't know" doesn't depend on the posterior probabilities, because the cost doesn't depend on $y$. This is why you see it appear "twice" (once for each outcome).
Answer: the summation is over $y$ ; in your case $-1$ and $1$. The $P(y|x)$ term will be once for $y=-1$ and once for $y=1$
\begin{align}&(\mathbb1[y_1\ne a]\mathbb1[a\ne D]P(y_1|x)+c\cdot \mathbb1[D=a]P(y_1|x)\\&+(\mathbb1[y_{−1}\ne a]\mathbb1[a\ne D]P(y_{-1}|x)+c\cdot 1[D=a]P(y_{-1}|x)\end{align}


if $\|AB\| / \|AC\| = \alpha$ show that $OB = \alpha OC + (1 - \alpha) OA$

Question: Link to the diagram. Note: this is for self study.
https://i.stack.imgur.com/fFz18.jpg

How do i get started to solve this?.
$ AB = OB - OA $ -> eq(1)
$ AC = OC - OA $ -> eq(2)
Taking norm on both sides of the eq(1), eq(2)
$ \|AB\| = \|OB\| - \|OA\| \  (triangle \ inequality \ \|A - B \| \geq \|A\| - \|B\| )$ -> eq(3)
$\|AC\| = \|OC\| - \|OA\| $ -> eq(4)
$\|AC\| = \frac{1}{\alpha}(\|OB\| - \|OA\|)$  [since $\frac{\|AB\|}{\|AC\|} = \alpha $]
From eq(3)
$\alpha \|AC\| = \|OB\| - \|OA\|$
$\alpha (\|OC\| - \|OA\|) = \|OB\| - \|OA\|$
$\|OB\| = \alpha \|OC\| + (1 - \alpha) \|OA\|$ [since norm is positive]
$OB = \alpha OC + (1 - \alpha) OA$
Answer: It is easier to solve the second equation for $\alpha$:
\begin{align}
OB
&= \alpha OC + (1-\alpha) OA \\
&= \alpha (OC - OA) + OA \iff \\
OB - OA &= \alpha \, (OC-OA) \iff \\
AB &= \alpha AC \quad (*)
\end{align}
Now we take the vector norm of each side:
\begin{align}
(**) \quad
\lVert AB \rVert &= \lVert \alpha AC\rVert \iff \\
\lVert AB \rVert &= \lvert \alpha \rvert \, \lVert AC\rVert \iff \\
\lvert \alpha \rvert &= \frac{\lVert AB \rVert}{\lVert AC\rVert} \ge 0
\end{align}
Alas we have to prove backwards. Thus two remarks:
If we define $\alpha$ by the right hand side, it must be non-negative, as a norm is non-negative, so we can drop the bars.
To make the step from $(**)$ to $(*)$ we need to know that $AB$ and $AC$ are multiples, this can be inferred from the diagram.
Comment: please comment on my solution
Answer: I assume you know that the line segment between any two points $a$ and $c$ can be written as
$$b = ta + (1-t)c$$
where $t \in [0,1]$. In fact, we say that $t$ parameterizes the line segment.
Then to get started to solve this, notice what happens at the endpoints: when $t=0$ and when $t=1$. Once you have answered that, answer: what can we say about a given particular point in between the two, say $t=\alpha$?
Comment: Please clarify the parametric line approach
Comment: It is just an equation that is often taught. However, to fully understand the equation, re-read my questions that I asked already in my answer. If you can answer these questions, then you'll more fully understand "the parametric line approach".
Answer: I would like not to give you the answer imidiately because this is a good excersice if u cannot then we talk about it. The hint here is that OB=OA+AB and that if you have a vector KL with ||KL||=1 RT=||RT||KL as long as they have the same adress. Now all u have to do is make what you have been given more simple.


A min function in query set Django to find the earlier date in a model?

Question: I have a model that where each entry has a date.  How do I use queryset to find the entry with the earliest date?  This is not dependent on 'pk' because I could add a row with an earlier date at a later time.
This is what I have so far and it doesn't seem to work:
Model.objects.get(min(date))

Thanks.
Comment: Check here [http://stackoverflow.com/questions/252242/in-django-how-do-i-sort-a-model-on-a-field-and-then-get-the-last-item](http://stackoverflow.com/questions/252242/in-django-how-do-i-sort-a-model-on-a-field-and-then-get-the-last-item)
and here [https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.earliest](https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.earliest)
Answer: Model.objects.earliest('date')

Answer: earliest = Model.objects.order_by('date')[0]

You need to handle the case where there's no objects in Model though.


Microsoft Windows SChannel SSPI Self test?

Question: The question pertains to some specs (FIPS) that require the SChannel modules used for TLS perform a self test prior to use to validate that they have not been compromised. FIPS is one example. In the Open source libraries there are self tests built in. Does SChannel have this capability?
I am not able to find any reference to this, yet it seems odd that this would be left out.
I hope that makes the question less vague and ambiguous. I believe this is a question that can reasonably be answered by someone that has deep knowledge of the SChannel API.
Added:
My understanding of FIPS level 1 and certainly level 2 is that the operation of the algorithms needs to be verified at run time (self test) not just when originally certified. Additionally, the image in memory needs to be validated with a hash or some such to make sure it has not been changed.
If these things are not done a run time, the potential for a patched library exists does it not?
Answer: FIPS certification is acheived for a given cipher suite on a given OS configuration. Certification will give you a level a  It will give you a level, 1 being the highest you can acheive on regular hardware, but the lowest level security wise.
Now back to your question...
Starting from the cipher suites used SChannel, we can work our way up to the FIPS certificate from this page.
According to the PDF scan of the FIPS certificate (for Windows 7 Ultimate) or the 1337 certificate of Windows Server 2008 R2 64bits, you see on their second page that Self-Tests were certified at level 1.
So yes, SChannel does have self test capability, because it leverages the self-test capabilities of lower components. But if shifts some of the burden to you to validate that your software uses only certified components in a certified environment.
Comment: I don't have that link. I guess you could acheive level 2 on regular hardware, but certification is not free. The premium you will pay for a level 2 server will probably remove it from the "regular" category. From http://csrc.nist.gov/publications/fips/fips140-2/fips1402.pdf "Security Level 2 enhances the physical security mechanisms of a Security Level 1 cryptographic module by adding the requirement for tamper-evidence, which includes the use of tamper-evident coatings or seals or for pick-resistant locks on removable covers or doors of the module."
Comment: "1 being the highest you can acheive on regular hardware" have you got a link to how this scoring works?


Vocal tuning (metering, not autotuning)

Question: I've tried using the built-in instrument tuner from Apple Logic Pro to check if I'm on tune when singing, but a lot of the time, it takes a few seconds to react to whatever's happening (even if playback, not live) and even when it does, I'd like to have a better monitor than making the red turn blue to know I'm hitting the right note. I almost never say on a blue, it's all over the place, as far as 50 cents, even when it sounds like the right note compared to the piano. I know I'm not THAT pitchy/have a teacher who says I'm pretty much on pitch. I'd like to see frequencies and such, and a better response. Does anyone know of a plugin like that for Mac?
I try searching on Google, but it gets confused and thinks I'm talking about autotuning.
Comment: Shameless plug follows: Sing-inTuna, an iPhone app I wrote to help me practice singing. Shows several different tuning markers that respond in under 100 milliseconds with an accuracy of a maybe few cents.  It's in the iOS App Store.
Answer: The tuner in Logic is designed for tuning instruments, so it takes a moving average. The properties you need for monitoring your singing - a fast response, and accuracy when you're quickly changing pitch, simply weren't design goals for that tool.
For the same reasons, a typical tuner from a music shop, probably won't achieve what you want. A basic guitar tuner requires that you play a single note firmly, on its own, to get an accurate reading.
There is software, and there are pieces of hardware, designed to give visual feedback on vocal pitch. Googling "vocal pitch meter" gave me the Roland VT-12 Vocal Trainer (hardware), PitchPerfector (Windows software) and Voice Tutor (iPhone and Android software). I cannot personally vouch for any of those, but they seem to support the purpose you have in mind. In addition to displaying your sung pitch visually, each of them also includes exercises targeted at improving your overall singing ability.
Comment: From what I can see from their website and video demo, Voice Tutor for iPhone and Android does not have anything to do with intonation or measuring pitch.
Comment: @WheatWilliams Is there not a pitch meter at the bottom of the last screenshot on the link for that software?
Comment: I do not know what that is, because I have not tried the program.
Comment: Hey thanks, I know it's late, but checked out the Vocal Trainer. Seems cool. Wondered about the price. OUCH, $200. The others sound good. Also I found a nag-ware app for mac called Vocal Lab by rustykat that does a pretty nice job of both pitch and frequency on a chart. It's free to try, but 15 to buy, and believe me it will let you know, every 2 mins or so. That's the only catch. I don't have money but when I do I'll be tossing money their way. Thanks, I never knew tuners worked differently. Neither did the Logic forum apparently.
Answer: I don't know what "Tuner" or "from Logic" is, but I can tell you that all tuners have some filtering, aka integration, built in.  If they didn't the meter would be oscillating all the time due to minor background noise, natural variation in pitch input, etc.
It may be that the tuner you're using has a very long filter, and thus takes longer than you would like to settle.  I do know that a couple Windows-based tuners have an option to change this filter time; perhaps yours does as well.
All in all,  why not spend a few bucks and get a standalone tuner such as the KorgCA40 or OT120 (or similar tuners from Seiko or others)?
Edit - since the other answer is close to mine anyway,  consider getting some freeware app which displays an FFT.  There are tons of those-heck, I found a few for Android :-) .   A typical FFT app updates several times a second, so you can see any waver in your pitch pretty easily.
Comment: Carl Witthoft, the questioner is referring to the built-in instrument tuner in the Apple Logic Pro digital audio workstation package, the chief competitor to Avid Pro Tools. http://www.apple.com/logicpro/

Vocal tuning (metering, not autotuning)


Probability of flipping heads where the coin's bias is drawn from a uniform distribution

Question: What is the probability of flipping a head on a coin where the coin's probability of flipping a head is drawn uniformly from $[0,1]$.
I think this question is easy, however I am getting a result that makes no sense. Please don't directly give me the answer. I can look that up in a book.
Let $H$ be the binary event that the a head is flipped. Let $Q$ be Uniform($0,1$).
Informal attempt: Firstly, it's easy to see that given that we know $Q=q$, the probability of flipping heads is simply $1/q$. Since the distribution of $Q$ is uniform, we just need the average value of the function $1/q$. In particular, the probability must be $\frac{1}{1-0} \int_0^1 \frac{1}{q} \, dq$, which does not converge!
Formal attempt: We wish to compute $P(H= \text{True})$. By the total law of probability, we have:
$$P(H=\text{True}) = \int_0^1 P(H=\text{True}\mid Q=q)f_Q(q) \, dq = \int_0^1 \frac{1}{q} (1) \,dq$$ Again, this does not converge!? What am I doing wrong? I have a strong feeling my mistake lies in the fact that I am mixing continuous and discrete distributions. That is, I am conditioning on an event that actually cannot exist, i.e. $P(Q=q)=0$.
Comment: In your calculation, where does $\frac 1q$ come from?
Comment: Why are using the inverse of q? If Q is a value uniformly selected from 0 to 1, in particular q, then q itself is the probability, not the inverse.
Comment: Without looking at your calculation, how could the answer be anything other than $\frac 12$ by symmetry?
Comment: Just to state the obvious, you are getting special case probabilities way over $1$.
Comment: @Pii not a problem. It often just takes another pair of eyes to spot these small little mistakes.
Comment: @Tyberius I have no idea. This question was stupid, ugh. Thank you, apologies for my ignorance.
Answer: I believe the issue is that you are using $1/q$ when you should be using $q$ for the probability. If $q$ is what you get by drawing from $0$ to $1$, then it is already the probability and you can simply integrate over $q$ from $0$ to $1$.
Answer: $$P(H = \text{true}\mid Q =q ) = q$$
$$f_Q(q) = 1$$
Hence you are suppose to evaluate $$\int_0^1 q \,dq$$


Why do I always hear the Zelda theme in Tchaikovsky's Violin Concerto in D Major?

Question: In the first movement of Tchaikovsky's Op. 35, Violin Concerto in D Major, in the very soft part at the middle of the song (07:19 in this Yehudi Menuhin recording), I've always heard the Zelda theme. It's definitely far from a 1:1 mapping though. Can someone who knows music theory describe what the semblance is, if they hear it too?
If there is similarity, do you think there's a chance Koji Kondo meant to pay some homage to Tchaikovsky? Or is this progression (if that's the correct word) "obvious" enough for anyone to discover independently during writing. (I know this is bordering on subjective / unanswerable but in Literature.SE it's par for the course to discuss likelihood of influence based on timelines and sources.)
Comment: I honestly don't hear it
Comment: Oh, I should have been more specific. It's exactly at 7:19 in the linked Tchaikovsky.
Comment: Is 7:09 in your OP a typo, then?
Comment: @Aaron - I was referring to the beginning of sections, but I see that's confusing now. I've edited my answer to point exactly to the part in Tchaikovsky.
Comment: Also it's hard to pinpoint times because it's the "emotion" that is strikingly similar to me. I don't know if it happens exactly that moment or anticipation of a future note is rebasing the "scale" or whatever for me. (More details in comments under @Aaron's answer.)
Comment: I listened to 7:19, and I still honestly don't hear the resemblance.
Answer: Note: This answer addresses the OP as originally written, dealing with 7:09 in the linked Tchaikovsky recording. A second answer addresses 7:19.

It's a combination of rhythm and melodic pattern.
How they can be heard in association
Rhythmic correlation
First, the Tchaikovsky
The Tchaikovsky violin part, at 7:09 (measures 162-63; see IMSLP), contains the rhythm
X: 1
T: Violin Concerto, Op. 35
T: m. 162-63, rhythmic abstraction level 1
C: Tchaikovsky
K: clef=perc stafflines=1 middle=B
M: 4/4
L: 1/16
!>!BBBB !>!BBBB BBBB !>!BBBB | !>!B

Not all of the accents above are literal, but they correspond to the most prominent moments (beats 1 & 2 are double stops; beat 4 is emphasized by grace notes).
So, one way to perceive this is
X: 1
T: Violin Concerto, Op. 35
T: m. 162-63, rhythmic abstraction level 2
K: clef=perc stafflines=1 middle=B
M: 4/4
L: 1/16
!>!B4 !>!B4 z4 !>!BBBB | !>!B4

Now, Zelda
The Zelda theme, from 0:08 to 0:10, is made of the rhythm
X: 1
T: Legend of Zelda
T: Main Title excerpt
K: clef=perc stafflines=1 middle=B
M: 4/4
L: 1/16
!>!B4 !>!B4-B3B !>!BBBB | !>!B4

This can be perceived as
X: 1
T: Legend of Zelda, Main Title
T: rhythmic abstraction level 2
K: clef=perc stafflines=1 middle=B
M: 4/4
L: 1/16
!>!B4 !>!B4 z4 !>!BBBB | !>!B4

Which is identical to the perceived Tchaikovsky rhythm.1
Melodic correlation
The (perceived) rhythmic similarity is reinforced by a melodic similarity. Both rhythmically similar portions of the piece contain major scales.
X: 1
T: Violin Concerto, Op. 35
T: m. 162, solo violin, A major scale
C: Tchaikovsky
K: D major
M: 4/4
L: 1/16
[=ce]G,EG [=ce]G,EG "A"A"B"B"C#"c"D"d {e=f}"E"eceg |

X: 1
T: Legend of Zelda, Main Title
K: Bb major
M: 4/4
L: 1/16
B4 F4-F3B "Bb"B"C"c"D"d"Eb"e | "F"f4

These two elements combined — the close (perceived) rhythmic similarity and the presence of a prominent major scale — are sufficient to create an association between the two pieces.
Is the association intentional?
Doubtful. The similarities are too abstract/obscure for a clear association to be made. To hide an homage so deeply (and minimally) would seem to defeat the purpose. Presumably Kondo would want a more easily noticed allusion.

1 Another point of correlation is the grace notes preceding beat 4 in the Tchaikovsky with the sixteenth note preceding beat 4 in Zelda. The anacruses into beat 4 may also be a part of the overall sense of similarity.
Comment: @AndrewCheong The opening of Zelda is in Bb minor, but when the main theme starts (just before the scale), there's a switch to Bb major. If one's ear doesn't make the shift, then the D in the scale will sound wrong, because a Db would be expected. In the Tchaikovsky double-stops, one of them contains C (below) and B (above). This is a dissonant interval and will also sound "wrong". Moreover, that measure of the Tchaikovsky contains a switch from A minor to C major. The "feel" you get is probably the shift from minor to major combined with the perceived/actual "wrong" notes.
Comment: Thanks! I'm sorry, I should have been more specific. (Part of me was hoping others would pick up exactly where without me being specific, supporting that there _is_ a similarity.) It's possible the points you mention "primed" me for the following: What do you think about the parts at 7:19 in the linked Tchaikovsky? Compared to the Zelda, they both have a descending scale there. But it's something about the surrounding notes that feels uniquely similar between them to me.
Comment: @AndrewCheong I can construct a reason you might hear them as similar, but it would be an even greater abstraction that in my current post. Briefly, the Tchaikovsky has notes that go down-up-down-up. The direction of those notes, and the distance between them is vaguely similar between the two pieces. There's also some rhythmic similarity.
Comment: I think It's something about the "wrong" notes in each piece. In Tchaikovsky, there's a "wrong" note around 7:20-7:21 in the double-stops. In Zelda, it's in the upward scale at 0:09, Bb Bb C D Eb F. I feel as if the D "should" be a Db. And there's more beyond but it's hard for me to isolate. Although I don't know any music theory, I've always been extremely keen on feelings, and the 2 just sound like the same "emotion" to me. No worries if you're still not seeing / hearing it. It must just be a very specific trigger to me. Thanks.
Answer: NOTE: This answer deals with the passage at 7:19 in the OP-linked Tchaikovsky recording (mm. 166-67; IMSLP). This is the second of two answers. The first answer deals with 7:09, which was referenced in the OP prior to editing.

The Zelda theme, at 0:15 in the OP-linked recording is
X: 1
T: Legend of Zelda, Main Title
T: Excerpt
K: Bb Major
M: 4/4
L: 1/8
EE/2F/2 _G4 FE | _D_D/2E/2 F4 ED |

Here is the Tchaikovsky solo violin part, measures 166-67.

Notice the accented notes in measure 166 and the parallel notes in measure 167. Isolating those gives:
X: 1
T: Violin Concerto, Op. 35
T: mvmt I, Solo violin, mm. 166–67 abstracted
K: D Major
M: 4/4
L: 1/16
azzb =c'zba g4 z4 | =fzz^g az=gf e4 |

For convenience, since the Zelda excerpt starts on E♭, transpose the Tchaikovsky reduction to start on the same pitch.
X: 1
T: Violin Concerto, Op. 35
T: transposed reduction
K: none
M: 4/4
L: 1/16
_EzzF _GzFE _D4 z4 | _Czz=D _Ez_DC _B,4 |

Now consider a rhythmic variation (excluding some pitches).
X: 1
T: Violin Concerto, Op. 35
T: rhythmic variation
K: none
M: 4/4
L: 1/16
_EzzF _G8 F2E2 | _CzzD _E8 _D2C2 |

And finally, observe the Tchaikovsky variation juxtaposed with Zelda.
X: 1
T: Zelda and Violin Concerto
T: thematic juxtaposition
K: none
M: 4/4
L: 1/16
V:V1 name=Tchaikovsky
_EzzF _G8 F2E2 | _CzzD _E4 _D2C2
V:V2 name=Zelda
_E2EF _G8 F2E2 | _D2_D_E F4 E2D2

Rhythmically and intervalically, they're nearly identical.
This is enough to make a connection between the two pieces given sufficient familiarity with each.
Comment: Finally got a chance to see the sheet music results of your ABCjs on mobile (I had to switch to the desktop version to see that) - **the second half of your transposed reduction is wrong - that portion needs to start with B or C flat instead of C**, as the first note of the second half and the first note of the first half are a major third apart. Nevertheless, even with the correct transposition, I still don't personally hear the Zelda theme in the Tchaikovsky work, partially because the chord progressions do not match.
Comment: @Dekkadeci Thanks. I've corrected the notation. I appreciate you read carefully enough to notice the mistake.


Why is "$P \Rightarrow Q$" equivalent to "$\neg Q \Rightarrow \neg P$"?

Question: Why is "$P \Rightarrow Q$" equivalent to "$\neg Q \Rightarrow \neg P$"?
I am trying to understand why this does always apply, in terms of pure logic. Can you please explain it to me?
Comment: http://en.wikipedia.org/wiki/Transposition_(logic).
Comment: Have you compared their truth-tables?
Comment: You could also search this site for the term [contrapositive](http://math.stackexchange.com/search?q=contrapositive).
Comment: @amWhy great idea, thank you!
Comment: @HaraldHanche-Olsen Never heard of it before, thank you!
Comment: For which logic?
Comment: @Doug He probably means the ordinary first-order logic.
Answer: Suppose it's true that:

If you hit my glass table with a hammer, it will break.
My glass coffee table is not broken.

If you accept premisses 1 and 2, wouldn't you conclude from this that
3. My glass coffee table has not been hit with a hammer?

If we let $P$ represent "my glass table was hit with a hammer" and $Q$ be "my glass table is broken", then (1) is $P\implies Q$, and (2) is $\lnot Q$.
From (1) and (2) we can conclude that the table has not been hit with a hammer; that's $\lnot P$.  One way to write this is $$((P\implies Q) \land \lnot Q) \implies \lnot P$$
but is $$(P\implies Q)\implies (\lnot Q \implies \lnot P)$$ which says that if we know $P\implies Q$ (that's (1)) then if we know $\lnot Q$ (that's (2)) then we can conclude $\lnot P$ (that's (3)).
On the other hand, it is not the case that
$$(P\implies Q)\implies (\lnot P \implies \lnot Q)$$
This says that if (1) is true, and you know my table was not hit with a hammer, then you know it is not broken.  But no, that's wrong, because actually it is broken because my brother-in-law got drunk and fell on it; no hammer was involved.
Answer: It is given that $P\to Q$. By material implication, $\neg P\vee Q$. By commutativity, $Q\vee\neg P$. Again, by material implication, $\neg Q\to \neg P$.
Comment: By "material implication" he means: the **definition** of $P\to Q$ is $\neg P\vee Q$.  Since you said "pure logic" this is the thing to use.
Comment: But this relationship holds in Lukasiewicz three-valued logic where (P→Q) is not logically equivalent to (¬P∨Q).
Answer: $$\begin{array} {|c|c|c|c|} \hline
P & Q & P \Rightarrow Q & \lnot Q \Rightarrow \lnot P \\ \hline
\top & \top & \top & \top \\
\top & \bot & \bot & \bot \\
\bot & \top & \top & \top \\
\bot & \bot & \top & \top \\
\hline
\end{array}$$
Equal.
Answer: You can check it simply by writing up every situation in a truth-table and see that they produce the same results for any choices of $ P $ and $ Q $.
$ P \Rightarrow Q $ is only false when $ P $ is true and $ Q $ is false.
Likewise $ \neg Q \Rightarrow \neg P $ is only false when $ \neg Q $ is true and $ \neg P $ is false, thus when $ Q $ is false and $ P $ is true.


how to get data from my database using dates between two dates

Question: My work is working it outputs my table column names in the DataGridView but my data doesn't show up is their someting im missing here?
<code>        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = this.dateTimePicker1.Value.Date;

            DateTime dt2 = this.dateTimePicker1.Value.Date;

            command.CommandType = CommandType.Text;
           command.CommandText = ("SELECT * FROM BillingSystem WHERE DateOfTransaction BETWEEN #" + dt.ToShortDateString() + "# AND #" + dt2.ToShortDateString() + "#");

           OleDbDataAdapter adapter = new OleDbDataAdapter(command.CommandText, connectionBilling);
           DataSet ds = new DataSet();

           adapter.Fill(ds);
           dataGridView1.DataSource = ds.Tables[0];
           connectionBilling.Close();

            }
</code>
Comment: how it will work if your using Between clause for same datetime variables.choose another datetimepicker for dt2.
Comment: ohhh crap that is so funny, still thank you LOL @Anand
Comment: it happens,increment my comment if it works for you.
Answer: Well, I see that the two dates are the same (probably you want to enclose a full day in the BETWEEN statement). So I would try to change your query to use a parameterized query instead of string concatenation and fix the date inclusion part.
<code>   private void button1_Click(object sender, EventArgs e)
   {
        string cmdText = "SELECT * FROM BillingSystem " +
                         "WHERE DateOfTransaction BETWEEN ? AND ?";

        DateTime dt = this.dateTimePicker1.Value.Date;
        DateTime dt2 = this.dateTimePicker1.Value.Date.AddMinutes(1440);
        command.CommandText = cmdText;
        command.Parameters.AddWithValue("@p1",dt1);
        command.Parameters.AddWithValue("@p2",dt2);
        OleDbDataAdapter adapter = new OleDbDataAdapter(command, connectionBilling);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        dataGridView1.DataSource = ds.Tables[0];
        connectionBilling.Close();
    }
</code>
Notice that I add 1440 minutes (24 hours) to your second date.
This will allow a full day to be enclosed in your BETWEEN syntax because Access stores also the TIME part in the DateOfTransaction field and without this fix no date will match the query (unless some trasactions happened exactly at midnight)
Also a parameterized query remove the need to specify any formatting around your dates (and no possibilities of Sql Injection)


Select by row in Oracle

Question: I know its non conventional, but I have a need to select data by row number (as this is how an error report gives us feedback)
Here is the SQL I came up with but it doesn't work. I did start with a MYSQL query, that did work and passed it through a converter as I don't know that much about Oracle's syntax.
SELECT *

FROM (
    SELECT ACCOUNTID, ACCOUNT_SSN, AGENTCODEID, @curRow := FROM dual @curRow + 1 AS row_number
FROM
    agent_code_details_stage_0808
    JOIN (
        SELECT <<@curRow>> = 0
    ) r
) sub

WHERE sub.row_number = 78 or
sub.row_number = 277

Update: When we open the table, there is no "order" specified, but the system shows row numbers. I believe we are using SQL Developer. Ill check again to see if there is a default column it's sorting by.
Is there a way to write this without requiring an order? Most databases are inherently sequential as new data is written to it.. Therefore, I need to pull by row as it was created ( there is no data in the table for me to compare to or sort by ) Think of this as a log file, more than a database. I get an email telling me there was a row in the data that failed, and I need to be able to get to that exact row as it falls in the DB to figure out what failed.
Comment: why don't you use window function `row_number()` and then select it by row?
Comment: You know, "@" is not valid in oracle
Answer: Oracle has row_number():
SELECT acd.*
FROM (SELECT acd.*,
             ROW_NUMBER() OVER (ORDER BY ?) as seqnum
      FROM agent_code_details_stage_0808 acd
    ) acd
WHERE sub.row_number IN (78, 277);

The one caveat is that SQL tables represent unordered sets.  There is no such thing as row 78 in a SQL table in general.  There is only row 78 when ordered by something.  The ? is for the column that specifies the ordering.

Select by row in Oracle


cisco parse show command

Question: I want to write a perl script which logs into cisco switches via telnet and parses the output of show mac-address-table. The problem is that different switches show me a different field layout.
The WS-C3548-XL shows the fields in the order
Destination Address  Address Type  VLAN  Destination Port
while the C2960 shows it like
Vlan    Mac Address       Type        Ports
is there a way to tell show to print the fields in a predefined order?
Comment: You can handle this in your script by checking the output header.
Comment: Have a look at how [NeDi](http://www.nedi.ch) does this.
Comment: telnet [shivers], I don't understand why this prehistoric, clear text protocol is still supported on these devices. Can't you use ssh instead?
Comment: Parsing the output is doing it wrong.  Use SNMP.
Answer: Poll the devices using SNMP.
I would think the MIBs are the same but you never know with Cisco.
Comment: yes, i solved it now via snmp. i only tried the telnet approach because i couldn't find the right oids, but it finally seems to work with RFC1286-MIB
Answer: No, not that I know of.
But since you're already using perl, just check the first line. From there on, you know what the meanings of the particular fields are, and fill your hash (I hope you're using one!) accordingly. This has the additional advantage that in the future, you can support other models with yet another output format. While still supporting the "old" ones. Robust programming.
...Oh, and make sure to handle the space in "Address Type" :)
Answer: Have you thought of scripting using SNMP
You can run external snmpget / snmpwalk utilities and parse the output or use the Net::SNMP module.
Answer: SNMP would be best.  But also remember that the "include" output modifier is regex capable.  You could just grab the info you want if you want stick with Perl-expect

cisco parse show command


CSS Positioning of Google Translate

Question: I am trying to place my google translate select dropdown perfectly in my navbar but am running into a css issue which is messing up all of my dropdowns. I am trying to get my select button to be where the link RIGHT HERE is located in my fiddle. Will someone please tell me what I may need to adjust in my css?
http://jsfiddle.net/bobrierton/5c1vbo2s/

                            RIGHT HERE

Answer: Just add this CSS (Updated):
.skiptranslate.goog-te-gadget, .skiptranslate.goog-te-gadget > div{
 display: inline-block;
}

Here is the updated, JSFiddle, hope you are looking for this.
Comment: Can you share the screenshot of the above problem which you are stating?, change the **display** attribute's value to **inline-block**. Let me know if it works.
Comment: The JSFiddle which you shared, is different from the image which you posted.
Comment: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97413/discussion-between-sahil-and-david-brierton).
Comment: It is happening because, you have used the class **navbar-right**, for **Nav Bar Right Block**, which floats google translate menu to right.
Comment: As I do not have the complete CSS of the page, you can try removing **navbar-right** class from **ul** tag.

CSS Positioning of Google Translate


Show $\lim_{p\to 0} m_Y(u)=\Big(\frac{1}{1-2u}\Big)^r$ where $Y=2pX$

Question:
For any $0<p<1$ and $r$ a positive integer, the probability function
  $$f(x)={{r+x-1}\choose{x}}p^r(1-p)^x \ \ \ \ \ \ x=0,1,2...$$
  defines a random variable $X$.

I have computed the mgf of the above distribution to be $$m_X(u)=\Big(\frac{p}{1-(1-p)e^u)}\Big)^r \ \ \ \ \ \ \ u<\text{ln}((1-p)^{-1})$$

Define a new random variable by $\ Y=2pX\ $. Write down the mgf $m_Y(u)$ of $Y$ and show that $$\lim_{p\to 0} m_Y(u)=\Big(\frac{1}{1-2u}\Big)^r$$

I am unsure of how to solve this, but here is my attempt.
\begin{align*}
m_Y(u)&=\mathbb{E}(e^{uY})\\
&=\mathbb{E}(e^{2pXu})\\
&=\sum_{x=0}^{\infty} e^{2pxu}{{r+x-1}\choose{x}}p^r(1-p)^x\\
&=\frac{p^r}{(1-(1-p)e^{2pu})^r} \sum_{x=0}^\infty \binom{r+x-1}{x} (1 - (1-p)e^{2pu})^r ((1-p)e^{2pu})^x \\
&=\frac{p^r}{(1-(1-p)e^{2pu})^r}
\end{align*}
But clearly in my response, $$\lim_{p\to 0} m_Y(u)\neq\Big(\frac{1}{1-2u}\Big)^r$$
I'm confident there's an error in my logic, but I don't know where.
Comment: $f(z) =z^k$ is continuous hence $a_n \to a$ implies $f(a_n) \to  f(a)$
Comment: You have that limit for $p \to 0$
Comment: Apologies, I have this. It is indeed $p\rightarrow 0$
Comment: taylorexpand $e^{2pu} = 1 + 2pu + O(p^2)$ and you can deduce the limit
Comment: I do not see how this will get the answer, as you arrive at $\ m_Y(u)=\mathbb{E}(1)$.
Comment: $1-(1-p)(1+2pu + O(p^2)) = p - 2pu + O(p^2)=p(1-2u + O(p)) $and simplify
Comment: why would you need that. You have a p factor both above and below the fraction. So they cancel algebraically.
Comment: Is it valid to use L'Hopital's rule om $m_Y(u)$? e.g. $$\lim_{p\to 0} m_Y(u)=\lim_{p\to 0} \Bigg(\frac{p}{1-(1-p)e^{2pu}}\Bigg)^r=\lim_{p\to 0} \Bigg(\frac{1}{-2ue^{2pu}+2pue^{2pu}+e^{2pu}}\Bigg)^r= \Bigg(\frac{1}{1-2u}\Bigg)^r$$
Comment: I just don't understand your method. Would you be able to write it up?
Comment: according to your derivation you have $e^{2pu}$ not $e^u$
Comment: Yes, you are correct. Typo has been fixed.
Comment: yes you can do it that way
Comment: Is there a reason why we can ignore the exponent ($r$)?
Comment: If you're able, I'd enjoy seeing your solution. I shall mark it as the accepted answer.
Answer: L'Hoptal's rule works fine. But one can use teh taylor expansion as well as in, for small $p$ we can use the taylor expansion of $e^x$ and assume $2u \neq 1$, we get,
$$
\frac{p}{1-(1-p)e^{2pu}} = \frac{p}{1-(1-p)(1+2pu + O(p^2))} = \frac{p}{1-1+p-2pu + 2up^2 + O(p^2)}
$$ Summing things up and factor a $p$ leads to
$$
\frac{p}{1-(1-p)e^{2pu}} =\frac{p}{p(1-2u+O(p))} = \frac{1}{1-2u+O(p)}\to\frac{1}{1-2u}
$$ continuity of $f(z) = z^k$ now gives the result.


Is Row(EA) = Row(A)

Question: If we have a square matrix A and an Elementer matrix E with the same ordo as A, can we say that Row(EA) = Row(A)?
I found this isn't true, because of a counter example of
A = \begin{bmatrix}1&2\\3&4\end{bmatrix}
we will have Row(A) = {(1,2),(3,4)}
and if we have,
E = \begin{bmatrix}0&1\\1&0\end{bmatrix}
we will have
EA = \begin{bmatrix}3&4\\1&2\end{bmatrix}
we have  row(EA) = {(3,4),(1,2)}
is that enough of a counter example?
But if we use the echeleon form of the matrices then we can say that Row(EA) = Row(A) because both are {(1,0),(0,1)}
so which one is true?
Comment: If you're reading about this in some book surely $Row(A)$ is not the set of rows of $A$, as you seem to think - in fact $Row(A)$ is the _rowspace_ of $A$, which is to say the _span of_  the rows of $A$. With that definition then yes, $Row(A)=Row(EA)$.
Comment: what does Row mean? the set of rows of a particular matrix? what does ordo mean? why are you performing $AE$ rather than $EA$?
Answer: Notice that matrix is not commutative. $AE \neq EA$.
Also, elementary row operations doesn't change the row space of $A$.
Hence if Row refers to the rowspace, then we have $\operatorname{Row}(A)=\operatorname{Row}(EA)$.
Notice that $AE$ correponds to elementary column operation, elementary column operation might change the row space in general.
Comment: sorry, edited just now, thank you
Comment: Notice that your $Row(EA)$ is wrong. Also, does $Row$ indicate row space?

Is Row(EA) = Row(A)


Storing values in list<> and changing them

Question: I have been trying to create a program that allows the user to edit things such as the supply of mp3 players which have a base value stored in the program:
public struct MP3_Players
{
    public int ID { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int MBsize { get; set; }
    public decimal Price { get; set; }
    public int Supply { get; set; }
}

MP3_Players player1 = new MP3_Players
{
    ID = 1,
    Make = "GET Technologies .inc",
    Model = "HF 410",
    MBsize = 4096,
    Price = 129.95m,
    Supply = 500
};
MP3_Players player2 = new MP3_Players
{
    ID = 2,
    Make = "Microsoft",
    Model = "SOFT1",
    MBsize = 1028,
    Price = 432.99m,
    Supply = 500
};
MP3_Players player3 = new MP3_Players
{
    ID = 3,
    Make = "PEAR",
    Model = "APPLE3",
    MBsize = 2056,
    Price = 130.00m,
    Supply = 500
};
MP3_Players player4 = new MP3_Players
{
    ID = 4,
    Make = "logitech",
    Model = "DEF0",
    MBsize = 2056,
    Price = 53.35m,
    Supply = 500
};
MP3_Players player5 = new MP3_Players
{
    ID = 5,
    Make = "Sennheiser",
    Model = "HEIS1",
    MBsize = 2056,
    Price = 399.95m,
    Supply = 500
};

Now making a method with a list isn't really an issue, I imagine it goes as follows:
public static void mutate()
{
    List<MP3_Players> PlayerList = new List<MP3_Players>;
    Playerlist.Add(player1);
    Playerlist.Add(player2);
    Playerlist.Add(player3);
    Playerlist.Add(player4);
    Playerlist.Add(player5);
}

My first question is simply a matter of improvement of code, but I suppose many people reading this will understand the problems I'm running into, primarily that player1 is not static, does this mean I will have to individually make every player static or is there an easier way?
The primary issue however lies with my attempts to create a small program to change the Supply, The idea is for a user to enter an ID and then allow them to edit the supply, this went well for me when I was using an array but I am having issues understanding how to use data in a list<> and change it(basically trying to reach the values stored in the list from other methods if possible at all).
Comment: Why is `mutate()` marked as  `static`?
Comment: Your struct should probably be a class.
Comment: I would use classes instead of structs. This allows you to easily change existing players: `list[0].Supply = 3;`
Comment: [How to: Update List Items](http://msdn.microsoft.com/en-us/library/ms440289%28v=office.12%29.aspx)??
Answer: An easier way would be to initialize the list by
List<MP3_Players> PlayerList = new MP3Players[]{
    new MP3_Players()
    {
        ID = 1,
        Make = "GET Technologies .inc",
        Model = "HF 410",
        MBsize = 4096,
        Price = 129.95m,
        Supply = 500
    }, <-- include more instances of MP3_Players here
}.ToList();

and furthermore, the indexer [] can be used to access the elements in a list very similar to an array.
Comment: Or you could just use a collection initializer: `new List { new MP3_Player() { ... }, new MP3_Player() { ... } };`
Comment: I'm gonna need to look into LINQ more!
Comment: In fact, my comment is a bit misleading. Converting the array to a list has nothing to do with Linq; however I consider Linq very useful.
Answer: In C#, Structs are Immutable. This means that they can't be edited. If you wish to make a change to an item, then it needs to be replaced by a totally new item. You can avoid this by using a class.
One problem that I have ran into editing collections in an enumeration is that the collection was modified and the enumeration can no longer continue. This was happening because I was using a foreach loop that evaluates each item in the collection. I avoided it by using for loops and fetching the element from the list using its index and performing my edit there.
Answer: To solve your "list problem" you could use classes and a dictionary:
public class MP3_Players
{
    /* ... */
}

public static void mutate()
{
    var PlayerList = new Dictionary<int, MP3_Players>;
    PlayerList.Add(player1.ID, player1);
    PlayerList.Add(player2.ID, player2);
    PlayerList.Add(player3.ID, player3);
    PlayerList.Add(player4.ID, player4);
    PlayerList.Add(player5.ID, player5);
}

Then you can access them via their ID and change them:
var player = PlayerList[ID];
player.Supply--;
player.Price -= 10;

BUT to solve your bigger problem of "persistency" you should think of a "data-store" to persist you data, e.g. Entity Framework and a DB.
Answer: Have you considered using collection initializer for inlining the creation of the PlayerList?
Where static is concerned, static method cannot access non static instance members.
Answer: One way is to create dictionary like other user explain if the ID of the players are Unique, if they are not doesn't start this approach. This is requirement of dictionary !
The other way using List.
List<MP3_Players> newList = PlayerList.FindAll(x=>x.Model == Apple3);
newList.ToList().ForEach(x => x.Storage = 30);

This will return every Player with Model Apple3 and second row will change all the values for Storage.


How do I get Table List in ODBC - Excel with nodejs

Question: I want to get Table List(Name List) in ODBC - excel width nodejs
In rdb, there is system table that include defined table list.
But in EXCEL, there is no one.
help, please
Answer: There's no direct way to interface with ODBC from Node that I know of. If you're on Windows, you can try launching a WSH script in a child process:
child_process.exec('cscript.exe //nologo wsh_script.js', function (error, stdout, stderr) { ... });

where wsh_script.js would interface with Excel and return the results (perhaps in JSON) to Node via stdout. For instance, wsh_script.js might look something like:
var objExcel = new ActiveXObject('Excel.Application');
objExcel.Workbooks.Open(xlsFilePath);
var cellValue = objExcel.Cells(row, col).Value;
WScript.stdout.write(cellValue);

See this question or this article for more info on how to interface with Excel/ODBC from WSH.


Picasso is not loading image if I use resize() function

Question: When I load a file using picasso with resize option, it doesn't show up. I'm using Picasso.with(mContext).load(file).resize(160,213).into(holder.skinImage);
The file has dimension of 4608 X 3456. It loads up when I use Picasso without resize but it takes too long to load.
Picasso.with(mContext).load(file).into(holder.skinImage);
EDIT: I just tried loading image of size 1080 X 1920 and it worked perfectly.
Comment: @JohnWowUs Done. Not even this version working on high resolution images
Comment: Is there any error in the `LogCat`?
Comment: @RaymondLukanta no error
Comment: resizing after downloading the `4608 X 3456` image is not going to help improve the download speed
Comment: What version of picasso are you using?
Comment: @JohnWowUs version 2.5
Comment: I'd suggest trying the latest version 2.5.2.
Comment: too long is how long, can you tell more?
Answer: Strange for me that you say that with no resizing downloading takes more time - it should take exact the same time.
Resizing with Picasso .resize() is bad if you planning to obtain that image anywhere else with no resizing, since it will be cached with some -resize-160,213 prefix and when you will try to obtain it via just URL you'll get cache miss. So why not resize by XML layout argument?
Also you can enable Http logging to see, whats going on under the hood. At least you will see, if it's not downloaded or not placed (and see the cache inside picasso instance - is it picture there or not?). If you find out the second option - try to change layout.
Comment: I'm loading it from local file. I just tried to load the same image in Target variable and onBitmapFailed() fired with errorDrawable null.
Comment: Are you sure you don't change anything else except adding resize() ?
Comment: Yeah. Actually it works fine when i load image of size 1080 X 1920
Comment: Do you use OpenGL ? It comes with its max image size limits. Maybe you faced with some android memory limit. Try to setup largeHeap into manifest. Or try to load it directly from resources into ImageView:src (just to be sure that it's not a some size/memory limit). Maybe this also will help https://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Comment: Could you try using `.fit()` instead of using `.resize()`?
Answer: Try to add scaletype like this
Picasso.with(mContext).load(file).resize(160,213).centerCrop().into(holder.skinImage);


Can I ask the following discussion question?

Question: So I'm working through the textbook " Introducing Advanced Macroeconomics: Growth and Business Cycles" by Sorrenson. I have noticed that it's very equation and math heavy, while I do enjoy maths, it all seems so futile.
I am getting the feeling that working through this book is just teaching me how to manipulate equations and not really teaching me practical economics that I can use in research.
I wanted to hear people's thoughts on this, but not sure I can post it.
Answer: I wouldn't really recommend that question on the main site as it is opinion based.
The reason why I think it is an opinion based question is due to the last point in your question.

I am getting the feeling that working through this book is just teaching me how to manipulate equations and not really teaching me practical economics that I can use in research.
I wanted to hear people's thoughts on this, but not sure I can post it.

However if you asked a question like:

What is the basis for structural macroeconomic models like those shown in  "Introducing Advanced Macroeconomics: Growth and Business Cycles" by Sorrenson? how will an equation like X help me in developing my macroeconomic research skills?

I think it would be an appropriate question as it demands not a set of opinions but an answer instead.
Comment: Thanks, I'm going to do just that.
Answer: As an alternative to EconJohn's solution, you could post a reference-request question asking references that discuss the role of mathematics in economics. Many leading economists with extensive practical experience have written on the debate.


Deserializing a model from an api that contains Enums

Question: We're developing a web application using ASP.Net MVC 5 C#, I'm having difficulties coming up with a nice solution for my problem.
We're accessing an api that has a few enums coming across as text (i.e. "yes", "no", "notfound", "na") on the model itself I'd like to have strongly typed enums that store in the database as an integer (save a little space I think) the issue comes when we're deserializing the response from the api. In the response it will comes across as text (see above) where as the enums will be expecting an integer.
How is this done? I really hate asking questions where I haven't tried anything but my web searches hasn't resulted in anything. If you needed any code please let me know and I'll update the question with the segments needed. As of right now the model has several strongly typed enums and the api is returning strings of the enum values. by the way The enum texts are the same as the return values.
In-case it makes any difference we're also using EF Code First 6
Comment: Does http://stackoverflow.com/questions/1526339/how-to-work-with-enums-in-entity-framework help you?
Comment: @LosManos Thanks for that link. I already know that EF6 works with Enums. My problem is the deserialization of the api response. it doens't transmit int for the enums. it returns the enum text (Yes, No, NotFound, etc)
Answer: You can use Enum.TryParse to convert the string to its enum value
public enum MyEnum
{
  NA
  Yes,
  No
}

then
MyEnum value = MyEnum.NA;
Enum.TryParse<MyEnum>(theTextValue, out value );


store key in a variable for each ng-repeat

Question: I am fairly new to angular js.
I have two different scope data in my controller ($scope.itemdata & $scope.skudata, both are simplified in the plucker). Both are guaranteed to have the same structure for any key.
I have a nested ng-repeat for item data and I want to store/keep a track of keys inside each ng-repeat so that I can use the path variable to get the value out of skudata.
Problem:  {{path}} expression in HTML is not resolving to correct key value.
Looking at the console log the keys are added and removed in the correct order.
I am not sure where I am going wrong.
Here is plucker : http://plnkr.co/edit/0q8tZ9JLsGdfR03YbpYB?p=info
HTML Code:

Hello

Controller Code:
 var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.path = 'skudata';

  $scope.isThisAnObject = function(input) {
    return angular.isObject(input) ? true : false;
  }
  $scope.addkeyToPath = function(input) {
    console.log("Adding");
    console.log($scope.path);
    $scope.path =  $scope.path + "." + input;
    console.log($scope.path);
   };

   $scope.removeKeyFromPath = function(input) {
     console.log("remove");
    console.log($scope.path);
    $scope.path = $scope.path.substring(0,$scope.path.lastIndexOf("."));
    console.log($scope.path);
   };

  $scope.itemData =
  { value:'item_name',
    length:'10'

  };

  $scope.skudata =
  { value:'sku_name',
    length:'20'

  };

});

Answer: The problem is that you're using the $scope path variable, when really you want to display path variable of the item itself. Make a few changes to get this working plunkr
Template:

  {{key}}: {{value}}
  {{path}}

Controller:
$scope.addkeyToPath = function(item, input) {
  item.path = $scope.path + "." + input;
 };

$scope.itemData = {
  value:'item_name',
  length:'10'
};

Comment: thanks azium, but I don't want to display the path variable. Rather my intend was to use the path variable to get the value from "skuData" present in my plunker. So basically I am iterating through itemData and wanted to store the keys that I have iterated through ( path = k1.k2.k3..) and use the path to get the value present at that parth in "skuData"..Does that make it clear.
Comment: I don't understand, why don't you just directly get `skuData` then? Why do you need to use `path` at all?
Comment: Problem : When you find a value for a key in "item data" ..look if a value exists for that key in "sku data" ..if yes..then fetch the value from the skudata. Please note that itemdata and skudata can be nested objects.


Finding the Bound of the Error in Interpolation

Question: The function $f(x)$ has the following values known at the points: $f(x_1) = 2, \quad f(x_2) = 8 \quad \text{and} \quad f(x_3) = -2$.
I've also been told that I can assume that $f$ and its derivatives satisfy the following bounds for all $x$: $$|f(x)| \le 10, \,\, |f'(x)| \le 20, \,\, |f''(x)| \le 30, \,\,
  |f'''(x)| \le 50 \text{ and } |f''''(x)| \le 100.$$
I want to be able to bound the error in approximating the polynomial $f(x)$ at $x=2$, for two different methods I have used to approximate the polynomial:

Lagrange Polynomial Interpolation: $P(x) = -2x^2 + 11x -7$
Piecewise Linear Interpolation: $S(x) = 3x - 1$ upon the interval $[1,3]$

I know that the formula for the error in polynomial interpolation is given as: $$f(x) - P(x) = \frac{f^{(n)}(\xi)}{n!} \prod_{k=1}^n (x-x_k).$$
But how can I use this formula to $\underline{bound}$ the error for these two types of interpolations? Any guidance would be appreciated.
Comment: There is a link between Newton Divided difference and the error term. You may be able to extract the individual $x_i$'s there. Once you have those getting the maximum of that error formula should be trivial.
Answer: In your case $n=3$, so
$$|f(x) - P(x)| = \frac{\left|f^{(3)}(\xi)\right|}{3!} \prod_{k=1}^3 |x-x_k|\le\frac {50}6 \prod_{k=1}^3 |x-x_k|$$
Since you know that $1 \le x \le 3$, you can also find bounds on the $|x-x_k|$ elements, but since you haven't told us what the $x_i$ are, we cannot take it any farther.


How to automatically assign a role based on email address domain?

Question: How do I automatically assign a role to new users based on the email address domain?
For instance if the email address is email@example.com, and the condition is that all users from examplecompany.com should always have the role specialclient. How do I acheive this?
I am using Rules, but there are no specific email conditions to be found. Can I create my own Rules Condition? If so, I would be very grateful for any tips on how to accomplish this.
Answer: Yes you could create your own (custom) Rules Condition. But in this case I don't think that's needed. The clue to what you're asking for, is a Rules Condition which is a "text comparison".
In that comparison, you use a regular expression to indicate "the email address should end with "@examplecompany.com".
For an example rule which also uses such RegEx-expressions, refer to the answer to "How to use the Rules module to perform a simple anti-spam validation using specific keywords?".
Be aware: I'm not a RegEx expert ..., but here a few dozens of similar examples using a RegEx ...
However, I predict you'll have multiple domains/roles to be processed. So the real Rules fun starts when this (typical) question arises: "I need to add something like a Rules Condition inside the Rules Actions, how can I do that?"
To resolve this typical question, there are basically 2 approaches (that I am aware of):

Use the Conditional Rules module. For an example using the Conditional Rules module (for which there is not a lot of docu available), refer to my answer to the question "How to prevent a node being saved when using the Rules module?".
Rework the relevant part of your Rules Action(s) into performing a Rules Component. Because in a Rules Component, it is possible to also add Rules Conditions.
Comment: Thanks a lot for your answer, Very useful and upvote. I will try to do as you suggest.
Comment: @TBJ Merci for the feedback ... you probably also want to try the 2nd part I just added ...


How to leverage a DB query with views?

Question: I have three things:

A user (User entity)
A league (Node)
A league entry (Node, with a numerical field for time and a noderef field to a league)

A user can create a league entry node, and set a time value and a node reference to a league node.
I want to produce a view that takes the league node id as an argument, and produces a table row for each user that has submitted a league entry node. The columns will be the user name, a user field that states the user's team, and finally, for every league entry node raised by them as author, that contains a node reference to the current league nid, a sum of the matching league entries numerical fields.
Now as you can imagine, doing this in views is very problematic because of aggregation issues on a single field; so a simpler alternative is to simple create a new DB table that stores the data I want to display in distinct fields.
So if I have a DB table like:
username, league, total of league entry nodes time value so far
How can I get this info into views so I can take advantage of table display, column sorting, and all that good stuff?
Answer: There are a couple of ways of doing this. You can create an entity. This requires the writing of a custom module to do so. Entities can be challenging if you have never done them before.
Another way and slightly simpler (still requires a custom module). Is to use the Views API. The first thing you would implement is hook_views_data(). This is a really good example of hook_views_data borrowed from Drupal Commerce.
Lastly, there are some modules out there that might help. One of them is Schema Views.
Comment: That's loads of entrypoints, thank you! I can go from here :)

How to leverage a DB query with views?


Add a index selected tensor to another tensor with overlapping indices in pytorch

Question: This is a follow up question to this question. I want to do the exactly same thing in pytorch. Is it possible to do this? If yes, how?
import torch
image = torch.tensor([[246,  50, 101], [116,   1, 113], [187, 110,  64]])
iy = torch.tensor([[1, 0, 2], [1, 0, 2], [2, 2, 2]])
ix = torch.tensor([[0, 2, 1], [1, 2, 0], [0, 1, 2]])
warped_image = torch.zeros(size=image.shape)

I need something like torch.add.at(warped_image, (iy, ix), image) that gives the output as
[[  0.   0.  51.]
 [246. 116.   0.]
 [300. 211.  64.]]

Note that the indices at (0,1) and (1,1) point to the same location (0,2). So, I want warped_image[0,2] = image[0,1] + image[1,1] = 51.
Answer: What you are looking for is torch.Tensor.index_put_ with the accumulate argument set to True:
>>> warped_image = torch.zeros_like(image)

>>> warped_image.index_put_((iy, ix), image, accumulate=True)
tensor([[  0,   0,  51],
        [246, 116,   0],
        [300, 211,  64]])

Or, using the out-place version torch.index_put:
>>> torch.index_put(torch.zeros_like(image), (iy, ix), image, accumulate=True)
tensor([[  0,   0,  51],
        [246, 116,   0],
        [300, 211,  64]])

Comment: Yes! If you set `requires_grad` to `True` on `image`, then `warped_image` will turn out with a `grad_fn` (used to backpropagate) set to ``.
Comment: @NagabhushanSN I don't think you can pass a gradient through the indices, as those are discrete and non-differentiable
Comment: Wow! Thank you so much! I hope this allows the gradients to flow.
Comment: I need the gradients to flow through the indices as well, but I'm getting an error for that. Any idea how to solve it or any workarounds? I've asked a separate [question](https://stackoverflow.com/q/66973502/3337089)
Comment: Yeah, I figured that. And it turned out that that wasn't needed after all.


Proof of Commutativity of the ring of endomorphisms over integers

Question: First I searched for some similar problems and studied some of the given references, but I was not able to understand them. So I pose the question, hopefully to reveal some aspects to a less advanced audience as myself. The problem is how to prove that the algebraic structure $ \operatorname{End}(\mathbb{Z},+,\circ)$ (The set of all Homomorphisms from $\mathbb{Z}$ into $\mathbb{Z}$ is a commutative ring, where $+$ and $\circ$ are binary operation of addition and composition of such functions respectively. I was able to prove that it is a ring, but I cannot prove how it is commutative. Specifically, how should I prove that for an arbitrary integer $a$ and for arbitrary $f,g \in \operatorname{End}(\mathbb{Z})$, we have $(f\circ g)(a)=(g\circ f)(a)$?
Answer: Every endomorphism of $\mathbb{Z}$ is determined by what it does to $1$, since $f(x + 1) = f(x) + f(1)$, so $f(n) = n f(1)$ by induction. Therefore $\mathrm{End}(\mathbb{Z}) \cong \mathbb{Z}$.
In general an endomorphism of $\mathbb{Z}^n$ is determined by what it does to the basis vectors $e_1, \dots, e_n$ by a similar argument. Hence $\mathrm{End}(\mathbb{Z}^n)$ is isomorphic to the ring $M_n(\mathbb{Z})$ of $n \times n$ matrices with integer entries. This is commutative iff $n = 1$.
Answer: If $f: \mathbb{Z}\rightarrow \mathbb{Z}$ is a unital ring homomorphism you get the following:
$$f(m)=f(1+\cdots +1)=f(1)+ \cdots +f(1)=mf(1)=m$$
hence a unital ring endomorphism of $\mathbb{Z}$ is the identity. Hence $End(\mathbb{Z})$ has only one element: $Id$


Scanning tagged traffic over bridge through iptables

Question: I am working on bridge on linux box using brctl.
Now I have to scan and monitor bridge traffic through iptables.
So for the same I changed:
net.bridge.bridge-nf-call-iptables = 0 to net.bridge.bridge-nf-call-iptables = 1 in /etc/sysctl.conf
And add below rules into iptables:
/sbin/iptables -A INPUT -m physdev --physdev-in eth2 -p tcp -m tcp --dport 3127 -m state --state NEW -j ACCEPT

/sbin/iptables -t nat -I PREROUTING -m physdev --physdev-in eth2 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3127

to acheive traffic coming in eth2 and port 80 should be redirect to 3127 on my linux box.
And it is working fine for normal (untagged) traffic.
So I have to do the same for tagged traffic, but it is not working.
I changed:
net.bridge.bridge-nf-filter-vlan-tagged = 0 to net.bridge.bridge-nf-filter-vlan-tagged = 1 in /etc/sysctl.conf
Help me for tagged traffic.
Thanks.
Answer: if your traffic is tagged, then your network I faces will be called eth1.X and/or eth2.Y. did you set new iptable rules accordingly?
Comment: I don't have VLAN configured on any bridge interface. I made a bridge on say eth2 and eth3, hosts under eth2 are under managed switch so tagged traffic is coming in through eth2 and I mentioned rule according to physical device.
Comment: Don't expect you'r eiptable rules to match if the kernelk is not aware it is dealing with a tagged 802.1q packet. It will fail to match port and hosst information because of the offset introduced by tagging. You have to let the kernel how you have tagged packets if you want it to manipulate those packets. take a look at ebtables and tagging, but you'll have to set vlan iface at some point if you want the kernel to play with tagged packets.   See http://ebtables.sourceforge.net/misc/brnf-faq.html (Do {ip,ip6,arp}tables see VLAN tagged IPv4/IPv6/ARP traffic on an untagged bridge? )


Flexible parametric survival (Royston-Parmar) model for non-proportional hazards

Question: I am reading more about flexible parametric survival models and their implementation in the flexsurvspline function in the flexsurv package in R (there also seems to be a very similar command in Stata - stpm2)
I have a Cox model with several predictors that violate proportional hazards. I have tried transformations, time interactions and splitting time to generate time-dependent coefficients, without much luck for remedying the proportionality of the hazards.
If I use a model such as:
mod <- flexsurvspline(Surv(time, event) ~ x1 + x2 + x3 + x4, anc = list(gamma1 = ~ x1 + x2), data = dat, scale = "hazard")

am I correct in my understanding that the model will not force the hazards for x1 and x2 to be proportional (thus creating a time-varying HR for those two predictors)? This would also be a reasonable incidental way then to circumvent the problem of non-proportional hazards, right?
Answer: You can try the Royston-Parmar spline-modeling approach to accomplish what you want, but it takes some care. As I understand the implementation in the flexsurv package, you first specify the overall number of interior knots for the baseline hazard via the k argument, then specify which of the spline parameters gammaN (where N represents a spline-parameter number, with maximum k+1) are allowed to vary with covariate values.
Your example seems to use the default of 0 interior knots, which is just a Weibull model in which both the shape and scale parameters could vary with x1 and x2. See Section 5.1 and Table 2 of the vignette.
Given the difficulties you have had with the proportional hazards assumptions and these data so far, you thus might need to specify more knots overall and more corresponding spline coefficients gammaN to model as functions of the covariates x1 and x2. You also might want to consider the default placement of knots, depending on your particular data set.
This modeling approach is easiest to understand for categorical covariates. As Royston and Parmar warn on page 2185 of the original paper (Statistics in Medicine 21: 2175-2197, 2002):

[covariates] may be ordinal or continuous, but then particular care is needed to ensure that the model fits the data and gives sensible predictions.


Converting a word macro to vbscript

Question: I am trying to create a docx file with a different first page and insert an image in the footer
Dim oWord
Dim oDoc

Set oWord = CreateObject("Word.Application")
Set oDoc = CreateObject("Word.Document")

oWord.Visible = False
Set oDoc = oWord.Documents.Add

With oWord.ActiveDocument.PageSetup
    .DifferentFirstPageHeaderFooter = True
End With

With oWord.ActiveDocument
    .ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    .Application.Templates( _
    "C:\Users\User\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx" _
        ).BuildingBlockEntries(" Blank").Insert Where=Selection.Range,     RichText _
    =True
.Selection.Fields.Add Selection.Range, wdFieldEmpty,  _
    "INCLUDEPICTURE  ""http://url.me"" \d ", PreserveFormatting=True
oWord.ActiveDocument.SaveAs("test.docx");

This errors out to
Line:   16
Char:   5
Error:  The requested member of the collection does not exist.
Code:   800A1735
Source:     Microsoft Word
Any clue to why this is happening?
Comment: Does the referred building block file exist and does it contain an entry named " Blank"?
Comment: Yes, This code was translated from a macro from Word 2013.
If needed ill edit the post to include the macro.
Comment: Try to call `.Application.Templates.LoadBuildingBlocks` immediately before line 16.
Comment: Same error.
Is it a bug with word 2013? I will try another Install.
Comment: I just ran a modified version of your script (removed constants, replaced path with my personal building block path, changed building block entry name) and it worked perfectly. You're getting that error either because the `Application.Templates` collection does not contain what you specified or the `BuildingBlockEntries` collection does not contain an entry named " Blank". Try to split the code to see which it is.
Comment: Can you share the code?
Im erroring out on
    .Application.Templates( "C:\Users\User\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx" )
Answer: Your code is failing to open the building block collection specified. Here's an optimized version of your code that gets the path to the "Built-In Building Blocks.dotx" template dynamically.
Dim oWord
Dim oDoc

Set oWord = CreateObject("Word.Application")
oWord.Visible = False

Set oDoc = oWord.Documents.Add

With oWord.ActiveDocument.PageSetup
    .DifferentFirstPageHeaderFooter = True
End With

'Get building block path.
Dim bbPath, entryName, savePath
bbPath = GetBuildingBlockPath(oWord, "Built-In Building Blocks.dotx")
entryName = " Blank"
savePath = "test.docx"

With oWord.ActiveDocument
    .ActiveWindow.ActivePane.View.SeekView = 10
    oWord.Templates(bbPath).BuildingBlockEntries(entryName).Insert oWord.Selection.Range, True
    oWord.Selection.Fields.Add oWord.Selection.Range, -1, "INCLUDEPICTURE  ""http://url.me"" \d ", PreserveFormatting=True
    .SaveAs(savePath)
End With

Function GetBuildingBlockPath(app, bbName)
    app.Templates.LoadBuildingBlocks

    For Each t In app.Templates
        If LCase(t.Name) = LCase(bbName) Then
            GetBuildingBlockPath = t.FullName
            Exit Function
        End If
    Next
End Function

Comment: This works perfectly,
But the document produced has the image in the body instead of the footer.
Comment: Thanks, I have accepted the answer.
Any clue how to resize the inserted image?

Converting a word macro to vbscript


How do I remove extra divs added by panels?

Question: Panels adds additional classes and divs which are unnecessary for my purpose. How can I remove these?
Comment: Same way as all theming, supply your own template.
Answer: There are a couple ways you can tackle this.
One option is to simply create your own panel layouts.  I often do this because I like control over the markup and I want to reduce the divitus in my theme.  It also lets me create layouts that more directly match what I need.  It's pretty easy to create your own panel layouts in your theme.  Read more here.
Another option is to override the panel template files in your theme.  Similar to any module that provides a template file, you can copy that template file to your theme and change the markup.  You'll find the panel template files in [path_to_modules]/panels/plugins/layouts/[each_layout].
Answer: Why don't you use Clean Markup Module.

This module aims to clean up, enhance and facilitate the customization
  of markup for Drupal core and several popular contrib modules such as
  Panels.
Comment: Hi and welcome to Drupal Answers.


Filtering in SQL Server table on multiple columns - T-SQL

Question: EDITED
We have a table like below where there are multiple rows with same Name with different Ops

Deal
Name
Ops

ABC
A
NULL

ABC
A
NULL

ABC
B
NULL

ABC
B
NULL

ABC
B
Default

ABC
B
Default

ABC
C
NULL

ABC
C
NULL

ABC
C
Default

ABC
C
Default

ABC
C
Aggr

ABC
C
Aggr

We need to get rows with Default when both NULL and Default is tagged and Aggr when NULL, Default and Aggr are tagged
Expected output:

Deal
Name
Ops

ABC
A
NULL

ABC
A
NULL

ABC
B
Default

ABC
B
Default

ABC
C
Aggr

ABC
C
Aggr

We need to get this using a SQL query.
I have tried this one here:
WITH PriorityRanking AS
(
    SELECT
        DENSE_RANK() OVER (PARTITION BY Deal, Name
                           ORDER BY
                              CASE COALESCE(Ops,NULL)
                                  WHEN 'Aggr' THEN 1
                                  WHEN 'Default' THEN 2
                                  WHEN NULL THEN 3
                              END) AS Rnk,
        *
    FROM
        Table_name
)
SELECT Deal, Name, Ops
FROM
    (SELECT Deal, Name, Ops,
     FROM PriorityRanking
     WHERE Rnk = 1)

But the rank is set to 1 for NULL values and so the final select is not working properly.
Please suggest what is the best way to get the required data
Comment: `COALESCE(Ops,NULL)` makes no sense; "if Ops is null, then use null". `WHEN NULL` is the same as `= NULL`, which obviously doesn't work
Comment: By the way, for the data as given, you can also select `MIN(Ops)`
Comment: I'm not sure why all current answers decided to replace `DENSE_RANK` with `ROW_NUMBER`, for your sample data it doesn't matter. But be aware a `ROW_NUMBER` will return a single row for each partition, while a `DENSE_RANK` will return ties too. Your question doesn't immediately make it clear which you need. I always prefer `DENSE_RANK`, because I want to know if there is unexpected data. But it all depends on the data and what you are looking to accomplish
Answer: I think you just need to use row_number and coalesce your NULL values to be ordered below the other values -
with r as (
    select *, Row_Number() over(partition by deal, name order by IsNull(Ops,'x') ) rn
    from t
)
select Deal, Name, Ops
from r
where rn = 1;

depending on your real data as mentioned you could also simply aggregate:
select Deal, Name, min(Ops) Ops
from t
group by Deal, Name;

Answer: The below logic can work
WITH cte1 as
(

Select deal,name,isnull(Ops,'Z') as Col from table
),
cte2 as
(
Select *,row_number() over(Partition by Deal,name order by col) as rnbr from cte1
)

Select deal,name,case when col='Z' then NUll else Col end as Ops from cte2 where rnbr=1

Answer: You can replace NULL with a text and sort after that
After changing your data, the basic concept stays valid
But now you need DENSE_RANK like you had it in your original question, as SQL SERVER can't handle NULL, so replacing will help

WITH PriorityRanking AS
 (
 SELECT DENSE_RANK() OVER (PARTITION BY Name ORDER BY CASE ISNULL(Ops,'NULL')
                                                                    WHEN 'Aggr' THEN 1
                                                                    WHEN 'Default' THEN 2
                                                                    WHEN 'NULL'  THEN 3
                                                                    END) AS Rnk,*
 FROM Table_name
 )
 SELECT [Deal], [Name], [Ops] FROM  PriorityRanking WHERE Rnk = 1

Deal | Name | Ops
:--- | :--- | :------
ABC  | A    | null
ABC  | A    | null
ABC  | B    | Default
ABC  | B    | Default
ABC  | C    | Aggr
ABC  | C    | Aggr

db<>fiddle here

WITH PriorityRanking AS
 (
 SELECT ROW_NUMBER() OVER (PARTITION BY Name ORDER BY CASE ISNULL(Ops,'NULL')
                                                                    WHEN 'Aggr' THEN 1
                                                                    WHEN 'Default' THEN 2
                                                                    WHEN 'NULL'  THEN 3
                                                                    END) AS Rnk,*
 FROM Table_name
 )
 SELECT Deal, Name, Ops FROM PriorityRanking where Rnk = 1

Deal | Name | Ops
:--- | :--- | :------
ABC  | A    | null
ABC  | B    | Default
ABC  | C    | Aggr

db<>fiddle here
Comment: Personally, I would not even mention or handle `NULL`, and just use `ELSE 3` instead
Comment: @HoneyBadger as this is only a sample we don't know really the complexity of the case, my answer shows only how to handle the NULL values, there could be hundreds of cases so the upvoted answer wouldn't work.
Comment: Very true. In most cases like this, `NULL` is the least preferred value, but it isn't a given
Comment: Sorry, I have edited the question. The table is having multiple rows with same Names and Ops values and we need to select all rows with names NULL and Default (when NULL and Default) and Aggr (when NULL , Default and Aggr)
Comment: there the dense_rank makes much more sense


Aspect data in linear regression

Question: I have a dataset of various ecological variables on which I want to run linear regression. The variables are continuous, but also include aspect data (sun exposure), in grades. My problem is that the aspect values ranges from 0 to 400, with 0=400=north. How can I include "cyclic" values in the regression? Should I keep them as they are, or cos/sin-transform them?
Does anyone of you have experience with these problem? I have had difficulties to find any reference papers...
All ideas and references are welcome and appreciated! I hope I was not too unclear, if you need any further information, I'd be happy to provide, as far as I can.
Comment: This is the subject matter of [circular statistics](http://stats.stackexchange.com/questions/105641/best-transformation-for-sinuous-data-sets). How to include these values depends on whether the aspect is explanatory or a response variable; presumably it is explanatory. In this case consider re-expressing aspect into a value more directly related to sun exposure, for which you say it is a proxy.  For instance, the pair (slope, aspect) can be converted into a rough numerical assessment of mean total daily insolation per unit area.
Answer: How you treat it - including whether you transform it in some way - depends on how you expect the aspect to relate to your response.
What is the response and how do you think aspect will tend to affect it?
You may find some value in the discussion here; it's not exactly the same kind of problem, but aspects of it have some potential relevance.
In particular, if there's a reason to have $\cos$, there may be a point in not just having $\cos$ of the aspect but also $\sin$, and also other harmonics (essentially, a set of orthogonal periodic components).
Comment: Uh, no. You have that all muddled. For a start, I think your measurements are in [gradians](http://en.wikipedia.org/wiki/Gradian). North would be weighting on $\cos(x\pi/200)$, east would be weighting on $\sin(x\pi/200)$; northeast would weight them both equally. The second harmonics would be $\cos(x\pi/100)$ and $\sin(x\pi/100)$ and would represent a change in the shape of the response from a pure sin-curve shape.
Comment: (To clarify, the sign of the coefficients (weights) depends on whether you measured your angles clockwise or anticlockwise from north.)
Comment: I am trying to predict wood production in a stand, with variables such as altitude, aspect, slope, over- and under-story density,  composition and cover, trees diameters... Stands where monitored over a 10-years period, in a mountainous area. I have a range of conditions over the stands, and I suppose that the moist and fresh stands, i.e. the north-facing ones, are the most productive (I am working mostly on fir trees in the Alps), but that's just a guess that I have to verify.
Comment: Okay, yeah, I'd think cos, maybe with sin and maybe a second harmonic for each.
Comment: I just ended reading the great answer you made for the temperatures on the Baltic Sea, makes things clearer. So if I get it, I will have two variables, one for the northness that will be cos(2*pi*aspect)+sin(4*pi*aspect) and one for the eastness that will be sin(2*pi*aspect)+cos(4*pi*aspect), in the case I use a second harmonic for each? I do not really understand the second harmonic part.
Answer: you can write expression in raster calculator:
(Aspect<25) * (Aspect+400)+(Aspect>=25) * (Aspect*1)
it will add 400 grade to all value less than 25 grade but not effect on value equal and greater 25 grade. result will be North class in the end of legend but start point would be northeast.you will never get zero value i your map
your north class range would be 375g to 425 g in the end of legend
legend would looks like this:
Norheast: 25g to 75g
East: 75g to 125g
southeast: 125g to 175g
south: 175g to 225g
southwest: 225g to 275g
west: 275g to 325g
Northwest: 325g to 375g
North: 375g to 425g
Comment: This is very hard to understand. Please edit it to clarify your meaning.

Aspect data in linear regression


Delta-epsilon proof for the integer part ( $[x]$ ) function.

Question: I was doing some of my exercises and started to think how could I prove, for example:
$\lim\limits_{ x\rightarrow3}[x]$. I know this limit doesn't exist, however, proofs for the lateral limits should be constructable, and I have no idea how.
Comment: Can you show that $\lim\limits_{ x\rightarrow 3^-}[x]=2$ and $\lim\limits_{ x\rightarrow 3^+}[x]=3$ using $\varepsilon$-$\delta$?
Answer: Here's a straightforward $\epsilon$-$\delta$ proof for the existence of the left-hand and right-hand limits
To get the left hand limit $\lim\limits_{x\rightarrow3^-}[x]=2$ we need to show that for every $\epsilon>0$, there exists a $\delta>0$ such that whenever $0<3-a<\delta$ then $|2-[a]|<\epsilon$.
However, this is easy to prove: All we have to do is choose $\delta=1$ for every $\epsilon$ since whenever $0<3-a<1$, we have $2<a<3$, so $[a]=2$ and so $|2-[a]|=0<\epsilon$.
The statement for the right hand limit $\lim\limits_{x\rightarrow3^+}[x]=3$ is worked out in exactly the same manner: we need to show that for every $\epsilon>0$, there exists a $\delta>0$ such that whenever $0<a-3<\delta$ then $|[a]-3|<\epsilon$.
Again it suffices to choose $\delta=1$ for every $\epsilon$ because we have $0<a-3<1$ implies $3<a<4$ which implies $[a]=3$ which gives us $|[a]-3|=0<\epsilon$.
Note that since we have shown the left-hand and right-hand limits are different, we have also proven that $\lim\limits_{x\rightarrow3}[x]$ does not exist.


How to apply HTML layout template to email using EXIM?

Question: My goal is to apply a standard, branded layout template to all emails when sending (on the server, not the client). I have been googling this topic and not found any resources specifically addressing this issue. The following is the flow I am envisioning:

Email is initiated from client via SMTP.
EXIM server manipulates the content of the email and applies the HTML layout.
EXIM then sends the email on to where it needs to go.

Step #2 is what I'm trying to figure out. Maybe this needs to be down outside of EXIM in something like PROCMAIL?
I have no experience with this, have just started learning. Any help is greatly appreciated!
Answer: Exim, like Postfix, Sendmail, qmail, and others, are Mail Transfer Agents (MTA) which facilitate the transfer of messages between systems. You should not modify message bodies, however if you still want to do this, the MTA is not where it should be done.
If your requirement is to modify the content sent from a conventional email client (Outlook, Thunderbird, Apple Mail) consider using a transport filter, described in the Exim wiki; however, you should be aware that if you modify content like this, you may make unintentional changes to the message.
Will you have some control over the content sent from the client? This is important if the client sends plain text, or HTML mail. Perhaps you could describe more about the specific application to help decide what type of template system you could use.
Comment: Thanks for trying to help ... I worked on this last week implementing a process and spent this week testing it and fine-tuning HTML manipulation aspects. I posted my answer here.
Answer: With some help I have identified a solution:

Capture all incoming email via transport condition and run it through a bash script.
The bash script then saves the email to a temp folder.
The bash script then triggers a PHP script (because that's what I'm most comfortable with, you could use any scripting language that can manipulate MIME emails.
The PHP script parses the MIME email into its component parts using a PHP package.
It then manipulates both the text and html version of the email as desired.
Using another package I recompose the parts into a new email with a custom header, and send it.
A dedicated transport that triggers if the custom header is set then send the email outbound to Postmark, or any other SMTP account you would like to use.


Change Link URL in preprocess function

Question: I need to slightly modify some URLs that are in a "Link" field on a custom entity type.
In a mytheme_preprocess_myentity I've found that field with $variables['elements']['field_my_url'][0] I get this output in Kint

I've tried using a number of methods on the #url object but I don't think I'm doing it correctly... I'm just confused on how to modify it at this point.
How can I change the external URL to which this particular field is pointing?
Answer: Drupal's Core Url has a static fromUri() method (see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromUri/8.2.x)
With that in mind you should be able to swap out the ['#url'] with a new one thusly:
...['#url'] = \Drupal\Core\Url::fromUri('externalURL');

Change Link URL in preprocess function


New official Magento Forums announced. What does that mean for Magento SE?

Question: Since its inception Magento SE has served as a less than ideal brace against the horroshow that was v1 of the Magento forums. Once those forums were shut down, Magento SE received a deluge of less-than-stellar content. Today the new version of the Magento forums went live, and I think there may be confusion about where to post which questions and in general what this means for Magento SE.
In short, I believe that Magento SE will remain healthy for the following reasons:

There's an active userbase which isn't going to suddenly disappear.
We (Magento) are encouraging folks to ask technical questions at Magento SE
There will still (and likely always will) be significant organic traffic bringing users to this forum

If anything, I expect the moderator workload to be reduced and the overall quality & stats to increase. That said I'm sure we'll see some crossover posts, and I'm fine with that as these may serve to bridge the two groups.
So why am I posting this here, now? Well, I want to get others' thoughts on my thoughts. I'd also like to encourage everyone to visit and participate in the new forums.
Comment: Does the new forum mean all the previous forum's stats have been reset? Specifically I mean _Total Forum Topics/Replies/Posts_ for users?
Answer: I think you're on point. Magento SE will be my first choice to ask questions.
I also feel that Magento SE is for technical questions for developers of all levels, whereas magento.com's forum can serve both technical and non-technical questions. Although, I think it's going to be more of the latter, allowing Magento SE to have get higher quality posts and returned uses (hopefully).
I'm optimistic that this forum's quality will go back up.
Answer: I'm really happy to see the new Magento forums are back to shine in new splendor. I think the official forums are an essential part for the Magento community, especially for non-technical people. MagentoSE - and also the forums - will hopefully benefit from that.
I hope the quality of questions here will increase again and I think MagentoSE will stay as a big player for technical Magento questions (with regard to the good rankings on Google and user count) even though we will see technical questions on both forums - which I think is totally fine.
The stackoverflow/stackexchange concept is well accepted within the developers which might also be a factor for future participation and MagentoSE to stay as an essential information resource.
Answer: What's with this spam? Trying to promote your forum through stackexchange?
JK.
Congratulations on the new launch. Let's hope that this forum will last longer and will not face the same spam issues the previous one had.
From my point of view this website and the new magento forum should work together to make the community stronger and more educated.
I'm sure that Magento SE is here to stay. Beside the reasons you listed I might add that Magento SE is part of the SE network that is kind of a live/job saver for most of the developers.
I strongly suggest a separate section for third party extensions on the forum (maybe subsections based on what the third party extensions do). This way we can send users from SE to a specific area, where even extension providers might be able to provide support for their extensions.
Comment: brb preemptively banning you
Comment: *"I strongly suggest a separate section for third party extensions on the forum (maybe subsections based on what the third party extensions do)."* > http://community.magento.com/t5/Extensions/ct-p/extensions
Comment: @benmarks. Excuse my ignorance. I'm new to the forum thingy. I missed that. Rookie mistake.
Answer: Good to hear the new official forums have been launched.
I can certainly confirm this

Magento SE received a deluge of less-than-stellar content

While it wasn't always the case the questions on here unfortunately seem to have (in my opinion) very much degraded, with a massive portion of questions being requests for bug fixes i.e. I'm getting this error message, or this is broken etc etc.  The vast majority of the time they are store specific, and often just cannot be answered without some in depth questioning and possible testing in a local development environment.  These kinds of questions (again in my opinion) add very little value to SE as the likely hood of any solution which may be given being relevant to another user in the future is extremely low due to it being so store specific.
I haven't been active on SE for a huge amount of time, but even I can clearly see the shift away from the kind of 'how to' questions which give the opportunity for a truly useful to the masses, tutorial type answer.  These kinds of questions now seem really very few and far between compared to the past.
I really enjoy sharing my Magento development knowledge from the past 5 years or so, but have in the last few months really been quite turned off SE purely because of how hard it has become to actually find good questions to answer which aren't so store specific as to require time I just don't have to get all the required information and run the required tests.  Shouldn't these kinds of issues be resolved by the stores developer (yes I know there won't always been one, but there really, really should be), with the likes of SE being there to answer core related queries on functionality and customisation?
My hope is that with the launch of the new official forums there will again be an increase in the quality of questions asked and answered on SE, time will tell.
Comment: Heh, see http://community.magento.com/t5/Programming-Questions/How-to-arrange-the-display-of-mutiple-product-in-one-group-in/m-p/846
Comment: I cannot agree more with your thoughts. I got turned off SE some time ago, as it started to feel that questions posted had no real developer effort behind them.  I enjoyed the first year or so, sharing my knowledge, but then it seemed people started expecting (us) to fix their sites for them, on demand.


Who gets possession after a shot is blocked off the rim?

Question: I was playing 3v3 today with some friends and a defender blocked the shot and the ball bounced off the rim and out of bounds. I claimed that the rim is considered part of the offense, so the defense would assume possession. No one else had heard of that rule but I've always thought that was how you play.
In this scenario who receive the ball after a blocked shot off the rim?
Answer: You don't say what rules you play under, but the NBA rules are fairly standard here.
From Rule 8:

The ball is caused to go out-of-bounds by the last player to touch it (including by touching the player's hair or uniform) before it goes out, provided it is out-of-bounds because of touching something other than a player. If the ball is out-of-bounds because of touching a player who is on or outside a boundary, such player caused it to go out. If a player has his hand in contact with the ball and an opponent hits that part of the hand causing the ball to go out-of-bounds, the team whose player had his hand on the ball will retain possession.

There is no special exception for the rim, and no other part of Rule 8 mentions the rim.  So, yes, if a defender blocks a shot into the rim, and then it goes out of bounds, the offense would regain possession.  In fact, if the shooter's hand is hit by the defender, causing the ball to go out of bounds, but the defender doesn't actually touch the ball, the offense still retains possession.