How to force non-breaking space in reStructuredText, in code environment?

Question: I have this snippet in my reStructuredText document:
  1. Find the file at ``~/Google Drive/code/mac/install.sh``.

Notice the code/quote environment.  Notice the space between Google and Drive in the path.
When rendered with Sphinx in HTML, there is a line-wrap between Google and Drive, and the space is gone.  (Since this is documentation, I need everything in the code environment to appear to the user exactly as it is entered, which includes the space between 'Google' and 'Drive'.  Not only should a space be present, but it should be shaded grey just like all the other code in the code environment)
How do I tell reStructuredText to use a non-breaking space in that location?
Comment: What about this solution: http://stackoverflow.com/a/12145490/5480526
Comment: No, this does not work.  The solution must work in a reST quote environment (see the double backticks), so there is a need to escape quote mode in order to input something special.
Comment: I get two `` elements separated by a space in the output: `~/Google` and `Drive/Quisquam/code/repo/latest/application-bundles/mac/install.sh`. Is that what you mean?
Comment: @mzjn Yes, and a regular old space between the two.  Either `~/Google Drive/Quisquam/code/repo/latest/application-bundles/mac/install.sh` or at worst case `~/Google Drive/Quisquam/code/repo/latest/application-bundles/mac/install.sh` would be preferable to the current behavior.
Comment: Oh man, Stack Overflow has the same bad behavior!
Comment: In the question you say that there is a line-wrap in the output. What does that mean exactly? Because now you say that it is a "regular old space". And why is this a problem in the first place?
Comment: @mzjn The HTML *source code* has a regular old space between `~/Google` and ` Drive/Quisquam/code/repo/latest/application-bundles/mac/install.sh`.  The HTML when *rendered* in a browser has a line-wrap between "Google" and "Drive".
Comment: @mzjn This is a problem because the quote environment is meant to quote content exactly.  But the content does not to appear to the user as it is (the space is missing).  This is bad in documentation, since the user is required to type quoted things verbatim into certain prompts.
Answer: Put some css in a file  _static/custom.css at top repertory in your project
code.docutils.literal {
    white-space : pre;
}

