Unexpected end of JSON input error in AWS Lambda SQS function

Question: This is my nodejs app serverless lambda function
<code>export const updateDailyForecastResults: SQSHandler = async (event) => {
  console.log('Function invoked!!!!');
  await env.check();
  event.Records.forEach(record => {
    const { body } = record;
    console.log(body);
  });
  return;
};
</code>
And I am sending was document example sqs event
<code>{
  "Records": [
    {
      "messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
      "receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
      "body": "test",
      "attributes": {
        "ApproximateReceiveCount": "1",
        "SentTimestamp": "1545082649183",
        "SenderId": "AIDAIENQZJOLO23YVJ4VO",
        "ApproximateFirstReceiveTimestamp": "1545082649185"
      },
      "messageAttributes": {},
      "md5OfBody": "098f6bcd4621d373cade4e832627b4f6",
      "eventSource": "aws:sqs",
      "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
      "awsRegion": "us-east-2"
    }
  ]
}
</code>
Or I tried other versions of Json objects but always I got the same error which is
<code>{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Unexpected end of JSON input",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Unexpected end of JSON input",
    "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:1015:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)",
    "    at Module.load (internal/modules/cjs/loader.js:879:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
    "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
    "    at internal/main/run_main_module.js:17:47"
  ]
}
</code>
Please can anyone help me?
Comment: Is this lambda invoked through API gateway? if yes, you should follow the response format: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
Answer: this exception means that your code has a syntax error.
It occurs before processing the event, so it does not really matter what event you'll give it.
Can you share all the code (and the dependencies)?


JS How to get matching id between 2 array object

Question: How do I get 2 matching id between 2 array object using javascript?
// Array 1
const array1 = [
  {id: 1, name: 'milla'},
  {id: 2, name: 'alice'}
]

// Array 2
const array2 = [
  {id: 3, name: 'bobba', height: '170cm', age: 22},
  {id: 2, name: 'alice', height: '169cm', age: 21},
  {id: 1, name: 'milla', height: '171cm', age: 24},
  {id: 4, name: 'ricky', height: '168cm', age: 32},
]

the expected output is to returned array of object of Array2 that mached with id's on array1
// expected result
[
  {id: 2, name: 'alice', height: '169cm', age: 21},
  {id: 1, name: 'milla', height: '171cm', age: 24},
]

Answer: You could filter and look if the same id exists.

const
    array1 = [{ id: 1, name: 'milla' }, { id: 2, name: 'alice' }],
    array2 = [{ id: 3, name: 'bobba', height: '170cm', age: 22 }, { id: 2, name: 'alice', height: '169cm', age: 21 }, { id: 1, name: 'milla', height: '171cm', age: 24 }, { id: 4, name: 'ricky', height: '168cm', age: 32 }],
    hash = array1.reduce((r, { id }) => (r[id] = true, r), {}),
    filtered = array2.filter(({ id }) => hash[id]);

console.log(filtered);
Answer: The most efficient way to do this is to generate a map of the IDs in array1 and then filter array2 against those IDs, like so:

let array1 = [{ id: 1, name: 'milla' }, { id: 2, name: 'alice' }];
let array2 = [{ id: 3, name: 'bobba', height: '170cm', age: 22 }, { id: 2, name: 'alice', height: '169cm', age: 21 }, { id: 1, name: 'milla', height: '171cm', age: 24 }, { id: 4, name: 'ricky', height: '168cm', age: 32 }];

let idMap = array1.reduce((res, curr) => (res[curr.id] = true, res), {});
let filtered = array2.filter((item) => idMap[item.id]);

console.log(filtered)
Answer: Honestly, this is basic JS, but anyway, here's the solution:

const array1 = [
  {id: 1, name: 'milla'},
  {id: 2, name: 'alice'}
]

// Array 2
const array2 = [
  {id: 3, name: 'bobba', height: '170cm', age: 22},
  {id: 2, name: 'alice', height: '169cm', age: 21},
  {id: 1, name: 'milla', height: '171cm', age: 24},
  {id: 4, name: 'ricky', height: '168cm', age: 32},
]

const map = array1.reduce((a, c) => ({ ...a, [c.id]: true }), {});
const array3 = array2.filter(item => map[item.id]);

console.log(array3);
Comment: This is O(n*m), you can do this in O(n) if you generate a list of IDs in a map first.


remover duplicados pela prespetiva de tuplos

Question: Tenho este código a funcionar, no entanto queria fazê-lo da prespetiva dos tuplos.
Podem auxiliar?
def remove_repetidos(lst):
    lista = [] #lista vazia
    for i in lst:
        if i not in lista:
            lista.append(i)
    lista.sort() #ordenada a lista por ordem ascendente
    return lista

Comment: Em que momento exatamente você gostaria de aplicar as tuplas?
Comment: Entretanto fiz um outro que não está a funcionar e não sei bem porquê.
Comment: def sensas(lst):
    lst_unica = [i for n, i in enumerate(lst_com_unicos) if i not in lst_com_unicos[n + 1:]]
    for lst_resulta_unica in lst_unica:
        return lst_resulta_unica
Comment: Esse outro código parece estar errado. Você utiliza uma variável `lst_com_unicos` que não foi definida.
Comment: desculpa sou nova no python, achei que ele assumia. vou refazer, já aqui coloco
Comment: def sensas(lst):
    lst_com_unicos = ()
    lst_unica = [i for n, i in enumerate(lst_com_unicos) if i not in lst_com_unicos[n + 1:]]
    for lst_resulta_unica in lst_unica:
        return lst_resulta_unica
Answer: Você não consegue utilizar tuplas para fazer o mesmo que fez com a lista pelo fato que as tuplas são imutáveis e, assim, não é possível adicionar novos valores. Para adicionar um novo valor na tupla é necessário criar uma nova tupla com os valores atuais mais o novo e essa adição é feita a partir de uma estrutura auxiliar.
Por exemplo, é possível fazer tuda a lógica utilizando listas e apenas retornar como tupla:
def remove_repetidos(lst):
    lista = [] #lista vazia
    for i in lst:
        if i not in lista:
            lista.append(i)
    lista.sort() #ordenada a lista por ordem ascendente
    return tuple(lista)

E para criar uma nova tupla adicionando o novo valor ficaria algo como:
def remove_repetidos(lst):
    unicos = tuple()
    for valor in lst:
        if valor not in unicos:
            unicos += (valor,)
    return unicos

Mas atente-se ao fato que mesmo que pareça que apenas está modificando a tupla na verdade você criando uma nova tupla e sobrescrevendo a antiga.
A maneira mais simples de remover os itens duplicados de uma sequência é utilizando o set, como pode ser visto em Remover inteiros repetidos lista em Python
Comment: Obrigada. Era isso mesmo. no entanto eu precisava que sito fucnionasse tanto para inteiros como para texto. remove_repetidos((a,b,c,d,a,b,c,d,a,d,c,b)) e neste caso diz-me que o 'a' is not defined. Será que tenho de validar os parametros de entrada?
Comment: @PipocaInsuflada Não, apenas utilizar *strings* corretamente. As *strings* são definidas entre aspas: `('a', 'b', 'a', 'a', 'c', ...)`
Comment: Obrigada faltava isso sim


Knot Theory: Mutations

Question: Show that if we have three tangles as in Figure 2.33a, we can mutate several times in order to permute the tangles.
Note that we can then permute n tangles in a row. This is from Colin Adams; The Knot book exercise 2.25.
Comment: What is a 'mutation'?
Answer: Denote a mutated $T_{i}$ by $T^{i}$ for convenience. Both $T_{2}$ and $T_{3}$ intersect a small region around themselves exactly 4 times. Consider then the region around both of them together. It also only intersects the region four times. Mutate this region. It is then $T^{3}$ and $T^{2}$. So our order of tangles is $T_{1},T^{3},T^{2}$. We can then mutate only $T^{3}$ to get the order $T_{1},T_{3},T^{2}$. Then we simply mutate $T^{2}$ to get the desired order $T_{1},T_{2},T_{3}$. I think this is correct based simply on the definition of mutation from wikipedia. I have not interacted with mutations before, very interesting.
Answer: To complement Jeremy's answer, you can also do the following. I'll use the same notation as it is convenient, keeping in mind that we will always mean left-to-right reflection when we say mutation.
First note that $\overline{T_1T_2T_3}$, the closure of the composition of the tangles $T_1,T_2$ and $T_3$, is equal to $\overline{T_2T_3T_1}$ as cyclic permutations respect closure of tangles. We may now mutate the subtangle $T_2T_3T_1$ to get the knot $\overline{T^1T^3T^2}$ and now if we mutate each component subtangle individually we get the knot $\overline{T_1T_3T_2}$ as required.
This relies on the following easy to prove result that if $\mu$ is the left-to-right reflection mutation, then if $T_i$ and $T_j$ are tangles, $\mu(T_iT_j)=\mu(T_j)\mu(T_i)$. Algebraically this says that $\mu$ is an anti-automorphism on the monoid of tangles (with product being horizontal composition of tangles and identity being two vertical straight line segments).

Knot Theory: Mutations


Pandas Merge returning only null values

Question: I am trying to use Pandas to merge a products packing information with each order record for a given product.  The data frame information is below.
     BreakerOrders.info()
   <class 'pandas.core.frame.DataFrame'>
    Int64Index: 3774010 entries, 0 to 3774009
    Data columns (total 2 columns):
    Material    object
    Quantity    float64
    dtypes: float64(1), object(1)
    memory usage: 86.4+ MB

     manh.info()
   <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 1381 entries, 0 to 1380
    Data columns (total 4 columns):
    Material       1381 non-null object
    SUBPACK_QTY    202 non-null float64
    PACK_QTY       591 non-null float64
    PALLET_QTY     809 non-null float64
    dtypes: float64(3), object(1)
    memory usage: 43.2+ KB

When attempting the merge using the code below, I get the following table with all NaN values for packaging quantities.
   BreakerOrders.merge(manh,how='left',on='Material')

    Material    Quantity    SUBPACK_QTY   PACK_QTY  PALLET_QTY
    HOM230CP    5.0             NaN         NaN        NaN
    QO115       20.0            NaN         NaN        NaN
    QO2020CP    20.0            NaN         NaN        NaN
    QO220CP     50.0            NaN         NaN        NaN
    HOM115CP    50.0            NaN         NaN        NaN
    HOM120      100.0           NaN         NaN        NaN

Comment: Then there are no matches of `Material` between the two dataframes.  Could be due to whitespace or some other annoying thing.  If this is unexpected, then you need to provide an example.  Read [mcve]
Comment: Try this for both dataframe before merging `df['Material'] = df['Material'].astype('str') `
Comment: What's the result of `manh[manh['Material'].isin({'HOM230CP', 'QO115',  'QO2020CP', 'QO220CP', 'HOM115CP'})]`?
Comment: Both data frames have been adjusted to a string prior to running the merge, and this does not fix the error.  When trying the above block of code, I get the expected output of packaging quantities.
Comment: And what is the result of `BreakerOrders.merge(manh.fillna(-1000), how='left', on='Material')`?
Comment: This returns the same original data frame with NaN for all fields in manh
Comment: should not make a difference, but did you try using `join` instead of merge?
Comment: If you're still getting NaNs when you do a left join after filling `manh` with `-1000`, then you have mismatching elements in the two tables' `'Material'` columns. Full stop. There is no argument, no matter what you think is going on. What if you change `how='left'` to `how='inner'`?
Comment: That is false, all of the Materials are in both tables and formatted in the same manner.  An inner join returns a completely blank table.
Comment: "An inner join returns a completely blank table." Then apparently it isn't false, and you have some work to do to understand why you think it is, and what the problem with your data formatting is. Please see the link provided by @piRSquared if you would like to receive further assistance.
Answer: I was having the same and I was able to solve it by just flipping the DFs. so instead of:
df2 = df.merge(df1)

