Find solution of maximum of a function with conditions

Question:
\begin{equation} \label{eqgen}
\begin{aligned}
& \underset{x}{\text{maximize}}
& & F(x)\\
& \text{subject to}
& & x \geq 0, \\
& & &x + f(x)\leq d,
\end{aligned}
\end{equation}
  where $F(x), f(x)$ are continuous functions from $\mathbb{R}^+ \to \mathbb{R}^+$.

Here is my approach. Intuitively, $x+f(x)\leq d, x\geq 0$ is equivalent to $x \in [a_1, b_1] \cup [a_2, b_2]\cup \cdots \cup [a_{n}, b_{n}]$,
where $a_i\leq b_i$, $a_1$ is either 0 or solution of $x+f(x) = d$, $b_n$ is either d or solution of $x+f(x) = d$, and $a_i, b_i$ are solutions of $x+f(x) = d$ for other cases.
Hence, the optimization problem becomes
\begin{equation}
\begin{aligned}
& \underset{x}{\text{maximize}}
& & F(x)\\
& \text{subject to}
& & x \in  [a_1, b_1] \cup [a_2, b_2]\cup \cdots \cup [a_{n}, b_{n}].
\end{aligned}
\end{equation}
max $F(x)$ on $[a_i, b_i]$ is attained at either $a_i, b_i$ or maximum of $F(x)$ on $[a_i, b_i]$. We can conclude that the optimal solution is either solutions of $x+f(x) = d, or x=0, x= d$  or local maximum of function $F(x)$. This is also what I want to prove.
How can I formally write down the solution of the above approach? I wrote this and my professor does not accept my solution. Thank you in advance!
Comment: Have you tried: https://en.wikipedia.org/wiki/Karush%E2%80%93Kuhn%E2%80%93Tucker_conditions
Comment: Thanks for your hint. But it does not work
Comment: Can you tell me what you mean by it does not work? You would not find any closed form solutions in any case. Under differentiability, you would get first order conditions.
Answer: There are a lot of intuitive tricks that one can use if one knows exactly the values of $f(x),\  d$, but given it is not given, I will lay out a general way of Kuhn-Tucker conditions.
Write: $L=F(x)+\lambda_1 x-\lambda_2 (x+f(x)-d)  $ with $\lambda_1, \lambda_2 \geq0$
Think of the intuition behind the sign of the lambdas. If $x$ becomes negative that is bad for our purpose given the constraint, and that is getting reflected by our objective function getting a negative "shock" through the positive $\lambda_1$. You can reason through similarly for the other lambda.
Now, take derivative w.r.t $x$ to get:
$F'(x)-\lambda_1+\lambda_2(1+f'(x))=0$
Couple this equation with the constraints:
$\lambda_1 x=0$
$\lambda_2 (x+f(x)-d)=0$
And these conditions have complementary slackness.
Case 1: Suppose both constraints are binding, then $\lambda_1, \lambda_2>0$. And, $x=0$, and, $f(0)=d$. If, $f(0) \ne d$, we can immediately move on from this case to the second case.
Otherwise, plug in $x=0$ in the derivative condition, solve for lambdas and check if they are actually positive, otherwise, as before, we move on from this case. Check here:
$F'(0)-\lambda_1+\lambda_2(1+f'(0))=0$
Case 2: Constraint 1 binds and 2 does not.
So, $\lambda_1>0$ and $\lambda_2=0$ and $x=0$.
Check back into the derivative condition and check if the values match up with it and with the condition
$ f(0)\leq d $
Case 3: Constraint 2 binds and 1 does not.
So, $\lambda_1=0$ and $\lambda_2>0$ and $x=0$.
Solve for $x$ from $f(x)=d$, given we are not in case 1, $x$ should not be zero. Use that value of the solution to check back if $x\geq 0$ holds, and if the derivative condition is satisfied for positive $\lambda_2$.
Case 4: Both constraints bind
So, $\lambda_1=0$ and $\lambda_2=0$
Solve it like an unconstrained problem, find $x$ and check back to confirm that the inequality conditions are satisfied.
Finally at any case when you find a solution, you check for the second derivative of the maximand (Lagrangian function L) to be negative at the optimal point ($x^*, \lambda^*$). Also, if you found multiple solutions from the different cases, choose among them by plugging them directly into the actual function $F$ to see which one gives the maximum value.
Comment: $d$ is given and plays the role of constant.
Comment: I mean, do you know how $f(0)$ and $d$ are exactly related? Also does the above answer help you?
Comment: Many thanks. I am checking. If it is correct I will mark it.
Comment: I think it's correct and make it as completed answer. But how can we deal with the case when $F, f$ are continuous but not differentiable. From my picture I think it still holds.
Comment: Without differentiability, your best hope would be to take the same four cases as above, and find the optimal in each case by plotting+eye inspection or by logical reasoning.


Quando o mysql_fecth_assoc, não acha, ele mata o resto do codigo, como fazer ele retornar algo?

Question: Ola, estou com um problema nesta função ao deletar, faço uma busca do que será comparado, e coloco duas condição de delete, pois preciso que ele apague em uma condição duas tabelas, e se não houver nada na outra tabela, apague apenas uma, na minha simplicidade usei um ifporém a busca quando ela não acha nenhum resultado na tabela, ela não executa o restante.
será que podem me ajudar
function deleta_pessoa_pg($conexao,$id){
        $id_valor = "SELECT viva.* from viagens_valor as viva where viva.id_pessoa = '$id'";
        $resultado = mysqli_query($conexao, $id_valor) or die(mysqli_error($conexao));
        $listaitem = mysqli_fetch_assoc($resultado) or die(mysqli_error($conexao));
        //echo $listaitem['id_pessoa'];

    if (!$listaitem['id_pessoa']) {
                //echo 'fala';
                $query2 = "DELETE FROM viagens WHERE id = '$id'";
                return mysqli_query($conexao, $query2) or die(mysqli_error($conexao));
    }else{
                //echo 'oi';
                $query = "DELETE v.*, vv.* FROM viagens as v left JOIN viagens_valor as vv ON vv.id_pessoa = v.id WHERE v.id = '$id'";
                return mysqli_query($conexao, $query) or die(mysqli_error($conexao));

        }
    }

Comment: Antes de mais nada, a o "viva*" do primeiro select está sem o ponto. Tem outros problemas no código, mas para comentar deles, precisaria entender o que quer fazer exatamente. Se seu IF depende de quantidades, tem que analisar o num_rows (ou affected_rows em outros casos que geram mudanças), e não se o fetch trouxe a primeira
Comment: Ola, a questão do código 'ponto' eu ja corrigi, vou explicar desde o começo do processo, vamos la: eu cadastro uma pessoa que ira viajar em uma tabela, e depois cadastro os valores que ela vem pagando, mas para apagar esta pessoa eu também preciso apagar os valores pagos por esta pessoa. ai então eu terei que apagar em duas tabelas ao mesmo, tempo, até ai tudo bem, porém quando eu precisar apagar só uma este comando de delete para as duas tabelas, não apaga só uma, e pra isso eu criei a consulta e o IF, e comparo o valor da consulta com o $ID que vem na 'function'.
Answer: Conforme citado pelo @Bacco você precisa usar uma contagem de registros para fazer isso. Tente fazer assim:
if (mysqli_num_rows($resultado) == 0) {
                //echo 'fala';
                $query2 = "DELETE FROM viagens WHERE id = '$id'";
                return mysqli_query($conexao, $query2) or die(mysqli_error($conexao));
    }

No exemplo acima, ele diz : "Se o numero de registros for igual a 0", ou seja, quando não há registros... entre nesta condição.
Não esqueça de apagar esses or die(mysqli_error($conexao)) quando coloca-lo em produção.
Comment: valeu amigo @AndreiCoelho, mais uma vez vc me ajudou, obrigado mesmo, deu certo, não tinha pensado nesta situação, obrigado, deu certinho, acontecia que depois da $resultado, dava algum problema e ele não execultava o restante qndo não tinha valor, e desta forma, e ja faz a validação dentro do if, e dai de uma forma ou de outra ele  conclui. muito obrigado.
Comment: Tranquilo @JuniorVenancio!


The set of real numbers contained in the interval (0,1) is countable. Here is my proof.

Question: Here is the interval (0,1).
Step 1 : Divide the interval by half. Then we get cut 1/2.
Step 2 : Divede the intervals of Step 1 by half. Then we get cuts 1/4, 2/4, 3/4.
...................
In this way, we get infinite cuts contained in (0,1).
And we can correspond each cut to one of binary number.
Like 1/2 -> 0.1
     1/4 -> 0.01
     3/4 -> 0.11
       ......
In this way, the binary numbers in the interval (0,1) are in 1:1 correspondence
with the cuts constructed in above manner.
And we know the real numbers in (0,1) are in 1:1 correspondence with the
binary numbers in the same interval.
Thus, the cuts are in 1:1 correspondance with the real numbers.
And the number of cuts is countable, because it's the countable sum of countable numbers.
Thus, the set of real numbers in the interval (0,1) is countable set.
Is it right?
Comment: Is it right?
NO
Comment: And to what cut do you map $1/3$? In binary: $0.010101\ldots$
Comment: This method only produces real numbers with terminating binary expansions.  Most real numbers have non terminating expansions.
Comment: @PHilo: at each step, you only add numbers which, when written as a fraction in lowest terms, can be written with a power of 2 in the denominator. The number $1/3$ cannot be written that way, so it is never added as as cut. The problem with your proof is that you only get numbers with a *finite* binary expansion.
Comment: Once you allow "infinitely many steps" you are no longer concerned with *finite* binary strings, but with infinite strings. And there are uncountably many infinite binary strings.
Comment: I don't think that it is countable!
Comment: Wrong. See Cantor's iconic result. The set of real numbers in the interval $(0,1)$ is not countable. See [Theorem 3.1](http://www.msc.uky.edu/droyster/courses/fall06/PDFs/Chapter03.pdf).
Comment: Can you state the first number you could count?
Comment: We can map 1/3 to some binary number with infinite sequence.
Comment: See Rohan's comment
Comment: Why does it generate only terminating binary expansions? Steps are infinite, so infinite expension is possible.
Comment: @PHilo: there are an infinite number of steps, but at each step you only add particular numbers as cuts, and the numbers you add always have terminating binary expansions.   If you argue that $1/3$ is added, for example, you should be able to say the number of the step at which it is added...
Comment: @Mummert: Even though I can not say the number of step,  is it not possible there 'exist' such infinite steps?
Comment: @PHilo: every number that is added to your countable set is added at a particular step. (That is, a number ends up in the overall union of sets if and only if it is one of the sets that you are taking the union of).  Because the number $1/3$ is never added at any step, it is not in the overall countable set of numbers that you generate.   That is where the argument goes wrong - it assumes that more things are in the union than are actually there.  In the end, the only numbers actually in the countable set are the ones that have finite binary expansions.
Answer: The binary numbers are not in one-to-one correspondence with the cuts you describe; you only obtain the binary numbers which have a finite binary expansion (which, you correctly prove is a countable set). However, binary numbers with an infinite expansion, for example
$$\frac{1}{3} = 0.0101010101\ldots$$
or, importantly, any irrational number, will be missed by this enumeration.
Answer: The problem is that you can not build a one to one correspondence from binary numbers with infinite expressions to that set of cuts.


Reducing Predicate calls