(I don't know how from CSS allow breaks say at slashes but not at spaces).
For the   approach you can do this:
1. Find the file in our Google Drive folder It is located at
   ``~/Google`` |_| ``Drive/Quisquam/code/repo/latest/application-bundles/mac/install.sh``.

.. |_| unicode:: 0x00A0
       :trim:

Edit: much better than second approach is as pointed in comment to use a NO-BREAK SPACE in the reST source. Then the HTML will use  , and the URL will still wrap at slashes contrarily to first approach above which makes it really unwrapping (in my brief investigation).
I had used second approach in other context (also targeting LaTeX output) to insert Unicode+202F, and it had slipped out my mind that for Unicode+00A0, one only has to use it directly in reST source.
Comment: didn't you try? in the first approach the space is like rest of the text, in the second the shading is interrupted. At least with alabaster theme which is default in current Sphinx. But this is because shading comes from some css for pre, if you transfer it to code.docutils.literal there will be no interruption of shading also in second approach (which I included only to answer your query about inserting a `` `` entity; the first approach seems the better way).
Comment: I get same result as in [mzjn answer](http://stackoverflow.com/a/42655779/4184837). But notice that when copying from the browser window I get a normal space from the ` ` entity in html source. There are may ways depending on platform and software to insert Unicode characters.
Comment: Is the space shaded grey like the rest of the text?  Is it apparent to the user that a space is between "Google" and "Drive"?
Comment: The CSS modification `white-space: pre;` is okay, but simply using a block quote is a better solution, since it will work on more than HTML outputs.  The NO-BREAK SPACE approach sounds fantastic!  Unfortunately, it doesn't seem to be working.  I am using Sphinx with Python3.5.  I use the `sphinx-build` command to kick things off.  The sphinx-build file has the flag, `# -*- coding: utf-8 -*-`.  I am manually copying and pasting a NO-BREAK SPACE character from a browser into the document.  I have tried multiple websites and multiple text editors, to no avail.
Answer: It works for me if the regular space character is replaced with a non-breaking space (U+00A0) in the reST file. That will produce a single  element containing Google Drive in the output:
~/Google Drive/code/mac/install.sh

Inserting a literal non-breaking space can be done in several ways. See https://en.wikipedia.org/wiki/Non-breaking_space#Keyboard_entry_methods.
Comment: this looks like the better solution because it still allows wrapping in browser at slashes. And copy paste from browser window will get a normal space. At least in my brief testing...
Comment: I was able to enter the character in Emacs using `Ctrl-x 8 `. It can also be copied from the Character Map program on Windows.
Comment: on some keyboards,  or  (not tested on Windows)
Comment: This is a good solution because it will work not only for HTML output, but also for any supported output types.
Comment: I'm not happy with this solution, because it will cause hard-to-debug errors if someone tries to copy-paste and use the code with a non-breaking space in it.
Answer: The fix for me was to add the following css to the custom.css and make background-color match the pre background-color
tt,
code {
    color: #839496;
    background-color: #073642;
}


column calculation based on a value

Question: using yfinance to get the below df, How do I calculate the "Day_Delta" column with +/-% delta from beginning day's Open_Value
symbol  Datetime                    Value_Type  Open_Value  Day_Delta
AAPL    2021-09-20 09:30:00-04:00   Close      143.839996
AAPL    2021-09-20 10:00:00-04:00   Close      142.940002
AAPL    2021-09-20 10:30:00-04:00   Close      142.750000
AAPL    2021-09-20 11:00:00-04:00   Close      143.089905
AAPL    2021-09-20 11:30:00-04:00   Close      142.490494

Comment: Can you update your post with the expected values for Day_Delta, please?
Comment: You can do it row by row, of course.  If you haven't seen this symbol on this date, save the base value.  Then set your new column to `(Open_Value - base) * 100 / Open_Value`
Comment: @TimRoberts: How do i create a column 'base' to perform the calculation?
Comment: `(df.Open_Value.pct_change() + 1).cumprod() * 100 - 100` ? I missed the `delta` part in my last comment.
Comment: Or @TimRoberts solution, which is much faster `(df.Open_Value - df.Open_Value[0]) * 100 / df.Open_Value[0]`
Answer: Solution:

Separate out the Datetime to 2 columns: Date and Time
 symbol  Date             Time       Open_Value
 AAPL    2021-09-20  09:30:00-04:00  143.800003

Sort and remove duplicate from 'symbol' and 'Date'
stock_day_open = stock_data.sort_values(['symbol', 'Date', 'Time', 'Open_Value'], ascending[True, True, True, True])
stock_day_open = stock_day_open.drop_duplicates(['symbol','Date'], keep='first')

merge to a new_df to add a column

Calculate the 'Day_Delta'
Comment: The main part of your question ("calculate the 'Day_Delta' column") remains vague.
Comment: well it was misunderstood from your part. you dont have to downgrade the question..

column calculation based on a value


Display HTML field

Question: I am using Views Database Connector to display safe data from external database in Drupal 8 within views. Some of the fields include simple html like a list which views encodes in html entities. I am using {{ fields.myfield.content }} in views-view-fields--myview.html.twig.
I have searched for other solutions about using the raw value on the site but none of them seems to work.
Is there a simple solution without getting involved in preprocess functions?
Answer: I am not sure if a module exists for this yet, but to do this, you would need to create a field handler for Views that does not escape HTML.
Here is an example I made a year ago to achieve the same thing you are trying to do.
For the field handler, I implemented this hook to change what handled the data:
/**
 * Implements hook_views_data_alter().
 *
 * We need to look through and change the field handler for our stored item to
 * our custom handler so HTML is unescaped.
 *
 * @param array $data
 */
function search_api_view_modes_views_data_alter(array &$data) {
  $indices = \Drupal::entityTypeManager()->getStorage('search_api_index')->loadMultiple();
  $views_data_keys = array_keys($data);

  if ($indices) {
    foreach (array_keys($indices) as $index) {
      $views_index_name = 'search_api_index_' . $index;

      if (!in_array($views_index_name, $views_data_keys)) {
        continue;
      }

      foreach (array_keys($data[$views_index_name]) as $item) {
        if (preg_match('/^view_mode_(.*)/', $item)) {
          $data[$views_index_name][$item]['field']['id'] = 'search_api_view_mode';
          $data[$views_index_name][$item]['field']['fallback_handler'] = 'search_api_view_modes';
        }
      }
    }
  }
}

Anytime one of my fields was seen in a View, the field handler was set to search_api_view_mode, defined below:
/**
 * Provides a default handler for rendered entity view modes in Search API Views.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("search_api_view_mode")
 */
class SearchApiViewMode extends FieldPluginBase implements MultiItemsFieldHandlerInterface {

  use SearchApiFieldTrait;

  /**
   * Overrides render_item to return our markup unescaped.
   * @param $count
   * @param $item
   * @return string
   */
  public function render_item($count, $item) {
    return check_markup($item['value'], 'full_html');
  }

}

check_markup is probably not advised anymore, over a Markup object, but nonetheless this is what I had to do to not have the HTML escaped. I am not sure you can reverse this at the twig level.
Comment: Thank you for your solution Kevin, but is there no way of using the equivalent of html_entity_decode or raw within twig?
I did come across the [link](https://www.drupal.org/project/twig_field_value)twig Field Value module.
Comment: I believe the problem is with Views, and not general field level rendering.
Comment: Problem as in, where you need to handle it. Not a bug.
Comment: Hi Kevin , sorry to ask, but I am more of a Drupal site builder than a developer, so where would I use this code, in the theme overide or create a custom module? Thanks
Comment: You would have to make a custom module. hook_views_data_alter can only be called at the module layer, and your module would contain a plugin at src/Plugin/views/field/(class name).php.
Answer: Probably not a best practice, but I was able to get a field from an external database that contained HTML to print using the markup, instead of printing out the tags by doing the following in twig (in Drupal 8.5.3).  I used the raw field contents and wrapped that in autoescape false.
In my case I put it in a views-view-fields-MY_VIEW_NAME.html.twig template in my theme using the following:
{% autoescape false %}{{ field.raw }}{% endautoescape %}

I'm imagining that you could use something like Twig Tweak and Rewrite Results of MY_FIELD directly in Views to do this, also:
{% autoescape false %}{{ MY_FIELD|raw }}{% endautoescape %}

I know it's a bit late, but hopefully it'll help other Googlers like me.

Display HTML field


Finding $\lim_{x\to\infty} \frac{(2x-5)^4}{(2x^2+1)(3x^2-2)}$

Question: Finding $$\lim_{x\to\infty} \frac{(2x-5)^4}{(2x^2+1)(3x^2-2)}$$
Do I multiply top & bottom by $\frac{1}{x^2}$ or $\frac{1}{x^4}$? How do I distribute them into the numerator tho?
In the answer given, I think with some typos:
$$... = \lim_{x\to\infty} \frac{(2-5/x)^\color{red}2}{(2+1/x^2)(3-2/x^2)} = \frac{2^{\color{red}4}}{2\cdot 3} = \frac{8}{3}$$
Either way, how do I distribute the $\frac{1}{x^?}$ into the numerator?
Comment: The highest power that appears in the numerator and denominator is $x^4$, so you use $\frac{1}{x^4}$.
Comment: Could you not just run l'Hôpital's Rule a bunch of times?  Or do you not have that theorem at your disposal?
Comment: You could be rather primitive (like me) and do a term by term application of the limit after expanding the powers.
Answer: The dominant term is $x^4$ (Imagine that you expanded both the numerator and denominator. What would the highest power of $x$ be?). So, you could either multiply numerator and denominator by $1/x^4$ (see below), or factor as follows:
$$\eqalign{
{(2x-5)^4 \over (2x^2+1)(3x^2-2)}
&={ \bigl(x(2-{5\over x})\bigr)^4\over     x^2 (2+{1\over x^2}) \cdot x^2(3-{2\over x^2 } )   }\cr
&={x^4 (2-{5\over x})^4 \over  x^2(2+{1\over  x^2})\cdot x^2(3-{2\over  x^2  })}\cr
&={(2-{5\over x})^4\over  (2+{1\over x^2})(3-{2\over  x^2})}.
}
$$
The limit as $x$ tends to infinity is $$ {2^4\over 2\cdot 3}=16/6 =8/3.$$
The $(2-5/x)^2$ is probably a typo..

Using the other approach, where you multiply numerator and denominator by $1/x^4$:
In the numerator, to distribute $1/x^4$ over $(2x-5)^4$:
$$\textstyle
{1\over x^4}(2x-5)^4 = \bigl(  {1\over x} (2x-5) \bigr)^4=(2-{5\over x})^4.
$$
(just using $a^nb^n=(ab)^n$ here).
In the denominator, you could do the following:
$$\textstyle
{1\over x^4}(2x^2+1)(3x^2-2)
={1\over x^2}(2x^2+1) {1\over x^2}(3x^2-2)= (2+{1\over x^2})(3-{2\over x^2})
$$
(or just expand $(2x^2+1)(3x^2-2)$  first and then distribute the $1/x^4$ across).


Bonferroni's t critical value

Question: Post hoc tests, like Bonferroni, allow us to detect which of the groups is the different one after ANOVA.
To use it, we calculate effectively what is like the independent t-test for each of the pairwise groups. This value is then compared to the $t$ critical value.
The $t$ critical value is found using Degrees of Freedom (DoF) $= N-k$ where $k$ is number of groups.
My question is why do we use $N-k$ when comparing to a $t$ value made up from 2 groups only. Why isn't DoF equal to total sum of items in the 2 groups, minus 2 like in independent $t$ test?
Comment: Could you cite a reference for this, please?
Answer: In your ANOVA I suppose you were making the assumption that all $k$
groups have the same population variance $\sigma^2.$ MS(Resid) is an estimate of that variance with $\mathrm{DF} = N-k.$ Appropriate ad hoc comparisons would use that estimate.
Because the Bonferroni method of guarding against false discovery is
based on an inequality, it is sometimes too conservative (requiring
P-values to be smaller than really necessary in order to declare
ad hoc differences). In this situation, you might do better to use
Tukey's HSD method to make all comparisons within a fixed error probability.
By contrast, if you are using the Welch ANOVA (implemented in the R
procedure oneway.test), which does not assume equal variances, you
might do Welch t tests for ad hoc comparisons along with Bonferroni.
In that case each Welch t test would use a variance estimate derived
from the two groups being compared. Then the (smaller) DF would be decided by
Welch's two-sample formula.

Bonferroni's t critical value


Different formatter's field by file extension

Question: I have Drupal 7 and a file field and users can upload pdf, office documents and image. For pdf i have pdf.js formatter, office doc's have simple link to download and image show by fancybox plugin. I am need use different formatters if file have different extension.
Thanks.
Answer: It work for me =)
<code>$field_link_file = field_get_items('node', $node, 'field_docs_files');
$file_url = file_create_url($field_link_file[0]['uri']);
$parts = pathinfo($file_url);
$extension = $parts['extension'];
    if ($extension == 'jpg' || $extension == 'png' || $extension == 'tif') {
    $options = array(
    'type' => 'colorbox',
    'label' => 'hidden',
    'settings' => array(
        'colorbox_node_style' => 'large',
        'colorbox_node_style_first' => ''
  )
);
    }
    elseif ($extension == 'pdf') {
  $options = array(
  'type' => 'pdf_default',
  'label' => 'hidden',
  'settings' => array(
    'width' => '100%',
    'height' => '800px'
  )
);
}
    else {
        $options = array(
  'type' => 'text',
  'label' => 'hidden',
  'settings' => array(
    'width' => '100%',
    'height' => '800px'
  )
);
    }
  $field_view_files = field_view_field('node', $node, 'field_docs_files', $options);
  print render($field_view_files);
</code>


Verificar itens de uma lista em outra lista

Question: Estou tendo dificuldade em fazer um método para verificar 2 listas. Este método vai receber uma lista que é a lista que foi adicionada ao pedido e o Id da requisição de compra ao qual esse pedido foi associado. O método chama outro método que retorna do banco a lista de itens na requisição de compra. Assim, nesse ponto tenho 2 listas: uma recebida como parâmetro com os itens que estão sendo adicionados ao pedido, e outra que busquei no banco com os itens da requisição.
Preciso verificar se os itens que estão no pedido estão na requisição, ou seja, verificar se os itens da lista que recebi como parâmetro estão nos itens da lista que veio do banco. Caso positivo, preciso verificar se este item já foi comprado (tenho uma propriedade booleana para isso), e se não foi comprado preciso marcar essa propriedade como true e deixar esse item passar para ser cadastrado.
Preciso verificar também se nessas listas não tem nenhum item que não foi solicitado na requisição de compra, ou seja, o comprador não consegue comprar sem um requisição desse item.
Todos os itens que não deviam estar no pedido, seja porque já foi comprado ou porque não foi pedido, devem ser retirados pelo sistema. Após cadastrado o pedido, que tenho que exibir a lista de itens que foram retirados pelo sistema.
Sei que ficou bem grande por que é um processo complexo mas se alguém puder me ajudar com isso, eu tentei hoje o dia todo, mas sempre fica uma brecha para erros.
Codigo do Metodo
public ControlePedidoRCM ControlePedidoRCM(int IdRCM, List<MaterialPedido> IdMaterial)
        {
            RCMDal rd = new RCMDal();
            ControlePedidoRCM Controle = new ViewModel.ControlePedidoRCM();
            List<int> Comprados = new List<int>();
            List<int> Fora = new List<int>();

            List<MaterialRCM> itens = rd.ListarItens(IdRCM).ToList();

            foreach(var item in itens)
            {
                for(int i = 0; i < IdMaterial.Count; i++)
                {
                    //se o material estiver na lista de RCM
                    if(item.MaterialID == IdMaterial[i].MaterialID)
                    {
                        if(item.Comprado == true)
                        {
                            //ja foi comprado
                            Comprados.Add(item.MaterialID);
                        }
                    }
                    else
                    {
                        //não está na RCM
                        Fora.Add(item.MaterialID);
                    }
                }
            }
            Controle.Comprado = Comprados;
            Controle.Fora = Fora;
            return Controle;
        }

Comment: Uma dica: sua pergunta está muito difícil de entender, então imagino que você está tendo problemas porque você não consegue dividir a tarefa em partes. Largue a IDE por alguns minutos e tente escrever - e em português, não em código - o que o programa deve fazer. Escreva uma sequência de passos. Depois destaque os passos que você consegue executar e os que não consegue. Isso faz com que você consiga isolar o erro mais fácil e nos ajuda a lhe ajudar.
Comment: Outra coisa que pode ajudar: já notou que vários programadores, mesmo os mais velhos, costumam ter bonequinhos em suas mesas? Arrume um bonequinho e tente explicar o seu problema para ele. [**Eu estou falando sério.**](http://blog.codinghorror.com/rubber-duck-problem-solving/) Pode até ser que com isso você não resolva o seu problema, mas ao menos você terá uma ideia mais clara das causas do seu problema.
Comment: @Renan Eu uso um pato de borracha.
Comment: Renan e Cigano, coloquei o código.
Comment: @AlanAlmeida Você pode colocar exemplos do seu código na pergunta?
Comment: Pessoal obrigado pelas dicas, vou ver se arrumo algum bonequinho colocar na minha mesa rsrs.. quanto ao codigo ja mexi tanto nele que agora acho que ja tem longe de atingir o objetivo, vou dar uma arrumada nele pra deixar ele do jeito que ficou quando cheguei mais perto de fazer o que preciso, ai atualizo a pergunta.
Comment: Melhorou bastante e ficou entendível agora. Só mais duas coisas: primeiro, não conhecemos toda a regra de negócio do seu cliente, por isso só podemos analisar a parte lógica de como o código funciona. Segundo, você conseguiu montar duas listas - uma de produtos "dentro" e outra de produtos "fora" da compra. O que, em não mais que cinco palavras, está faltando?
Comment: Olha, difícil em menos de cinco palavras, essa é a primeira versão desse método, o problema é, suponha que eu receba como parâmetro uma lista com 3 materiais, Ids 1, 2, 3 e na lista que veio do banco a mesma coisa, o que acontece que no Id um passa normal, mas os outros dois ele coloca na lista de itens que estão fora da requisição, ou seja, os outros dois caem no else e são colocados na lista como se estivessem fora da requisição de compra.
Answer: Alan,
É mais fácil para outros desenvolvedores entenderem seu código quando você segue as convenções, parâmetro em .NET começa com letra minuscula, e apesar dos nomes estranhos, espero conseguir responder sua pergunta. (ah, e antes que eu me esqueça, não há necessidade de comparar "== true", o compilador é mais esperto do que parece)
Antes de eu ir para a resposta em si, um outro detalhe, os nomes dificultam o sentido do seu código, então pode ser que eu sugira nomes que não batam com a realidade ou apenas vou inverter o sentido, pois não sei ao certo qual é seu objetivo. Mas vou tomar essa liberdade mesmo assim, pois acho que você poderia limpar bastante o código, deixando ele bem menos "complexo", o que quero dizer é que a complexidade do código não vem do requisito e sim de você mesmo.
Existe alguma restrição quanto ao uso de Linq no seu ambiente? uma das formas de verificar se elementos fazem parte de uma lista seria o seguinte:
var idDosMateraisNoCatalogo = rd.ListarItens(IdRCM).ToList().Select(m => m.MaterialID);

var materiaisNaListaDeRCM = materiaisRequisicao.Where(m => idDosMateraisNoCatalogo.Contains(m.MaterialID));

var materiaisForaDaListaDeRCM = materiaisRequisicao.Where(m => !idDosMateraisNoCatalogo.Contains(m.MaterialID));

No caso, eu troquei IdMaterial por materiaisRequisicao.
Não querendo fazer "propaganda" mas acredito ser pertinente, se quiser algumas dicas de como melhorar o design do seu código, dá uma olhada no www.chegadegambiarra.com
Comment: Obrigado pela ajuda Breno, as vezes acaba faltando um pouco de qualidade no código por que aqui na empresa infelizmente temos uma equipe muito reduzida, apenas 2 e os gerentes e donos da empresa querem tudo pra ontem.
Comment: @AlanAlmeida Você é bem-vindo! lendo novamente minha resposta, talvez eu tenha pegado um pouco pesado com você. Peço desculpas por isso. Espero que você tenha entendido a resposta e que consiga fazer uso dela para melhorar parte desse algoritmo.
Comment: Fica tranquilo, eu intendi que você só estava querendo ajudar e agradeço as dicas.


Reference for the idea that a simpler model can be used when the range of data values is smaller

Question: When we build a statistical-physical model, generally, a simpler model can be justified when the range of data-values is smaller.
I can't be the first person to use this idea, but I also can't find it mentioned anywhere.
As a simple example of what I am talking about, consider a falling rock.  If the rock only falls one metre, then a constant constant-acceleration model is probably useful.  If the rock falls a kilometre, then I likely need to consider aerodynamics.  If the rock falls 1000 kilometres, orbital-mechanics will need to be there.
My actual situation doesn't involve falling rocks, but I do suggest limiting the range of data-values as a means of managing the required complexity of a model.
Can anybody recall a name for the concept - or suggest a reference?
Comment: There's certainly an idea around that when (conditional) variation is larger (relative to the variation in $x$), simpler models will tend to work better, and the two ideas are connected (if the x-variable is only over a small range, the conditional variation in $y$ will be relatively larger than if $x$ covers a wider range). I don't have any references for that right now though.
Answer: I think that this is not a reference-able concept, it's just about relative error. What you call "range of data values" is usually called "scale", and you would just say that a certain theory is enough descriptive for this scale. In the example of the rock, $F=-mg$ is enough to describe the dynamics of the rock, in the sense that the error you make by the theory is smaller than the typical measurement error. In this particular case, is called linearisation, and is typical when you have non lineal equations of motion and you want to use them in a small neighbourhood.
In statistics, I would see an equivalent in error measures that contain a cost for the amount of parameters of the model (Akaike or Bayesian Information Criteria, for example). You measure the error you make for models with a different amount of parameters, and at some point the error reduction that you make for adding a new parameter would not be enough to compensate the cost you predefined; be careful with those though, since the definition of the cost is crucial.


Need active tab URL in IE8/IE9 using WIN32 API

Question:
Possible Duplicate:
Get Current URL in IE Using Visual Basic

I need to get the active tab url of the user's browser in IE8/IE9 using WIN 32 API.
I need the URL which is accessed by the user in the browser which is currently accessed. I have to use the URl value in Windows service.
User can open any number of tabs as per his requirement. But at a time only one tab of the browser is active. I need the the URL of the tab which is active.
I have tried to get the active tab URL using ShellWindowsClass. But we get all the URL of the browser. Also i need to get the URL in windows service. So kindly share your thoughts.
Comment: Do you have any idea where to start? (What's your question?)
Comment: there is a similar question on SO http://stackoverflow.com/questions/11158341/get-current-url-in-ie-using-visual-basic
Answer: Try these :
Internet Explorer URL Crawler .. dont know if it works on latest browsers.
http://stoyanov.in/code/ie-url-crawler/
and one more similar link..
http://www.c-sharpcorner.com/Forums/Thread/46881/how-to-get-the-current-url-in-internet-explorer.aspx


Using Ltm package to calclate 3PL some lines of data seem to go missing

Question: I am hoping someone can help with the following:

I have been trying to use the ltm package in R to get person fit estimates using a 3PL model.  The problem I have been having is that I start off with 4947 response patterns (dichotomous) but when I run tpm() I end up with 4851 ability estimates and 4851 person fit estimates.  I tried with another data set and ended up with a similar problem.
Here is the line of code I've used---gr3 is the dataset with 4947 lines of data containing zeroes and ones.
 threePL <- tpm(gr3, na.action = NULL)

In addition, in order to run the 3PL model, I have to get rid of the person IDs, but I would like to retain the person IDs so that I can accurately map the ability and person fit estimates onto the right person.
Comment: Welcome to the site, @nikki0111. Are you thinking of this as a conceptual statistics / psychometrics question, or an R programming assistance question? I'm not sure, but I wonder if it is the latter. If so, this question would be off-topic for CV (see our [help page](http://stats.stackexchange.com/help/on-topic)). It *could* be on-topic on [Stack Overflow](http://stackoverflow.com/), but it would need to have a [reproducible example](http://stackoverflow.com/q/5963269/).
Comment: If you want help with the *ideas*, please edit to clarify; if you just want help w/ the *coding*, add a reproducible example & flag your Q. We can migrate it for you. (*Please don't cross-post, though.*)
Answer: rows that have the same response pattern will be combined to save space, try,
    threePL <- tpm(gr3, na.action = NULL)
    ability.est <- factor.scores(threePL, gr3)

and you will get the ability estimates that correspond to each of your rows.


using CryptoJS with hex-string

Question: I want to connect to a bluetooth device. Communication is via Hex-Strings only. I need to encode a 16 byte value. As a result I also expect a 16 byte value. In my implementation CryptoJS always returns a longer result. According to the documentation the IV is not needed. ("All the 16-byte data must be encrypted with the Customer Master Key currently stored in the device, using AES128 CBC
cipher mode") Therefore I set the IV to 00000000000000000000000000000000 because CryptoJS seems to require it. What am I doing wrong?
const CryptoJS = require('crypto-js');
const value =  CryptoJS.enc.Hex.parse('5ff58680541c5a5903f4833dfaa4281f');
const key  = CryptoJS.enc.Hex.parse('41435231323535552d4a312041757458'); // known master key
const ivvar   = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
const encryptedString = CryptoJS.AES.encrypt(value, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}).toString();

// current result is edijc9R7sl3zwZVrBBBrFQ==
Sure, the result is only a string, but it is too long anyway.
Comment: The ciphertext is Base64 encoded and is 16 bytes long after decoding: `79d8a373d47bb25df3c1956b04106b15`, e.g. [here](https://cryptii.com/pipes/base64-to-hex). Instead of a Base64 encoding you can also directly use a hexadecimal encoding. To do so, you just have to replace `toString()` with `ciphertext.toString()`, e.g. [here](https://cryptojs.gitbook.io/docs/#the-cipher-output).
Comment: Note: Different IVs produce different ciphertexts, if the device uses a different IV than a `0`-IV, the connection is likely to fail (but since according to the description no IV is needed, a `0`-IV is plausible). By the way, for a 1 block (16 bytes for AES) plaintext, CBC with a `0`-IV is identical to ECB, so an implementation without IV would also be possible: `CryptoJS.AES.encrypt(value, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding}).ciphertext.toString();`
Comment: @Topaco: Thanks a lot, this already helped and works for the encryption. But now I am stuck with the decryption.
`const value =  CryptoJS.enc.Hex.parse('79d8a373d47bb25df3c1956b04106b15');
 const key  = CryptoJS.enc.Hex.parse('41435231323535552d4a312041757458');
 const decryptedStringHex = CryptoJS.AES.decrypt(value, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding});`
 decryptedStringHex is empty, seems we give the wrong input. Help appreciated.
Comment: In `decrypt`, `value` must be replaced by `{ciphertext: value}`. The hexadecimally encoded plaintext is then contained in `decryptedStringHex.toString()`.
Comment: Awesome, works as well. Thank you so much.
Answer: For the sake of completeness I would like to add the solution, which works for me. (Thanks to @Topaco)
encrypt(valueStringHex, keyStringHex) {
    const CryptoJS = require('crypto-js');
    const value =  CryptoJS.enc.Hex.parse(valueStringHex);
    const key  = CryptoJS.enc.Hex.parse(keyStringHex);
    const ivvar   = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
    const encryptedStringHex = CryptoJS.AES.encrypt(value, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding}).ciphertext.toString();
    return encryptedStringHex;
    }

// encrypt('5ff58680541c5a5903f4833dfaa4281f', '41435231323535552d4a312041757458')
// returns 79d8a373d47bb25df3c1956b04106b15

decrypt(valueStringHex, keyStringHex) {
    const CryptoJS = require('crypto-js');
    const value = CryptoJS.enc.Hex.parse(valueStringHex);
    const key = CryptoJS.enc.Hex.parse(keyStringHex);
    const ivvar   = CryptoJS.enc.Hex.parse('00000000000000000000000000000000');
    const decryptedStringHex = CryptoJS.AES.decrypt({ciphertext: value}, key, {iv: ivvar, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.NoPadding});
    return decryptedStringHex.toString();
    }

// decrypt('79d8a373d47bb25df3c1956b04106b15', '41435231323535552d4a312041757458')
// returns 5ff58680541c5a5903f4833dfaa4281f

using CryptoJS with hex-string


Stop Crosstab summary display string count from Resetting on each page

Question: I have a crosstab in Crystal Reports XI with a display string that displays a count for each time the condition is met:
Basically I have a summary column amountspent that I need to compare to avblcredit, and count every order where amountspent (the summary) exceeds avblcredit for each group, customer. I then have to display the total amount of orders for that customer where available credit is exceeded.
After much struggle due to the fact that I cannot use calculated members in Crystal Xi. I
created a second duplicate summary for item exditures and edited the display string of the second summary to compare itself to avbl credit then count:
global numbervar count;
 if currentvalue > avblcredit
    then count := count + 1;
count;

The count then increments everywhere it finds the current value (sum of items) > available credit.
This works correctly if the crosstab prints fully on the page, however if the crosstab extends to the next page the count resets back to 0.
So basically as an example page 1 looks as follows:
customer 1
orders avblcredit amountspent count itema itemb itemc
ord1    4000         6000        1   2000  3000  1000
ord2    3734         5001        2   1000  2000  2001
ord3    4123         5000        3   4000  1000  0
ord4    2321         5000        4   5000  0     0
ord5    4000         5003        5   1200  3800  3
ord6    4000         6000        6   1000  2000  3000

page 2 with customer 1 group continued:
orders avblcredit amountspent count itema itemb itemc
ord7    4000         6000        1   2000  3000  1000
ord8    3734         5001        2   1000  2000  2001
ord9    4123         5000        3   4000  1000  0
ord10   2321         5000        4   5000  0     0

My question is how can I get my count from resetting on each new page?
Thanks
Answer: If you can modify your datasource, you can do the calculation upstream.  Just extend your existing dataset with a new field named something like spentExceedsCredit with a value of 0 or 1.  Then you can sum them per customer, or use custom formatting in the report, or have the list of customer expenditures exceed a page, or whatever, and you're not dependent on Crystal's global variables.


Can every statement that can be proved using the well-ordering principle be proved using weak mathematical induction?

Question: The following is problem 30 of chapter 4.4 of Discrete Mathematics with Applications, 3rd ed. by Susanna Epp:

Prove that if a statement can be proved by the well-ordering principle, then it can be proved by ordinary mathematical induction.

I am not sure how to answer this and could not find the answer online.
Interestingly, this problem appears to have been replaced by a problem asking to prove that weak mathematical induction implies the well-ordering principle in later editions of the book.
Edit: Clarification
I am asking for an answer to the problem in the textbook. I am not asking about the equivalence of the well-ordering principle (WOP) and weak mathematical induction (MI). In other words, if you can write a proof by WOP for a statement, can you also write a proof by MI for the statement that doesn't use WOP?
My meaning may be made clearer by considering the converse of the problem: prove that if a statement can be proved by MI, then it can be proved by WOP (this is problem 29 of the textbook). This is true because we can construct a proof by WOP from a proof by MI as follows: take the proofs for the base case and inductive step from the proof by MI, then assume to the contradiction that the set $S=\{n>=a:\neg{}P(n)\}$ is nonempty, then use WOP to arrive at a contradiction. This forms a complete proof of the original statement that uses WOP without ever using MI.
The above example shows that the converse is simple because a proof by MI can be easily converted to a proof by WOP. Can a proof by WOP be converted to a proof by MI? An example where this might not be true is Bézout's identity; Bézout's identity can be proven by WOP, but how would you prove it by MI?
Perhaps this is merely a problem with the wording of the textbook's problem. But I still hope to get an answer to the problem at face value (unaltered). I would appreciate any clarification.
Comment: I don't know what you are asking here. You know they are equivalent. What does it even mean "to write a valid proof without knowing the well-ordering principle". This doesn't make sense, if the principle is equivalent to weak induction, then (1) you cannot not know it and (2) everything deducible from well-ordering is deducible from weak induction by first proving weak induction. The question is weird to be honest.
Comment: I understand your confusion and have added clarification to the post. What I am really asking it that if you have a proof by induction for a statement, can you create a proof that uses WOP (instead of induction) for the statement?
Comment: I am not sure why this got downvoted (( I will try to rectify that with my +1 )) OP is trying to learn something & has put in the effort. Yes , OP is confused & that is why the Question was asked.
Comment: @Cynicrom I think you are confused. Formally define "proof by induction" and "proof that uses WOP" so that we can know how these are different. If you try that you will quickly realize that if $A\leftrightarrow B$ then "$A\to X$" statement can be replaced freely with "$B\to X$" and vice versa. Using the language of logic I don't even see a way to formalize your question.
Comment: When you say 'Using the language of logic I don't even see a way to formalize your question" , @freakish , that is what I am hinting at when I say "we can not escape that Equivalence" : there is no way to avoid one & use the other , because the two are actually the Exact Same !!
Answer: Property $WOP$ is Well-Ordering Principle.
Property $MI$ is Mathematical Induction.
(Exercise 1) Statement $S$ is given with a Proof , hence we are given $WOP \implies S$.
We are asked to show that $MI \implies S$.
(Exercise 2) Statement $S$ is given with a Proof , hence we are given $MI \implies S$.
We are asked to show that $WOP \implies S$.
(Exercise 3) We are asked to show that $WOP \iff MI$.
While (Exercise 1) is the OP Query in the text book $3^{rd}$ Edition , we might also consider (Exercise 2) which might not be given in that text book , though (Exercise 3) is there in Newer Editions & all 3 Claims are essentially the Same Idea.
More-over , (Exercise 3) is the Best Way to state that Core Idea.
That can be seen with this intuitive thinking :
(Exercise 1) $[ (WOP \implies S) \land (WOP \iff MI) ] \implies (MI \implies S)\tag{X1}$
(Exercise 2) $[ (MI \implies S) \land (MI \iff WOP) ] \implies (WOP \implies S)\tag{X2}$
We can also see it using logical manipulation.
We are simply assuming & using (Exercise 3) here.
Proving (Exercise 3) is simple enough : Check out https://brilliant.org/wiki/the-well-ordering-principle/#equivalence-with-induction
I am skipping the Proof here , because OP says that the Proof is known & that Proof is not what the Question is about.
OP : To clarify, I know that the well-ordering principle is equivalent to weak mathematical induction; this is not what I am asking.
Proof Over-view : We show that $WOP \implies MI$ & $MI \implies WOP$ , to then conclude that $WOP \iff MI$.
Naturally , the Newer Edition is better focused on the Core Issue.
OP : Another way to state the problem above is: if I can write a valid proof for a statement using the well-ordering principle, can I write a valid proof for the same statement that uses weak mathematical induction without knowing the well-ordering principle?
Yes , we can achieve that with the Equivalences given by (X1) & (X2) , where we can use the Proof Method itself , while not invoking WOP.
OP : I am not sure how to answer this and could not find the answer online.
In general , we just have to show the Equivalence between the two.
Even though we have the Stronger Claim that $MI \iff WOP$ here , within the Context of the Exercise , we have to just show $MI \implies WOP$.
Basic Out-line : Prove WOP with MI , then use the given Proof that "$WOP \implies S$" to get the Chain "$[ MI \implies WOP ] \land [ WOP \implies S ]$" to eventually conclude that "$MI \implies S$"
Diagram & Analogy :

When WOP gives S , we can start at MI , then show WOP & then show S. Hence MI gives S.
When MI gives S , we can start at WOP , then show MI & then show S. Hence WOP gives S.
Here is one analogy to see what is going on.
P1 : When $x$ is Even number , then $x^3+5$ is not divisible by $8$.
P2 : When $x=2y$ , then $x^3+5$ is not divisible by $8$.
Both are true because "$x$ is Even" $\iff$ "$x=2y$" & we have to use that fact (Directly or not) : We can not escape that Equivalence.
Here is one non-mathematical analogy :
Prove that "X1 : US President can press the nuclear bombing button"
Prove that "X2 : Biden can press the nuclear bombing button"
Prove that "X3 : White House Boss can press the nuclear bombing button"
Every Proof of X2 will utilize the fact (Directly or not) that the Authority lies with US President who is "Currently" Equivalent to Biden.
Every Proof of X3 will utilize the fact that White House Boss is Biden who is "Currently" US President.
We can not escape that Equivalence.
Here is one last scientific analogy :
Suppose we have Chemical Ingredient X.
Suppose liquid water is necessary to convert X to Chemical Y through a well-known reaction.
Suppose ice will not react with X.
Prove that we can make Chemical Y with Chemical Ingredient X & ice , even though ice is inert & will not react with X.
Proof : Heat the ice to make liquid water. Then use that water in the well-known reaction to convert X to Y.
We have used the inert ice to convert X to Y !
That occurs because ice (WOP) is equivalent to water (MI) !
We can easily convert X (Premises) to Y (Conclusion S) with that Equivalence
More-over , we can not escape that Equivalence.
Comment: I appreciate the answer, but I believe you've only proven the equivalence between WOP and MI, and not whether you can write a proof using WOP given a proof using MI, which is what I (and the textbook problem) is asking.
Comment: Eventually , that is what it is : [[ (1) Take a Proof of S using WOP (2) Ignore WOP (3) But use MI to show that WOP is true (4) Then use WOP to conclude S ]] : Hence we have used MI to conclude S.
Comment: You have opened my eyes to a possibility I did not consider. I wonder if this is the author's own answer hinted by her choice of replacing this problem in later editions with a problem asking for a proof that MI => WOP. I still wonder if there is another way to obtain a proof by MI from a proof by WOP.
Comment: Correct ! Indeed , "I wonder if this is the author's own answer hinted by her choice of replacing this problem" : That is Exactly what I said in with "More-over , (Exercise 3) is the Best Way to state that Core Idea" : In My Way of thinking here , (Ex 1) & (Ex 2) could be merged to the Stronger Ex3 & that is why the Author did that in the New Editions.
Comment: Would it still be possible to construct a proof by MI without first proving WOP? Because a proof by MI that proves WOP in order to arrive at the conclusion technically uses WOP.
Comment: There is no such way : Since the 2 are "Mathematically Equivalent Statements" , every way will include the 2 [Directly or not] : In Case there were such a way , it would mean that MI & WOP are Independent & unrelated , which is not the Case.
Answer: In logic if we have two statements $A$ and $B$ such that $A\leftrightarrow B$ (they are equivalent) then this pretty much means there is no essential difference between them. You can think of logic as description of reality, and then $A\leftrightarrow B$ means that both $A$ and $B$ describe (some piece of) reality in equally the same way. In particular we can replace $A$ with $B$ and $B$ with $A$ anywhere in logic, and nothing will change. Or in other words every statement of the form $A\to X$ is equivalent to $B\to X$.
So $A$ and $B$ are just different names for the same phenomenon. Therefore it is unclear what exactly you mean by "use one and not the other". You can of course replace the name "weak induction" with "well ordering" in you proof and everything will be fine and correct. Some conclusions might not be clear, but it is guaranteed to be correct.

My meaning may be made clearer by considering the converse of the problem: prove that if a statement can be proved by MI, then it can be proved by WOP (this is problem 29 of the textbook). This is true because we can construct a proof by WOP from a proof by MI as follows: take the proofs for the base case and inductive step from the proof by MI, then assume to the contradiction that the set $S=\{n>=a:\neg{}P(n)\}$ is nonempty, then use WOP to arrive at a contradiction. This forms a complete proof of the original statement that uses WOP without ever using MI.

So you've taken the proof of "$WOP\to MI$" and then given a statement "$MI\to X$" you combined it to get "$WOP\to MI\to X$", which you can simplify to "$WOP\to X$".
Yeah, the inverse is obvious then. Take the proof of "$MI\to WOP$" and replace statement "$WOP\to Y$" with "$MI\to WOP\to Y$", and finally simplify it to "$MI\to Y$".
But again: this is artificial. There's nothing substantial here, just different wordings.


Rate of Convergence of a sequence of functions

Question: I know that the rate of convergence $\mu$ of a convergent sequence $(x_n)_{n\in\mathbb N} \to x$ in $\mathbb R$ is given by
$$\mu = \lim_{n\to\infty}\frac{|x_{n+1} - x|}{|x_n - x|}.$$
But if I have a sequence of functions $(f_n(x))_{n\in\mathbb N} \to f(x)$, is there an analogous way one can determine the rate at which $f_n$ converges to $f$? In particular, I am interested in the Fourier series $f_n, g_n : [0,1] \to \mathbb R$ given by
$$ f_n(x) = - \sum_{k = 1}^n \frac{2(-1)^k}{k\pi} \sin (k\pi x) \qquad \text{and} \qquad g_n(x) = \frac{1}{2} + \sum_{k = 1}^n\frac{2((-1)^k-1)}{k^2 \pi^2} \cos (k\pi x)$$
where both $f_n$ and $g_n$ converge to $f(x) = x$ for $x\in [0,1]$.
Experimentally (through different plots for different values of $n$), I know that $f_n$ converges to $f$ more slowly than $g_n$ does, but I do not know how to prove this rigorously. I appreciate any assistance.
Comment: You can consider the quotient $\frac{d(x_{n+1},x)}{d(x_n,x)}$ in every metric space as a measure of the rate of convergence. That has been done for $L^1([0,1])$ and for $L^{\infty}([0,1])$ here in the comments. However, for Fourier series $L^2$ seems to be the natural choice, and the orthogonality of the systems makes it easy to see that $\lVert f_n - \operatorname{id}\rVert_{L^2} \sim c_1 \frac{1}{\sqrt{n}}$ and $\lVert g_n - \operatorname{id}\rVert_{L^2} \sim c_2 \frac{1}{n^{3/2}}$.
Comment: Well, I can see a problem there, as equality $g(x)=f(x)=x$ only exist on the limit $x\in [0,1]$. The Fourier series describe a periodic function, not a 1st order/linear one.
Comment: @JHBonarius But I only want to show convergence in that interval (that is, on the domain of $f_n$, $g_n$. Why should that be a problem?
Comment: I'm not sure that will work. Maybe if you integrate? $\mu = \lim_{n\to\infty}\frac{\int_0^1(|f_{n+1}(x) - x|)dx}{\int_0^1(|f_n(x) - x|)dx}$
Comment: @JHBonarius In that case, I think it should be $$ \mu = \lim_{n\to\infty} \frac{\int_0^1 |f_{n+1}(x) - f(x)|\, \mathrm{d}x}{\int_0^1 |f_n(x) - f(x)|\, \mathrm{d}x}.$$ But how could we interpret the limit as a measure of rate of convergence?
Comment: No, I don't think so. You state you want to determine the distance/error from $f_n(x)$ to $lim_{n\to\infty}f_n(x)=x$, which is given by $|f_n(x)-x|$. What you'd expect is that $\mu_g<\mu_f$, i.e. $$\lim_{n\to\infty}\frac{\int_0^1|g_{n+1}(x)-x|dx}{\int_0^1|g_{n}(x)-x|dx}<\lim_{n\to\infty}\frac{\int_0^1|f_{n+1}(x)-x|dx}{\int_0^1|f_{n}(x)-x|dx}$$
Comment: @JHBonarious Yes you're right, I meant $f(x)$ in general, but yes in this case $f(x) = x$.
Comment: ah, true, because you defined $f(x)=\lim_{n\to\infty}f_n(x)$ to be so. We're on the same page. By the way, I'm not posting an answer, as I'm not sure about my theory. It could be that there's a better way to do it. My theory is: the rate of convergence is determined by the change in distance/error from one $n$ to the next. My suggested formula does that for $f_n(x)$. It might be challenging to solve analytically, maybe requiring Taylor expansion.
Comment: From my point of view the natural thing to do is $\mu:=lim_{n\to \infty}\frac{\lVert f_{n+1}-f\rVert_{\infty,[0,1]}}{\lVert f_{n}-f\rVert_{\infty,[0,1]}}$, since you have some standard estimates relating uniform convergence of fourier series, and the derivative of the function you are expanding. integrating from my poin of view sounds quite weird.
Comment: @Diesirae92 Can you please define $\lVert f \rVert$?
Comment: It is the standard norm of uniform convergence on $[0,1]$
Comment: It is also worth noting that $(g_n)$ converges uniformly, but $(f_n)$ doesn't. That's because $(g_n)$ is the Fourier series of the even extension $\lvert x\rvert$ to $[-1,1]$, whose $2$-periodic extension is Lipschitz continuous, while $(f_n)$ is the Fourier series of the odd extension $x$ to $[-1,1]$ whose $2$-periodic extension has a jump disctontinuity at the odd integers. (See also Gibbs' phenomenon.)
Answer: I've decided to try and answer my own question. First of all, as has been mentioned in the comments, $f_n \to f$ only pointwise, whereas $g \to f$  uniformly. This already indicates that $g_n$ converges more strongly.
Now treating $f$ and $g$ as two elements of the function space $\mathbb R ^ {[0,1]}$ with the $L^2$ norm $$\left\| \phi \right\| = \int_0^1 |\phi(x)|^2 \,\mathrm d x,$$
    we define the rate of convergence of $\phi_n$ to $\phi$ by
    $$\mu_{\phi_n,\,\phi} = \lim_{n \to \infty} \frac{\left\| \phi_{n+1}-\phi \right\|}{\left\| \phi_n - \phi \right\|}.$$
What we want to show is that $\mu_{f_n,\,f} < \mu_{g_n, \, f}$.  Using Paversal's identity, we have that
    $$ \|\phi\|^2 = \sum_{n} |\alpha_n|^2,$$ where $\alpha_n$ are the Fourier coefficients of $\phi$. In our case, we have
    $$\| f - f_n \|^2 = \sum_{k=n+1}^\infty |\alpha_k|^2 = \sum_{k=n+1}^\infty \frac{|a_k|}{k^2}.$$
    This gives an expression of the form $ (c - H_n^{(2)})/(d - H_n^{(4)})$ where $H_n^{(2)}$ denotes the generalised harmonic number, and c,d are the corresponding $H_\infty$ terms.  So the question boils down to showing that $\sum_k 1/k^2$ converges more slowly than $\sum_k 1/k^4$, which can be done easily using the original definition of rate of convergence.


Usage of data structures

Question: I have a common question about the usage of data structures in coding assessments. When giving any coding assessments (with hackerrank etc) for example if an array is passed as an argument to a function which needs to be completed, is it acceptable to restore the data to hashmap or other data structures according to the problem? Well I know there is no restriction explicitly but while evaluating performance would that matters?
Comment: If you're asking whether it's worthwhile to create a data structure from existing data in order to accelerate operations on that data, the answer is "sometimes".
Comment: Yes, data structure choice affects performance - that's largely the point of there being different data structures. You'd need to understand the complexity and frequency of the operations you want to perform on the different data structures to be able to pick the best one (and use it efficiently).
Comment: Okay. I think I need to rephrase and ask in this way. Suppose the function input is an array and expected output is array, however, I feel its good to use Hashmap for doing operations on data, Is it a good idea to feed the array to hashmap and again back to array in case of giving online assessments especially.
Answer: Best approach would be to try to avoid copying the received array into some other data structure if this data structure of yours does not really give you an upper hand with regards to performance. However, if the new data structure enhances the performance of your algorithm, then YES you should do it without hesitation as it would only be a linear operation ( O(n) ). Though this is just my humble opinion, hope you get a better answer for this.
Comment: Okay. I think I need to rephrase and ask in this way. Suppose the function input is an array and expected output is array, however, I feel its good to use Hashmap for doing operations on data, Is it a good idea to feed the array to hashmap and again back to array in case of giving online assessments especially.
Comment: You should avoid it as online assessments tend to test your code with huge data sets hence your code might end up failing certain tests as copy action adds some additional overhead. However, if it significantly improves the performance of your algorithm, then you should definitely consider using HashMap as it makes life simpler quite often.
Answer: There have no straight forward answer for these type of questions. The answer totally depends on the problem you are solving.
For example, if someone asks should I use linear search or binary search to find a value from a unsorted array. Even if the binary search is much more faster than the linear search, still using binary search is not always efficient.
For instance, if he/she has to search in the array for just one time then using binary search will give him/her complexity of O(nlogn) as the array needs to be sorted first, where he/she can search using linear search with O(n) complexity.
I hope you got the answer :)

Usage of data structures


Show that $H_s$ is subgroup of $G$

Question: Let $S$ be any subset of a group $G$. Show that $H_s = \{x \in G\mid xs = sx \text{ for  all } s \in S\}$ is a subgroup of $G$.
Answer: Hint: I'll start you off $e \in H_s$ since $es=se=s, \forall s \in S$.
Comment: please give me more detail hint..
Answer: Note that $e$, the identity element of the group $\in$ $H_S$ as $es=se\ \forall s\in S$. Now let $x\in H_S$ $\Rightarrow$ $xs=sx$ $\Rightarrow$ $x^{-1}xs=x^{-1}sx$ and hence $sx^{-1}=x^{-1}s$ $\forall s\in S$, so $x^{-1}\in H_S$.

Show that $H_s$ is subgroup of $G$


How can i sell files using ubercart

Question: I want to sell files as product using ubercart.
How to add files using product content type?
I want user to pay for the file first and then get file downloaded.
Is it possbile?Then how ??
Also i want each role called 'vendor' to have there own payment details and get paid from buyers. Is that possbile?
Thanks.
Answer: There is already module File downloads in the ubercart which allow user to sell file with the product.

Install the module File downloads from the module list.

Assigning directory to file donwload:

Go to Administration -> Store -> Configuration -> Product
In the File download settings section, set the path of the directory of the files and save the form.

Uploading files:
- Go to Administration -> Store -> Products -> View file downloads
- Upload files into directory (which you have configured in above step) using upload file action.
Adding files to product:

Edit your product and you will get the feature link in top of page. click on this link.
In feature form, you can add file download feature to the product and it will redirect you to form to assign file to product.
Select file name in File download text field. This is autocomplete field, so you need to configure correct files and save form.

When the user purchase product and pay the amount, it will send a link to user in his mail to download the files.
Regarding your second question, there is module Ubercart Marketplace.

Ubercart Marketplace adds multiple seller support to the Ubercart
  module. It includes a series of modules that limit the access to
  Ubercart functions for sellers. Additional functionality is added to
  support this from both a marketplace seller's point of view and the
  store administrator's view. Sellers (with the proper access
  permissions) gain the ability to view their products, track and
  fulfill their orders, and monitor their sales reports. Store
  administrators can view seller reports based on seller order statuses
  and seller sales. Support is included for Paypal Mass Payments, with
  which an administrator can automatically pay his/her sellers for
  custom periods.

Kindly see that Ubercart marketplace is still in development version for Drupal 7, so may be there could be some issue in this module.
Comment: In feature form, you can add file download feature to the product and it will redirect you to form to assign file to product. Select file name in File download text field. This is autocomplete field, so you need to configure correct files and save form. All this in drupal 6 or drupal 7 ? because in drupal7 i cant find those things.
Comment: This is for Druapl 7. Have you installed module `file downloads'?
Comment: yeah.. i got tat autocomplete field for file download. but its throwing me this error
Error message
sites/default/files/new.png is not a valid file or directory inside file download directory. In the file downloads i have given this path(sites/default/files), File download settings also uploading files gave same directory.
Comment: @NewToDrupal, Your file download directory should be different to `sites/default/files` directory.
Comment: after adding the cart and after buying only i will get the pdf file or  wat ever file in mail ?
Comment: @NewToDrupal, Yes.. link in a mail to download file.
Comment: About second question, after adding to cart by buyer, then entering the details, i want that money to go to tat vendor only. Not to any other vendor. Like wise each vendor should get respective money for their files after clicking on add-to-cart and while paying after tat ?? is tat possible?
Comment: @NewToDrupal, Have you read my answer completely? I already mentioned a module for this.
Comment: yeah read tat, but this line makes me to ask that question , " Support is included for Paypal Mass Payments, with which an administrator can automatically pay his/her sellers for custom periods." BUt i want seller to directily paid after selling a product to his account, like wise for other sellers.
Comment: @NewToDrupal, I have not worked on this module, so you need to check module.
Answer: There are so many tutorials available, i found it by Googling

Many stores online deal in digital goods. Known Ubercart sites are
  already selling music, photos, movies, and more using the core file
  download system. Large sites like Riff Trax and corporations like AOL
  are using this system to sell and distribute files. Props go to early
  community member CpILL for getting the ball rolling and torgosPizza
  for his extensive testing and use of the system. Its creation was and
  its improvement continues to be a community effort.

https://www.google.co.in/search?q=ubercart+file+downloads+&oq=ubercart+file+downloads
Selling Files
Setting Up Ubercart File Downloads
Comment: Hello, The answer which is providing only link are not acceptable.

How can i sell files using ubercart


For all integers $n \ge 1$, prove 6 divides $n(n+1)(n+2)$ by PMI.

Question:
For all integers $n \ge 1$, prove 6 divides $n(n+1)(n+2)$ by PMI.

I check for my base case, it holds.
Then, my inductive hypothesis that for any arbitrary $n \ge 1$, 6 divides $n(n+1)(n+2)$ so there exists integer $k$ s.t $n(n+1)(n +2) = 6k$. Assume $(n+1)(n+2)= 6k/n$.
Inductive step: $(n+1)(n+1+1)(n+1+2) = (n+1)(n+2)(n+3)=(6k/n)(n+3)=
6k + (18k/n)$.
From here I can simply factor out a $6$ but how do I know that $18k/n$ is an integer?
I also struggle when it comes to the substitution part. When there is one $n$ involve is simple but when I see various $n$'s in my equation I'm unsure about which one I really want substitute for, any pointers?
Lastly, am I allowed to make various substitutions or am I confined to only substitute once? Thank you for your time.
Comment: See also: http://math.stackexchange.com/questions/527300/prove-that-6-divides-nn-1n-2
Answer: Note: You may prove your result directly by noting that among $n,(n+1)$, and $(n+2)$, one is a multiple of $3$ and at least one is even, yielding a factor of $6$. However, an inductive proof is still quite possible. Just something useful to keep in mind.
For each $n\geq 1$, let $S(n)$ denote the statement
$$
S(n) : 6\mid n(n+1)(n+2).
$$
Base step: Since $6$ divides $6, S(1)$ holds.
Inductive step: Fix some $k\geq 1$, and suppose that $S(k)$ holds; that is, suppose that $\ell\in\mathbb{Z}$ so that
$$
k(k+1)(k+2)=6\ell.
$$
Then trying to prove $S(k+1)$,
\begin{align}
(k+1)(k+2)(k+3) &= k(k+1)(k+2)+3(k+1)(k+2)\\[0.5em]
&= 6\ell+3(k+1)(k+2),
\end{align}
and since one of $k+1$ or $k+2$ is even, $6$ divides $3(k+1)(k+2)$, and so $6$ divides $(k+1)((k+1)+1)((k+1)+2)$, proving that $S(k+1)$, and concluding the inductive step that $S(k)\to S(k+1)$.
By PMI, for every $n\geq 1, S(n)$ holds. $\blacksquare$
Comment: @curiousmind1995 Don't foil. Note the following:
$$
(k+1)(k+2)(\color{red}{k}+\color{red}{3}) = \color{red}{k}(k+1)(k+2)+\color{red}{3}(k+1)(k+2).
$$
Make sense now?
Comment: how do you go from (k+1)(k+2)(k+3) = k(k+1)(k+2) + 3(k+1)(k+2).
Comment: @curiousmind1995 You see it now I presume?
Comment: I see they're equal but is there a property or something you used to arrive to the RHS. when I foil my LHS I get k^3 + 6k^2 + 11k + 6. care to explain how you arrived to the RHS?
Comment: WOW.....okay. thank you so much! I have a habit of overcomplating things..
Comment: @curiousmind1995 No problem. Glad it helped. :)
Comment: Nice proof but here : "$6$ divides $3(k+1)(k+2)$", I think it is better to say that $2$ and $3$ divides this expression. Since $\gcd(2,3)=1$ by the Chinese lemma we can conclude that $6$ divides this expression.
Comment: may this be generalized to every $n$ by the same proof ? meaning the product of sequences of $L$ consecutive numbers is divisible by $L!$ ...
Answer: Here's an alternative: To prove that $(n+1)(n+2)(n+3)$ is divisible by $6$, you just have to prove that $(n+1)(n+2)(n+3)-n(n+1)(n+2)$ is divisible by $6$. And if we compute that last expression we find it to be equal to $3n^2+9n+6=3(n+1)(n+2)$ which is divisible by $3$ and by $2$ (since it is a product of two consecutive integers), hence it is divisible by $6$, the thing that completes the proof.
You may ask yourself why does this method work? The reason can be demonstrated as follows: If $g(n):=(n+1)(n+2)(n+3)-n(n+1)(n+2)$ is divisible by $6$, then that means that there is some $k$ in $\mathbf N$ such that $g(n)=6k$. Now by the inductive hypothesis we have that $n(n+1)(n+2)$ is divisible by $6$, hence there is some $h$ in $\mathbf N$ such that $n(n+1)(n+2)=6h$. So now we can write the very first equality as :
$$
6k=(n+1)(n+2)(n+3)-6h\iff(n+1)(n+2)(n+3)=6(h+k).
$$
Since $h+k\in\mathbf N$, it follows that $(n+1)(n+2)(n+3)$ is divisible by $6$. $$\tag*{$\small\square$}$$
This technique is very powerful in the sense that it works in most cases: If you have an expression $P(n)$ and you want to show that it is divisible by some number $m$, then it all boils down to showing that the base case holds and that $P(n+1)-P(n)$ is divisible by $m$.
Answer: Write $$(n+1)(n+2)(n+3) = n(n+1)(n+2) + 3(n+1)(n+2) = 6k +3(n+1)(n+2)$$ and prove by induction that $(n+1)(n+2)$ is divisible by $2$.


Can scrapy download pdf completely

Question: I want to download many pdf files from the pdf web links crawled by scrapy spiders, but when I use scrapy.Request(pdf_url) in filespipeline to crawl the pdf web, eventually it downloaded incomplete pdf files. all the pdf files was 1 KB Other than the first few files( which are complete). I had to use requests.get(pdf_url, stream=True) to download all the pdf files completely, but it is too slow. I want to know if scrapy filespipelines has similar method like this ?
Comment: Is it possible that the server is preventing many concurrent downloads from the same IP address?
Answer: Not really sure if scrapy can do that job. You can use wget library to download it.
import wget
pdf_url = "url_of_the_pdf.pdf"
wget.download(pdf_url)

Comment: Ok, thank you for your answer. I just want to make consistent with scrapy framework, and I am not sure whether scrapy have the same stream method to download pdf file. I appreciate scrapy 's speed at crawling websites, so it is hard for me to discard it.

Can scrapy download pdf completely


node.js express maxAge setting

Question: If I set maxAge when I called sendFile(), just like below:
res.sendFile('public/index.html', {maxAge: 100000})
Does this mean the file 'public/index.html' will cached in server's memory for 100 seconds? Or, This is just a message send to client side, and server side's does nothing with memory cache?
Answer: maxAge is a directive for the Cache-Control header. In your case, this tells the client that index.html will be considered 'fresh' for 100000ms, so it is unnecessary to ask the server again for that file until maxAge has elapsed. The client keeps this file for the duration, it has nothing to do with server caching.
However, who caches what does depend on which way the directive is going.
As stated on MDN:

max-age=
Specifies the maximum amount of time a resource will be considered fresh. Contrary to Expires, this directive is relative to the time of the request.

And

The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses. Caching directives are unidirectional, meaning that a given directive in a request is not implying that the same directive is to be given in the response.
Comment: Thank you . That's very clear. And do you know what should I do if I want to make sure my server cache the file in memory.
Comment: @欧阳维杰, the file is already on your server and in it's memory. There would be no need to cache static assets on your server.
Comment: @欧阳维杰, you may want to look into serving your assets from a CDN if you do want some sort of intermediate caching based on the user's location.

node.js express maxAge setting


Is there a way to alter a node save confirmation form?

Question: I would like to add a confirmation form that is presented to a user on node update/delete operations triggered through the UI.
Specifically, I would like the form to present more than two choices for the user. i.e. instead of Submit/Cancel I would Option A/Option B/Option C.
Is this possible?
Answer: Yes. I was writing a huge response before regarding confirm_form in Drupal 7. I recommend you google and look at the examples module for confirm_form usage.
In short you probably want to:

use hook_form_alter to override the #submit or #action (or pre-pend to the list) the form will send to.
then you want to show a new form -- just like confirm_form does and do more logic in your custom confirmation form submit handler.
then redirect wherever using drupal_goto for instance or $form['#redirect'].

This may sound vague -- but the Form API can do all of this. You just have alot of learning to do. I have used confirm_form. I've made workflows for 8 Node forms to wizard through 8 Node add forms; and even made a "login" form fake login people from a login form completely overridden with a dropdown list of students on a Test server (to mock out users) using hook_form_alter().
Drupal can do all of this -- you just have to read the API and look at examples and experiment until you get what you want.
Comment: Thanks for pointing me in the right direction.  So, I basically need to create a similar function to confirm_form but with multiple choices?
Comment: essentially. also look at how confirm_form is linked into like the node delete operations in the default `node.module` file (or 1 of the `node` form files ... it might be node.pages.inc or something similar.
Comment: "Drupal can do all of this -- you just have to read the API and look at examples and experiment until you get what you want." This. Yes. A hundred times yes. And Form API is one of the deeper parts of the Drupal pool in my experience :)


UE-V 2.x Agent and support files, where do you get them from?

Question: Apologies for such a simple question, but I've been searching for how to download the UE-V 2.x client files for the last few days and all I can find is references on how to install them, nothing on where they are located to begin with.
I must be missing something basic, for anyone who has setup EU-V before can you fill me in on what I'm missing?
Answer: I followed a link for a beta version of the client and found that it was included in Microsoft Desktop Optimisation Pack. Apologies for wasting your time... and mine


How to get current language of node in twig?

Question: I have a tranlatable node, and I need to get its translation language. You know, I must to know is current node is in Polish or English. So... below there are my devel dumps from a node in Polish and a node in English. I see that there is a difference in #activeLangcode (x-default and en). But how to get that value? Or is there any better way to check current language of node in twig? Please help.
Comment: You can do this with a hook_preprocess_node. Take a look at this: https://stackoverflow.com/questions/37186520/drupal-8-and-twig-get-language
Answer: A more elegant Drupal way without typing code:

Go to the Manage display of the content type of your node (/admin/structure/types/manage/YOUR_CONTENT_TYPE/display/full). Select your display mode (or use the default). You will notice the field 'Language', move it to the upper side so it gets out of 'Disabled'.
Once you have done that, the language of the node is available in Twig. You can access the id this way {{ node.langcode.langcode }}; or you can access the whole Object to get other data like the name {{ drupal_dump(node.langcode) }}
Comment: This is the only simple way to get the langcode in <D 10. What a horrible desaster. Shouldn't be that complicated to get it right from the entity. However, thank you so much!
Comment: finally found an answer that fits Drupal 10. thanks


spring-data-mongodb requires a spring ApplicationEventMulticaster to have been initialised before it can be used

Question: I'm using shiro with spring data and mongodb in a web application.
 I've created a simple mongodb realm which is also configured via spring.
I'm also using the spring DelegatingFilterProxy along with ShiroFilterFactoryBean.
when I try to use it I invariably get:
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: Root WebApplicationContext
the problem is that spring-data-mongodb requires a spring ApplicationEventMulticaster to have been initialised before it can be used.
ShiroFilterFactoryBean is a beanPostProcessor, and as such, during initialisation, spring attempts to configure its realms(and hence my realm and spring data mongo based userDao).
it fails because ApplicationEventMulticaster has not yet been created.
the relevant part in spring.
org.springframework.context.support.AbstractApplicationContext.refresh()
<code>...
try {
     ...
     registerBeanPostProcessors(beanFactory);

     // Initialize message source for this context.
     InitMessageSource();

     // Initialize event multicaster for this context.
     initApplicationEventMulticaster();

     // Initialize other special beans in specific context subclasses.
     onRefresh();
     registerListeners();
     ...
    }
</code>
you can see that the BeanPostProcessors are setup before initApplicationEventMulticaster() is ever called.
so my query is, whats the best way to solve this? whats the sanest option?
Thanks
Comment: I put annotation lazy to my annotation Autowired service .and then it seem ok .
Answer: you said something about WebApplicationContext , so i suppose you have a *-servlet.xml that configures the specified controller . Spring allows you to have a global context that spring will be responsible to pass it to all other WebApplicationContexts , so you have to define the DAO logic that will be global in an applicationContext.xml and let spring do the others
Comment: where do you configure the DelegatingFilterProxy  and ShiroFilterFactoryBean beans? in which context.xml ? or you do it in a configuration class?
Comment: I put annotation lazy to my annotation Autowired service .and then it seem ok .


Running Connect-EXOPSSession in Powershell ISE

Question: I'm struggling to get our PowerShell script for creating mailboxes, assigning licenses etc. to work with MFA.
If I use this script, only;
$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName =  Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName =  Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]

I can use "Get-Mailbox"
However, when I run the same script as a function in our script;
function O365Logon
{

$modules = @(Get-ChildItem -Path "$($env:LOCALAPPDATA)\Apps\2.0" -Filter "Microsoft.Exchange.Management.ExoPowershellModule.manifest" -Recurse )
$moduleName =  Join-Path $modules[0].Directory.FullName "Microsoft.Exchange.Management.ExoPowershellModule.dll"
Import-Module -FullyQualifiedName $moduleName -Force
$scriptName =  Join-Path $modules[0].Directory.FullName "CreateExoPSSession.ps1"
. $scriptName
$null = Connect-EXOPSSession
$exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]

}

I do not get any errors, I can log in with MFA without any issues... but, Get-Mailbox is not recognized.
This is the only thing in our script that actually calls the O365Logon;
function Main
{
    write-host "`nThis script will create a mailbox in Office 365 for an AD user (with correct attributes), continue? (y/n)."

    $response = read-host
    if ($response.ToLower() -ne "y"){
        Quit
    }

    O365Logon

    return MainMenu
}

All help I can get is extremely appreciated.
Answer: function connect-365 {
    Import-Module CreateExoPsSession
    $null = Connect-EXOPSSession -ConnectionUri "https://ps.outlook.com/powershell/" | Out-Null
    $global:exchangeOnlineSession = (Get-PSSession | Where-Object { ($_.ConfigurationName -eq 'Microsoft.Exchange') -and ($_.State -eq 'Opened') })[0]
    Import-Module -AsCustomObject (Import-PSSession $exchangeOnlineSession -AllowClobber) -Global
}

Originally fixed here.
The code above will help you to wrap your code inside a function that correctly imports all the commands.
It uses "Global" parameter from Import-Module that can not only import modules but also import Custom Objects.
Answer: This is the correct code to successfully using Connect-EXOPsSession or Connect-IPPsSession in ISE console using MFA (even within VSCode, Powershell Manager, etc - be sure that your default shell is set to 5.1, do not use core 6, 7, etc):
$MFAExchangeModule = ((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter CreateExoPSSession.ps1 -Recurse).FullName | Select-Object -Last 1)

. "$MFAExchangeModule"

Connect-EXOPSSession -UserPrincipalName "insert UPN here"

You can then use Exchange Online cmdlets as you would normally (Get-Mailbox, etc).


Create a json document based on existing document with powershell 5.1

Question: I'm trying to build a json document which takes it's structure, column names and some values from an already existing base json document, and sets other values to variables assigned via powershell 5.1 in a foreach-object loop.
I tried editing a copy of the base json and creating a new one entirely and I can get close to what I want but not exactly.
A cut down example json document is:
[
    {
        "name": "Application1",
        "tags": ["monitor"],
        "description": "Get information about application 1.",
        "query":
        [
            {
                "target": {
                    "version": ["v1","v2","v3","v4","v5"],
                    "platform": ["Windows","Linux"]
                },
                "implementation": {
                    "action": "do something"
                }
            }
        ]
    },
    {
        "name": "Application2",
        "tags": ["monitor"],
        "description": "Get information about application 2",
        "query":
        [
            {
                "target": {
                    "version": ["v1","v2","v3"],
                    "platform": ["Windows"]
                },
                "implementation": {
                    "action": "do something for v1, v2 and v3"
                }
            },
            {
                "target": {
                    "version": ["v4","v5"],
                    "platform": ["Windows"]
                },
                "implementation": {
                    "action": "do something for v4 and v5"
                }
            }
        ]
    }
]

My script currently looks like:
$BaseJson = (Get-Content "$SourcePath\probes.json" | ConvertFrom-Json)

$ClientConfig = @"
{
    "targetHost": "$DigitalSymphonyHost",
    "targetPort": "$DigitalSymphonyPort",
    "source": "$TargetSqlInstance",
    "sensor": {}
}
"@

$ClientJson = ConvertFrom-Json -InputObject $ClientConfig

$BaseJson | Where-Object {$_.tags -contains "monitor"} | ForEach-Object {
    # For each sensor in the base-sensors json document
    $SensorName = $_.name
    $Severity = 2
    $Version = $_.query.target.version
    $Platform = $_.query.target.platform
    $Query = $_.query.implementation.action

    $Sensor =@"
                {
                    "severity": "$Severity",
                    "sensorId": "$SensorId",
                    "type": "$Type",
                    "target": {
                        "version": "$Version",
                        "platform": "$Platform",
                        "engineEdition": "$Edition"
                    },
                    "implementation": {
                        "query": "$Query"
                    }
                }
"@
    $ClientJson.sensor | Add-Member -Name $SensorName -Value (ConvertFrom-Json $Sensor) -MemberType NoteProperty
}

$ClientJson | ConvertTo-Json | Out-File "$DestinationPath\client-sensors.json"

My desired result is to add the $SensorId as per:
[
    {
        "targetHost":  "servername",
        "targetPort":  "port",
        "source":  "servername",
        "sensor":  [{
            "name": "Application1",
            "sensorId": "<value_from_$SensorId>",
            "tags": [],
            "description": "Get information about application 1.",
            "query":
            [
                {
                    "target": {
                        "version": ["v1","v2","v3","v4","v5"],
                        "platform": ["Windows","Linux"]
                    },
                    "implementation": {
                        "action": "do something"
                    }
                }
            ]
        },
        {
            "name": "Application2",
            "sensorId": "<value_from_$SensorId>",
            "tags": [],
            "description": "Get information about application 2",
            "query":
            [
                {
                    "target": {
                        "version": ["v1","v2","v3"],
                        "platform": ["Windows"]
                    },
                    "implementation": {
                        "action": "do something for v1, v2 and v3"
                    }
                },
                {
                    "target": {
                        "version": ["v4","v5"],
                        "platform": ["Windows"]
                    },
                    "implementation": {
                        "action": "do something for v4 and v5"
                    }
                }
            ]
        }]
    }
]

My current result is
{
    "targetHost":  "servername",
    "targetPort":  "port",
    "source":  "server",
    "sensor":  {
                   "applicationname1":  {
                                    "tags": ["monitor],
                                    "description": "Get information about application 2",
                                    "sensorId":  "420",
                                    "target":  "@{version=v1,v2,v3,v4,v5; platform=Windows Windows;}",
                                    "action":  "do something for v4 and v5"
                                }
                            }
}

Comment: What is it that this doesn't do that it's supposed to do? What's your desired output?
Comment: I've added some more detail to the question.
Answer: I'm not sure where you're getting sensorID, but I'd do this more like the below:
param(
    $SourcePath = $PSScriptRoot,
    $DestinationPath = $PSScriptRoot,
    $DigitalSymphonyHost = 'SomeHost',
    $DigitalSymphonyPort = '8765',
    $TargetSqlInstance
)

$BaseObj = (Get-Content "$SourcePath\probes.json" | ConvertFrom-Json)
$Client = [PSCustomObject] @{
    targetHost = $DigitalSymphonyHost
    targetPort = $DigitalSymphonyPort
    source = $TargetSqlInstance
    sensor = @()
}

$BaseObj | Where-Object {$_.tags -contains "monitor"} | ForEach-Object {
    $sensor = $_

    # TODO: How are you getting sensorId?
    $sensor | Add-Member -Name 'sensorId' -Value 777 -MemberType NoteProperty
    $Client.sensor += $sensor
}

ConvertTo-Json @($Client) -Depth 10 | Out-File "$DestinationPath\client-sensors.json" -Force

That will get you the JSON you're looking for.


If spark isn't a storage system, how do tables work?

Question: I am reading "Learning spark, 2nd edition" and I find two confusing parts:
"Unlike Hadoop, which included both storage and compute, Spark decouples the two. That means you can (...) read data from sources, (...) process it in memory"
Also on reddit and in courses I found info that spark doesn't store data, it processes it only.
Now I am reading the spark sql part and it says that I can create tables that will be managed by spark and will hold data (in contrast to views, which do not hold data). I have trouble understanding it.
"To enable you to query structured data (...) Spark manages (...) creating and managing views and tables, both in memory and on disk"
I tried googling and reading, but it only mentions that the data can be held in hdfs etc, but the examples in the book don't mention that. So I would like to clarify - let's say I install spark only, without separate hdfs, hive installation - can I create the tables and hold data in them? What will be the difference between those tables and dataframes? How are the tables stored? Metadata is stored in the catalog, but what about the actual data? If I take a csv, create a table with data from the csv, then delete the csv, can I still read the data? If so, then where is the data? And if not, then maybe tables do not hold data and are therefore no different than views?
Answer: Spark is a tool specifically designed for processing data, not storing it. In the context of Hadoop, it's more akin to MapReduce, the processing engine, rather than HDFS, the storage layer. In fact, Spark can operate on data stored in HDFS, effectively serving as a replacement for MapReduce in the Hadoop ecosystem.
Regarding tables, Spark creates a (temporary) view on top of the storage system, facilitating the processing of that data. When you utilize dataframes or SQL in your code, Spark translates this into its own internal format for processing. This is somewhat comparable to how Pandas reads data from a file system and lets you query the dataframe using SQL. Similarly, Spark reads data from a storage layer (like HDFS or S3), then generates an internal table/view based on your instructions.
There are two kinds of tables in Spark:

Global Temporary View: This type of table is accessible in all
sessions, but it disappears when the application is closed.

Local Temporary View: This table is only available in the current
session and disappears when that session ends.

You can also construct tables using Spark SQL, which are akin to tables in a standard database. This offers a familiar structure for querying and manipulating the data.
Answer: In simple words: Spark is just a data processing engine. It is used to read files from the local file system, HDFS, S3 or a database table, and write out an output to another or to the same destination.
The sentence you have found "creating and managing views and tables, both in memory and on disk" most likely refers to the fact that you can cache data during the process in memory and/or on disk. This is usually done to reuse data that you may need across multiple steps in your job, without creating any temporary output that you need to clean up manually later. Besides, using cached data it's much faster than reading a file or a table twice.


Question on Combinations

Question: There are $10$ points in a plane, no three of which are in the same straight line, except $4$ points, which are collinear. Find the number of straight lines obtained from the pairs of these points? Answer is $40$.  I didn't get it how, please someone help me.
Comment: There are 45 unordered pairs of points, but 6 of these pairs all define the 4-point line ...
Answer: If no points were collinear, the number of lines would be
$$\binom {10}2=45$$
Since four points are collinear, we count $\binom 42=6$ lines only as $1$, so we get
$$
45-6+1=40
$$
Comment: This method nicely extends to cases with multiple sets of collinear points. (The brute force approach I stated in my answer doesn't.)
Answer: Start with a line with four points on it. Adding a fifth point allows us to draw a line through each of the four collinear points. Adding a sixth point lets us draw straight lines through the collinear, but also through the fifth point. We repeat this up to the tenth point, and then count up all these lines to give the total.

Question on Combinations


How to express exhaustion without rallentando or diminuendo?

Question: I'm working on a track that has more of an EDM feel to it, so the obvious methods of expressing exhaustion—rallentando and diminuendo—do not fit the music I'm imagining, since I want it to keep a constant tempo and mostly steady average loudness.
What techniques can I use to make the music feel like it is worn-out near the end of the piece besides fading out or slowing down?
Comment: EDM stands for electronic dance music?
Answer: I am not sure what you mean exactly, but here are some suggestions:

There are terms like "morando", or "mancando", meaning dying/dying away. You could also literally write out "exhaustion" or the various derivatives and synonyms of that word (e.g. "expiring"). I suppose they're rather similar to rallentando/diminuendo so just a change in semantics/terminology is probably not what you're looking for.
Manipulate accents and note values. (1) More accented notes could give the feeling of more effort and energy (one might indicate "con forza" or simply with sforzandi and similar accents); and perhaps change it near the end, such as by playing notes with less effort, as if exhausted. (2) Hold notes for a longer time (one might indicate "tenuto") and then maybe change to staccato or half the note value near the end, which also helps with the feeling of less effort, of "expiring away".

P.S. In regards to note values, one specific way of doing it might be as follows. If you want to keep a constant tempo (rhythmic, on the beat) and perhaps even roughly equal volume, but still give some semblance of "running out of steam", one way I can think of is to assign increasing/decreasing note values. For example, like a train gaining steam and slowing down, one might use quarter notes first, then 8th notes, then triplet 8ths, then 16ths, 32nd, and trill, and then perhaps decrease the note value after the trill peak or simply return right away to longer note values like quarter notes, half notes, etc. near the end. I suppose usually such passages are still phrased with changes in tempo and volume, but I think even at a constant tempo or volume this can produce an effect of build-up and exhaustion. If you're looking for a specific example of such an instance, one that I particularly love is that of the 6th variation of the third movement of Beethoven's Sonata No. 30, Op. 109. At the end after the trill, he returns to the opening theme and it ends serenely. (If you haven't listened to this sonata before, I would personally recommend listening to it from the beginning and going all the way through, but it is quite long, like 20 minutes, and I understand if you may not have the time or interest.) When you say "exhaustion", I kind of assume there should be some sort of build-up or at least loudness and energy to exhaust from, but I might be wrong.
Sorry, I spoke essentially from a classical piano music perspective; I'm not sure if that is relevant or helps at all.
Answer: Another suggestion: gradually cut off higher frequencies. The sound gets more and more muffled.
Answer: Three other methods for making the music run out of steam:

More staccato notes will make the music sound like it needs to take a breath between each note.  This implies that the music previously was mostly comprised with connected notes.
Alter the melodic rhythm such that the notes sound like they no longer have the mental energy to play themselves at the correct time.  Extra care would be needed so the notes feel exhausted, rather than frantic.
Move to a higher register, as if the music can't take in enough air to support full baritone notes.  This one is most effective if you keep the same instrumentation, as moving to higher-pitched instruments at the same time will more likely sound like an instrumentation change for the brighter than an energy change.
Comment: Staccato as well as higher register will typically demand more action on the players side (I can only definitely say this for woodwinds), so both may point into the wrong direction. I see as only remaing choice to simplify the rhythm towards longer note values, as replacing triplets by quarters and the whole notes.
Comment: @guidot: well, for strings it is indeed rather the other way around: spiccato in high positions requires less action than tenuto in low registers. Still I'm not quite convinced that this would well express _exhaustion_.


jQuery: focusout to exclude some elements

Question: focusout on input field will trigger every time the specific input looses its focus.
But, I want to exclude some specific a tag from triggering that focusout function
Example:
<input type="text" id="name_input">
SAVE

Then the focusout function:
$("#name_input").focusout(function(e) {
  //do something here
});

Clicking on "#apply_name" also triggers focusout function of an input. How can I exclude that specific element ID from triggering it.
Note: I tried some tricks already posted on StackOverflow and none of them seams to work...
Comment: As far as I know there is no direct solution to this as the focusout will trigger before the click event... try the above hack
Comment: why don't you do this filtering in your callback?
Comment: try something like http://jsfiddle.net/arunpjohny/sVM74/1/
Comment: @ArunPJohny ha ok, you make question more clear now ;)
Comment: @A.Wolff I'm still doubtful whether it has to be an answer until OP clarifies whether that was the problem he was looking at
Answer: Another way of doing this is checking what your target id is
var evt;
document.onmousemove = function (e) {
    e = e || window.event;
    evt = e;
}
$("#name_input").focusout(function (e) {
    if (evt.target.id == "apply_name") {
        //apply_name clicked
    } else {
        //focus out and applyname not clicked
    }
});

DEMO
Answer: You can use "blur" - event.
$("#name_input").on("blur", function(e) {
    //your code
});

Comment: blur doesn't bubble, but seems strange this could be the issue because INPUT cannot contains any element
Answer: Solution from How to exclude Id from focusout
Added .hasClass which I also needed:
$('#id').focusout (function (e) {

    if (e.relatedTarget && $(e.relatedTarget).hasClass('dontFocusOut')) {
        return;
    }
    //do your thing

});


Orthogonal transformations and metric tensor: prove that $U^{T} g U=g$

Question: I'm attempting to prove that, if $U=(U^i_j)_{n \times n}$ is the matrix associated to an orthogonal transformation and $g=(g_{ij})_{n \times n}$ is the metric tensor, then
$$U^{T} g U=g \tag{1}$$

My attempt
Applying the condition that an orthogonal transformation must preserve the scalar product, $\big(U(\boldsymbol{x}), U(\boldsymbol{y})\big) = (\boldsymbol{x}, \boldsymbol{y})$, I have found that that
$$ \sum ^n _{i=1} \sum ^n _{j=1} \sum ^n _{k=1} \sum ^n _{l=1} g_{i j} U_{k}^{i} U_{l}^{j} x^{k} y^{l}= \sum ^n _{i=1}\sum ^n _{j=1} g_{i j} x^{i} y^{j} \tag{2}$$
Using the Einstein summation convention and changing in the RHS the indexes $i \rightarrow k, j \rightarrow l$
$$g_{i j} U_{k}^{i} U_{l}^{j} x^{k} y^{l} = g_{kl} x^{k} y^{l} \tag{3}$$
Since the terms $x^{k} y^{l}$ appears at both sides, if we could identify $g_{i j} U_{k}^{i} U_{l}^{j}=g_{k l}$, this would correspond to the matrix product $U^{T} g U=g$, and the proof would be complete.
But I'm stuck here, because I don't see how we can match parts of different sums... Could someone tell me how could it be justified from $(3)$ that $g_{i j} U_{k}^{i} U_{l}^{j}=g_{k l}$ (if it is actually possible to do this)?
Comment: This doesn't hold for just some $\boldsymbol x, \boldsymbol y$. It holds for *every* $\boldsymbol x, \boldsymbol y$.
Answer: @PaulSinclair is right. Suppose $x^k$ ($y^l$) is $0$ except for being $1$ when $k=m$ ($l=n$) in our coordinates, so $g_{ij}U^i_mU^j_n=g_{mn}$.
Comment: @user206148 The value of $g_{ij}U^i_mU^j_n$ doesn't depend on what you contract it with. The only way for $g_{ij}U^i_kU^j_lx^ky^l=g_{kl}x^ky^l$ to be true for all choices of $x,\,y$, including mine, is to have $g_{ij}U^i_kU^j_l=g_{kl}$.
Comment: Okay, but with this reasoning you only show that it is true for the particular case that $\boldsymbol{x}$ and $\boldsymbol{y}$ are vectors of the canonical base, doesn't you? $\boldsymbol{x}$ or $\boldsymbol{y} = (1,0,...,0), (0,1,...,0)$, etc.
Answer: As already said, the key is that the equality must be satisfied for any values of $\boldsymbol{x}=(x^1,...,x^n)$ and $\boldsymbol{y}=(y^1,...,y^n)$. Also notice that in the LHS, once the sum is performed over $i$ and $j$, there is a term that only depends on $k$ and $l$. This is clear by reordering equation $(3)$ as follows:
$$ \sum ^n _{k=1} \sum ^n _{l=1} x^{k} y^{l} \bigg(\sum ^n _{i=1} \sum ^n _{j=1} g_{i j} U_{k}^{i} U_{l}^{j} \bigg) = \sum ^n _{k=1}\sum ^n _{l=1} x^{k} y^{l}g_{kl} $$
$$ \sum ^n _{k=1} \sum ^n _{l=1} x^{k} y^{l} h_{kl} = \sum ^n _{k=1}\sum ^n _{l=1} x^{k} y^{l}g_{kl} $$
So $h_{kl}=g_{i j} U_{k}^{i} U_{l}^{j}=g_{kl}$.