try
df2 = df1.merge(df)

Looks silly, but it solved my issue.

Pandas Merge returning only null values


How to add one more field in disqus form

Question: i have been installed disqus module in my drupal setup.it gives only one textarea field how to i add one more field to get subject from user.
Answer: Disqus does not provide titles, therefore, it is not possible.
What the Disqus module does is embed the Disqus chat/comments as an iframe. For security reasons, you cannot modify/alter anything that is inside an iframe.
Comment: Is it possible to add another field in disqus itself. i mean disqus admin panel
Comment: @udhayakumarUK really? I never seen a Disqus that has multiple fields. If it's possible, then you would have to do it through Disqus. Login to your Disqus account, and see if there is some sort of option to allow for multiple fields.


Python, counting the number of even digits

Question: for  homework in an introductory python class, one of the questions is to count the number of even numbers in n. here's my code so far:
def num_even_digits(n):
    i=0
    count = 0
    while i < n:
        i+=1
        if n%2==0:
            count += 1
    return count

print(num_even_digits(123456))

Answer: Pythonic answer:
def num_even_digits(x):
    return len([ y for y in str(x) if int(y) % 2 == 0])

print(num_even_digits(123456))

Disclaimer: I recognized that, for an introductory Python class, my answer may not be appropriate.
Answer: you are comparing the whole number every time. you need to convert it to a string and then loop over every number in that string of numbers, cast it back to an integer and see if its remainder is 0
def num_even_digits(numbers):
    count = 0
    numbers = str(numbers)
    for number in numbers:
        try:
            number = int(number)
        except ValueError:
            continue

        if number % 2 == 0:
            count += 1
    return count

print(num_even_digits(123456))

if you want to actually loop through every possible number in the range of 0 to your large number you can do this.
def num_even_digits(numbers):
    count = 0
    for number in range(0, numbers):
        if number % 2 == 0:
            count += 1
    return count

print(num_even_digits(10))

problems with your current function:
def num_even_digits(n): # n is not descriptive, try to make your variable names understandable
    i=0
    count = 0
    while i < n: # looping over every number from 0 to one hundred twenty three thousand four hundred and fifty six.
        i+=1
        if n%2==0: # n hasn't changed so this is always going to be true
            count += 1
    return count

print(num_even_digits(123456))


create automount entries

Question: on a Linux or Solaris server, using automount/autofs, how do i create/populate a directory with automount entries without mounting them ?
For example, i have /data yp:auto.data and /data/ subdirectories are only created after the first access. This is not persistent across reboot on Linux.
Is there a way to populate my /data without mounting all the disks ? So that users/applications see /data/disk1..N even if they are not mounted.
Comment: I've just found BROWSE_MODE for RedHat http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-The_sysconfig_Directory.html it's poorly documented, i'm going to try it on monday.
Comment: Yep, BROWSE_MODE=yes seems to do the trick on RedHat.
Answer: On Solaris, that's the default mode, and you need to specify -nobrowse in automount maps to disable it.   (For instance, we have our source code workspaces browsable, but home directories under auto_home are not, since no one wants to wait for 30,000 subdirs to load when trying to save a file in their home directory using a GUI file selection dialog, especially not in GUI's that stat each one, causing a mount, just to determine whether to display a folder or file icon.)
For more details see the Browsing section of the automount(1m) man page and the autofs browsability sections of the System Administration Guide.

create automount entries


Likelihood function when only $\max_{1\le i\le N}X_i$ is observed and $N$ is parameter

Question:
Let $X_1,X_2,\ldots,X_N$ be i.i.d random variables having $\text{Exp}(1)$ distribution where $N$ is unknown. Suppose only $T=\max\{X_1,X_2,\ldots,X_N\}$ is observed.
I have to derive a most powerful test for testing $H_0:N=5$ versus $H_1:N=10$.

So $N$ is my parameter of interest. The joint distribution of $X_1,\ldots,X_N$ is of the form
$$f_N(x_1,\ldots,x_N)=\exp\left({-\sum_{i=1}^N x_i}\right)\mathbf1_{{x_1,\ldots,x_N>0}}$$
But this cannot be my likelihood function since $x_1,\ldots,x_N$ is not observed.

I am not sure how to express the above joint density as a function of $t$, the observed value of $T$. If I can write the joint density as some $f_N(t)$, then that would be my likelihood function given $t$.

I understand that the test is of the form $$\varphi(t)=\mathbf1_{\lambda(t)>k}$$
, where $\lambda$ is the likelihood ratio $$\lambda(t)=\frac{f_{H_1}(t)}{f_{H_0}(t)}$$
Any hint would be much appreciated.
As an aside to the actual question, I am curious if it is possible to find MLE of $N$.
Comment: @whuber Somehow did not realize that I am supposed to use the distribution of $T$. That makes it a standard problem.
Comment: Your joint distribution does not depend on the individual $x_i:$ it depends only on their sum, which *is* observed.
Comment: @whuber Yes it depends only on the sum. So $0<x_1,\ldots,x_N\le t\implies 0<\sum_{i=1}^N x_i\le Nt$. Does *this* imply $\sum_{i=1}^N x_i$ is observed? It only bounds the sum.
Comment: There's a missing step here: under your model assumptions, you can easily derive the distribution of   $T.$
Answer: Hint: the likelihood for $T$ is
$$
f_{T}(t ; N) = N(1-e^{-t})^{N-1}e^{-t}.
$$
To verify this, first find the cdf of $T$, and then differentiate.
\begin{align*}
F_T(t) &= P(T \le t)\\
&= [F_{X_i}(t)]^N.
\end{align*}
Regarding your other question as to whether there is an MLE estimate for this: yes there is, but the likelihood is only defined on $\mathbb{N}^{+}$. This means that you cannot take a derivative and set it equal to zero.
Answer: If $N=5$ then $\Pr(X_1 \le x\ \&\ \cdots \ \&\ X_N\le x) = \left( 1-e^{-x} \right)^5,$ so you have a density function
$$
\frac d {dx} \left( \left( 1-e^{-x} \right)^5 \right) = 5\left( 1 - e^{-x} \right)^4 \cdot e^{-x}.
$$
And similarly if $N=10.$ So the likelihood function is
$$
\begin{cases} L(5) = 5(1-e^{-x})^4 \cdot e^{-x} \\[8pt] L(10) = 10(1-e^{-x})^9 \cdot e^{-x} \end{cases}
$$
where $x$ is the observed maximum value, and the ratio is
$$
\frac{L(5)}{L(10)} = \frac 1 {2(1-e^{-x})^5}.
$$
A small value of this ratio favors the alternative hypothesis $N=10.$ Equivalently, a large value of the observed maximum favors the alternative. Given the probability distribution of the maximum assuming the null hypothesis $N=5,$ you can find the critical value as a function of the level of the test.
Answer:
As an aside to the actual question, I am curious if it is possible to find MLE of $N$.

This is quite straightforward, and simply requires you to derive the log-likelihood function and then use standard (discrete or continuous) calculus techniques to maximise.  Using rules for the distribution of order statistics, the sampling distribution for $T$ is:
$$f_T(t) = N e^{-t} (1-e^{-t})^{N-1}.$$
The log-likelihood is:
$$\ell_t(N) = \ln N - t + (N-1)\ln(1-e^{-t}).$$
Now, maximising this function can be done either by using discrete calculus (i.e., using difference operators), or it can be done by treating $N$ as continuous and maximising using continuous calculus, and then discretising the result.  For simplicity, we will do the latter.  Taking $N$ as a real variable, the log-likelihood has corresponding score function and Fisher information (essentially the first and second derivatives, but with the sign reversed on the second) given by:
$$\begin{equation} \begin{aligned}
s_t(N) &\equiv \frac{d \ell_t}{dN}(N) = \frac{1}{N} + \ln(1-e^{-t}), \\[12pt]
I_t(N) &\equiv - \frac{d^2 \ell_t}{dN^2}(N) = \frac{1}{N^2} >0. \\[12pt]
\end{aligned} \end{equation}$$
Since $I_t(N) > 0$ the likelihood function is a strictly concave function, so this means it has a unique MLE at its sole critical point $s_t(\hat{N}(t)) = 0$.  This gives you the continuous MLE:
$$\hat{N}(t) = \frac{1}{\ln(1-e^{-t})}.$$
This will generally not be an integer value, so you obtain the corresponding discrete MLE by looking at the two integers either side of this value; the bigger one is the discrete MLE (which is almost surely unique).


Exchange account with rogue permissions?

Question: I am running Exchange 2003 and recently removed a former employee's mailbox. The email for this former user is firstname.lastname@example.com. This user had "Send on behalf of" privileges for firstname.lastname@example.com.
Now whenever anyone accepts appointments from userB, they get a bounce message trying to send email to userA. The userA account is not in any groups and has been removed from "send on behalf of" for userB, but the bounce messages still occur.
Is there a way I can search and find a rogue permission or setting in Exchange involving userA that is causing mail to still attempt to send to userA's old email address?
Answer: I've seen this before and it drove me crazy. I finally found this with caused with delegation.
In userB's Outlook go to Tools > Options > Delegates tab. Remove userA from there.
Comment: As well check the permissions on userB's calendar.  If the user or dead SID is these will continue.
Comment: This was it. Delegates! They have caused pain in the past...
Answer: Check the forwarding settings in the properties of user B's account - it's likely user A had user B's email set to silently forward to their mailbox.
Comment: It is indeed only for appointments...not regular email. Smells liked delegates, agh... I will find out in the morning. I did check that userB's mailbox was not set to forward to user A
Comment: Now I first thought this too, but the lack of NDR's for anything but appointments made me think otherwise.
Comment: @Ben Yeah, that doesn't make sense to me, either, so I kinda assumed that all they've noticed it for was appointments, but that it might be happening for everything.  How about it, Joshua - can you confirm that it's only for appointment messages?
Comment: +1 for the answer regardless


why do i get view not found after publishing .net core application

Question: I am building docker image and running a container from it. this container is running a .net core 2.0 web application. however after running it i get the error diplayed in the image below.
I have added this to my Program.cs already
.UseContentRoot(Directory.GetCurrentDirectory())

Comment: did you update your docker image with new changes ?
Comment: I'm facing the same issue. It doesn't work in docker, but it does when I just run it on linux. Did you solve it?
Comment: @Turowicz .net core version might be your issue, see the changes i made in the answer bellow
Comment: @Turowicz let me know if that helps.
Comment: @ifelabolz I can't remember what I did but I believe it was the .NET version indeed.
Answer: Old way
public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        host.Run();
    }
}

New Way
public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
}


Newly added Column can't be updated even in pgadmin

Question: When i do:
 select * from realtimes   where realtime_id = 538189069

i can see my columns is_limitation_zone_overspeed and zone_speed_limit

but when I update it
update realtimes
   set is_limitation_zone_overspeed = true and zone_speed_limit = 90
where realtime_id = 538189069

these field are not affected despite pgadmin
return Query returned successfully: 1 row affected, 181 ms execution time
any idea ? thanks
Answer: is_limitation_zone_overspeed = true and zone_speed_limit = 90 only updates a single column and that is: is_limitation_zone_overspeed.
And that column is updated with the result of the boolean expression:
true and zone_speed_limit = 90

To update two columns you need a comma:
update realtimes
   set is_limitation_zone_overspeed = true, --<< here!
       zone_speed_limit = 90
where realtime_id = 538189069