Question: I have this predicate:
check_matrix([[_, E2, E3],
             [E4, E5, E6],
             [E7, E8, E9]]) :-
     E5 = E9,
     is_valid([E4, E5, E6]),
     is_valid([E7, E8, R9]),
     is_valid([E2, E5, E8]),
     is_valid([E3, E6, E9]).

Which checks if my 2 x 2 matrix is valid, with the top left cell, _ being completely ignored.The E2, E3, E4 and E7 cells are just headers for the 2 x 2 matrix.
Is their a nicer way I could is_valid(), whereby I don't need to call is_valid() 4 times. If the matrices get bigger, such as 5x5, this predicate would need to call is_valid() 10 times. Is this normal for Prolog? Or is their a more elegant way to do this?
I was thinking of just having another predicate which creates a nested list of of all the possible lists I want to pass to is_valid(), and call that predicate on each of the lists, then pass the result back to check_matrix(). I feel like their must be a more elegant way to do this. Any suggestions would be appreciated.
UPDATE
E5 = E9 is just a diagonal checker, which checks if the diagonals of a matrix are the same, if they are not, then the predicate fails.
Answer: If you want to avoid such hard-coded predicates you could try recursion:
You check for is_valid in each list (except first one) so you could write:
check_matrix([H|T]) :- is_valid_each_list(T),....

is_vqlid_each_list([]).
is_vqlid_each_list([L|T]):- length(L,3),is_valid(L),
                            is_vqlid_each_list(T).

Note that the restriction length(L,3) says the elements are lists of length 3 (or any parameter K if you wish, just add another parameter).
Now for checking E2,E5,E8 (elements in same position > 1):
is_valid_pos(L):-length(L,3), is_valid_pos(L,1).

is_valid_pos(_,4).
is_valid_pos(L,K):- findall(X,(member(L1,L),nth0(K,L1,X)),L2),
                    is_valid(L2),K1 is K+1, is_valid_pos(L,K1).

So the full general solution:
check_matrix([H|T]) :- is_valid_each_list(T), is_valid_pos([H|T]).

is_vqlid_each_list([]).
is_vqlid_each_list([L|T]):- length(L,3),is_valid(L),
                            is_vqlid_each_list(T).

is_valid_pos(L):-length(L,3), is_valid_pos(L,1).

is_valid_pos(_,4).
is_valid_pos(L,K):- findall(X,(member(L1,L),nth0(K,L1,X)),L2),
                    is_valid(L2),K1 is K+1, is_valid_pos(L,K1).

Answer: Prolog has some libraries like library(apply) that provide higher order predicates: predicates where you pass another predicate.
A higher-order predicate that seems applicable here is maplist/2. The predicate takes as arguments a predicate (:Goal) and a list (?List) and applies the :Goal on the elements of ?List until a predicate fails, or the list is exhausted. You can somewhat compare it to a function in an imperative language that iterates over the list, and checks if the predicate holds for all the members of the list. Like for instance all(..) in Python.
We can thus use the following approach to check all but the first row:
:- use_module(library(apply)).

check_rows([_|T]) :-
    maplist(is_valid,T).

We however have to check all but the first column as well. Therefore we can use the transpose/2 predicate. It takes two arguments and the second argument is the transpose of the first one. So:
?- transpose([[1, 2, 3],
|             [4, 5, 6],
|             [7, 8, 9]],X).
X = [[1, 4, 7], [2, 5, 8], [3, 6, 9]].

So we can transpose the matrix, and then again use maplist/2 to check all-but-the first rows of the transpose of the matrix M. So we can now implement the predicate like:
:- use_module(library(apply)).
:- use_module(library(clpfd)).

check_matrix(M) :-
    M = [_|MT],
    maplist(is_valid,MT),
    transpose(M,[_|TT]),
    maplist(is_valid,TT).

EDIT:
In case you want to check if all but the first diagonal elements, I propose to define two additional predicates:

all_same/1 that checks if all the elements of the list are the same; and
diagonal/2 that constructs a list of diagonal elements.

all_same/1 can be implemented as:
all_same([]).
all_same([_]).
all_same([H,H|T]) :-
    all_same([H|T]).

and diagonal/2 can be obtained with:
diagonal(M,L) :-
    diagonal(M,0,L).

diagonal([],_,[]).
diagonal([R|RT],I,[D|DT]) :-
    nth0(I,R,D),
    I1 is I+1,
    diagonal(RT,I1,DT).

Now we can check the diagonal as follows:
check_matrix(M) :-
    M = [_|MT],
    maplist(is_valid,MT),
    transpose(M,[_|TT]),
    maplist(is_valid,TT),
    diagonal(M,[_|DT]),
    all_same(DT).

Reducing Predicate calls


WCF binding I not understanding what is wrong

Question: I started coding this in vs 2013. I have two endpoints (besides the Mex) - I then wanted to download a lot more data for the last endpoint. So - I added to my config - My own http binding > BHBinding with (sorry for the flaling) 2147483647 everywhere - but my client seams to be getting the old error 65536 not big enough. "The maximum message size quota for incoming messages (65536) has been exceeded. "
I have not delt with WCF too much - but was able in the past to get it to work, not sure what I am not seeing or understanding. Thanks and Regards.
here is my config:
 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BHBinding" allowCookies="true" maxBufferPoolSize="2147483647"
          maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" >
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxNameTableCharCount="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647"/>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="FTPServiceLibrary.FTPService">
        <endpoint address="" binding="basicHttpBinding" bindingName=""
          contract="FTPServiceLibrary.IFTPError">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint address="/FTPSettingAddress" binding="basicHttpBinding"
          bindingConfiguration="BHBinding" bindingName="" contract="FTPServiceLibrary.IFTPSetting" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/FTPServiceLibrary/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />

          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Comment: Did you adjust the client's app.config file?
Comment: No, I hoped that update would do the trick for the client side. I guess I would change the client bindingConfiguration?
Comment: Ok, poked around some more - I guess my thoughts on how the client and server fit together needs to change. I did get the wcf connection to work by putting more configuration information that was already in the server config into the client config. Thanks
Answer: Thanks to Tung to pointing me in the right direction- I will have to now further learn not just about the server side but how server and client have different needs.  Here is my updated client side:

<bindings>
            <basicHttpBinding>

              <binding name="BHBinding_IFTPSetting" allowCookies="true" maxBufferPoolSize="2147483647"
          maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" >
                <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxNameTableCharCount="2147483647"
                  maxArrayLength="2147483647" maxBytesPerRead="2147483647"/>
              </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://server.company.com/EI.SecureFTP/FTPServiceLibrary.FTPService.svc/FTPSettingAddress"
                binding="basicHttpBinding" bindingConfiguration="BHBinding_IFTPSetting"
                contract="ftpservice.IFTPSetting" name="BHBinding_IFTPSetting" />
        </client>


Generate random values using an empirical cumulative distribution function