You might want to go through the SQL tutorial in the manual:

http://www.postgresql.org/docs/current/static/tutorial-update.html
http://www.postgresql.org/docs/current/static/dml-update.html


Caeser Shift Output

Question: I want the output to print the string with the specified Caeser shift but my output seems to repeat the input within the output. Any help would be much appreciated.
# Prompt the user for a string and integer
string = input('Enter the string to encode: ')
i = int(input('Enter integer value to use: '))

# Create a for loop based on the characters in the string
# Build a new string of encoded characters based on the given rules
string= string.upper()
coded=""
for y in string:
    if y in "ABCDEFGHIJKLMNOPQRSTUCWXYZ":
        num=ord(y)
        num+=i
        if num>ord("Z"):
            num-=26
        elif num<ord("A"):
            num+=26
        coded=coded+chr(num)
    else:
        space = string.replace(" ","#")
        coded=coded+space

# Display the new string
print("Your encoded message is:",coded)

Comment: `space = string.replace(" ","#")` doesn't look right to me.
Comment: Two notes: you have a typo (repeated C where there should be V in your alphabet), and when I tried your program myself, it seemed to work mostly properly. Can you show the problematic input and output?
Comment: Ah, I see, it acted up when you had non-letters in the input.
Answer: Your error occurred at space = string.replace(" ","#")
What's happening here is that you're getting the whole String but with characters not in the alphabet changed to #
To fix that, simply remove space = string.replace(" ","#") and change  coded=coded+space to coded=coded+"#"
# Prompt the user for a string and integer
string = input('Enter the string to encode: ')
i = int(input('Enter integer value to use: '))

# Create a for loop based on the characters in the string
# Build a new string of encoded characters based on the given rules
string= string.upper()
coded=""
for y in string:
    if y in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
        num=ord(y)
        num+=i
        if num>ord("Z"):
            num-=26
        elif num<ord("A"):
            num+=26
        coded=coded+chr(num)
    else:
        coded=coded+"#"

# Display the new string
print("Your encoded message is:",coded)

Comment: Also, changed the repeat C to V in "ABCDEFGHIJKLMNOPQRSTUCWXYZ".
Comment: This fixes the output in reference to spaces, but what about accounting for characters like ! or others? I want to omit nonalphabet characters from the output. That's why I attempted to use the replace operator on the string
Comment: If it's not an alphabetical character the else statement will cause it to be a '#'
Comment: Is there any way to omit characters not in the alphabet from the output

Caeser Shift Output


Usando draggable erro ao calcular a porcentagem

Question: Estou usando draggable do Jquery UI para fazer um efeito de arrastar (drag), a dificuldade está em calcular, de 0 a 100, a posição do drag dentro da barra. Veja o que já fiz:

$('.drag').draggable({
 axis: 'x',
 containment: 'parent',
 drag: function() {
  var largura = $('.barra').width();
  var posicaoX = $(this).position().left;
  posicaoX = (posicaoX*100)/largura;
  var l = (100 * parseFloat($(this).position().left / parseFloat($(this).parent().width())));
  $('#mostra').val(l);
 }
});
.barra{
  width: 150px;
  height: 5px;
  background: #000000;
  position: relative;
  margin-top: 20px;
  margin-bottom: 20px;
}
.drag{
  width: 20px;
  height: 20px;
  background: #d18f4f;
  position: absolute;
  top: -7px;
  left: 0px;
  cursor: pointer;
}

<input type="text" value="0" id="mostra">

O código aparentemente está certo, acredito que eu tenha que calcular o tamanho do drag e somar ou diminuir do total, ou algo assim, conseguem me ajudar?
Comment: Não vai chegar a 100 nunca porque está se baseando na borda esquerda do quadrado, e essa borda nunca irá tocar o final da barra.
Answer: Conforme o @Sam disse você tem que descontar a largura do botão, montei um exemplo com a correção.

    $('.drag').draggable({
  axis: 'x',
  containment: 'parent',
  drag: function() {
   var largura = ($('.barra').width()) - 20;
   var posicaoX = $(this).position().left;
   var valor = (posicaoX * 100) / largura;
   $('#mostra').val(valor);
  }
 });
 .barra{
  width: 150px;
  height: 5px;
  background: #000000;
  position: relative;
  margin-top: 20px;
  margin-bottom: 20px;
 }
 .drag{
  width: 20px;
  height: 20px;
  background: #d18f4f;
  position: absolute;
  top: -7px;
  left: 0px;
  cursor: pointer;
 }

<input type="text" value="0" id="mostra">


Regression Methods in Causal Inference

Question: In the most basic regression methods of causal inference (randomized experiment case), it's known that we can use covariates to predict the observed outcome, i.e. $Y^{obs}$ and the model is
$$
Y^{obs}_i=\alpha+\tau W_i+\beta X+\epsilon_i
$$
in which $\tau$ is ATE and $W_i$ is assignment. And we know that the causality of regression coefficients is guaranteed by randomized experiments. In other words, even if the linear relationship is wrong, we can still get the correct ATE. Regression helps to reduce the variance of ATE.
Therefore, my question is whether we can use another model, such as Tree or Neural Networks to add the covariates, that is
$$
Y^{obs}_i=\tau W_i+f(X)+\epsilon_i
$$
If so, can this model still reduce the variance? How to prove it and how to reduce possible overfitting?
Answer: Yes, we can use a non-linear estimator to reduce the variance and get more accurate results. There are many different techniques. To start of, for example, BART (Bayesian Additive Regression Trees) has been found to be an excellent algorithm for such  "out-of-the-box" causal inference tasks, see Automated versus do-it-yourself methods for causal inference: Lessons learned from a data analysis competition by Dorie et al. (2017) for a more detailed investigation. In the last 5 to 6 years representation learning-based methods have also blossomed (starting with the work of Johansson et al.  (2016) Learning Representations for Counterfactual Inference) offering often very competitive results too.

Regression Methods in Causal Inference


Fourier transform of $f^{n}$

Question: It is true that if a function $f$ has fourier transform $\hat{f}$ then, $(\hat{f})^n =  FT(f^{n})$ (where $FT$ denotes the fourier transform). If so, why?
Answer: In general, no. The Fourier Transform of a product of functions is the convolution of their Fourier Transforms, e.g.
$$\widehat{(fg)}(x) = (\hat{f}* \hat{g})(x) = \int_{-\infty}^{\infty} \hat{f}(x-y) \hat{g}(y) \, \mathrm{d} y. $$
Showing this is a play with the definition of the Fourier Transform, and a good exercise to do to convince yourself.
Comment: $\widehat{f \ast g} = \hat{f} \hat{g}$ the opposite direction needs the Fourier inversion theorem

Fourier transform of $f^{n}$


Best estimate of variation for duplicate data?

Question: I'm simplifying a bit here, but my dataset contains 10 stations, each with measurements of growth at time increments.  For each measurement, there are between 1-4 duplicates measured at the same day+station.  The response data are roughly lognormal, and about 10% of the growth values are negative.  There is a fair amount of error/high variability/heterogeneity in both the measurement technique and the natural system.
The challenge is that for a subset of day+station (when there are at least  duplicates), one of the individual measements appears to be an outlier when assessed graphically.
What is then the best method to identify values (for each day+station) that have the highest (higher than normal) variation? Median average deviation (MAD) is the obvious candidate, but it is correlated with the number of duplicates - MAD decreases with the # of dupes. However, MAD does not 'respond' to outliers, so is not helpful in this case.  Stdev of log+1 dataset has no real meaning for folks comparing two values.
My goal is to identify the least repeatable measurements to flag as suspicious.
Answer: If these are pairs of measurements (ie 1-4 duplicates means 2 to 8 individual, paired measurements), you could scan the difference between the pairs for obvious outliers if the goal is to 'flag'. However, several concerns with that are: are 'duplicates' (or subsamples) taken at the same time? Could the presence of outliers be a function of your treatment or time of day? What determined the number of subsamples at each point? Finally, if willing to accept the likelihood of outliers in the better-subsampled points, it is important to consider how to incorporate that information for the samples with only 1 duplicate.


prove that in a cyclic group of even order, there is exactly one element of order 2.

Question: prove that in a cyclic group of even order, there is exactly one element of order 2. Please prove the requirements.
Answer: Let $G$ be a cyclic group of even order, that is, a group with an order of $2n$ where $n$ is a natural number. We know that every cyclic group G verifies the inverse of Lagrange's Theorem, that is, there exists a unique subgroup H of G with order m, for every divisor $m$ of $n$.
Now, considering that $G=<g>$ where $g$ is a generator of the cyclic group $G$, we have that $g^n$ is an element of order $n$ in $G$, because $(g^n)^2=g^{2n}=1$. This element generates the subgroup $<g^n>=\{1,g^n\}$ of $G$, which has order 2, and is therefore the only subgroup of $G$ with order two (according to the inverse of Lagrange's Theorem, and because $n$ is a divisor of the order $2n$ of our group $G$).
Now let's suppose that there exists another element $x\neq g^n$, $x\in G$, such that the order of $x$ is 2. Then, $<x>=\{1,x\}$ would be a subgroup of order 2 of $G$, which would be different than $<g^n>$, contradicting the uniqueness stated above. This means that $g^n$ is the only element of $G$ with order 2.
Edit: Would anyone mind explaining the downvote? I think my answer is correct and solves the problem.
Comment: I know it's a late reply but I just upvoted your answer. Don't understand as well why it hadn't any upvotes until now, the answer is pretty much helpful and well constructed.


Form model not validating before POST

Question: I'm trying to make a registration page in my web application. However, the validation on my form is not working. For example I have my password minimum length be 4 charterers but if I enter a blank password it still makes the POST request. How do I tell the form that the model is not valid and that they should change their password,email, etc?
AccountController.cs
[HttpPost]
public ActionResult Register(RegisterModel model){
    if (ModelState.IsValid){
        // Insert into database
    }
    // There was an error
    return View(model);
}

RegisterModel.cs
public class RegisterModel
{

    [Required]
    [Display(Name = "UserName")]
    public string UserName { get; set; }

    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 4)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

Register.cshtml
@model RegisterModel
    <form asp-controller="Account" asp-action="Register" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post" class="form-horizontal">

Create a new account.

                <input asp-for="UserName" class="form-control" />

                <input asp-for="Email" class="form-control" />

                <input asp-for="Password" class="form-control" />

                <input asp-for="ConfirmPassword" class="form-control" />

                <button type="submit" class="btn btn-default">Register</button>

    </form>

Soultion
Since The Register page was under Home controller, and the Register logic was in the Account controller i had to define the the view to load the Register.cshtml page.
[HttpPost]
public ActionResult Register(RegisterModel model){
    if (ModelState.IsValid){
        // Insert into database
    }
    // There was an error
    return View("~/Views/Home/Register.cshtml",model)
}

Comment: have you included the jquery unobtrusive validation files (probably should be in your `_Layout.cshtml`)?
Comment: OP: this is **not** *ASP.NET MVC*, this is *ASP.NET Core*
Comment: @cFrozenDeath , yeah its still ASP.NET MVC, its call ASP.NET MVC 6 or ASP.NET 5
Comment: https://blogs.msdn.microsoft.com/webdev/2016/02/01/an-update-on-asp-net-core-and-net-core/
Comment: Look at the generated markup.  Do you see data-val-* attributes?
Comment: When you submit the form, it should be posted. rite ?What is the behavior you are  expecting ?
Answer: When you click on the submit button, it posts the form to the server where your action method has code to check whether the form is valid ( ModelState.IsValid). This is server side validation. The form has to be posted to server for this to happen.
If you want to the validation to happen at client side (and not submit the form if the validation fails), you need to make sure that you have the relevant javascript libraries/files loaded to your page.

jquery.validate.js
jquery.validate.unobtrusive.js

You may include those in your layout file or your specific view. If it is the specific view, make sure you include those in the Scrips section.
@section Scripts
{

}

Update the path of the files to match with where you have those files in your  project. You may also use the location to a public cdn.
Comment: Yeah ok so if i change it to             return View("~/Views/Home/Register.cshtml");, the validation works. Since you helped me figure it out. Ill give you the credit.
Comment: Well im ok with it happening server side. For example i have ConfirmPassword element. That checks to see if the password is the same as the confirmed password. In the model i compare the two, so if they don't equal each other how do i then send that back to the client. Is there a way to check the Model state for which field is invalid ?
Comment: The `return View(model);` line will send the posted model back to the view(with relevant error messages). If you are purticular about seeing which one failed, Take a look at [How to figure out which key of ModelState has error](http://stackoverflow.com/questions/15296069/how-to-figure-out-which-key-of-modelstate-has-error);
Comment: return View(model); just process "Error.
An error occurred while processing your request.".  Could this be a routing issue. My registration page is in my Home controller but the form POST to the Account controller.
Comment: Then you have to explicitly return to that view like `return View("~/Views/Home/Registration",model);`
Comment: return View("~/Views/Home/Registration",model); gives me the same error. Do i have to change it from  "public IActionResult Register()" to "public IActionResult Register(RegisterModel model)" ? Or maybe a different address?
Comment: No. Your get action dont need to be updated. You should find out why it is throwing the errror. Sorry i have no idea why it fails because i can't see your code.
Comment: return View("~/Views/Home/Register"); returns an error as well, so maybe the route is messed up, ill play around with it
Comment: Sorry ! I missed the file extension last time. Try `return View("~/Views/Home/Registration.cshtml",model);`
Comment: Thanks Shyju. I was wondering if i could ask you one more question. Do you know of a way where, i can do a check, say the username already exists, and if it does i can set the model to be invalid? Would i have to make a new property in the model ?
Comment: You can use `ModelState.AddModelError` See this post http://stackoverflow.com/questions/35144221/username-availability-and-insert-new-user
Comment: Thanks for all the help i wish i could give you ten more points.

Form model not validating before POST


Create win8.1 boot USB from temp boot environment?

Question: Need to create a Win 8.1 boot USB drive.  However don't have any native Win8.1 machines to run MS mediacreationtool.exe from, all Win8.1 environments I have available are virtualised.  Only native OS's we have are Server 2012, WinXP & various Linux.
So question is are there any Win8.1 run from DVD type environments available that I could boot to and run mediacreationtool.exe from there?
Comment: Eh? It runs fine on Server 2012, and it even runs in virtual machines, if you pass the USB device through.
Comment: Doesn't for me, mediacreationtool.exe returns error "This platform is not supported"
Comment: Also I have no "Enhanced Session Mode" available under Hyper-V settings so can't do USB passthrough that way.
Comment: Try using the [Windows USB/DVD Download Tool](https://www.microsoft.com/en-us/download/windows-usb-dvd-download-tool) instead.
Comment: Just tried that and it returns: "This application requires .Net framework 2.0"
Not sure I want to risk destabilizing the 2012 server by installing that.
Comment: If you have a 2012 server without 2.0 installed I'd argue stability is already an issue.
Answer: Answer now found - create bootable USB manually and simply copy files there from the .iso, didn't realise that it was that simple
Would probably be more useful if I explained how:

diskpart
list disk
select disk 2 - (USB drive)
clean create partition
primary select partition 1
active
format fs=ntfs
exit
XCopy {path to mounted .iso}*.* {drive letter of USB stick} /s/v


Proof of Set Theory Algebra using Logic

Question: Let A, B and C be sets.  I am trying to prove that
$$A\cup(B \setminus C)=(A\cup B)\setminus(C\setminus A)$$
And I am supposed to use logic to work through this problem, so first I let
A={x|P}, B={x|Q}, and C={x|R}
Then I did
$$x \in (A\cup(B \setminus C)) \iff x \in \{y| P \lor (Q \land  \neg R\} $$
I have done algebra on it to come to a lot of different things but none of them seem to be getting me in the right direction.  I will list a few of the different places I have gotten if somebody wants to go from there, but besides distributing the P, I don't know what to do.

$x \in \{y |(P\lor Q) \land (P\lor \neg R\}$
$x \in \{y| ((P\land P)\lor (P \land \neg R)) \lor ((Q \land P) \lor (Q \land \neg R))$
$x \in \{y|((P \lor P) \land (P \lor Q)) \lor ((P \lor Q) \land \neg R)\}$

Obviously, my goal would be to get to $x \in \{ y|(P \lor  Q) \land \neg(R \land \neg P)\}$, I could finish from there, but not from where I am.  Thank you to anyone who helps!
Comment: Asaf Karagila, is there any way you can help me with this? I am totally lost!
Comment: If you want to have your comments notified to a concrete person you must use, in this case, @AsafKaragila
Comment: Hint: ~(R ^ ~P) iff (~R v ~~P) iff (~R v P) where ~ is the negation operator. Relate this to your 1st equation.
Answer: $
\newcommand{\calc}{\begin{align} \quad &}
\newcommand{\calcop}[2]{\\ #1 \quad & \quad \unicode{x201c}\text{#2}\unicode{x201d} \\ \quad & }
\newcommand{\endcalc}{\end{align}}
\newcommand{\ref}[1]{\text{(#1)}}
\newcommand{\true}{\text{true}}
\newcommand{\false}{\text{false}}
\newcommand{\empty}{\varnothing}
\newcommand{\diff}{\mathbin \triangle}
$Here is how I would approach a problem like this: start at the most complex side of the equality, and calculate the elements of that set by expanding the definitions, and using the laws of logic to simplify.

In this case, we start with the right hand side of the statement, and calculate for all $\;x\;$ as follows:
$$\calc
x \in (A \cup B) \setminus (C \setminus A)
\calcop\equiv{definition of $\;\setminus\;$}
x \in A \cup B \;\land\; \lnot (x \in C \setminus A)
\calcop\equiv{definitions of $\;\cup\;$ and $\;\setminus\;$}
(x \in A \lor x \in B) \;\land\; \lnot (x \in C \land x \not\in A)
\calcop\equiv{logic: DeMorgan}
(x \in A \lor x \in B) \;\land\; (x \not\in C \lor x \in A)
\calcop\equiv{logic: factor out $\;x \in A\;$, using the fact that $\;\lor\;$ distributes over $\;\land\;$}
x \in A \;\lor\; (x \in B \land x \not\in C)
\calcop\equiv{definitions of $\;\setminus\;$ and $\;\cup\;$}
x \in A \cup (B \setminus C)
\endcalc$$
By set extensionality, this proves the statement.

Note how we make things easy for ourselves by starting with the most complex side, since it is simpler to simplify then to introduce complexity: you have less choice when trying to simplify, so you have less options to consider. That often makes it more straightforward to complete your proof.
Answer: Your goal is to show that
$$x \in \{ y|(P \lor  Q) \land \neg(R \land \neg P)\}=\{ y|(P \lor  Q) \land (P \lor \neg R)\},$$ where we have used $\neg(R \land \neg P)=P \lor \neg R.$
But note that you have shown
$$x \in \{y| P \lor (Q \land  \neg R\}=\{y| (P \lor Q) \land (P \lor \neg R)\},$$
and so you are done.

Proof of Set Theory Algebra using Logic


Getting content from url which requires cookies to be enabled

Question: Using java, how do I get the content from a webpage where cookies and javascript are required to load the page?
Comment: SO is not a search engine.
Comment: If cookies and JavaScript are required, then it may mean that the author specifically doesn't want you to do exactly this!
Answer: Without code or more information it's hard to help.
You'll want a URLConnection with a CookieHandler, you can find some example code here:
https://blogs.oracle.com/CoreJavaTechTips/entry/cookie_handling_in_java_se
Answer: You should try learning about HttpURLConnection and how to send information like cookies to websites via Http headers


How to Add Data to a CollectionView for Just the Newly Append Index from an array Swift

Question: Hey I'm trying to figure out. After I append a string to the array that is the collection views data reference. How do i just/only reload that latest string/ index recently append the the array and not the whole collection view
Ok so I know so far this reloads all which will cycle through all index in the datasource / Array of Strings :
log.reloadData()

So I append the string:
// collectionViewDataSource is a UICollectionView

var collectionViewDataSource = [String]()

collectionViewDataSource.append("new string")

Reload:
// now to figure out how to update the view for just that index and not complete reload every cell, but just add a new cell

log.reloadItems(at: <#T##[IndexPath]#>)

Answer: You need to determine the index path of the newly added data. In your case it's trivial. Use the count of the array before adding the new object.
let newIndexPath = IndexPath(item: collectionViewDataSource.count, section: 0)
collectionViewDataSource.append("new string")
collectionView.insertItems(at: [newIndexPath])

Note that you need to insert a new item into the collection view, not reload one.
Comment: Thanks, You know your like a legend on here. You Answer a lot of questions


How to get value from Chrome Extension

Question: I am trying to retrieve tagraw from storage. link
I put breakpoints in my background page, but can't see it go in my code.
Thanks for looking. I appreciate it!
------------popup.js--------------
 handleClick_: function () {
   var tagraw="";
   chrome.storage.sync.get({
      likesName: 'yourname'
    }, function(items) {
         tagraw= items.likesName;
   });
 }

----------------------popup.html-------------------

  <form>
    <select id="timeframe">
      <option value="mustpk">-pick-</option>
    </select>
    <button id="button">Send</button>
  </form>

--------------options.js--------------------
function save_options() {
    var likesName  = document.getElementById('likename').value;
    chrome.storage.sync.set({
        likesName:  likesName
    }, function() {
      setTimeout(function() {
          status.textContent = '';
      }, 750);
     });
}

Answer: --Instead of using chrome.storage I switched to instead use the new html5 storage.
//to save it to html5 storage
localStorage.setItem("likename", likesName);

//to retrieve it from html5 storage.
var tagraw= localStorage.getItem("likename");

How to get value from Chrome Extension


Significado de sinais em C#

Question: O que são os sinais  < >, [ ], { },  ( ) fazendo jogo no Unity?
Comment: sugiro voce dar uma olha em logica de programação
Comment: A resposta resolveu sua dúvida? Acha que pode aceitá-la? Veja o [tour] se não souber como faz. Isso ajudaria muito indicar que a solução foi útil para você. Também pode votar em qualquer pergunta ou resposta que achar útil no site todo (quando tiver 15 pontos).
Answer: Isoladamente não servem para muita coisa, você precisa entender os contextos de cada uma.
Importante dizer que isso nada tem a ver com Unity que é só uma biblioteca, responderei sobre C#.
Todos esses são delimitadores assim como '' e "" são delimitadores de caractere e texto. Eles usam o mesmo caractere para iniciar e finalizar o texto. Outros delimitam de forma diferente.
Muitos deles tem funções e significam coisas diferentes dependendo do contexto que estão sendo usados.
<> delimita informações de parametrização de generics. Pode começar estudar em Qual a diferença entre "generics" (Java/C#) e "template" (C++) e Quais as diferenças entre Tipos Genéricos em C# e em Java?. Depois falarei de outro par. Eles (chamados de angle brackets) funcionam como se fossem os parênteses de uma função, então ali tem parâmetros, mas aí é para tipos genéricos em vez de variáveis.
[] é usado como indicador de tipo array ou operador de uma coleção de dados onde dentro pode ir o índice de acesso ao elemento desejado (pode ser um pouco diferente disso, mas não vale a pena entrar em detalhes aqui). Esse operador pode ser sobrecarregado em cada tipo. Quando é usado só para indicar um array dentro deles pode ter o tamanho dele.
Outro contexto que ele é usado é para delimitar atributos personalizados.
É chamado de brackets (colchetes).
{} é chamado de braces (chaves).
Pode ser usado para delimitar blocos de código em várias formas. Sempre que precisar ter vários statements sendo tratados como uma coisa única precisa usá-las. Pode ser um bloco para unir ações que devem ser executadas condicionalmente após um if, else, ou uma repetição como o while, for, foreach, ou ainda em um try ou catch ou finally. Dá até para criar um bloco solto só para criar escopo mesmo não estando associado a um comando desses.
Além disso serve para agrupar comandos de uma função, de uma classe ou outro tipo de dados ou até de um namespace. Em alguns deles só é obrigado usar se tiver mais que uma linha no bloco.
Também pode ser usado para inicializar alguns tipos de coleções de dados como os arrays, dicionários, listas e qualquer outra que tenha uma capacidade de ir adicionando elementos. Também serve como inicializadores de objetos.
() é principalmente o operador de agrupamento de expressões. É igual a matemática, sabe? Só que na matemática tem colchetes e chaves também para dar uma diferenciação e uma certa importância entre eles, em programação não diferenciamos isso, vai colocando parênteses dentro de parênteses, os mais internos são os mais precedentes. Em C# não pode sobrecarregá-los.
Também pode ser usado como delimitador de parâmetros de função, mesmo que não tenha parâmetros precisa ter um par vazio para indicar que é uma função. O mesmo vale na chamada onde passa argumentos que devem estar entre eles ou se não tiver algum argumento devem estar vazios.
Outro contexto que ele é usado é para definir tuplas, que são estruturas compostas por 0 ou mais dados de tipos heterogêneos, é como se fosse uma struct mas de forma mais simples e sem ter um tipo nomeado explicitamente. Tem exemplos no fim da minha resposta.
Esqueci algum? Pode ser que depois de eu ter escrito isso alguns deles sejam usados em outros contextos.
Está confuso com tudo isso? Talvez o mais recomendado é aprender tudo de forma mais estruturada, e ver esses símbolos usados nos seus devidos contextos, com explicações mais detalhadas.

Significado de sinais em C#


Second Division League popularity

Question: I have a general question regarding soccer (football), in terms of which events are more widely viewed by fans:
Do fans tend to follow second division leagues, or are they more likely to follow domestic cups which might involve second division league clubs?  Do second division leagues tend to have a significant following at all (I realize it will vary from nation to nation - I'm asking for a general assessment.)
Comment: -1 ["You should only ask practical, answerable questions based on actual problems that you face. Chatty, open-ended questions diminish the usefulness of our site..."](http://sports.stackexchange.com/faq#dontask)
Answer: I think there are two types of so called "fans" or "supporters". I'm a big fan of my local team (first kind), but I do also like teams from other leagues (a secondary type of supporting). My point is that the first kind of fans will support a team no matter the division. Examples: Argentian teams like Rosario Central and River Plate. Juventus (sent to serie B because of legal issues) in Italy is another example. The thing is popular teams play on the premier league because they have money and support to have the best players (which are paid by the supporters in the end).

Second Division League popularity


Is stats.queues method removed in Sidekiq 3.3.1? How to fetch queues now?

Question: I just upgraded to Rails 4.1.9 and Sidekiq 3.3.1 (running on Heroku).
I've been using the Sidekiq API and based on the documentation https://github.com/mperham/sidekiq/wiki/API the stats.queues method should still be there, but in practice it doesn't seem to be:
irb(main):001:0> stats = Sidekiq::Stats.new
=> #<Sidekiq::Stats:0x007f73db350290 @stats={:processed=>464, :failed=>3, :scheduled_size=>2, :retry_size=>0, :dead_size=>2, :processes_size=>1, :default_queue_latency=>0, :workers_size=>0, :enqueued=>0}>

irb(main):002:0> stats.queues
NoMethodError: undefined method `queues' for #<Sidekiq::Stats:0x007f73db350290>

irb(main):006:0> Sidekiq::Stats.new.methods - Object.methods
=> [:processed, :failed, :scheduled_size, :retry_size, :dead_size, :enqueued, :processes_size, :workers_size, :default_queue_latency, :fetch_stats!, :reset]

Is it deprecated and the docs are outdated? Or could this be an environment problem?
Is there a way to list the queues in Sidekiq 3.3.1?
Answer: They remove queues method from stats by mistake, pr here
You can use Sidekiq::Stats::Queues.new.lengths now, and old behavior after new release


Campos de telefone e CPF no SQL?

Question: Estou com dúvida de que tipo de variável criar para os campos de CPF e Telefone no banco de dados, eles devem ser salvos assim:
(34) 9652-5214
134.124.214-47

Com nchar não complicaria na busca? Mas no caso do nchar, como coloco pra ele não poder armazenar letras? E como coloco para quando o usuário buscar, poder buscar só pelos números, sem () etc? Só colocar mascará no campo de busca?
Comment: http://pt.stackoverflow.com/questions/47871/tipo-do-campo-cpf-ou-cnpj-no-banco-de-dados-varchar-ou-int/47873#47873
Comment: Pra que gravar a mascará?
Comment: Eu gravaria apenas os números e aplicaria máscara na hora de realizar o registro, visualizar a consulta ou fazer uma busca.
Comment: em outro form vou exibir uma tabela como `DataGridView` daí lá pra  ficar exibindo com pontuação etc.. Ou dá pra por mascara? Se der então pode gravar sem mascara. O problema é que não dá pra colocar `int` pois variavel int tem limite curto de tamanho
Comment: Grave ambos sem mascara em campo do tipo texto e quando for exibir formate como deseja na saída
Comment: ok, mas tipo campo texto armazena letra também, tem como fazer para ler somente números? Como `int` e `numeric`?
Comment: Você faz o tratamento no campo que está recebendo esses dados no c#. Você vai usar o que? `TextBox` normal mesmo?
Comment: sim sim, apenas textbox
Answer: No C# existe um componente chamado MaskedTextBox. Nele é possível especificar uma máscara para o campo.
Por exemplo no seu campo telefone pode ser a máscara: (##) #####-#### na propriedade Mask. Ele aceitará somente numéricos.
Na hora de ler o valor, você utilizará o seguinte código:
this.maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
String telefone = this.maskedTextBox1.Text;

Assim você terá apenas números na String telefone, para assim salvar no banco de dados.
Comment: no caso eu declararia os dois campos do banco de dados como `nchar(14)` né?
Comment: Não precisei nem da segunda linha, só mudando o formato da mascara ele já grava sem a mascara.. Vlw
Answer: Qual é o banco de dados?
Para o CPF aconselho utilizar o tipo Char e especificar um tamanho de 11 caracteres, isso sem guardar os pontos e o hífen.
Para o telefone aconselho um campo VARCHAR de tamanho máximo 11, já que em algumas regiões do país utilizam um campo a mais ou um campo a menos.
ALTER TABLE tabela ADD COLUMN cpf CHAR(11);
ALTER TABLE tabela ADD COLUMN telefone VARCHAR(11);

Espero que esta resposta tenha ajudado, qualquer dúvida estamos a disposição.
Comment: mas aí que tá, `char` e `varchar` aceitam letras etc, daí se o bixinho de orelha (usuário) gravar dado errado é considerado falha no sistema, tem como restringir uma char ou varchar para ler apenas números?
Comment: Faça a verificação no servidor, quando a aplicação for salvar os dados, se não for feita a verificação quem vai retornar erro para a aplicação vai ser o banco de dados. Veja sobre a diferença entre char e varchar, o char sempre armazena um tamanho fixo para a coluna.
Answer: Todas as verificações você pode fazer antes de armazenar no Banco de dados as informações, por exemplo um formulário pode ter mascara que adiciona o (xx) xxxx-xxxx, mas você faz um tratamento para remover os demais caracteres que não sejam número, desta forma armazenando apenas número em um varchar no banco.
Lembrando se você quer que seja verificado o valor antes de enviar para confirmar que esta mandando apenas dígitos corretos, utilize expressão regular para reconhecer a cadeia algo como ([0-9]{2})\s[0-9]{4}-[0-9]{4} não estou falando que esta expressão regular é a certa, mas é algo bem próximo do que você pode usar, assim ele vai obrigar que tenha dois números no DDD entre parênteses e 8 números seguidos separados por -.
Isto já garante, depois um split ou um tokenizer dependendo do que você usar para separar e remover o que não é digito, e concatena todo o resto como número e salva no banco de dados.
Quando for acessar, para apresentar os dados da forma visual só criar uma mascara novamente para aplicar em cima do valor restaurado. Como banco de dados os valores precisam estar corretos, os tratamentos quem faz e sempre o programa para pegar o valor e demonstrar, pelo menos a melhor forma para você pensar é esta.

Campos de telefone e CPF no SQL?


why split data between train and test?

Question: I am looking to buy a house, and I therefore look at a database of houseprices up to today (some are sold, some ads are live). I want to do a regression so i can see if a house for sale is over or underpriced. Lets assume i have a simple model:
Houseprice = sqm + renovated + swimmingpool + area
Now I want to do a random forest to see what the predicted price for a house should be. The normal wisdom is to split the dataset in a training and testing set. However I dont get that.
For my purpose, isn't it better to use all the data to estimate the model, and then use the residual to see if a house is over or underpriced?
I dont care about future predictability. Is now I need to estimate the house that is the best bargin.
Answer: The rationale for splitting the dataset into training, (validation) and testing subsets is to try to reduce overfitting (by getting an estimate of it). The concern for overfitting implies that you want to generalize results to a broader population.
In your scenario, it seems your database of house prices today is the total population you care about, i.e. you want to choose one of the houses in your dataset, and you do not concern yourself with future hypothetical houses that might come up. If that is the case, you are right in that there is no need to subdivide your dataset and you should estimate your model with all the observations you have.
In other words, what you are trying to do is to describe your dataset as a function of certain attributes you care, hence no inference is involved.
Comment: Your answer reads a little bit like: "data splitting avoids overfitting", which is wrong. It helps to measure the amount of overfitting and supports model selection. But the model can still be overfitting on the training data, like e.g. a random forest.
Comment: I am not sure I follow your comment. The model can overfit the training data even without the data split? 'Over'-fitting with regards to what? If the sample is the population of interest, to what are you comparing the fit?

why split data between train and test?


Best Chassis in F1 cars

Question: I have read that recently last year that Red Bull racing cars have the best chassis.
How do we analyse the chassis of an F1 Car?
what is the science behind the best chassis and the worst?
Answer: There are various aspects to what makes a good chassis.
The ideal chassis gives lots of downforce to be fast through corners, but has little drag in order to be fast in a straight line. Unfortunately adding downforce tends to add drag, so it's a trade-off.
A good chassis also gives consistent downforce: it's no good if it only provides downforce when travelling straight, and not when turning through a corner. It should also provide balance - i.e. similar amounts of downforce on the front and rear wheels; if there isn't enough downforce on the front wheels the car will understeer; if there isn't enough on the rear wheels it will oversteer.
Generally when F1 commentators and fans talk about "the best chassis" they're talking about the one that produces the most downforce, i.e. is fastest through the corners. Telemetry from F1 (not always publically available as far as I know) can show the minimum speed a car has used when going through a corner, the higher the minimum speed the better the chassis. Articles such as this German GP one from formula1.com compare relative gaps in seconds between the different teams in slow, medium and fast corners, as well as on the straights. (As speed is distance/time, then comparing time through a corner is effectively the same as comparing who carried the most speed through the corner.)

Best Chassis in F1 cars


How can I borrow diminished chords with Modal Interchange?

Question: I once heard a song that had two diminished chords in it, which probably has something to do with Modal Interchange, it's just I've never encountered it before. When I heard this song it sounded really good, so how can I use it on my own?
Answer: The concept of modal interchange (or "modal borrowing" or "mode mixture") is that you "borrow" a chord quality that is diatonic in the parallel key. If you're in, say, C major, you can borrow chords that are diatonic to C minor (and vice versa).
So to find opportunities to borrow diminished triads, let's first find where those diminished triads are. In major, it only exists as vii°. And in minor, it exists both as vii° and as ii°.
Since both major and minor have the same vii° chord, you can't really borrow one into the other. But there is one remaining diminished triad: ii° in minor. Thus, from a standpoint of modal interchange, the only other way to introduce a diminished triad is by using ii° in a major key.
The same logic applies for the seventh chords. In major, the seventh chord on scale-degree 7 is half diminished; in minor, it's half diminished when built on scale-degree 2 and fully diminished when built on scale-degree 7.
There are other ways to introduce diminished harmonies, especially with secondary leading-tone chords (vii°/x). But in terms of strict modal interchange, this is the only way.


Verbal problem with I think is exponential function! help.

Question: I need help with this exercise.
In a city of 5,000 inhabitants, the daily rate of infection with a flu virus varies directly with the product of infected people and the number of uninfected people. When 1,000 people have been infected, the flu spreads at a rate of 40 new cases per day. For what number of people infected, is the daily infection rate the highest? (using precalculus)
I used this exercise as a guide.
http://matema.ujaen.es/jnavas/web_modelos/problemas/ptema4.pdf
It is in Spanish. It is different but the context is quite similar. Here is a translation of it:
https://tinyurl.com/y7akm37r
So the function is
$$y(t)=\frac{5000}{1+Ae^{-rt}}$$
$$y(0)=\frac{5000}{1+Ae^{-r(0)}}$$
$$1=\frac{5000}{1+Ae^{0}}$$
$$1=\frac{5000}{1+A(1)}$$
$$1=\frac{5000}{1+A}$$
$$(1+A)\cdot1=\frac{5000}{1+A}\cdot (1+A)$$
$$(1+A)\cdot1=5000$$
$$1+A=5000$$
$$A=5000-1$$
$$A=4999$$
My question is $y(0)=1$ because in day 0 there is just 1 infected, the carrier? My other question is there any formula to determine that the function is $$y(t)=\frac{5000}{1+Ae^{-rt}}$$
Therefore,
$$y(t)=\frac{5000}{1+4999e^{-rt}}$$
Then, the problem says when 1,000 people have been infected, the flu spreads at a rate of 40 new cases per day. So is this right?
$$1000=\frac{5000}{1+4999e^{-40t}}$$
$$(1+4999e^{-40t})\cdot1000=\frac{5000}{1+4999e^{-40t}}\cdot(1+4999e^{-40t})$$
$$(1+4999e^{-40t})\cdot1000=5000$$
$$1000+4999000e^{-40t}=5000$$
$$4999000e^{-40t}=5000-1000$$
$$4999000e^{-40t}=4000$$
$$\frac{4999000e^{-40t}}{4999000}=\frac{4000}{4999000}$$
$$e^{-40t}=\frac{4}{4999}$$
$$\ln e^{-40t}=\ln\frac{4}{4999}$$
$$-40t\ln e=\ln\frac{4}{4999}$$
$$-40t(1)=\ln\frac{4}{4999}$$
$$-40t=\ln\frac{4}{4999}$$
$$\frac{-40t}{-40}=\ln\frac{4}{4999}\cdot\frac{1}{-40}$$
$$t=\ln\frac{4}{4999}\cdot\frac{1}{-40}$$
$$t\approx0.1783$$
So the function is:
$$y(t)=\frac{5000}{1+4999e^{-r\cdot0.1783}}$$
$$y(t)=\frac{5000}{1+4999e^{-0.1783r}}$$
Anyway, this is an exponential function, won't have a maximum. So then I came back to the same point in the circle where I started. :-( So I can't answer the question.
Please help.
Comment: See [the logistic function](https://en.wikipedia.org/wiki/Logistic_function)
Comment: I don't follow this at all.  If the rate of infection varies directly with the product of the number of persons infected and those not infected, shouldn't the equation be $y(x)=Kx(5000-x)$ for some constant $K$, where $x$ is the number of people infected and $y$ is the daily infection rate?  You seem to be taking $y$ to be a function of time, although the problem statement says nothing about how the rate varies with time.
Comment: First, you can solve the diferential equation. After doing that you could take the derivative, find the time where it reaches the maximum, then evaluate $y(t)$ and find the solution. That's way much more work that they expect from you (solving a ODE and taking derivatives is not pre-calc). Second, $r$ is not the rate of infected, it's a constant defined in your PDF. Note that you ended with a function $y(t)$ with no variable $t$, that should say to you that there's something wrong. My answer is what they expect you to do (pre-calc) and the easy way to solve the question.
Answer: For the question you want to answer there is no need in solving the diferential equation. The diferential equation you solved is
$$y'= \alpha y(K-y)$$
where $\alpha$ is a constant and $K$ is the total population.
When there are $y = 1000$ infected people, the rate of infection for a total population of $K = 5000$ is $y'=40$.
You plug the values into the diferential equation and you get $\alpha = 1*10^{-5}$
You have now the function
$$y'(y) = 1*10^{-5}y(5000-y)$$
which is a 2nd degree polinomial. Can you take it from there?
PD: computing $\alpha$ isn't necesary to know at what $y$ the maximum will be, but to know what will be the value maximum infection rate.
Edit: I forgot to answer 2 questions you asked in the middle.

$y(0)=1$ is the initial condition. That means that they decided to be 1 infected on day $0$.

There are no "formulas" to solve an ODE. In general it's not even posible to solve them analitically. There are many methods, but this particular ODE can be solved "separating variables". Again, you don't need to.
Comment: but how is done without using calculus? It is supposed to be done using pre-calculus. : - /
Comment: I haven't used calculus. I only have replaced values and now you have to solve a 2nd degree polinomial.


I am getting Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes) on line 21 unble to understand it

Question: $testCase="";
$strength="";
$i="";$j="";$sum=0;
$submit=$_POST['submit'];

if(!empty($submit))
{
    $testCase=$_POST['TestCase'];
    $strength=$_POST['strength'];
}
else
{
    die("Please enter the Number of test case
 and  Strength of audience");
}
while($testCase>0)
{
    for($i=1;$strength;$i=$i+1)
    {
        $user[$i]=$i+1;
    }

    //finding the total number of stickers

    for($j=1;$j<$strength;$j=$j+1)
    {
        $sum=$sum+$user[$j];
    }

}
echo "The total number of Stickers are = " . $sum;

Comment: You have an infinite loop, `$testCase` never changes inside the loop.
Comment: Few problems here. Why is `$testCase` initialized as a string when you are comparing it with 0?
Comment: $testCase is not initialized to anything.
Comment: Thanx for the help , so silly of me for not noticing it.
Comment: This should not be closed as a duplicate for memory issues, the problem is unrelated to the actual amount of memory available.
Answer: Because you do this: while ($testCase > 0) { and do not decrase the $testCase ever. Also, it's better if you put your loops into your if condition.
And you do not need to create $strength variable. Try this:
$sum = 0;
if (!empty($_POST["submit"]) && !empty($_POST["testCase"]) && !empty($_POST["strength"])) {
    $testCase = $_POST["testCase"];
    while ($testCase > 0) {
        for ($i = 1; $i < $_POST["strength"];$i++) {
            $user[$i] = $i + 1;
        }
        for ($j = 1; $j < $_POST["strength"]; $j++) {
            $sum = $sum + $user[$j];
        }
        //Here you need to decrase testcase
        $testCase--;
    }

} else {
    die("Please enter the Number of test case
 and  Strength of audience");
}
echo "The total number of Stickers are = " . $sum;

NOTE: In PHP arrays are always started with 0, if you do not specify keys directly. Check your for loops for this.
Answer: This line:
  for($i=1;$strength;$i=$i+1)
should probably be:
  for($i=1;$i<$strength;$i=$i+1)


Em um programa de console em C#, onde é definido a classe principal?

Question: Estou fazendo meu primeiro programa de console em C#, apenas para fazer alguns testes.
Tenho duas classes: HelloConsole.MainClass e HelloConsole.Calc.
Tenho o seguinte código:
using System;

// Aprendendo o alias de namespace

using C = System.Console;

namespace HelloConsole
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Calc calc = new Calc (3);

            /**
             * Agora já sei que WriteLine aceita múltiplos parâmetros
             * Tipo o Python
             * */

            C.WriteLine ("O valor é {0}", calc.value);

            C.WriteLine ("Soma de {0}", calc.sum (3));

            C.WriteLine ("Multiplo de 3 é {0}", calc.mul(3));

            Calc calc_2 = new Calc (1, 2, 3, 4, 5);

            C.WriteLine("Valores da instância diferente {0}", calc_2.value);

            C.WriteLine ("Valores de multiplicação é {0}", calc_2.mul(5));

        }
    }
}

Esse programa funciona perfeitamente. E eu entendi que tudo que é colocando dentro do método estático Main é executado no console. Porém ainda não entendi onde é que o compilador "sabe" que é a MainClass que é a classe principal.
Isso é definido em algum lugar, ou o Compilador do C# entende que é a classe principal por causa do nome MainClass?
É possível definir outra classe principal?
É possível ter mais de uma classe principal?
Nota: Estou usando Linux e compilando pelo Monodevelop.
Answer: Quando tem apenas uma classe com Main() em todo o projeto (onde vai gerar um assembly (arquivo executável) o compilador se vira em achar qual é ela já que não há ambiguidade. O nome da classe pouco importa.
Quando tem mais que uma classe com o método chamado de entry point (o que parece estar sendo chamado de "classe principal" na pergunta), aí tem que definir na compilação qual deve ser usado com a flag de compilação /main:
/main:NomeDaClass

Documentação.
No Visual Studio em Startup Object:

Não tenho o MonoDevelop, mas tem algum lugar pra configurar isto. Só achei como fazer meio manual:

Coloquei no GitHub para referência futura.


Problems with Pointer in Parse.com

Question: I've been using Parse.com for two months, but this is the first time I get a stupid error which I can't figure out.
I've been working in an app for iOS, but in the beginning I've been working with my own user. Now I registered other users and I want to fetch them from the server.
Well, I add one object in the table Changes, where I have a user property which is a Pointer<User> (it's not _User, but User, it's custom, just an object). Well, when I try to fetch all the rows I have, the one that I have with my user are ok:

so in my debug console is like :

but when I fetch other users:

my debug console is:

so there's not any variable!!!! :(
This is my code:
PFQuery *closestChanges = [PFQuery queryWithClassName:@"Changes"];
[closestChanges whereKey:@"coordinates" nearGeoPoint:geoPoint withinKilometers:0.5f];

[closestChanges findObjectsInBackgroundWithBlock:^(NSArray *changes, NSError *error) {

    arrayChanges0 = [[NSMutableArray alloc] init];
    arrayChanges1 = [[NSMutableArray alloc] init];

    if (changes == nil && changes.count == 0) {
        [_tableView reloadData];
        return;
    }

    for (int i = 0; i < changes.count; i++) {
        PFObject *currentChange = [changes objectAtIndex:i];
        PFObject *user = [currentChange valueForKey:@"user"]; // here my user is null when it's other users.

        PFObject *changeToStore = [PFObject objectWithClassName:@"Changes"];
        [changeToStore setValue:currentChange[@"changetype"] forKey:@"type"];
        [changeToStore setValue:currentChange[@"quantity"] forKey:@"quantity"];
        [changeToStore setValue:currentChange[@"date"] forKey:@"enddata"];
        [changeToStore setValue:user forKey:@"user"];

        if ([currentChange[@"changetype"] intValue] == 0)
        [arrayChanges0 addObject:changeToStore];
        else
        [arrayChanges1 addObject:changeToStore];
    }

    [_tableView reloadData];

}];

Is there anything wrong I'm doing by adding new users???
Thank you very much in advance.
Comment: Where are you doing this logging? When using a user you get from accessing the pointer? Where is that code?
Answer: When you fetch a pointer type from a table, you will only get back the metadata which is objectId. You need to call - (instancetype)includeKey:(NSString *)key in order to get all the data back from Changes table. If you query directly from User table, you will get all the data. But in this case, you User object is a subdocument from Changes objects.
Add this line before performing querying:
[closestChanges includeKey:@"user"]
Comment: And now... to be a complete 100% genius (you already are!)... why the hell was I able to fetch the user with some users and not with another one ?? Thank you very much. I'm not very interested in this last question, but it's something I'd like to know for curiosity =)
Comment: Maybe you accidentally fetch it directly from `User` table? lol. I am glad this helps.

Problems with Pointer in Parse.com


How to schedule a task with airflow

Question: Unfortunately even after reading the many questions here and the FAQ page of the airflow website, I still don't understand how airflow schedules tasks. I have a very simple example task here:
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta

default_args = {
    "depends_on_past": False,
    "start_date": datetime(2020, 5, 29),
    "email_on_failure": False,
    "email_on_retry": False,
    "retries": 1,
    "retry_delay": timedelta(minutes=5),
}

dag = DAG(
    "example_dag_one",
    schedule_interval="30 8 * * *",
    catchup=False,
    default_args=default_args,
)

with dag:

    t1 = BashOperator(task_id="print_hello", bash_command="echo hello", dag=dag)

    t1

My naiv view would be that this task would be run on May 29th 08:30. But as the time passes, airflow has not scheduled that task. If I change the cron expression to something like: '* 8 * * *' It will schedule a task every minute.
When I however use the same DAG with a start date of yesterday (so May 28th in that case) the task will be scheduled at 08:30, yet it's execution date is the 28th (even though it ran on May 29th) and the start date in the web ui is May 29th. This is VERY confusing.
What I want from airflow in the end is simple: "Here is python code, run it on this time day". So how could I achieve that. Again let's say I want to schedule a task on 08:30 every day starting tomorrow.
Answer: The answer can be found in Airflow official documentation:
Note that if you run a DAG on a schedule_interval of one day, the run stamped 2016-01-01 will be trigger soon after 2016-01-01T23:59. In other words, the job instance is started once the period it covers has ended.
Let's Repeat That The scheduler runs your job one schedule_interval AFTER the start date, at the END of the period.
So applying to your case, if you put start date 29th of May, with the original cron, it will run every day at 08:30 starting from tomorrow 30th of May.
Anyway, if you don't need a dag specifically at some point in the day, you can just set schedule interval to '@daily', and it will be triggered at the beginning (00:00) of each day. If there are a lot of dags with @daily, don't worry, the scheduler and the workers will know how to handle it to execute all of them. If you have dags that depend on other dags, there are mechanisms to concatenate them so that you still don't have to worry about specifying hours.
Comment: Thank you for the reply. I guess I just have to accept that as a fact, yet in my mind it makes no sense, and this also makes it really hard to wrap your head around when you want to execute a task daily (since you will see a delay of one day every time)
Answer: Actually Airflow will wait for the entire scheduling interval (1 day) to be completed, then the execution would start !
So if you want your task to be executed today 2020/ 5/ 29, you should set the start time in a way that the schedule interval finishes. So set the start time to : datetime(2020, 5, 28)
If the schedule interval is 1 week, so the task would be launched 1 week later of the start time and so on ...

How to schedule a task with airflow


Conditional expectation when conditioning on a set

Question: Consider two random variables $X,Y$. $Y$ has support $\mathcal{Y}\equiv \mathcal{Y}_1\cup \mathcal{Y}_2$. $X$ has support $\mathcal{X}$. $\mathcal{X}$ and $\mathcal{Y}$ are finite sets.
Take a function $f(X,Y)$ and a parameter $\beta$. Suppose that
$$
E(f(X,y)-\beta)\geq 0 \text{ }\forall y\in \mathcal{Y}_1
$$
The textbook asks me to show that this implies
$$
E(f(X,Y)-\beta| Y\in \mathcal{Y}_1)\geq 0
$$
I would like your help to understand
1) What is $E(f(X,Y)| Y\in \mathcal{Y}_1)$? I know that $\forall y \in \mathcal{Y}_1$
$$
E(f(X,Y)| Y=y)\equiv \sum_{x\in \mathcal{X}} f(x,y)P(X=x| Y=y)
$$
but I don't understand how $E(f(X,Y)| Y\in \mathcal{Y}_1)$ is defined.
2) How to get that implication.
Answer: Hint: When faced with a random variable $X$ and an event $A$, the following may help:
$$E[X|A] = \frac{E[X1_{A}]}{P(A)}$$
where $1_{()}$ is the indicator function.
In your case replace $X$ with $f(X,Y)$ and $A$ with $\{y \in \mathcal{Y}\}$.
Let me know if you need more help.
Comment: Thanks, I need more scholastic help, sorry, Could you add details?
Comment: Looks like the next answer has the details you seek!!
Answer: You can view $E\{f(X,Y)|Y=y\}$ as a special case of conditioning on a set.  $E\{f(X,Y)|Y=y\}=E\{f(X,Y)|Y\in\{y\}\}$. In general,
\begin{align}
E\{f(X,Y)|Y\in\mathcal{Y}_1\} &= \sum_{x\in \mathcal{X}, y\in \mathcal{Y}_1}f(x,y)P(X=x, Y=y|Y\in \mathcal{Y}_1)\\
&=\sum_{x\in \mathcal{X}, y\in \mathcal{Y}_1}f(x,y)P(X=x|Y=y)P(Y=y|Y\in \mathcal{Y}_1)\\
&=\sum_{x\in \mathcal{X}, y\in \mathcal{Y}_1}f(x,y)P(X=x
|Y=y)\frac{P(Y=y)}{P(Y\in \mathcal{Y}_1)}\\
&=\sum_{x\in \mathcal{X}, y\in \mathcal{Y}_1}f(x,y)P(X=x|Y=y)\frac{P(Y=y)}{\sum_{y\in\mathcal{Y}_1}P(Y\in \mathcal{Y}_1)}.
\end{align}
Can you complete the answer now?
Comment: Thanks. In the first equality of your derivations, you write $E(f(X,Y)|Y\in \mathcal{Y}_1)=\sum_{x\in \mathcal{X}}f(x,y)P(X=x|Y\in \mathcal{Y}_1)$. What is SMALL $y$?
Comment: Sorry! That was a mistake. Fixed it!
Comment: Thank you. This was useful (+1)! I have posted another question on conditional expectations https://math.stackexchange.com/questions/3254384/showing-a-claim-involving-conditional-expectations. Please help if you can. Thanks.


Meu código não executa por completo

Question: Boa noite, pessoal. Preciso implementar um programa que imprima sempre uma sequência de três números onde não pode haver nenhuma repetição.
Ex.: t = 4.
Output esperado:
(0 1 2)
(0 1 3)
(0 2 3)
(1 2 3)
O programa executa para t = 4, no entanto, para t = 10, ele executa apenas até certo ponto (até i = 6). Já testei cada parte do código e não consigo encontrar o erro, será que alguém poderia me ajudar?
Obrigada! :)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

typedef struct{
  int v1;
  int v2;
  int v3;
}Triangulo;

int main(){
int n;
scanf("%d", &n);

Triangulo T[n];

int t;
t = (n*(n-1)*(n-2))/6;
printf("t = %d\n", t);

int c = 0;
int f = c+1;
int s = f+1;

for(int i = 0; i < t; i++){
  T[i].v1 = c;
  T[i].v2 = f;
  T[i].v3 = s;

if(s != n-1){
  s++;
}else if((s == n-1)&&(f == n-2)){
  c++;
  f = c+1;
  s = f+1;
}else if(s == n-1){
  f++;
  s = f+1;
}
}

for(int j = 0; j < t; j++){
  printf("%d %d %d\n", T[j].v1, T[j].v2, T[j].v3);
}

  return 0;
}

Comment: Você pode colocar o código completo? No parte que postou não está sendo definido `n`, `T` e `t`. Você deseja só as triplas com valores diferentes ou as permutações? Por exemplo, você quer que tenha `(0 1 2)` e `(2 1 0)` na saída, ou basta `(0 1 2)`?
Comment: Olá. Com o exemplo que você deu, apenas (0 1 2) basta. Vou atualizar o post e colocar o código completo :)
Comment: Você quer todas as permutações que são do tipo `(i j k)` tal que `i < j < k`, correto? Eu deixei uma resposta que faz isso, mas ainda não tenho certeza se é exatamente isso que você quer.
Answer: Você quer todas as permutações que são do tipo (i j k) tal que i < j < k, correto? Você não vai conseguir fazer isso usando apenas um for. Observe que quando fixamos valores para i e j, por exemplo, ainda temos os valores de j+1 até t-1 para atribuir a k. Vejamos um exemplo:
Considere que em determinado momento do algoritmo i = 1 e j = 3, e que t = 10. As sequências possíveis fixando esses valores seriam:
(1 3 4)
(1 3 5)
(1 3 6)
(1 3 7)
(1 3 8)
(1 3 9)

Note que precisamos de um for que vai de j+1 até t-1 apenas para iterar os possíveis valores de k. Da mesma forma, se fixarmos apenas o valor i, precisamos de um for para percorrer os possíveis valores para j e, para cada valor de j, um novo for para valores de k. Acho que já ficou claro que precisamos de um for para cada variável.
Segue uma implementação:
#include <stdio.h>

int main(void) {

    int t;

    scanf("%d", &t);

    // Considera todos os valores de 0 até t-1
    for(int i = 0; i < t; i++){

        // Considera todos os valores de i+1 até t-1 para impedir que os valores se repitam
        for(int j = i+1; j < t; j++){

            // Considera todos os valores de j+1 até t-1 para impedir que os valores se repitam
            for(int k = j+1; k < t; k++){
                printf("(%d %d %d) ", i, j, k);
            }
        }
    }

    printf("\n");
    return 0;
}

Demonstração aqui.
Comment: Entendi! Meu problema é que preciso encontrar uma solução em até ordem linear, ou seja, não poderei executar esses 3 loops. Você teria alguma sugestão de como eu poderia resolver isso?
Comment: Então acho que você não precisa imprimir todas as sequências, já que o número de subconjuntos de tamanho `3` em `n` elementos é da ordem de `n^3`, então não dá para imprimir todos em tempo linear. Você pode verificar se é exatamente isso que precisa fazer? É uma questão? Se sim, poderia adicionar o enunciado?
Comment: Então, eu tenho um trabalho para entregar sobre Árvore Geradora Mínima e eu tenho que executar em tempo O(m), onde m é o número de arestas. Eu quero usar esse trecho para gerar subconjuntos de tamanho 3 que seriam vértices de triângulos dentro do meu grafo original. Nesse trecho eu uso o for em função de n, basicamente, então ele acaba ficando maior que o número de arestas e isso, pela especificação, não poderia acontecer.
Comment: Entendi. Existe alguma propriedade particular no grafo de entrada? Não existe algoritmo `O(m)` para o problema da `MST` em grafos arbitrários (ao menos não conheço e não achei nada sobre nas buscas que fiz). Existem algoritmos para a `MST` que são lineares no grafo, isto é `O(m + n)` e é possível achar uma árvore geradora em `O(m)`, mas não a mínima. Também não consegui ver como o algoritmo acima te ajuda com a árvore geradora mínima. Você tá seguindo algum algoritmo específico?
Comment: Na verdade eu estava tentando coisas sem base em nenhum algoritmo, porque minhas buscas também me indicaram isso que você encontrou. Reli a especificação do trabalho e tentarei resolver com caminhos mínimos ao invés de MST's. De toda forma, obrigada pela atenção e pelas respostas!
Comment: Ok. Se puder colocar parte da especificação para tentar te dizer se está indo no caminho correto...
Comment: Então, a professora nos disse que a solução para o trabalho é o algoritmo de Camerini para Minimum Bottleneck Spanning Tree. Você sabe algo a respeito? Estou com dificuldades para interpretar como ele deve ser implementado.
Comment: Tentei fazer uma implementação com Kruskal, mas não consigo fugir do O(m log n).
Comment: Acho que não tem como fugir do `O(m lg n)` usando `Kruskal` (apenas se não ordenasse as arestas, mas aí você não conseguiria garantir nada). O [Algoritmo de Camerini](https://bit.ly/2H73Nac) é realmente o que você precisa. A maior dificuldade é calcular a mediana em `O(n)`, mas você consegue usar o algoritmo [median of medians](https://bit.ly/2gPtxMP), que te devolve uma mediana aproximada, e é suficiente. Fiz uma implementação do `median of medians` em `c`, segue o [link](https://bit.ly/2Jy30Sv). Mais sobre o resto do algoritmo você pode ver [aqui](https://bit.ly/2Hdqqew).
Comment: Para criar a floresta com o subgrafo formado apenas pelas arestas com valor menor que a mediana, basta selecionar essas aresta e aplicar uma [DFS](https://pt.wikipedia.org/wiki/Busca_em_profundidade). No anterior link tem exemplos de implementações da `DFS`.
Comment: Li o link mas continuo com dúvidas quanto ao funcionamento do algoritmo. Eu não entendo por que essa parte deve ser feita: If the subgraph is connected, V is the upper limit of the answer, and decrease the V, repeat the step 1, 2.
* If the subgraph is not connected, let the connected component to become a node, and increase the V, repeat the step 1, 2.
Comment: Não entendo o motivo dessa condição. A verdade é que ainda não consigo visualizar como o algoritmo deve funcionar, entendi que tenho que calcular a mediana mas não entendo como isso vai ajudar nas outras partes. Enfim, obrigada pelo código de exemplo da median of medians!
Comment: O que o algoritmo faz é dividir as arestas em dois conjuntos: `A` formado pelas menores que a mediana e `B` pelas maiores. Se for possível construir uma árvore geradora usando somente as arestas menores (de `A`), o algoritmo repete o processo considerando apenas as arestas de `A`. Caso não seja possível, o algoritmo pega a maior árvore que  conseguir usando apenas arestas de `A` e considera essa árvore como um vértice e repete o processo agora para o novo grafo. Observe que todas as arestas de `B` são maiores que as de `A`, logo, no segundo caso é sempre seguro pegar arestas de `A`.
Comment: Usando a mediana, a ideia é que os tamanhos de `A` e `B` sejam aproximadamente `m/2`, assim a cada passo o número de arestas do grafo é diminuído pela metade.
Comment: Certo, mas ainda tenho umas perguntas. Pela descrição do TP, o grafo será completo. Nesse caso teria como eu garantir que a árvore formada não tenha ciclos? Outra dúvida é que eu não vou precisar ordenas as arestas para encontrar a mediana, então como eu encontro um bom meio de encontrar esse valor? E eu ainda não entendo muito bem essa questão de considerar a árvore um vértice e uni-la a outra aresta. Que aresta seria essa visto que minhas arestas estariam desordenadas? Vou tentar pegar alguns valores de um dos arquivos de entrada e postar aqui para tentar esclarecer outra dúvida.
Comment: 8615
11916
16355
12755
15219
11364
9739
13373
3151
3500
8566
15285
6617
16484
13096
5947
11737
5067
14165
3569
14263
14288
2728
15063
10913
3163
9893
13660
3026
18147
10743
8019
3565
13810
10111
12865
12077
3696
18201
10430
11542
8936
15773
7915
16064
Comment: A resposta para essa entrada é 8615.
Comment: O problema é que quando eu seleciono o valor na posição da mediana, o valor é 2728 que é o meu menor valor, então o lado com arestas de menor valor fica vazio.
Comment: Dado o conjunto `A` de arestas, você precisa escolher um subconjunto `T` de arestas que formam uma árvore. Você não precisa pegar todas as arestas de `A`, basta pegar um subconjunto. Dessa forma você garante que é uma árvore (basta escolher arestas de forma a ser uma árvore). Note que nem sempre você vai conseguir ligar todos os vértices nessa árvore, daí você cai no segundo caso do algoritmo.
Comment: Para pegar a mediana você pode usar o algoritmo `median of medians` que mandei em um comentário anterior, ele vai te dar uma mediana aproximada em `O(n)`. Para a entrada que colocou a saída dele é `12148.50`.  Ao particionar as arestas em menores ou iguais (`A`) e maiores (`B`) usando a mediana devolvida pelo algoritmo, o conjunto `A` teria `27` arestas e o `B` teria `18`. É uma media aproximada, o algoritmo nem sempre vai dividir exatamente na metade, mas vai ficar próximo. [Aqui](https://ideone.com/lXOWSd) o algoritmo calculando a mediana para a entrada que você colocou.
Comment: Sobre considerar a árvore como um único nó. Note que no final você precisa devolver uma árvore geradora (isto é, uma árvore que conecta todos os vértices), logo, se você já tem uma árvore `T` com um subconjunto de vértices, conectar um novo vértice `u` à `T` pode ser feito por qualquer aresta que liga `u` à um vértice `v` em `T`, pois os demais vértices de `T` também ficarão conectados a `u` após a adição da aresta `(u, v)`. E todos os vértices de `T` já estão conectados entre si, então você pode abstrair essas conexões e focar apenas nas que faltam.
Comment: Uma correção. Você precisa encontrar uma floresta usando as arestas em `A` e não uma árvore. Isso significa que pode ser um conjunto de árvores (e, mais formalmente, significa que basta o grafo ser acíclico, mas não necessariamente conexo). Para isso, basta aplicar o `Algoritmo de Kruskal` **sem ordenação**. Se no final o algoritmo encontrar apenas uma árvore, você chama a recursão para o conjunto `A`, se encontrar mais de uma árvore, você considera cada árvore como um nó e chama para `B`. Tem várias implementações do Kruskal na internet, segue [uma](http://www.codcad.com/lesson/44).
Comment: Então seu algoritmo ficaria mais ou menos assim: [pseudocodigo1](https://gist.github.com/mauroberto/a54a3dd226d80b4cd2eb7cd264e4c178) e [pseudocodigo2](https://www.overleaf.com/read/wchnqjjjdptv).
Comment: Tentarei uma implementação e postarei aqui!

Meu código não executa por completo


When to use passport.js?

Question: I've been developing in node for the last months to try and understand the framework, and now I'm actually going to try to make an app for multiple devices.
I want to use a node project as the api, which will handle all post and get requests.
In another node project I would have a react web interface communicating with the node api with ajax and socket.io.
In a mobile android/ios or cordova I would have the mobile interface communicating as above.
Now that I've explained the background of my question I will describe my problem.
For now I have a node project that uses bcrypt to encrypt passwords for registration and login. I also use passport to establish a session and for flash messages.
router.post('/login',
    passport.authenticate('local', {successRedirect:'/', failureRedirect:'/users/login',failureFlash: true}),
    function(req, res) {
        res.redirect('/');
    });

passport.use(new LocalStrategy(
    function(username, password, done) {
        db_functions.getUserByEmail(username,function(error, results)  {
            if (error) throw error;

            bcrypt.compare(password, results[0].password, function(err, res) {
                if(res === true){
                    return done(null,results[0])
                } else {
                    return done(null, false, {message: 'Invalid password'})
                }
            });
        })
    }));

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    console.log(id)
    db_functions.getUserById(id,function(error, results)  {
        if (error) throw error;
        done(error,results[0])
    })
});

I read here http://passportjs.org/docs/overview, that if I'm going to use my node project as an API, I should disable sessions and I will therefore use:
app.get('/api/users/me',
  passport.authenticate('basic', { session: false }),
  function(req, res) {
    res.json({ id: req.user.id, username: req.user.username });
  });

Now, that I have sessions disabled, why exactly should I keep using passport? I use bcrypt to register and check for password match when a user logs in. If I do remove it, does that mean that each request that comes from a web browser or a mobile device would have to also transmit the user and hashed password?
Answer: passport is an authentication library and sessions are a part of authorization — a different step in the process. You could use passport to issue a token such as a jsonwebtoken, instead of using sessions with a different library to verify those, such as ejwt, or create a middleware function to validate these tokens on routes which require authentication
passport is also extensible with a lot of plugins already developed to allow many forms of authentication to issue your authorization method.
You don't have to use it, it just makes it a bit easier. bcrypt, on the other hand, you probably have to use.
Comment: Thank you for making that clear. I read about jwt and I understand the subject better now.

When to use passport.js?


Programa em C que lê um ficheiro de texto e faz print das linhas por ordem inversa

Question: Gostaria de saber porque ao rodar o script abaixo, as linhas aparecem espaçadas, exceto a primeira linha.
Input
Linha1
Linha2
Linha3
Linha4

Output esperado
Produtos:
- Linha3
- Linha4
- Linha2
- Linha1

Output obtido
Produtos:
- Linha1-Linha 2
- Linha3
- Linha4

Código
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct linha {
  char *produtos;
  struct linha *prev;
} linha;

FILE *file;
int main() {

  linha *tail = NULL;
  file = fopen("text.txt","r");
  char linha1[255];

  while (fgets(linha1,255,file)!=NULL) {
    linha *l1 = malloc(sizeof(linha));
    l1->produtos = malloc(strlen(linha1)+1);
    strcpy(l1->produtos,linha1);
    l1->prev = tail;
    tail = l1;
  }

  linha *current;
  current = tail;
    printf("Produtos:\n");
  while (current != NULL) {
    printf("- %s",current->produtos);
    current = current->prev;
  }
  return 0;

}

Comment: Porque o output esperado é `3,4,2,1` e não `4,3,2,1` ?
Answer: Primeiro, é bom você fazer um cast para o retorno de malloc(), pois em alguns compiladores pode não compilar, o meu por exemplo.
Ficaria assim:
linha *l1 =(linha*) malloc(sizeof(linha));
l1->produtos =(char*) malloc(strlen(linha1)+1);

Vou considerar que o output esperado é
Produtos:
- Linha4
- Linha3
- Linha2
- Linha1

Isso acontece porque na última linha do arquivo de texto não tem um \n e sim o EOF. Por isso, ele não dá a quebra-de-linha. Se quiser testar, dê um enter na última linha do seu arquivo de texto, você vai ver que agora ele vai estar "printando" de maneira correta.
Comment: não tinha pensado nisso, obrigado! já agora, gostaria de meter um ";" após cada linha, já experimentei sizeof(linha)-1 mas aparece sempre no inicio, antes do "-"