Question: I have a set of data points that I have used to generate my empirical CDF which looks like this (to simplify things I have reduced the number of points for this question but it shouldn't matter):

Given this data and plot I need to somehow generate random values which follow this distribution. I admit that I am quite rusty when it comes to probability, but as far as I understand I need to first generate the PDF and then from there I can do what I want. Is that accurate? Or what is the best way to get what I want?
If interested in giving an example for the answer, here are the X and Y array for the data points of the ECDF plot. I am using Python but I guess a language agnostic answer would also be really helpful
x = [107.6697676209896, 430.70331251794784, 1975.0646306785532, 7793.524079329409, 27569.66699567533, 62566.73646946178, 222847.1449910263, 832591.8949493016, 2827054.7454871265, 10000733.572934577]

y = [0, 0.04812202549318534, 0.09825964339269633, 0.14190143419466905, 0.27204351414405636, 0.46590411495145756, 0.6008552899988212, 0.6796719668120879, 0.8400864397710662, 1]

Comment: Shouldn't the first point on the cdf be 0,0?
Comment: @kpie I don't think it is strictly necessary for the x values (could be wrong though). Anyway you can imagine adding a first point at (0,0), I don't think it would change much about the question though.
Comment: Does this answer your question? [Generate random values using an empirical cumulative distribution function](https://stats.stackexchange.com/questions/561409/generate-random-values-using-an-empirical-cumulative-distribution-function)
Answer: The standard "graphical" answer is that you pick a random number between 0.0 and 1.1, and find that on your y axis.  Move right until you hit your graph.  Then move down to the x axis and that's the value.
If your graph really is just an empirical graph, then the second step is a binary search through a monotonically increasingly list.  The index of that found value (or the closest value) then is mapped to an x value, which is the value you want to produce.


Python : Broke phone troubleshooting app

Question: So I'm trying to make a simple troubleshooting app for broken phones and solutions to the problems, about 5 minutes in and I can't get passed line 4 when the user enters the phone model, it just stops instead of continuing the dialogue. I have no clue why. Any help is much appreciated. Also on line 6, I'd love to make it so that when the user writes a line of text about whats wrong with the phone, it picks out words like "broke" "cracked" "wet" etc. but I have no clue how so again, any help is much appreciated!
brand = raw_input("Please state your phone brand. ")
if brand.lower() == ("iphone"):
    iphone = raw_input ("Please state the model. ")
    if iphone.lower() == ("2G"):
        iproblem2g = raw_input ("Please state your problem. ")
        if iproblem2g.lower() == ("broken") or ("broke"):
            ibroke = raw_input ("Is the hardware broken or the software? ")
            if ibroke.lower() == ("hardware") or ("hard"):
                print ("...")

Comment: You need to indent line 2.
Comment: @pp_ I think you mean line 3.  To my experience text editors don't start at 0. :)
Comment: Although this question has a relation with another question, it is not a duplicate, but a super-set.
This code has many problems that can be addressed.
Answer: There were a few issues.  Try this:
brand = raw_input("Please state your phone brand. ")
if brand.lower() == ("iphone"):
    iphone = raw_input ("Please state the model. ")
    if iphone.lower() == ("2g"):
        iproblem2g = raw_input ("Please state your problem. ")
        if iproblem2g.lower() in ("broken", "broke"):
            ibroke = raw_input ("Is the hardware broken or the software? ")
            if ibroke.lower() in ("hardware", "hard"):
                print ("...")

Indentation is very important in Python.  Also, "2G" would have never have matched an input value since the input values are being lowercased.  Finally, matching multiple values is simpler in a list or tuple and wouldn't have worked the way it was (it would have always returned True).
Why always True?  Take for example if iproblem2g.lower() == ("broken") or ("broke"):.  This is checking iproblem2g.lower() == ("broken") OR ("broke").  ("broke") will always be True since it is considered a Truthy value in Python thus the entire conditional will always be True.
Cheers!
Answer: This line is probably semantically wrong (i.e., it compiles but does not work as you expect):
iproblem2g.lower() == ("broken") or ("broke")

You are actually asking if
(iproblem2g.lower() == ("broken")) or ("broke")

that means
(iproblem2g.lower() == ("broken")) or True

And this will always be True
You can´t check if something is "==" two things in this way. If you want to do this kind of question (is a == b or is a == c) you should use either:
 (iproblem2g.lower() == ("broken")) or  (iproblem2g.lower() ==  ("broke"))

or, more Pythonesc
iproblem2g.lower() in ["broken","broke"]

However, even this question is not a good choice, because it will only match the perfect word "broken" or "broke", and not texts that contain it. It will probably be much better to ask:
   "broken" in iproblem2g.lower() or "broke" in iproblem2g.lower()

In this way you will find the word in any sentence.
You can make it better by creating a simple function that checks if any of a set of words is in a sentence:
def check(words,sentence):
    for word in words:
        if word in sentence:
            return True
    return False

You can even do some pre-processing using .lower() and .trim()
Finally, there is a kind of small trick that you didn´t notice: if "broken" is the string, "broke" always will be (since it is a substring), so you do not need to check for both.
Comment: He didn't notice because he wasn't looking for it.  He was looking at case-insensitive equality.


Parsing through the path of an URL

Question: I am working with data from tweeter and I am trying to obtain the domain plus the first element in the path after .com. For example, I have an URL :
'https://www.facebook.com/estebanfarfanr/videos/1281699612020348/'
In this case all my dataset has Facebook as the domain. What I need is to have the URL link that contains the domain plus in this case, the user id. Therefore I need to be left with:
https://www.facebook.com/estebanfarfanr/
and get rid of the rest of the URL. I have not found a simple way to do it yet, if anyone has an advice.
Comment: use regular expression to parse the string as per your requirements.
Answer: To manipulate urls in python, you can use the urllib.parse module.
from urllib.parse import urlsplit, urlunsplit

def first_level_url(url: str) -> str:
    """Return the url trimmed after the first path level."""
    split_url = urlsplit(url)
    path_parts = split_url.path.split("/")  # The first level of the path

    # Edit the path, trimming everything after the first level
    # beyond root, eg: /first/second/third/ -> /first/
    new_path = "/".join(path_parts[:2]) + "/"

    new_url = split_url._replace(
       # Replace the original path with our edit
        path=new_path,
        # Remove the query string if it exists -- ?example=query
        query="",
        # Remove the fragment if it exists -- #example:fragment
        fragment="",
    )

    # Put the url back together with changes
    return urlunsplit(new_url)

Here's how it works with your example url:
>>> first_level_url(
        "https://www.facebook.com/estebanfarfanr/videos/1281699612020348/"
    )
'https://www.facebook.com/estebanfarfanr/'

>>> first_level_url("https://stackoverflow.com/questions/68093654/")
'https://stackoverflow.com/questions/'

# It will remove query strings and fragments too
>>> first_level_url(
        "https://github.com/blackrobot/foo/bar/?hello=world#lorem:ipsum"
    )
'https://github.com/blackrobot/'

Comment: why do we need to return urlunsplit(new_url) instead of just new_url.  ?
Comment: Just so you now I used it and it worked with the exception of some urls in my list that still output something like this:                            "https://www.facebook.com/1133685943410489/?sfnsn=mo&d=n&vh=e"                              I saw all of them in the list and it seems like all the link that did not get trimmed  s how the same pattern of /? after the id. But overall the function, with my question above , I am just trying to understand the reason why we need to use unsplit to return the object.
Comment: @Wara I've updated the `first_level_url` function to drop query strings (and fragments too). The function uses `urlunsplit(...)` because the value returned by `urlsplit(...)` is a `tuple`, and not a `str`. Try playing with `urlsplit(...)` in the python terminal and you'll understand.

Parsing through the path of an URL


On integer solutions of $3(u-v)(u+v)(3u+v) = (1+2u)(1-2u+4u^2)$

Question: How to solve the Diophantine equation $3(u-v)(u+v)(3u+v) = (1+2u)(1-2u+4u^2)$ over integers?
PS. I saw it here on AoPS, but could not solve it and no one has answered there.
Answer: Your equation is equivalent to ${u}^{3}+3\,{u}^{2}v-9\,{v}^{2}u-3\,{v}^{3}-1=0$ which is birationally equivalent to the elliptic curve ${y}^{2}+y={x}^{3}+20$ which is rank 1 over the rationals.  The obvious point (u,v) = (1,0) corresponds to the group identity, but the point (u,v) = (-2,-1) corresponds to the point (x,y) = (9/4, 41/8) which has infinite order.
The torsion group has order 3.
This says nothing about integer points but still may be useful.  For example, you can generate points at will on the elliptic curve and move them back to your curve to see if they are integral. I strongly suspect there are only the two integral points mentioned above.
Comment: To learn how to derive all the facts in Rita's 1st paragraph, you need several hours of lectures on (or reading about) elliptic curves. This is one very clever dog.
Comment: So shall we assume it cannot be solved without elliptic curves?
Answer: Here's a start:
Simplifying both sides, we get, $u^2 (u+3v)=1+9uv^2+3v^3$. Writing the RHS as a factor of $(u+3v)$, $u^2(u+3v)=1+9v^2(u+3v)-24v^3$. This yields:
$$(u+3v)^2(u-3v)=(1-24v^3)$$
and then make the substitutions $a=u+3v$, $b=u-3v$ to obtain $(a-b)^3=9(1-a^2b)$.
This means that $(1-a^2b)=3k^3$ for some $k \in \mathbb{Z}$. Multiplying $a-b=3k$ by $-a^2$ and using the previous equation, we get: $$3(k-a)k(k+a)=(1-a^3)$$
Comment: Yes. This seemingly innocuous problem seems to have connections to elliptic curves (Mordell's Theorem), and I too am beginning to suspect that an elementary solution can't be found. For instance, using arithmetic modulo $m$ helps only if you want to prove non-existence of solutions to a diophantine equation; but we know this equation has at least two solutions in integers.
Comment: @quantumelixir, yes see http://math.stackexchange.com/questions/30659/strange-cubic-diophantine-equations and http://math.stackexchange.com/questions/30518/where-is-the-mistake-in-this-incorrect-proof-in-eisenstein-integers if you are curious where this equation comes from.
Comment: @quantumelixir, Brilliant! Thanks for your solution!
Comment: Thanks. Glad it helped! :)
Comment: I got stuck trying to reconstruct the Cardano part: Somehow I use the fact that $a^6+\tfrac{7}{3}a^3+1$ is a square? (of course that implies $3|a$ but not sure what to do next) and that $\tfrac{1}{3}(\frac{a^3-1}{2}-\sqrt{\text{the square}})$ is a cube?
Comment: I second quanta's query about use of Cardano. While I haven't done the calculations, I'd be surprised if Cardano didn't lead to some diophantine equation at least as hard as the cubic in $k$ and $a$.
Comment: Thanks for the solution! :)
Comment: @quanta, @gerry-myerson: My bad; It looks like I paid the price for a hand-waving argument. You are right in pointing out that the diophantine equations that arise out of the Cardano method are much harder than the original cubic itself. I *presumed* that they could be solved by Cardano's method, as I was able to find the solutions on my computer.
Comment: @amir-hossein: Treat this question as unsolved, until we are able to find a solution that circumvents the problem of finding the roots of a cubic. I suggest you remove the "accepted" status of my answer; I'm reverting my answer to the previous edit. I'm sure there is a better way to solving this that I'm not able to see right away.
Comment: Cubic diophantine equations in two variables are hard; there are techniques that generally work, but they are not easy to master (and I have not mastered them myself). If no suitable answer is posted here, you might try asking for pointers on MathOverflow.
Comment: @quanta: I think the solution to this question could use ideas from the links you have provided. Thanks for pointing it out. Will take a look at them in detail a little later.


Prove about big O

Question: I need to prove that:
for every $$d>0,\ \epsilon>0,\ n^d=O((1+\epsilon)^n)$$
I'm trying to use the definition:
$$n^d\leq c*(1+\epsilon)^n$$
but I don't know how to continue, maybe it is wrong.
Thank you for the help!
Answer: You need to show that there is some $M>0$ and some $N>0$ such that $\forall n\geq N$, $n^d\leq M(1+\epsilon)^n$. Pick $M=1$ and let's find $N$:
$$
n^d\leq(1+\epsilon)^n\iff d\log n\leq n\log(1+\epsilon)\iff\frac{\log n}{n}\leq\frac{\log(1+\epsilon)}{d}.
$$
Consider the last inequality. At $n=e$, the LHS evaluates to $\frac{1}{e}$ and for $n\geq e$, the LHS is decreasing. In fact, you can verify (e.g. with L'Hopital's Rule) that $(\log n)/n\to 0$ as $n\to\infty$. Thus, given that $d^{-1}\log(1+\epsilon)>0$, there is some $N\geq e$ so large that for all $n\geq N$, we have $(\log n)/n\leq d^{-1}\log(1+\epsilon)$.
Comment: I understand you until you choose e as $$n_0$$. Why e? $$log_2e$$ is not 1. I don't understand this.. Thank you!
Comment: @user3479031 $\log$ without a base is logarithm with base $e$. I didn't select $N$ to be $e$; $e$ was merely imposed as a lower bound because the function $(\log n)/n$ decreases for $n\geq e$. You can check this last fact by taking the derivative.

Prove about big O


Dupla consulta em 1 query

Question: tenho 2 tabelas uma de usuarios e outra de inscritos, tenho um insert que envia para a tabela de inscritos o email do usuario que cadastrou e o nome do canal
1ª usuarios
id | usuario  | meucanal
1  | firstname.lastname@example.com  | portados fundos
2  | firstname.lastname@example.com  | parafernalha
3  | email@example.com  | Ei nerd
4  | firstname.lastname@example.com  | Canal Nostalgia

2ª inscricoes
id | usuario | se_inscreveu_no_canal
1  | firstname.lastname@example.com   | Ei nerd
2  | email@example.com   | parafernalha

Resumindo o que preciso é fazer um SELECT liste somente os canais que o usuario ainda nao se inscreveu no caso do exemplo o email@example.com precisa ser exibido somente o Canal Nostalgia
$sql = "SELECT * FROM usuarios WHERE canal NOT EXIST IN(SELECT * FROM inscricoes WHERE o_usuario NOT IN se_inscreveu_no_canal) ORDER BY RAND();

Não sou profissional da area, apenas programo como hobby porem coisas mais simplre esse tipo de consulta é mais avançada se puderem me auxiliar em um caminho pra solução desde ja agradeço.
Segue um pouco da estrutura para melhor entendimento(bem resumida)
cadastro.php

 insira seu email e o nome do seu canal

INSERT INTO usuarios (usuario, canal) VALUES ('$_POST[email]',$_POST[canal]')";
CADASTRO PRONTO
listarcanais.php ( o usuario nesta parte ja estara logado $user=session[email])
SELECT * FROM usuarios ORDER BY canal LIMIT 1";
echo " CLIQUE AQUI PARA SE ISNCREVER ";
inscrever.php ( o usuario nesta parte ja estara logado $user=session[email])
$canal= $GET[canal];
INSERT INTO inscricoes (usuario, canal) VALUES ('$user,$canal')";
//redirect to listarcanais.php
PRONTO JA CONSTA NO BD QUE O USUARIO JA INSCREVEU NAQUELE CANAL
listarcanais.php <<< como na minha cabeça acha que o sistema deveria ler
SELECIONE canal FROM (tabela usuarios) ONDE  o (usuario=$user) NÂO se inscreveu ( na tabela inscricoes)
no grosso modo: 1° um select que pega o (email/$user) e compara as tabelas USUARIOS e INSCRICOES afim de eliminar os outros email que nao é o do $user
feito isso 2° um select que lista todos os canais da tabela USUARIOS e compara com todos os canais da tabela INSCRICOES . Os que não forem iguais serão listado
Comment: A estrutura do seu banco pode (acredito que deve) ser melhorada, sugiro uma tabela usuario e outra canal, numa relação N:N em que a terceira tabela gerada contenha o id do usuario e o id dos canais que ele está inscrito
Answer: SELECT
    distinct(canal)
FROM
    usuarios
WHERE
    email <> '$email'
    AND
    canal NOT IN (
        SELECT canal FROM inscricoes WHERE email = '$email'

   )

Answer: Só existe apenas essas duas tabelas, não existe uma tabela de canal? Como é registrado todos os canais existentes?
Havendo três tabelas: Usuarios, Canais e Inscricoes, onde Inscricoes é relacionamento de muitos-para-muitos entre Usuarios e Canais, então assim basta trazer os Canais que não existem em Inscricoes por Usuarios.
select u.nome as usuario, c.nome as canal
from usuarios u, canais c
where not exists (select 1 from inscricoes i where i.cod_canal =c.cod_canal and i.cod_usuario = u.cod_usuario);

Comment: entao todos os canais registrados estao na tabela usuario cada usuario so pode ter 1 canal, na hora que o usuario vai cadastrar no sistema ele ja informa o nome do canal basicamente so pego o email e o nome do canal
Comment: Sugiro que faça como foi sugerido pelo Guilherme, faça uma relação de muitos para muitos, assim vai ficar normalizado, mais simples para a manutenção e realização de consultas

Dupla consulta em 1 query


Do high ranked players need wild card to play a Challenger tournament?

Question: I have noticed that Benoit Paire plays this week challenger tournament in Sophia Antipolis and he is listed among players who received wildcard. Paire is currently No. 40 in ATP rankings.
Does a player from top 50 need a wildcard in order to play in a challenger tournament? Are there some other restrictions which could prevent players which are high in ATP rankings from playing ATP Challenger Tour?
Comment: I have encountered the claim about top 50 players needing wild card in an internet discussion - although it seemed a bit suspicious to me. Then it occurred to me that the reason might be late entry - I have asked a [separate question](http://sports.stackexchange.com/q/15886), which mentions also cases where this happened in higher level tournaments (ATP World Tour, WTA Tour). Sorry for posting two questions which are rather similar so close after each other.
Comment: See your other question where I provided a comment on it - since it seems you were already familiar with the reason why this happens.
Comment: @jamauss I found some rule in the ATP Rulebook which seems to confirm it. Still, I am able to find a few cases where top 50 players played in challenger tournaments without wild cards.
Comment: Interesting. I'm not sure if those rules are strictly enforced or not and I also wonder if the tournament directors are given discretion to allow certain players to participate in challengers. It seems as though you've found some exceptions to the rule(s).
Answer: After checking 2017 ATP Rulebook
 (Internet Archive) I found out that there are indeed restrictions for high ranked players which disallow them playing at challenger level without receiving a wild card.
Here is quote of the relevant section:

7.07 Play-Up Regulation (ATP Challenger Tour Tournaments)
A. Restrictions
1) Players positioned 1-10 in the Emirates ATP Rankings twenty-one (21) days prior to the first Monday of the ATP Challenger Tour tournament are prohibited from entering, accepting a wild card and/or competing in an ATP Challenger Tour tournament.
  Players who would have been, had they and all other players entered, a direct acceptance on the original acceptance list for a Grand Slam are prohibited from entering, accepting a wild card and/or competing in an ATP Challenger Tour tournament in the first week of the Grand Slam tournament.
2) Players positioned 11-50 in the Emirates ATP Rankings twenty-one (21) days
  prior to the first Monday of the ATP Challenger Tour tournament are prohibited
  from entering but may receive an ATP-approved wild card in order to compete
  in the Challenger tournament. The Challenger Supervisor will make wild card
  determinations consistent with the limitations outlined below. Players positioned 11-50 are also prohibited from entering, accepting a wild card or competing in ATP Challenger Tour tournaments that offer less than $75,000/€64,000 in on-site prize money plus Hospitality.
3) ATP Challenger Tour tournaments scheduled the same week as an ATP World
  Tour tournament may offer wild cards to players positioned 11-50 in the Emirates
  ATP Rankings who have received approval from the ATP on-site Supervisor according to the following breakdown:
  $150,000/€127,000 in prize money up to (2) two wild cards
  $125,000/€106,000 in prize money up to (2) one wild card
  $100,000/€85,000 in prize money up to (1) one wild card
  $75,000/€64,000 in prize money plus Hospitality up to (1) one wild card
  $75,000*/€64,000* in prize money no wild card
  *Plus Hospitality
4) ATP Challenger Tour tournaments not scheduled in the same week as ATP World
  Tour tournaments, or during the 2nd week of a Grand Slam, ATP World Tour Masters 1000 Miami and Indian Wells, may offer wild cards to players positioned 11-50 in the Emirates ATP Rankings who have received approval from the ATP on-site Supervisor according to the following breakdown:
  $150,000/€127,000 in prize money up to (4) four wild cards
  $125,000/€106,000 in prize money up to (4) four wild cards
  $100,000/€85,000 in prize money up to (3) three wild cards
  $75,000/€64,000 in prize money plus Hospitality up to (2) two wild cards
  $75,000*/€64,000*in prize money no wild card
  *Plus Hospitality

I will ad also link to this blog post from 2015, which analyses success rate of Top 50 players in challengers: Benoit Paire and Overqualified Challenger Contenders.
Comment: Still if I look at [2017 Irving Tennis Classic](https://en.wikipedia.org/wiki/2017_Irving_Tennis_Classic), both Lorenzi and Granollers are listed in the draw [without wild card](http://www.protennislive.com/posting/2017/6486/mds.pdf).
And both [Lorenzi](http://www.atpworldtour.com/en/players/paolo-lorenzi/l503/rankings-history)
([Internet Archive](http://web.archive.org/web/20170411001531/http://www.atpworldtour.com/en/players/paolo-lorenzi/l503/rankings-history))
and ...
Comment: ... [Granollers](http://www.atpworldtour.com/en/players/marcel-granollers/g710/rankings-history)
([Internet Archive](http://web.archive.org/web/20170411001539/http://www.atpworldtour.com/en/players/marcel-granollers/g710/rankings-history))
have been in Top 50 for some time. This challenger was played during the second week of Indian Wells masters, so it should fall under part 4 of this rule.


How to dynamically populate an HTML Table with XML Data?

Question: I have an XML like below:
<?xml version="1.0" encoding="utf-8"?>
<TestRun id="abc-jhhg-yuh" name="Demo" runUser="Admin" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
 <ResultSummary outcome="Failed">
    <Counters total="5" executed="5" passed="3" failed="2" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
  </ResultSummary>
</TestRun>

I have an HTML file, where I want this data to be populated dynamically in a tabular format something like below.

My "script" in HTML goes like this
<!DOCTYPE html>

 Test Summary Details

But I am not able to populate the data. How can I do this?
Answer:

// Simulate a http response
const mock_response = new Response(`<?xml version="1.0" encoding="utf-8"?>
<TestRun id="abc-jhhg-yuh" name="Demo" runUser="Admin" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
 <ResultSummary outcome="Failed">
    <Counters total="5" executed="5" passed="3" failed="2" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
  </ResultSummary>
</TestRun>`)

// use the new fetch api instead of the old XMLHttpRequest
// TODO: change url to 'Demo.xml'
fetch('https://httpbin.org/get').then(async response => {
  // use the simulated response
  const str = await mock_response.text()

  // TODO: remove above and switch to use the actual response.text() instead
  // const str = await response.text()

  const xml = new DOMParser().parseFromString(str, 'text/xml')
  const counters = xml.querySelector('Counters')

  const table = document.createElement('table')
  table.border = 1
  const body = table.createTBody()
  for (let x of 'total,executed,passed,failed,inconclusive,warning,notExecuted'.split(',')) {
    const row = body.insertRow()
    const attr = row.insertCell()
    const value = row.insertCell()
    attr.innerText = x
    value.innerText = counters.getAttribute(x)
    console.log()
  }
  document.body.append(table)
})
Comment: Thanks for the solution. The above code works fine for static xml but the values (total,executed,passed,failed,inconclusive,warning,notExecuted) in XML changes for me. So is there any way to read the XML dynamically while you are simulating the http response.
Comment: change `const str = isDemo ? await res.text() : await response.text()` to just `const str = await response.text()` and also change `https://httpbin.org/get` to your own url `Demo.xml`
Comment: Thanks. Shall I do
fetch('C:\Users\Downloads\Demo.xml')?  Like this I was not able to get the value. I used fetch('Demo.xml') as well. But is didn't work. Am I missing anything?
Comment: You would have to put up a server to respond with the file. Browser don't have any access to clients filesystem (unless you request permission for it using the new experimental native file system access) Then you are not even using ajax (XMLHttpRequest or fetch api) any more then you are reading a local file instead
Comment: Yes, I want to read a local XML file then populate the data in HTML. Is it possible?
Comment: lets chat instead: https://chat.stackexchange.com/rooms/124918/readxml
Comment: I have pinged you in the chat.


Foreach - verificar e gravar somente os que não estão no BD

Question: Dentro de um foreach preciso fazer um insert no meu banco de dados somente dos registros que não estiverem gravados, evitando assim duplicidade de registros, mas não consegui imaginar uma solução para verificar se um registro está gravado no banco e se estiver ignorar e verificar o próximo, o que tenho é isso:
<code>    // Recebendo o array com os ID´S
$checkboxes = $_POST['check'];

// laço para buscar ID´s e efetuar cadastro
foreach($checkboxes as $IdColaborador) {

    $IdTreinamento = $_POST['IdTreinamento'];

    // Verificando se Treinamento e Colaborador já existe
    mysql_select_db($database_conexao, $conexao);
    $query_rsVerifica = "SELECT * FROM treParticipantesTreinamento WHERE IdTreinamento = $IdTreinamento AND IdColaborador = $IdColaborador";
    $rsVerifica = mysql_query($query_rsVerifica, $conexao) or die(mysql_error());
    $row_rsVerifica = mysql_fetch_assoc($rsVerifica);
    $totalRows_rsVerifica = mysql_num_rows($rsVerifica);

    // Buscando dados do Colaborador
    mysql_select_db($database_conexao, $conexao);
    $query_rsRegistro = "SELECT * FROM comColaborador WHERE IdColaborador = $IdColaborador AND ativo = 1";
    $rsRegistro = mysql_query($query_rsRegistro, $conexao) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($row_rsRegistro);

    // Buscando dados do Treinamento
    mysql_select_db($database_conexao, $conexao);
    $query_rsTreinamento = "SELECT * FROM treTreinamento WHERE IdTreinamento = $IdTreinamento";
    $rsTreinamento = mysql_query($query_rsTreinamento, $conexao) or die(mysql_error());
    $row_rsTreinamento = mysql_fetch_assoc($rsTreinamento);
    $totalRows_rsTreinamento = mysql_num_rows($row_rsTreinamento);

    // Dados do Colaborador
    $Nome = $row_rsRegistro['nome'];
    // Dados do Treinamento
    $Horas = $row_rsTreinamento['CargaHoraria'];
    $FezTreinamento = 0;
    $Log = $row_rsTreinamento['Log'];

    // Inserindo dados no banco de dados se não existir cadastro
    mysql_select_db($database_conexao, $conexao);
    $sqlTreinamento = "INSERT INTO treParticipantesTreinamento (
                    IdTreinamento,
                    IdColaborador,
                    Nome,
                    Horas,
                    FezTreinamento,
                    Log )
                VALUES (
                    '$IdTreinamento',
                    '$IdColaborador',
                    '$Nome',
                    '$Horas',
                    '$FezTreinamento',
                    '$Log')";
    $resultado = mysql_query($sqlTreinamento, $conexao) or die ("Erro Inserindo Registro: " . mysql_error());

    // 1 = TRUE, 2 = FALSE
    $status = $resultado;

} // fim do foreach*/
</code>
Comment: Duas opções, usar o `REPLACE` no lugar do `INSERT` ou fazer uma consulta comparando os dados que não podem ser repetidos e se resultar registros dar um `continue` no foreach...
Comment: Olá @Jader, tem como me dar um exemplo do comando Continue? Valeu.
Comment: algo assim após a consulta: `if ($num_rows > 0) continue;`
Answer: Faz assim:
<code>    // Recebendo o array com os ID´S
    $checkboxes = $_POST['check'];

    $IdTreinamento = $_POST['IdTreinamento'];

    // Verificando os Colaboradores existente

    mysql_select_db($database_conexao, $conexao);
    $query_rsVerifica = "SELECT * FROM treParticipantesTreinamento WHERE IdTreinamento = $IdTreinamento";
    $rsVerifica = mysql_query($query_rsVerifica, $conexao) or die(mysql_error());
    $totalRows_rsVerifica = mysql_num_rows($rsVerifica);
    $trePT = Array();
    if ($totalRows_rsVerifica > 0)
       while ($row_rsVerifica = mysql_fetch_assoc($rsVerifica)) $trePT[] = $row_rsVerifica['IdColaborador'];

// laço para buscar ID´s e efetuar cadastro
foreach($checkboxes as $IdColaborador) {

    // Se colaborador existir passa para próximo
    if (in_array($IdColaborador, $trePT)) continue;

    // Buscando dados do Colaborador
    mysql_select_db($database_conexao, $conexao);
    $query_rsRegistro = "SELECT * FROM comColaborador WHERE IdColaborador = $IdColaborador AND ativo = 1";
    $rsRegistro = mysql_query($query_rsRegistro, $conexao) or die(mysql_error());
    $row_rsRegistro = mysql_fetch_assoc($rsRegistro);
    $totalRows_rsRegistro = mysql_num_rows($row_rsRegistro);

    // Buscando dados do Treinamento
    mysql_select_db($database_conexao, $conexao);
    $query_rsTreinamento = "SELECT * FROM treTreinamento WHERE IdTreinamento = $IdTreinamento";
    $rsTreinamento = mysql_query($query_rsTreinamento, $conexao) or die(mysql_error());
    $row_rsTreinamento = mysql_fetch_assoc($rsTreinamento);
    $totalRows_rsTreinamento = mysql_num_rows($row_rsTreinamento);

    // Dados do Colaborador
    $Nome = $row_rsRegistro['nome'];
    // Dados do Treinamento
    $Horas = $row_rsTreinamento['CargaHoraria'];
    $FezTreinamento = 0;
    $Log = $row_rsTreinamento['Log'];

    // Inserindo dados no banco de dados se não existir cadastro
    mysql_select_db($database_conexao, $conexao);
    $sqlTreinamento = "INSERT INTO treParticipantesTreinamento (
                    IdTreinamento,
                    IdColaborador,
                    Nome,
                    Horas,
                    FezTreinamento,
                    Log )
                VALUES (
                    '$IdTreinamento',
                    '$IdColaborador',
                    '$Nome',
                    '$Horas',
                    '$FezTreinamento',
                    '$Log')";
    $resultado = mysql_query($sqlTreinamento, $conexao) or die ("Erro Inserindo Registro: " . mysql_error());

    // 1 = TRUE, 2 = FALSE
    $status = $resultado;

} // fim do foreach*/
</code>
Comment: Olá @Carlos, só fiz uma alteração, coloquei a sua verificação inicial dentro do meu foreach, pois não tinha como pegar o ID do colaborador e ai ficou perfeito, valeu pela grande ajuda.
Comment: Opa @adventistapr beleza, na verdade era pra mim ter retirado o ` AND IdColaborador = $IdColaborador` que ai não precisa ficar fazendo mais uma consulta dentro do loop, corrigi ali, veja se funciona ai.
Comment: Isso é realmente verdade, valeu pela ajuda e preciosa ajuda @Carlos.
Answer: Evite a utilização das funções mysql_* pois as mesmas estão em processo de descontinuação.
Ao invés disso utilize PDO ou mysqli.
Um exemplo de otimização do seu código seria utilizando prepared statements:
<code>$conn = new PDO('connstr', 'usuario','senha');
$prepared = $conn->prepare("SELECT * FROM comColaborador WHERE IdColaborador = :ID AND ativo = 1");

foreach($checkboxes as $IdColaborador) {
  $prepared->bindValue(':ID', $idColaborador, PDO::PARAM_INT);
  $exec = $prepared->execute();
}
</code>
Com a técnica acima você 'prepara' a consulta de funcionário apenas uma vez no banco e muda apenas seu parametro para cada funcionário. Isto tornará o código bem mais rápido.


Convert a series of 0s and 1s into another series where it only consists of 1s or only consists of 0s

Question: Ali Baba is trying to enter a cave. At the entrance, there is a drum with four openings, in each of which there is a pot with a herring inside. The herring may be lying with its tail up or down. Ali Baba can put his hands into any two
openings, feel the herrings, and put any one or both of them either tail up or tail down as he pleases. After this, the drum rotates and once it stops, Ali Baba cannot determine into which openings he put his hands before. The door to the cave will open as soon as the four herrings are either all tail up or tail down. What should Ali Baba do?
This question is similar to a "binary" question, where I have to convert a series of 1s (up) and 0s (down) into all 1s or 0s, but I am not sure how to do that here with randomization.
Comment: The verbiage is unhelpful.  There are four coins and you get to examine two of them (setting those two however you like).  You want all four to be the same.  Is that the problem?  How many moves do we get?  As stated, if there are only finitely many moves, you might fail (you might accidentally inspect the same two over and over again).  So you are looking for a probability?
Comment: @lulu it seems like the drum is a cylinder, so you can effectively choose to inspect adjacent openings or opposite openings. I still think you have the danger of being unable to guarantee access to the one pot needed to open the door.
Comment: @lulu I misunderstood the question and I have edited it. You are allowed infinite moves, but how should the "coins" be inspected and changed with randomization to create a solution with the least possible moves (there doesn't have to be a numerical value)?
Comment: @Joffan  In [the usual version of this problem](http://forums.xkcd.com/viewtopic.php?t=110425) the orientation of the coins is fixed (as you suggest) but all you can do is to specify that the Adversary flip a given subset (without inspecting the subset).  Perhaps that is what is intended here.  Who knows?
Comment: Are you allowed to choose one opening, check the herring, and then choose the second opening?
Comment: Please look at the link I posted. In the solutions there you will see a $7$ move solution that always works without inspection (though some moves require three flips, not two). Perhaps that is what you intend to ask?  I think one of the solutions avoids flipping $3$ (flipping just $1$ instead).
Comment: @lulu Yes, it is. Thank you!
Answer: So lulu's link can be adapted to solve this problem in $7$ moves, flipping herrings in either adjacent or opposite openings.
The initial state is either 2 tail-up ($U$) and 2 tail-down ($D$) $=2U+2D$, or $3U + D$ or $U+3D$.
If we have $2U+2D$, flipping opposite herrings will either open the door or maintain a state where the similar herrings are adjacent. Flipping adjacent herrings will convert similar adjacent herrings to either a win or to similar diagonal herrings. So in this case, flipping opposite-adjacent-opposite will encounter a winning state.
Flipping any two herrings will convert $3U + D$ and $U+3D$ into one of those two states. Flipping a single herring will convert both $3U + D$ and $U+3D$ into either a winning state or $2U+2D$.
So opposite-adjacent-opposite-single-opposite-adjacent-opposite is a sequence of seven moves that will open the door (at some point). No inspection of herrings required in this - you just flip them, whatever the state.
Answer: When inspection is allowed (as described in the post) one can open the door in maximum 5 steps: Two opening that are next to each other will be addressed as a N-pair, and two openings that are opposite to each other will be addressed as a O-pair. Now just follow the steps below:

Choose any N-pair and put both tail up.
Choose any O-pair and put both tail up. If the door still had not opened, there must be three tails up and one down.
Choose any O-pair. If one of them is tail down, flip it and you are done. Otherwise both are tail up, just flip one of them. Now you have a N-pair with tail down and a N-pair with tail up.
Choose any N-pair and flip both. If both tails were in the same direction you are done. Otherwise you have a O-pair with tail up and a O-pair with tail down.
Choose any O-pair and flip it.

Now all tails are in the same direction - Enter the cave.


function to return all S3 methods applicable to an object

Question: Has anyone put together/found a good method for listing all the S3 methods available for a given object?  The built-in methods() function will give all available methods for a specified class, or for a specified generic function, but not for an object.
The example I have in mind is a glm object, which is of (minor?) class "glm" but also inherits from "lm"
g <- glm(y~x,data=data.frame(x=1:10,y=1:10))
class(g)
## [1] "glm" "lm"

There are 35 methods for class "lm" and 22 for "glm".  I'm interested in an answer that combines the results of
lapply(class(g),function(x) methods(class=x))

in a sensible way, so that I can immediately see (for example) that there is a glm-specific method for add1, but that the method for alias is inherited from the lm class.
Does someone have a slick way to do this, or does it already exist?
PS Steve Walker's S3-S4-reference class glossary shows that this works automatically for reference classes, where we have to use an object to get the methods (x$getRefClass()$methods()).
Comment: It's a little misleading to ask for the methods of an S3 class, since in S3 it's functions that have methods, not classes.
Answer: Here's a function that will at least tell you which S3 methods an object will initially trigger:
findMethodsS3 <- function(object) {
    x <- unlist(lapply(class(object),function(x) methods(class=x)))
    sort(x[!duplicated(tools::file_path_sans_ext(x))])
}

findMethodsS3(g)
#  [1] "add1.glm"           "alias.lm"           "anova.glm"
#  [4] "case.names.lm"      "confint.glm"        "cooks.distance.glm"
#  [7] "deviance.glm"       "dfbeta.lm"          "dfbetas.lm"
# [10] "drop1.glm"          "dummy.coef.lm"      "effects.glm"
# [13] "extractAIC.glm"     "family.glm"         "formula.glm"
# [16] "hatvalues.lm"       "influence.glm"      "kappa.lm"
# [19] "labels.lm"          "logLik.glm"         "model.frame.glm"
# [22] "model.matrix.lm"    "nobs.glm"           "plot.lm"
# [25] "predict.glm"        "print.glm"          "proj.lm"
# [28] "qr.lm"              "residuals.glm"      "rstandard.glm"
# [31] "rstudent.glm"       "simulate.lm"        "summary.glm"
# [34] "variable.names.lm"  "vcov.glm"           "weights.glm"

Comment: That only works as long as the class name doesn't have a "." in it, right?
Comment: It's the easiest-to-remember complicated name for an R function that I know of. Remembering whether it's in `utils` or `tools` is what usually gets me...
Comment: looks like it should work: `tools::file_path_sans_ext("a.b.c")` is `"a.b"`
Comment: @MrFlick -- Yes, very good point. Looks like the only solution is to explicitly strip off the known class names, as your function does.
Comment: @BenBolker I was thinking more like `tools::file_path_sans_ext("print.data.frame")=="print.data"`
Answer: Here's an attempt to replicate the "standard" behavior
classMethods <- function(cl) {
    if(!is.character(cl)) {
        cl<-class(cl)
    }
    ml<-lapply(cl, function(x) {
        sname <- gsub("([.[])", "\\\\\\1", paste0(".", x, "$"))
        m <- methods(class=x)
        data.frame(
            m=as.vector(m),
            c=x, n=sub(sname, "", as.vector(m)),
            attr(m,"info"),
            stringsAsFactors=F
        )
    })
    df<-do.call(rbind, ml)
    df<-df[!duplicated(df$n),]
    structure(df$m,
        info=data.frame(visible=df$visible, from=df$from),
        class="MethodsFunction")
}

And then you can try it out with
g <- glm(y~x,data=data.frame(x=1:10,y=1:10))
classMethods(g)
#or classMethods(c("glm","lm"))

and that will return
 [1] add1.glm*           anova.glm           confint.glm*        cooks.distance.glm*
 [5] deviance.glm*       drop1.glm*          effects.glm*        extractAIC.glm*
 [9] family.glm*         formula.glm*        influence.glm*      logLik.glm*
[13] model.frame.glm     nobs.glm*           predict.glm         print.glm
[17] residuals.glm       rstandard.glm       rstudent.glm        summary.glm
[21] vcov.glm*           weights.glm*        alias.lm*           case.names.lm*
[25] dfbeta.lm*          dfbetas.lm*         dummy.coef.lm*      hatvalues.lm
[29] kappa.lm            labels.lm*          model.matrix.lm     plot.lm
[33] proj.lm*            qr.lm*              simulate.lm*        variable.names.lm*

   Non-visible functions are asterisked

It's not as elegant or short as Josh's, but I think its a good recreation of the default behavior. It's funny to see that the methods function is itself mostly just a grep across all known function names. I borrowed the gsub stuff from there.


Having significant difficulty with Bach's two part inventions for piano

Question: I'm wondering if there are any pieces I could look at before attempting the inventions.  Im working through invention 1 in c major and it's quite challenging for me. I suspect the problem is that I need to play... Two parts... and there is lots of melody in the LH.  I generally find Baroque music like the inventions challenging due to its constant motion in both hands. Problem is I haven't been able to find studies that are helpful. I know that the inventions themselves are supposed to be studies, but what does one do when the study needs it's own study?
Also, is there a common set of technical skills one needs to play the inventions?
Comment: The skills you need to develop aren't technical as much as mental, it seems from your post (unless you encounter technical problems when playing just one of the two voices with one hand). Learning to play two (or more!) seemingly independent parts is simply one of the challenges that are encountered when learning to play the piano. A set of slightly easier pieces in the same style would be the "Kleine Präludien und Fughetten" (small preludes and fugues), also by Bach.
Comment: Maybe try the F Major?  It might be easier for you.  But you should still approach it hands separately first.
Answer: Make sure you are ABSOLUTELY fluent hands separately before attempting to put them together.  Then try it VERY slowly.  Slowly enough to not make mistakes.  If you do make a mistake, go even slower.   When you can play a section 10 times, without errors, at the slow speed, try a bit faster.  Sleeping on it helps.
You know, just the standard How To Practice stuff.  But you REALLY have to do it with contrapuntal music.  It's hard to fake!
Comment: IMHO Hanon is worthless for anything - except lighting a fire, if you have a paper copy. Much better to play *music* - e.g. start with some easier teaching pieces by Bach, like the Anna Magdelena notebook http://imslp.org/wiki/Notebook_for_Anna_Magdalena_Bach_(Bach,_Johann_Sebastian), or this collection of preludes he wrote as teaching pieces: http://imslp.org/wiki/42_Little_Keyboard_Preludes_(Bach,_Johann_Sebastian)
Comment: Is the primary difficulty then just the counterpoint? I have always found pieces like these tremendously difficult. In fact, I've recently gone back to my old books, and I find that, say, my grade 7 (Canada's RCM) book is generally not that difficult except for the Baroque pieces, which I can't make much headway on. I've always wondered why they are grouped with the other pieces at the same grade level, although I guess difficulty is subjective...
Comment: Not so much "the counterpoint," but the fact that the hands are independent. That's a basic necessity for keyboard playing, though you can avoid it for a long time by only choosing music that doesn't need it! (Counterpoint gets "hard" when you have to play 5 or 6 independent parts with two hands, not just two!)
Comment: @MichaelStachowsky: I'd be wary of saying Hanon is worthless for literally *anything* (besides starting a fire), but I would agree with alephzero that it's not really useful for this.  Independence of motion is presumably what you're having problems with, and Hanon has essentially none of that.  I would reiterate Laurence Payne's advice to practice *hands separately* first and master those first before trying to combine the hands.  Trying to play hands together prematurely may permit you to play the correct *notes*, but you will likely not retain proper articulation and phrasing.
Comment: In that case, would Hanon exercises be good, or just choose pieces to practice that have independence of fingers built in? I'd rather avoid Hanon if I can
Answer: I am guessing that you are having difficulty with the independent nature of the left hand part.
I would suggest that you start practising your scales, especially in contrary motion.  If you can play the chromatic scale (2 octaves) in contrary motion starting from any note then you will have developed some of the skills needed to play these pieces.
Also normal scales in contrary motion and scales,again in contrary motion but with one hand starting on the third of the scale.
Arpeggios might be useful as well.
I'm sure other people will be able to suggest some easy pieces that will help you develop this further.  Good luck.
Answer: One book I have personally loved is First Lessons in Bach, edited by Walter Carroll (and easy to find online). It contains a variety of somewhat easier pieces by Bach and his contemporaries, many but not all of them from the Anna Magdalena Bach notebook. In his introduction, the editor specifically recommends it as good preparatory material for the two part inventions.
Answer: However, listening to interpretations by other pianists is very good advice. Myself, I am playing five of the fifteen inventions on harpsichord and am planning to learn them all. I listen to Gould who is interesting and a few harpsichordists. It's fun to compare not only the interpretations but also playing times.


Geting console warnings 'Unable to simultaneously satisfy constraints' while updating UITableView's cell programmatically

Question: I have created a Cell Which Have one UIView.
Here is the image:
In -cellForRowAtIndexPath I am calling an -updateData selector which make some labels in it. And once all Labels are added I am increasing the height constraint of tableViewCell (I have made an outlet of Height Constraint)
Following are the last two lines of my -updateData Selector:
self.cellHeightConstraint.constant = yPosition + Padding;
[self layoutIfNeeded];

Edit
Here is my Table View configuration for Autolayout:
   self.tableView.estimatedRowHeight = 90;
   self.tableView.rowHeight = UITableViewAutomaticDimension;

CellForRowAtIndexPath method
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCell];
    [cell updateDataWithBillDetail:wrapper];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

When I run this code I am getting following warning:
  Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
    (1) look at each constraint and try to figure out which you don't expect;
    (2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x13d8f9cf0 V:[UIView:0x13d8fb090(114)]>",
"<NSLayoutConstraint:0x13d8fb200 UIView:0x13d8fb090.top == UITableViewCellContentView:0x13d8faf10.topMargin - 8>",
"<NSLayoutConstraint:0x13d8fb250 V:[UIView:0x13d8fb090]-(0)-|   (Names: '|':UITableViewCellContentView:0x13d8faf10 )>",
"<NSLayoutConstraint:0x13db6cd80 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x13d8faf10(35.5)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x13d8f9cf0 V:[UIView:0x13d8fb090(114)]>

 Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

Can someone helps me how can I remove this console warning?
Comment: This can be due to the `height` returned by `cellForRowAtIndexPath:` does not matches the one set by the `height constraint`
Can you share some related code
Comment: why -8 in your constrains (...but thats another thing)? just to test it out, have you tried to just give the cell a width and height constraint?
Comment: @zero3nna yes If I don't call my `-updateData` method then No warnings are coming.
Comment: have you set your Row Height to custom in the Storyboard/XIB? Inside the Size Inspector of your Table View Cell?
Comment: @zero3nna yes I Have
Answer: I think the problem is with the yPosition. Replace it with initial cell's height.
Comment: I have to change the cell's height based on new content.
Comment: Thats what I am telling. Your code has a line self.cellHeightConstraint.constant = yPosition + Padding; Remove the "yPosition" and replace it with your tableview cell's initial height.
Answer: I'm not sure but can you even do this for a cell, it prob. conflicts with the height like @ystack sad.
Have you tried to work with dynamic cell sizes?
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat

Comment: Thanks bro for your time. Actually my cell's height is calculated by Autolayout so I haven't implemented this method.


WooCommerce Get the product object from the product title

Question: I am making a code to get product object from product title.
I am reading product title from my notepad file and passing it to Wordpress function.
Below is a function in which $pval is a product title.
 $productdetail_by_title = get_page_by_title($pval, OBJECT, 'product');
 print_r($productdetail_by_title);
 exit;

product title is something like this: 200x Slopupovací pleťová Rusk
But I am not able to get product object. If i pass statically this title like this:
$productdetail_by_title = get_page_by_title("200x Slopupovací pleťová Rusk", OBJECT, 'product');
print_r($productdetail_by_title);
exit;

Then I am able to get product object. Please help.
Answer: With get_page_by_title() WordPress function, you will not get the WC_Product objet but if it's working you will get the WP_Post object.
So here is a custom built function that will output the WC_Product object, if the title matches with a real product title:
function get_wc_product_by_title( $title ){
    global $wpdb;

    $post_title = strval($title);

    $post_table = $wpdb->prefix . "posts";
    $result = $wpdb->get_col("
        SELECT ID
        FROM $post_table
        WHERE post_title LIKE '$post_title'
        AND post_type LIKE 'product'
    ");

    // We exit if title doesn't match
    if( empty( $result[0] ) )
        return;
    else
        return wc_get_product( intval( $result[0] ) );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
EXAMPLE USAGE:
// Your title set in a variable
$title = "200x Slopupovací pleťová Rusk";

// Using our custom function to get an instance of the WC_Product object
$product_obj = get_wc_product_by_title( $title );

// Testing the output
print_r($product_obj);

This code is tested on WooCommerce 3+ and works.
Comment: @RWS "product" is not the product type **but the WordPress  custom "post_type"** when querying the "wp_posts" table… The product type is defined as a specific term in custom taxonomy "product_type". There is many product types as "simple", "variable", "external"…
Comment: Why did you use LIKE in this string `AND post_type LIKE 'product'`? shouldn't the `product_type` exactly match `product`?
Comment: I actually misspelled, i meant post_type. Yes, you are right, I totally forgot about other product types, thank you for clarification. it worked for me only when I changed the respective part of the query to `post_title LIKE '%$post_title%'`


Covering map associated to a fibration

Question: I am reading about the generalization of the monodromy action to fibrations.
In this settings, the lift of a path is not unique, but it is unique up to free homotopy.
In particular there is a well defined right action of $\pi_1(X,x)$ on $\pi_0(F_x)$ ($F_x$ is the fiber over $x$) which sends a path component of the fiber and a homotopy class of loops, to the path component of the ending point of a lift of a path representing the class in $\pi_1(X,x)$.
Of course if $F_x$ is discrete, then $\pi_0(F_x) \cong F_x$ and we recover the usual monodromy for coverings.
This got me thinking about the following construction.
Let $p: E \to X$ be a fibration (not sure which hypothesis you need). Consider the set:
$$ \pi_0^v(E) := \bigsqcup_{x \in X} \pi_0(p^{-1}(x))$$
with the function $\pi_0^v(p) : \pi_0^v(E) \to X$ defined by $$\pi_0^v(p)|_{\pi_0(p^{-1}(x))} : [e] \mapsto x$$
I used a superscript $v$ for "vertical".
By putting some topology on $\pi_0^v(E)$ (quotient topology?) we could end up with a covering map. At this point, for example, the two notion of monodromy action will coincide.
This construction might be a functor from some category to the category of coverings.

Questions: Is this construction known/used? (First: Does it actually work?)

I am not asking to provide full details (but of course they are gladly accepted). I am mainly interested in knowing if this is known to work and used somewhere in mathematics.
Answer: In general your construction does not work.
Consider fibrations with unique path lifting. These are more general than covering maps. A nice treatment an be found in

Spanier, Edwin H. Algebraic topology. Springer Science & Business Media, 1989.

It is easy to see that a fibration has unique path lifting if and only each fiber has only constant paths. In that case $\pi_0(p^{-1}(x))$ can be naturally identified with $p^{-1}(x)$ so that $\pi_0^v(E)$ is the same set as $E$. Thus the quotient map $E \to \pi_0^v(E)$ is a bijecton and (if you are not willing to give $\pi_0^v(E)$ an arbitrary topology) you will agree that the only reasonable topology on $\pi_0^v(E)$ is that making $E \to \pi_0^v(E)$ a homeomorphism.
Now there are fibrations with unique path lifting which are no covering maps, for example the infinite product $\Pi_{n=1}^\infty p_n :  \Pi_{n=1}^\infty\mathbb R  \to \Pi_{n=1}^\infty S^1$, where each $p_n(t) = e^{2\pi it}$ (see Example 9 in Spanier, Ch. 2 Sec. 2). For such maps your construction does not produce a covering map. Note that both $\Pi_{n=1}^\infty\mathbb R$ and $\Pi_{n=1}^\infty S^1$ are nice in the sense that they are path connected and locally path connected.
In this context also have a look at Theorem 10 in in Spanier, Ch. 2 Sec. 4 which says that if $p : E \to B$ is a fibration with unique path lifting such that $B$ is locally path connected and semilocally $1$-connected and $E$ is locally path connected, then $p$ is a covering map.
Comment: Thanks for the detailed answer and for the reference.

Covering map associated to a fibration


Definition of simultaneous equation modelling

Question: I have been asked to comment on a statistical analysis proposal that suggests using simultaneous equation modelling  as a method. I have a basic general knowledge of statistics (Masters level) but very little experience with econometrics-specific modelling.
Does this technique go by any other name?
If it is solely used in econometrics, I'm looking for a somewhat non-theoretical applied definition.
I'm also looking for an indication of which software it can be implemented in out of a choice between Stata/SAS/SPSS.
I have looked through various online sources and am either not convinced of their accuracy or they present the information too theoretically.
Comment: A definition, references and links can be found here - http://en.wikipedia.org/wiki/Simultaneous_equations_model
Comment: Actually I had already read through that prior to posting. The article is poorly written and does not address my queries above namely does this technique go by another name, can I get a non-theoretical definition, which software can it be implemented in.
Comment: Are simultaneous equation models still used by (macro) econometricians? It has been quite a while since Chistopher Sims told 'em to *get real*!
Answer: Take a look at this chapter in "Baby" Wooldridge on Simultaneous Equation Models. In particular, example 16.1 on estimating the relationship between police force and the murder rate in very illuminating. Briefly and very roughly, you need for each equation to have a ceteris paribus (all else equal) behavioral interpretation. For example, the equations may describe the causal, behavioral relationships of different economic agents (the potential murderers and city officials). Secondly, these equations are simultaneous, in the sense that the decisions are interlinked. Presumably, criminals murder less when there are lots of cops around, and police forces grow when there are long-run crime sprees.
Economic theory imposes certain restrictions/relationships on the parameters that allow you to recover the parameters of these behavioral equations in several ways from data (these are mentioned in the Wiki entry). Sometimes this only happen when the assumptions are so strong that no one believes them. Other times the assumptions are sufficiently confusing so that no one understands them and the analyst can get away with murder (pun intended). Sometimes only functions of parameters can be recovered. In short, lots of heavy lifting needs to happen before you fire up your statistical software.
Finally, I think of simultaneous equations as a type of structural equation model. These can easily be handled with Stata, R, SPSS and SAS). There's also specialized software like LISREL. SEM models pop up in other social sciences, such as psychology, especially in psychometrics.
Comment: Thankyou, that's fantastic. I wasn't sure how it related to structural equation models which I am more familiar with. I own that text and will retrieve it from the person I lent it to.


Compare agreement between multiple different methods

Question: There are 3 subjective methods to measure the severity of a condition. Each method measures it on an ordinal scale, but the scales are different:
method 1 scale is 1-3
method 2 scale is 1-4
method 3 scale is 1-5
Evaluation of a condition according to each method is performed by 3 raters.
Krippendorff's alpha seems like an optimal approach to calculate the agreement between those 3 rates.
How could I compare the 3 resulting Krippendorff's alphas for each method and estimate if one method gives significantly better agreement than the other 2?
Answer: Assuming zero bias among the raters, the "1-4" and "1-5" will have the best agreement. This is actually a simple math problem.  When you round a number to fit in a scale of "1-3," you lose more information than you lose with bigger scales.
The bias issue may be a problem.  If you ask person A to rate using both scales: (disagree, agree, strongly agree) and (strongly disagree, disagree, agree, strongly agree), would person B do it the same way? People have different intuitive thresholds for "strongly."  And then there's the standard bias issue.  Seeing things differently.
Comment: I would expect the opposite: the bigger the number of categories is, the less is the chance for raters to agree. However, it does answer the question: how can I compare if one method gives significantly better agreement than the other 2. Can I test it or shall I bootstrap and look at CIs?
Comment: How are you measuring bias if you reduce the available scale, hoping for more consistency? You may not actually get that. -- If you are just looking for absolute agreement percent between the two methods, run two tests on two different groups.  Group A gets 3 choice answers.  Group B gets 5 choice answers.  Then count the number of exact matches in both groups.


Remover caracteres de listas python

Question: Olá eu tenho um código que le dois arquivos um txt e um XML e depois verifica uma string dentro de uma lista
O XML:
<nfeProc xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
<infNFe Id="NFe35200861974911000104550010000742491174625832" versao="4.00">
<ide>
...
</ide>
<emit>
...
</emit>
<dest>
...
</dest>
<det nItem="1">
<prod>
<cProd>20517-0</cProd>
<cEAN>7897748715180</cEAN>
<xProd>SAFEBLOCK FUSE BLOQUEADOR AUTOMOTIVO (COM FUSIVEL DE EMERGENCIA / SIRENE OPCIONAL)</xProd>
<NCM>85123000</NCM>
<CEST>0110000</CEST>
<CFOP>5401</CFOP>
<uCom>PC</uCom>
<qCom>20.0000</qCom>
</prod>

O TXT:
0|7897748715180|||||20

O meu código:
def tratar_linha(linha):
    dados = linha.strip('\n').split('|||||')
    if len(dados) > 0:
        return dados
    return None
lista = []
#função do EAN
#------------------------------------------------------------------------------------------
def search (lista, valor):
    return [(lista.index(x), x.index(valor)) for x in lista if valor in x]
#função do EAN
#------------------------------------------------------------------------------------------
#text = input("Coloque o diretorio do fisico: ").strip('"')
with open(r"C:\Users\Expedição\Downloads\74249.txt","r", encoding="utf8") as arquivo:
    for linha in arquivo:
        dado = tratar_linha(linha)
        if dado is not None:
            lista.append(dado)
#localiza arquivo,lista todos os itens
#------------------------------------------------------------------------------------------
#XML = input("Coloque o diretorio do XML: ").strip('"')
tree = ET.parse(r"C:\Users\Expedição\Downloads\74249.xml")
root = tree.getroot()
    Contagem = 1
    ns = {'nfe': 'http://www.portalfiscal.inf.br/nfe'}
    for det in root.findall('.//nfe:det', ns):
        nItem = det.attrib['nItem']
        quantidade = det.find('.//nfe:qCom', ns).text.strip('000').strip('.')
        EAN = det.find('.//nfe:cEAN', ns).text
        if EAN is not None:
            FEAN =  "0|" + EAN
    #importa XML, obtem os campos EAN,quatidade
    #------------------------------------------------------------------------------------------
            if search(lista, FEAN):
                print(f"{Contagem} - EAN encontrado: {EAN}")
                lista.remove(FEAN)
            else:
                print(f"{Contagem} - EAN INVALIDO: {EAN}")

Eu tentei usar o lista.remove(FEAN) mas esta dando esse erro e não sei como concertar.
File "c:/Users/Expedição/Videos/python/Consulta.py", line 49, in <module>
    lista.remove(FEAN)
ValueError: list.remove(x): x not in list

Answer: Basta ver como está sua lista:
lista = []
with open(r"texto.txt", "r", encoding="utf8") as arquivo:
    for linha in arquivo:
        dado = tratar_linha(linha)
        if dado is not None:
            lista.append(dado)

print('lista=', lista)

Resultado:
lista= [['0|7897748715180', '20']]

Ou seja, a lista possui um elemento, que é outra lista (e esta contém 2 elementos: a string '0|7897748715180' e a string '20'). Isso acontece porque split retorna uma lista, que você inseriu na lista. E ela só tem 2 elementos porque você fez o split por '|||||' (ou seja, ele usou '|||||' - uma sequência de 5 caracteres | - como o separador).

Enfim, se quer procurar pelo código que está no txt, coloque-o diretamente na lista. Faça o split pelo | e só inclua os elementos não-vazios :
def tratar_linha(linha):
    dados = linha.strip('\n').split('|')
    if len(dados) > 0:
        return dados
    return None

lista = []
with open(r"texto.txt", "r", encoding="utf8") as arquivo:
    for linha in arquivo:
        dado = tratar_linha(linha)
        if dado is not None:
            lista.extend([x for x in dado if x]) # exclui as strings vazias

Assim a lista terá o '0', '7897748715180' e '20' como elementos separados.

Ou pegue somente o segundo campo da linha, que parece ser o que você quer:
def tratar_linha(linha):
    dados = linha.strip('\n').split('|')
    if len(dados) >= 2:
        return dados[1] # retorna o segundo campo (7897748715180)
    return None

lista = []
with open(r"texto.txt", "r", encoding="utf8") as arquivo:
    for linha in arquivo:
        dado = tratar_linha(linha)
        if dado is not None:
            lista.append(dado) # insere o campo (7897748715180)

Assim eu só retorno um campo (o 7897748715180).

Tendo a lista só com as informações que me interessam, a busca fica fácil, não precisando concatenar o zero na frente e nem usando uma função extra com index nem nada disso:
for det in root.findall('.//nfe:det', ns):
    nItem = det.attrib['nItem']
    quantidade = det.find('.//nfe:qCom', ns).text.strip('000').strip('.')
    EAN = det.find('.//nfe:cEAN', ns).text
    if EAN is not None:
        if EAN in lista: # se está na lista
            print(f"{Contagem} - EAN encontrado: {EAN}")
            lista.remove(EAN)
        else:
            print(f"{Contagem} - EAN INVALIDO: {EAN}")

Veja que para verificar se um elemento está na lista, basta usar o operador in.
Comment: sua proposta é bem valida mas no meu caso em especifico eu preciso que a `lista` tenha os int tbm (`20`)

Remover caracteres de listas python


Why is Bonse Aba beamed the way it is?

Question: Bonse Aba — a Zambian children's song — is written in 2/4. However, the beaming, as shown in this link, does not appear to reflect the quarter note main beats. Instead, it starts beams on beat 1 and the 'a' of 1 of every odd-numbered measure; however even-numbered measures appear normal:

1 (e &) a (2 e & a) | 1 (e & a) 2 (e & a)

I would think that this should be notated with common time, with measures combined in pairs and the implication that the time signature is otherwise 16/16 — a 16-beat pattern of uneven pulses.
Would that be correct? Is the sheet music correct? Why or why not?
Comment: Link is dead, but question stands without it.
Answer: The sheet music is what it is.  Modern instrumental notation beams on the major accents and subdivides note values with ties that run across a major accent.  Modern vocal notation these days tends to do the same but beamed according to syllables in past centuries.
This does neither.  It follows the syncopation but otherwise still beams per beat.  We are talking about a song for young children: the typesetter likely felt that using ties in bean-counting style was more likely to damage than help the execution, either by confusing the singers or by resulting in extra stresses in the middle of notes, defeating syncopation.  The other options of not beaming at all or at beaming across the whole measure apparently did not appeal to the typesetter, possibly because of introducing too much of a visual discrepancy to every second measure.
In the end, notation, like typography, is supposed to serve its audience.  It does that by being helpful and unambiguous.  The core audience here isn't a brass ensemble, but even a brass ensemble would have no problems figuring out what to play here in spite of being used to seeing it notated differently.
The main problem posing itself for notation like this is just at which point you do switch to more standard notation.  Because the underlying principles of this notation are easy to keep consistent with this song.  But as complexity grows, making decisions becomes harder.

Why is Bonse Aba beamed the way it is?


How large should the size of array M be in linear probing hash table?

Question: I tried to understand an implementation of linear probing hash table using Java. However, I am frustrated with understanding why M is given an initial value of 30001. The skeleton of the code is given below.
<code>    public class LinearProbingHashTable<Key, Value>{
      private int M = 30001;
      private Value[] vals = (Value[]) new Object[M];
      private Key[] keys = (Key[]) new Object[M];

      private int hash(Key key){...}
      public void put(Key key, Value val){...}
      public Value get(Key key){...}
    }
</code>
My question is why M is initialised to 30001 here. Is this a rule-of-thumb? How should I decide the size of M when initialising the linear probing hash table?
Comment: The magnitude (i.e. roughly 30k) was probably chosen heuristically to be "good" for "most" use cases (i.e. a trade-off between memory use and reallocation).  The exact value makes it close to prime (not sure why not an actual prime).
Answer: You have to know what this part of code serve for in order to understand this better. Maybe, or probably, keys are within [0, 30000].

Further reading:

[1] [2]
Selecting an appropriate HashTableSize is important to the success of this method. For example, a HashTableSize of two would yield even hash values for even Keys, and odd hash values for odd Keys. This is an undesirable property, as all keys would hash to the same value if they happened to be even. If HashTableSize is a power of two, then the hash function simply selects a subset of the Key bits as the table index. To obtain a more random scattering, HashTableSize should be a prime number not too close to a power of two.
Check out [3] on how to chose a suitable table size for a Hash.
Comment: @OliCharlesworth I added some more info for the long story of choosing a suitable hash table size.
Comment: Ok, but I was merely commenting on your claim "30001 is a prime number" ;)
Comment: @OliCharlesworth I see what you mean. You are right, 30001 is not a prime number as `30001=19*1579`. :-)


Determining non-isomorphic models of a formula

Question:
Let $Q$ be the formula arising from the conjunction of the following formulas (of first-order classical logic with equality):

$\exists x P(x)$
$\exists x \neg P(x)$
$\forall xy (P(x) \wedge P(y) \rightarrow x = y)$
$\forall xy (\neg P(x) \wedge \neg P(y) \rightarrow x = y)$

How many models does $Q$ have? How many of them are non isomorphic?

I'm having trouble with counting or giving a formal proof regarding the models of $Q$. Also, I don't exactly get what it means with non-isomorphic models.
Answer: Let's first see what the axioms say.
(i) The first axiom says there is an object that has property $P$. More formally, in any model $M$ of the axiom system, there is an object $a$ such that $P_M(a)$ is true in $M$.
(ii) The second axiom says there is an object which doesn't have property $P$.
(iii) The third axiom says there is no more than one object with property $P$. It does so by saying that if the objects $x$ and $y$ have property $P$, then $x$ must be the same as $y$.
(iv) The fourth axiom says there  is no more than one object that doesn't have property $P$.
So any model of the full set of $4$ axioms has exactly two elements, one of which has property $P$, and the other of which doesn't. And any two element structure $M$, where $P$ is interpreted to be true at one of the elements and false at the other, is a model.
There are infinitely many models. The underlying set of the model could be $\{1,2\}$, or $\{17,24\}$, or $\{a,b\}$ where $a$ is the set of integers and $b$ the set of reals. Any two-element set can be made the underlying set of a model, by interpreting $P$ to be true at one of the objects and false at the other.
However, up to isomorphism there is only one model. More precisely, if $M$ and $M'$ are any two models, then $M$ and $M'$ are isomorphic.
For let $M=\{a,b\}$, and $M'=\{a',b'\}$, where $P_M(a)$ holds and $P_{M'}(a')$ holds. Then the mapping $\varphi$ that takes $a$ to $a'$ and $b$ to $b'$ is a (structure-preserving) isomorphism from $M$ to $M'$.
Comment: If I understood you correctly, in the case of isomorphism we have just one model, because all the models will map to the same thing. Can you give me an actual example with it? On the other hand, when you say $\{1,2\}$ can be a model, can take for example $P(x)$ being "x is prime". So, in the case of $P(1)$ it will be false, in the case of $P(2)$ it will be true. So, first two axioms hold, for the third and forth one, conjunction will be false, and equality will be false, so I will get $0 \implies 0$, is my logic correct?
Comment: All four axioms hold for your example. As I wrote, there are infinitely many models. But they are all abstractly "the same." More precisely, as done in the answer, for any two models $M$ and $M'$ there is an isomorphism from $M$ to $M'$.
Comment: Thanks, good to hear that my example is a valid one too.
Comment: How can I extend the notion of isomorphism a bit? I mean, instead of just saying "there is an isomorphism from $M$ to $M'$, can I explain it a bit more? Like maybe also an example or something, or a simpler explanation that explains your notion "they are all abstractly the same"?
Comment: I don't know what you mean by extending the notion. In (abstract) algebra, the notion comes up frequently, and is even of practical importance (it can be useful to know whether two graphs are isomorphic). More general notion is *homomorphism*, in which a mapping preserves structure but is not necessarily one to one.
Comment: What I meant is that, for example in graphs, two graphs which contain the same number of graph vertices connected in the same way are said to be isomorphic. Meaning if $v$ and $u$ is adjacent then, applying a bijective function $f$ to them, should result that $f(v)$ and $f(u)$ are still adjacent. But, here I'm having difficulty in seeing this. You said there any two models are isomorphic, but how? How is $\{1,2\}$ and $\{17,24\}$ isomorphic? Simply, how is isomorphism defined in logic?
Comment: Defined as usual as a bijection that preserves structure.  With this very simple language that has only a single unary predicate symbol $P$, an isomorphism $\phi$ from an $L$-structure $A$ to $L$-structure $B$ is a bijection from $|A|$, the underlying set of $A$, to $|B|$ such that for all $a\in |A|$ we have $P_A(a)$ iff and only if $P_B(\phi(a))$. In other words, the interpretation of $P$ in $A$ is true at $a$ if and only if $\phi(a)$ is true in $B$. Same as your graph theory  example, except that instead of having a binary predicate symbol we have a unary one.
Comment: For richer languages, we want the structure preservingness to hold for all predicate symbols, function symbols. So for example if we have in addition a binary function symbol $F$, we want $\phi(F_A(c,d))=F_B(\phi(c),\phi(d))$ for all $c,d$  in $|A|$.
Comment: Thank you for the explanation.


Using rolling windows to compute out of sample accuracy

Question: I've already written this question, but probably I didn't specified it well, for this reason I write it again.
I need to use a random walk model (no-change) yt = yt(1+t) to compute
the ratio of RMSFE.
What I would like to do is:

Fit the model to the data yt,...,yt+k−1 and let yˆt+k be the forecast for the next observation.
Compute the forecast error as et=yˆt+k−yt+k.
Repeat for t=1,...,n−k"
residuals1 <- rep(0,58)
residuals6 <- rep(0,58)
residuals12 <- rep(0,58)

y1 <- t(y[,1])
for (i in 1:58) {
  residuals1[i] <- y1[134+i+1]-y1[134+i]
  residuals6[i] <- y1[134+i+6]-y1[134+i]
  residuals12[i] <- y1[134+i+12]-y1[134+i]
}

Is it a correct way to compute the out.of sample forecasting errors or am I missing something?
I would appreciate any suggestions. Thanks!
Comment: I'm confused. You're subtracting something in y1 from something in y1. Is y1 the forecast or the actual data (outside of your sample)?  When you say "accuracy" I assume you're comparing forecast to actual (on non-training data, "out of sample" data)?
Comment: Thanks. I think that in a RW model the forecast data (t+k) is given by the observation at time t, this is why I'm subtracting something in y1 from something in y1.
Comment: Sorry that I'm still confused... Forecast error is the difference between a forecast you make with your RW and the actual data. Presumably, you used the first N observations from your data to adjust (train) your RW model, holding back the last M observations to test with. Then you forecast M steps ahead with your model and compare that to the actual M observations. You don't compare your forecast to your forecast or your data to your data. Or am I misunderstanding?
Comment: Yeah I understand your point. But since I'm doing a RW I say that my forecast (t+k) is y at time t.
Comment: Anyhow I tried to do the loop in your way, by estimating a RW model in DLM form, but it gave me the same result cause V=0.
Comment: It will be clearer if you make your model a function (can store all your models in a list and loop or apply over the list).  Also, the traditional term for what you're trying to do is LOOCV (leave-one-out-cross-validation).
Answer: As you are using a naive (random walk) model, there are no parameters, so it makes no sense to talk of in-sample and out-of-sample, or of training sets and test sets. There is nothing to train.
On the other hand, you may be doing this to compare the results with other methods applied to a training set. I'll assume that's the case.
I'm guessing your data consists of 204 observations (134+58+12) and you are using the first 135 as a training set. There's no need for loops here as the computation is easily vectorized.  Also, because you use a loop you ignore some of the available forecast errors in the test set.
I think you can get the results you want as follows:
y <- rnorm(204)
residuals1 <- diff(y[135:204],lag=1)
residuals6 <- diff(y[135:204],lag=6)
residuals12 <- diff(y[135:204],lag=12)

Comment: Thanks. Yes what I'm trying to do is compare the RW results with other methods. I tried your specification, but I obtain the same results I had with the loop. Maybe this specification is too much naive?
Comment: You can't have the same results for 1-step and 6-step as there are different numbers of residuals doing it my way compared to the loop.
Comment: Yes I just checked the 12 step ahead. I have less numbers of residuals, your way it's more correct and faster, but it didn't solve my problem, although I can't think any other way to express it. Thanks