socket program - reliable data transfer using UDP

Question: I have written code for reliable data transfer in C. Here the client sends the file to the server in chunks of 1024 bytes. And the server writes the packets received into another file.
The program works fine for string file/binary data file. But when I try to copy image file there comes the problem.   when I try transferring .jpg file there is no compilation error or run time error. But when I try opening the destination file it says,
"could not open image file','error interpreting jpeg file' ' improper call to jpeg library in state 201".
Can anybody look into the codes and suggest any solution?
Here is my client code
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<arpa/inet.h>

char *itoa(long i,char *s,int dummy_radix)
{
    sprintf(s,"%ld",i);
    return s;
}

int main(int argc, int *argv[])
{
    int sock,length,n;
    struct sockaddr_in server,from;
    struct hostent *hp;
    long int packets =0;
    unsigned char buff[1024] = {0};

    // checking if hostname and the port address is provided //
    if(argc!=3)
    {
        printf("insufficient arguments\n");
        exit(1);
    }

    //create a socket//
    sock = socket(AF_INET,SOCK_DGRAM,0);

    if(sock<0)
    {
        printf("error in opening socket\n");
        return 1;
    }

    //to get  the hostname of the system or the machine//
    hp= gethostbyname(argv[1]);

    if(hp==0)
    {
        printf("Unknown host\n");
        return 1;
    }
    //build the server's IP address //
    bzero((char *)&server,sizeof(server));
    bcopy((char*)hp->h_addr,(char *)&server.sin_addr,hp->h_length);
    server.sin_family = AF_INET;
    server.sin_port =  htons(atoi(argv[2]));
    length = sizeof(server);

    /*open the file that we wish to transfer*/
    FILE *fp = fopen("landscape.jpeg","rb");
    if(fp==NULL)
    {
        printf("file open error");
        return 1;
    }
    fseek(fp,0,SEEK_END); //if exists read the size of the file
    size_t file_size = ftell(fp);
    fseek(fp,0,SEEK_SET);

    printf("size of the file is %d\n", file_size);

    /*find the number of packets*/
    packets = (file_size/1024)+1 ;

    /*send the number of packets to the server*/
    itoa(packets,buff,10);
    n = sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&server,sizeof(struct sockaddr));
    if(n<0)
    {
        printf("error in sending message to the serveR");
        return 1;
    }

    /*Read data from file and send it*/
    int packetNum = 0;
    while(1)
    {
        /*First read file in chunks of  1024  bytes */
        int nread = fread(buff,1,1024,fp);
        //printf("Bytes read %d\n",nread);

        /*if read was success ,send data*/
        if(nread>0)
        {
            //printf("data sent now is %s\n",buff);
            n = sendto(sock,buff,strlen(buff),0,(struct sockaddr *)&server,sizeof(struct sockaddr));
            printf("Sending %d, numBytes sent: %d\n", packetNum, n);
            packetNum++;
            if(n<0)
            {
                printf("error in sending message to the server");
                fclose(fp);
                return 1;
            }
        }

        /*There is something tricky going on with the read..
         * Either there was error ,or we reached end of  file.
         */
        if(nread<1024)
        {
            if(feof(fp))
                printf("End of file\n");

            if(ferror(fp))
                printf("Error reading\n");
            break;
        }
    }
    close(sock);
    fclose(fp);

    return 0;
}

Here is my server code
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<arpa/inet.h>

int main(int argc, char *argv[])
{
    int sock,length,fromlen,n;
    struct sockaddr_in server;
    struct sockaddr_in from;
    char buf[1024];
    char file_buf[1024];
    int packets = 0;
    int received = 0;
    FILE *newfp;
    newfp = fopen("output.jpeg","wb");
    if(newfp==NULL)
    {
        printf("error opening the file\n");
        return 1;
    }
    if(argc<2)
    {
        fprintf(stderr, "no port number specified\n");
        exit(0);
    }
    sock = socket(AF_INET,SOCK_DGRAM,0);
    if(sock<0)
    {
        printf("error in opening socket\n");
        return 1;
    }
    length = sizeof(server);
    bzero(&server,length);
    server.sin_family= AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(atoi(argv[1]));
    if(bind(sock,(struct sockaddr*)&server,length)<0)
    {
        printf("cannot bind\n");
        return 1;
    }
    fromlen = sizeof(struct sockaddr_in);
    n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
    if(n<0)
    {
        printf("recvfrom  error\n");
        return 1;
    }

    packets = atoi(buf);
    printf("Num packets expected: %d\n", packets);

    while(received<packets)
    {
        n = recvfrom(sock,buf,sizeof (buf),0,(struct sockaddr *)&from,&fromlen);

        //printf ("%d\n", n);
        printf("Packet num %d, numBytes received: %d\n", received, n);

        if(n<0)
        {
            printf("recvfrom  error\n");
            return 1;
        }
        //printf("%s",buf);
        if((fwrite(buf,1,n,newfp)) < n)
        {
            printf("error in writing to the file\n");
            return 1;
        }
        received++;
    }
    printf("Finished\n");
    fclose(newfp);
    return 0;
}

Comment: Hard to do that if you don't include the code.
Comment: 'It displays error' is not a problem description.
Comment: It looks like you're relying on UDP packets arriving in order (or at all), but that's not necessarily the case. Try some text that's long enough to get split across multiple chunks. Is this a school assignment?
Comment: Yes it works fine for text that is long enough. The problem is with the .jpg file or any other image file.
Comment: You are assuming that `fread()` null-terminates the buffer: it doesn't; and that the data doesn't contain other nulls: it can. You should use `nread` instead of `strlen(buffer)` as the count parameter when sending the file data. And the correct test for end of file is not `nread < 1024`, which can happen any time: it is `nread == 0`.
Comment: @EJP I corrected my mistakes in the code.Unfortunately I still have the error with .jpg files. It says could not load image file if I try opening it after running my program:(                                                                                The corrections I made were on the lines  n= sendto(sock,buff,1024,0,(struct sockaddr *)&server,sizeof(struct sockaddr)); and to check end of file I  have to check (n==0) But that did not solve my issues.
Comment: `sendto(sock,buff,1024, ...)` should be `sendto(sock,buff,nread, ...)`. I already stated that clearly. And you still aren't handling out of sequence packets, or missing or duplicated ones. In fact there is nothing reliable about this code at all. You need sequence numbers *in the packets*, which are removed when writing the data to the file; you need an ACK or NACK mechanism for missing packets; you need a mechanism that ignores duplicates; ... You've barely started.
Comment: ok. I will improve my code
Comment: tcp an option? Unless this is an assignment specifically about writing a reliable protocol, just use tcp. I never understand people trying to morph udp into reliability. A reliable protocol already exists (although somewhat bloated).. use it!  /rant. Now, I also highly recommend using some kind of packet capture tool (wireshark, tcpdump, message analyzer, etc) so you know exactly what you're sending. Forget the pictures, use big files of known data. Hash them on both sides. Keep decreasing file size until you get hashes that match. This should help narrow down when the problem manifests.
Comment: You can also heavily decrease your packet size and use smaller files. Send 5 bytes a packet for a 20 byte file, then for a 23 byte file (something not evenly divisible). This will be far easier to keep a handle on what's happening. You say it works fine for a string, maybe you're just getting some extra junk at the end that you're not seeing in notepad. If it was working for binary files, then it would also be working for .jpegs. If you're getting some junk at the end or something, the jpeg view may not know what to do with that and then it complains.
Comment: @EJP ..I could fix the bug with your suggestions. Thanks for the help:)
Answer:
You are assuming that fread() null-terminates the buffer. It doesn't.
You are assuming that the data doesn't contain other nulls: it can.
You should use nread instead of strlen(buffer) as the count parameter when sending the file data.
The correct test for end of file is not nread < 1024, which can happen any time: it is nread == 0.
You aren't handling out of sequence packets, or missing or duplicated ones. In fact there is nothing reliable about this code at all. You need sequence numbers in the packets, which are removed when writing the data to the file; you need an ACK or NACK mechanism for missing packets; you need a mechanism that ignores duplicates; ...

You've barely started.


Requisição ajax com append antes de abrir modal

Question: Por algum motivo não estou conseguindo fazer este append funcionar.
function mostrarUploads(id) {
    $.ajax({
      url: "/painel/mostrarUploads",
      async: false,
      method: 'POST',
      data: {_token: jQuery(".token").val(), id: id},
      success: function(e) {
        e.forEach(function(item, indice) {
          $('#bodyTabelaUploads tbody').append('
TESTE
');
        });
      }
    });
}

Tenho essa tabela:

                                Nome do arquivo
                                Ações

Estou chamando o método antes de abrir um modal. Ele é um método assíncrono, mas estou usando async: false.
Será que por este método ser assíncrono, o modal abre antes, e eu não vejo a linha sendo adicionada? OBS: console.log(e) retorna resultados!!
Comment: Somente pra complementar: Ajax síncrono está deprecado desde o ```jQuery``` 1.8, então recomendo que procure fazer tudo de forma assíncrona
Comment: Ok, obrigado!!!
Answer: primeiro, async: false é sempre uma má ideia. O $.ajax retorna uma promise, então, abra o seu modal na continuação do mesmo.
function mostrarUploads(id) {
  return $.ajax({
    url: "/painel/mostrarUploads",
    method: 'POST',
    data: {_token: jQuery(".token").val(), id: id},
    success: function(e) {
      e.forEach(function(item, indice) {
        $('#bodyTabelaUploads tbody').append('
TESTE
');
      });
    }
  });
}

mostrarUploads(id).done(function(data, textStatus, jqXHR) {
  //exibir modal.
}).fail(function(jqXHR, textStatus, errorThrown) {
  //exibir mensagem de erro.
});

Comment: Obrigado pelo seu comentário.

Quando utilizo desta maneira que você disse, o browser retorna "cannot read property done of undefined" como se a função não existisse. Ela está declarada em cima do uso do método.

Alguma ideia do que possa ser? Vou tentar utilizar o $.when() que li a respeito também.
Comment: Olha, a sua resposta me deu uma boa inspiração que funcionou. Usei o .done na própria requisição ajax. O único problema que eu vejo nesta aproximação é que pode demorar bastante pra carregar isso e a experiência de usuário ficar ruim. Porém, não deixa de ser uma solução. Alguém conhece alguma maneira melhor de se fazer isso?
Comment: @mas se fizer um `async: false` não fará que o retorno seja mais rápido, a diferença é que a pagina irá deixar de responder enquanto espera pelo retorno.
Answer: O problema no seu código, é que sua tabela se chama tabelaUploads, e você está procurando por um tbody no corpo dessa tabela aqui: $('#bodyTabelaUploads tbody'), o que não irá retornar nada.
Troque $('#bodyTabelaUploads tbody') por $('#tabelaUploads tbody')
PORÉM
Gostaria de ressaltar que requisições AJAX síncronas já estão deprecadas e devem ser evitadas, pois eventualmente provavelmente serão totalmente removidas (daqui a muitos, muitos anos).
O próprio jQuery já nos avisa disso:
Comment: Realmente não havia visto este detalhe do tbody, obrigado!

Porém, ainda preciso de ajuda na questão do sincronismo. Me parece que preciso terminar esta requisição antes de abrir o modal. Ou será possível abrir o modal e mesmo assim depois dele aberto eu modificar a tabela 'dinamicamente' com o append?
Comment: Você pode fazer de diversas maneiras. Pode abrir a modal e mostrar algo como ```carregando...``` enquanto faz a requisição. Ou então pode mostrar a modal somente quando a requisição terminar
Comment: Sim, obrigado pela resposta! Acredito que não exista algo que fuja disso.
Comment: Você não tem como escapar de uma eventual demora do servidor. Você só tem que avisar o usuário de que está esperando, e travando a tela com uma requisição síncrona não é a melhor maneira :)


Usar uma variavel int de uma form em outro form C#

Question: Eu tenho tentado usar minha variavel double dinheiro do form1 no form2, pois preciso do valor recebido no form1 no form2, eu deixei a variavel publica e tentei instanciar mas continua dando erro:
codigo no form1
public partial class Form1 : Form
{
   public double dinheiro = 0;
  public  Boolean jogar = false;
    public Form1()
    {

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        bnjogar.Visible = false;
        label4.Visible = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dinheiro = 1.75;
         String i = Convert.ToString(dinheiro);

        Dinheira.Text = "$" + dinheiro;
        jogar = true;
        if(jogar == true)
        {
            bnjogar.Visible = true;
            label4.Visible = true;
        }

    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form3 form3 = new Form3();
        form3.ShowDialog();

    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();

        this.Hide();
    }
}

}
e no form2 esta assim
public partial class Form2 : Form
{
    int dinheiro2;
    Form1 meuform = new Form1();
    public Form2()

    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

         dinheiro2 = meuform.dinheiro;
    }
}

}
se alguem puder me ajudar por favor
Comment: Dentro do form2 não é necessário criar uma instância de form1, o que você fez foi criar um outro form1 diferente do form1 que chamou o form2. Vou direto ao ponto, passe o valor da variável dinheiro por parâmetro que resolverá seu problema.
Ex:
 public Form2(int dinheiro) { dinheiro2 = dinheiro}

private void button1_Click_1(object sender, EventArgs e)
    {
        Form2 form2 = new Form2(dinheiro);
        form2.Show();

        this.Hide();
    }
Answer: Podes fazer de várias formas.
Uma delas é passar os valores no construtor do seu form:
//Isso você faz no Form1
private void button1_Click_1(object sender, EventArgs e)
{
    //Aqui você constrói o form passando o valor por parâmetro
    new Form2(dinheiro).Show();
}

//Isso é no Form2
public Form2(double valor)
{
    //Aqui você pode pegar o valor e armazenar em outro lugar
    InitializeComponent();
}

Da pra fazer buscando diretamente do seu form1:
//Isso é no Form1
private void button1_Click_1(object sender, EventArgs e)
{
    //Aqui você diz que o form1 é o "dono" desse form2
    new Form2().Show(this);
}

//Isso é no Form2
protected override void OnShown(EventArgs e)
{
    base.OnShown(e);

    //Aqui o Owner é o form1, você pode fazer Cast para ele e acessar o que precisa
    //Funciona também em outros métodos do form2
    var dinheiro = (Owner as Form1)?.dinheiro;
}

Comment: Funcionoou perfeitamente, Muito obrigado
Comment: Show de bola, valeu


Arithmetic progression question I don't get

Question: Given all of the three-digit numbers: 100, 101, 102, 103......999.
A. out of these numbers, find out how many numbers can be divided by 3 without remainder.
B. out of these numbers, find out how many numbers can not be divided by 3.
A is easy:
$$999 = 100 + (n - 1)d => 999 -100+3 = 3n => 3n = 300$$
But how can I do the second one? I have no way actually...
Comment: Can a number be both divisible by 3 and not?
Comment: @doppz Im not sure about your question, but isn't the answer is 999 - 300? but its 699, the answer is 600.
Comment: You're close, just a little off. How many numbers are between $100$ and $999$?
Comment: 899, but 899 - 300 = 599, very close. What did I miss?
Comment: Still a little off. There are 900 such numbers.  There are 1000 from 0 to 999, but you throw away the first 100 from 0 to 99, of which there are 100. Hence, 900 in total.
Comment: I don't get why did 100  become 99, is that because of the n - 1? Doubt it cause n = position
Comment: No need to include $n$. Look at what I said again, you should be able to see it. You want to calculate 999-99, not 999-100, because you're including 100 in your interval.
Answer: Your work has gone a bit astray in the first part. $102$ is the first of the numbers on the list that is divisible by $3$, $999$ is the last, and the common difference between consecutive numbers divisible by $3$ is $3$. Hence, if $n$ is how many such numbers there are on the list, we are looking at $$999=102+3\cdot(n-1),$$ which we can solve to get $n=300.$
Letting $m$ be how many numbers are on the list altogether, we can take a similar approach. Once again, we are in an arithmetic progression, with $100$ the first number, $999$ the last, and $1$ the common difference, so we have $$999=100+1\cdot(m-1),$$ which we can solve to get $m=900.$ (Another way to see this: $100$ is first, $101$ is second, $102$ is third, and in general, $100+k$ is the $(k+1)$th on the list, so $999=100+899$ is the $900$th on the list.) Since there are $900$ altogether, and $300$ of them are divisible by $3$, then how many are not divisible by $3$?
Answer: I like Cameron Buie's answer, but let's generalize!
How many integers between $a$ and $b$ are divisible by $d$?
Let $\#S$ be the size of a set $S$, and let's only consider integer-type variables, then we want
$
\;\;\;\;\;\#\{n : n \in a .. b \text{ and $d$ divides } n \ \}
\\
= \#\{n : a \leq n \leq b \text{ and $n$ multiple-of } d \ \}
\;\;\;\; \text{, rewrite condition}
\\
= \#\{d \times n : a \leq d \times n \leq b \text{ and $d \times n$ multiple-of } d \ \}
\;\;\;\; \text{, dummy rename}
\\
= \#\{d \times n : \frac{a}{d} \leq n \leq \frac{b}{d}  \}
\;\;\;\; \text{, arithmetic and second condition is true}
\\
= \#\{d \times n : \lceil\frac{a}{d}\rceil \leq n \leq \lfloor\frac{b}{d}\rfloor  \}
\;\;\;\; \text{, inequalities ---see below$(\star)$}
\\
= \lfloor\frac{b}{d}\rfloor - \lceil\frac{a}{d}\rceil - 1
\;\;\;\; \text{, integer intervals $p..q$ have $q-p+1$ elements}
$
Neato! Now for your case $a,b,d := 100,999,3$ and so we have $\lfloor\frac{999}{3}\rfloor - \lceil\frac{100}{3}\rceil - 1 = 333-34+1=300$ numbers between 100 and 99 that are divisible by 3.
With a bit more effort we have a more general result :)

$(\star)$ If $n$ is an integer less than a real $x$ then it's also less than the floor of $x$; eg $n \leq 3.14$ iff $n \leq 3$ since $n$ is an integer. In-general,
$$
\;\; \forall n \in \mathbb{Z},x \in \mathbb{R} :: n \leq \lfloor x \rfloor \equiv n \leq x \;\;
$$
is the characterization of the floor function; similarly for the ceiling function. Enjoy!

The fact that $\#(p .. q) = q - p + 1$ can be proven by induction; where for integers $p,q$ we define $p .. q := \{ n \in \mathbb{Z} : p \leq n \leq q \}$, which I call ``$p$ upto $q$''.


Use the Services-3.x node resource to create a new *unpublished* node

Question: Is there a way that I can force a node to be unpublished by using the
node resource?
Requirement: Site A defines a content type 'item' which is published
by default. Site B needs to create 'item' nodes on A, and these
auto-created nodes should be unpublished.
function toolreq_test_create() {
$toolreq_endpoint = variable_get("toolreq_endpoint", "");
$options = toolreq_service_login();
if (!is_array($options)) {
    print "error
";
    return;
}
//create a node

$node_data = array(
                      "title"=>"DHC Tool 04 dev",
                      "type"=>"item",
                      //"status"=> FALSE, //ensure unpublished
                      "status" => 0,
                      "language" => "und", //lanugage neutral
                      "field_webpage"=>array(
                                                  "und"=>array(0=>array("url"=>"http://berkeley.edu",)
)
)
);

//use json
$options['headers']['content-type'] = "application/json";
$options['method'] = 'POST';
$options['data'] = json_encode($node_data);
$request = drupal_http_request($toolreq_endpoint . '/node', $options);
}

Since the node table's status field is the mysql INT type (with a
default of 1), I would think that I could pass status=>0 in the above
code, but doing that results in a published node. I have tried passing
0 using both JSON and x-www-form-urlencoded data.
If I use status=>FALSE and use JSON, the node is created as
unpublished, but I think that this is equivalent to inserting '' in the node.status field which results in a mysql warning:
mysql> INSERT INTO `bamboodirt`.`node` (`nid`, `vid`, `type`, `language`, `title`, `uid`,
`status`, `created`, `changed`, `comment`, `promote`, `sticky`, `tnid`, `translate`)
VALUES (NULL, 21, 'item', 'und', 'sql insert test', '0', '', '0', '0', '0', '0', '0', '0',
'0');
Query OK, 1 row affected, 1 warning (0.03 sec)

mysql> SHOW WARNINGS
+---------+------+----------------------------------------------------------+
| Level   | Code | Message                                                  |
+---------+------+----------------------------------------------------------+
| Warning | 1366 | Incorrect integer value: '' for column 'status' at row 1 |
+---------+------+----------------------------------------------------------+

I would live with that warning, but my webhost's mysql is less
permissive and I get a real error (durning node_save() I think) on
that server:
"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'status' at row 1"
(Here's the thread over at Pantheon http://help.getpantheon.com/pantheon/topics/json_node_create_works_on_localstack_fails_with_error_500_on_pantheon.)
Is there a way that I can force a node to be unpublished by using the
node resource, or do I need to write my own resource?
Answer: Try setting the 'status' field to TRUE or FALSE instead of 0 or 1.
Comment: Using FALSE works as noted above. I think the FALSE/NULL value for status gets converted to 0 by MySQL. Common.inc L6833:

    // Type cast to proper datatype, except when the value is NULL and the
    // column allows this.
    //
    // MySQL PDO silently casts e.g. FALSE and '' to 0 when inserting the value
    // into an integer column, but PostgreSQL PDO does not. Also type cast NULL
    // when the column does not allow this.


Do I need to include .meta file when build unity assetbundle?

Question: In editor, the meta file records reference with other asset, so I think when build assetbundle, I need to include the .meta file also, so at runtime the asset will find the reference with each other.
But according to this link https://answers.unity.com/questions/910422/assetbundleloadmainassetatpath-not-loading-meta-fi.html , the answer said there is no need for it, I'm confused about that
Comment: No, you don't need to. ".meta" is an Editor extension.
Answer: From what I know, Unity uses these .meta files in the editor only. I'm pretty sure that the asset bundle contains data that tells it what files do what. The guy at the link you've posted seems to be pulling .meta files from the asset bundle and I don't think this is the correct way to do this, since you are basically doing it in the reverse of what unity was designed for. Try finding only the assets you are looking for, not the meta files. Also you should not have to manually include .meta files in your asset bundle, unity should do this automatically. And one last thing, Use unity's built in functions for accessing asset bundles rather than System.IO, since the system does not know what an asset bundle is, only unity does. Once the files are extracted onto your drive, then use System.IO on them.
Good luck.
Answer: Unity's .meta files contain two important pieces of information:

The GUID that Unity uses to track any references to the Asset.
The settings for the Asset that you see in the Inspector window.

Hence, if you don't include them you risk breaking references and losing Asset settings.
This article on my blog explains meta files and GUIDs in more detail.


Creating an array of ArrayList which store object of Generic type

Question: I have to create an array of ArrayList which is storing an object of the generic type.
ArrayList<Entry>[] bucket;

when I initialize this to
bucket=(ArrayList<Entry>[])new Object[m];

I get java.lang.ClassCastException at runtime.
Entry class uses generics. Below is the code:
class Entry<K,V>{
    K key;
    V value;

    public Entry(K key,V value){
        this.key=key;
        this.value=value;
    }

After going through several posts on why Array of Objects cannot be cast to an ArrayList of a generic type, I have understood my problem. But I am not able to get my head around this to solve my particular case.
Some of the solutions involved:
Changing it FROM "an array of ArrayList" TO "an ArrayList of ArrayList",
but I can not do that in my case.
Full code:
import java.util.ArrayList;
class MyHashMap<K,V>  {

    int m;
    int loadFactor;
    int n;

    ArrayList<Entry>[] bucket;

    public MyHashMap(){
        this(10,2);
    }
    public MyHashMap(int m){
        this(m,2);
    }
    public MyHashMap(int m,int loadFactor){
        this.m=m;
        this.loadFactor=loadFactor;
        n=0;

        bucket=(ArrayList<Entry>[])new Object[m];

    }

    class Entry{
        K key;
        V value;

        public Entry(K key,V value){
            this.key=key;
            this.value=value;
        }

        public int hashCode(){
            return 0;
        }
    }

    public void put(K key, V value){
        Entry entry=new Entry(key,value);
        int hash=entry.hashCode();
        int index=hash%m;
        bucket[index].add(entry);
    }

    public static void main(String[] args) {
        MyHashMap hashMap=new MyHashMap();

        hashMap.put("faisal",2);
    }
}

Comment: Why did you create  a class called `Entry` which looks awfully similar to Java's `Map.Entry`?
Comment: why do you think you can cast a `Object` array into an array of `ArrayList` ?
Comment: Please show the code in context where you plan to use this `ArrayList`.
Comment: Polymorphic type declaration doesn't work with collections.The type should be exact type.it cant be parent or subtype.but this is diff from with arrays.`Entry[] bucket=new Object[m];`
Comment: @Tim I am actually implementing HashMap here,and  I am using chaining.
Comment: @TimBiegeleisen added the rest of the code in problem description.
Answer: You can't create arrays of generic type. Use this instead:
ArrayList<Entry>[] bucket = new ArrayList[m];

It shows unchecked warning though, which you can suppress using @SuppressWarnings("unchecked") like so:
@SuppressWarnings("unchecked")
ArrayList<Entry>[] bucket = new ArrayList[m];

Comment: The compiler does not complain nor there is any problem at runtime. I am now wondering if it is okay to do that.! I mean is this the only option left.
Comment: It's fine to do it if you know you are going to store only the ArrayList objects in it. Better not to expose it outside your API class.
Answer: Object can be cast to ArrayList only if the actual object it holds is an arrayList. i.e let's look at the below code,
Object obj = new ArrayList<String>();   // object variable holding array list type.
ArrayList<String> arr = (ArrayList<String>) obj;

Comment: Question is about array of array list


Deleting multiple occurrences of a string in notepad ++

Question: I have the following strings in an error log:
> [Mon Mar 17 20:14:34 2014] [error] [client 220.127.116.11] File does not exist: /home/dom/public_html/404.shtml, referer: http://www.dom.com/
  [Mon Mar 17 20:14:47 2014] [error] [client 22.214.171.124] File does not exist: /home/dom/public_html/404.shtml, referer: http://www.dom.com/
  [Mon Mar 17 20:15:05 2014] [error] [client 188.8.131.52] File does not exist: /home/dom/public_html/404.shtml, referer: http://www.dom.com/

I want to be able to sort them so that I only have one report of /home/dom/public_html/404.shtml, but the strings are not technically duplicates because the client IPs are not the same. I used notepad++ textfx function but I don't see how to sort based only on the /home/dom/public_html/404.shtml string.
Thanks in advance
Comment: Your question is rather specific, because most people use scripts or other programs to evaluate logfiles. I know nothing about the textfx function or plugin (?) and what you do with it. Are you familiar with any programming language?
Answer: If you have (or you can adjust) all the left parts of log entries ([Mon Mar 17 20:14:34 2014] [error] [client 22.214.171.124] ) of the same length, here is an unique sort functionality you are searching for:

ensure that menu item TextFX > TextFX Tools > +Sort outputs only UNIQUE (at column) is checked (if not, select it so it becomes checked)
create column selection which includes all the right parts of log entries (Left Alt+Drag or Letf Alt + Shift + ↑↓←→)
select menu item TextFX > TextFX Tools > +Sort lines case sensitive (at column)


copying actual drupal content to a different drupal site

Question: I know that one can use the export link to copy a content type or a view to another drupal site. But if that content type has a lot of content, is there a way to also copy that over as well?
Answer: It may be too late for this, but I needed the same thing and came across this thread so thought others might also. There's a new module called Data Export Import which does exactly what you need:

The purpose of this module is to be able to export nodes, taxonomy
  terms and users to data files and then be able to import these data
  files into Drupal sites. There may be many uses for this module but
  the main one would be that it can provide a way to export data from a
  live Drupal site and then to import that live data into an updated
  version of that site.
Answer: I think you're probably looking for the Node export module:

This module allows users to export nodes and then import it into another Drupal installation, or on the same site.

The Migrate and Feeds modules might also be of interest.
Comment: My most recent adventure with node_export had me crying under my desk.  Let's just say it didn't go well.
Answer: I believe you can do this with the Node Export (export) and Feeds (import) modules.
Comment: If I want to export taxamony term lists too, how would I do this?
Comment: Drupal 7 : [link](http://drupal.org/project/taxonomy_csv), Drupal 6 : [link](http://drupal.org/project/taxonomy_export), [link](http://drupal.org/project/taxonomy_xml). It depends on your drupal version / data format.


Constructors for literal strings and common strings

Question: I'm making an Identifier class which is basically an immutable string. It maintains a single constant char array in memory, making it suitable for identifiers which require frequent comparisons.
Here are my constructors.
// Default constructor for Identifier.
Identifier();

// Constructor for Identifier that takes a const char* and its size.
// This constructor is used to create an Identifier from a given string and size.
Identifier(const char* str, uint16_t size);

// Constructor for Identifier that takes a string literals.
// This constructor is used to create an Identifier from a string literals.
template <size_t Len>
Identifier(const char(&str)[Len])
    : Identifier(str, Len - 1)
{
    std::cout << "template" << std::endl;
}

// Constructor for Identifier that takes a const char*.
Identifier(const char* str);

The method I want to use is like:
// using the template constructor
Identifier id = "identifier";
// using the const char* constructor
const char* str = "Identifier";
Identifier id = str;

However, when I run my test program, it seems that the compiler trend to select the non-template constructor, making the template constructor is never used.
I expect a solution to make the compiler select template constructor first.

The problem is solved. I re-thought my design and found that the best way to create an Identifier is to submit data and size in the same time.
Comment: *"compiler treats a literal string as a const char* first,..."* No a string literal is `const char[N]` which **decays** to a `const char*` in some contexts.
Comment: Do you know about std::string_view and how to use it as a constexpr constant? In a header file use `inline constexpr std::string_view Identifier{"Identifier"};` and you will have your immutable string constant. You can also make your identifier constexpr friendly if you need it as a strong unique type.
Comment: Why do you even need the array constructor?
Comment: Actually, Identifier provides fast comparison and lookup for short immutable
strings  which only maintains a single constant char array in memory. It cannot be replaced by string_view.
Comment: @X1ngChui The problem is that when the template and non-template ctors are equally good(as in your case), the non-template version is preferred over the templated version as explained in the [answer](https://stackoverflow.com/a/77659834/12002570) below.
Comment: I re-thought my design and found that the best way to create an Identifier is to submit data and size in the same time.
Answer: Get rid of the template constructor completely, pass by std::string_view (a universal string wrapper) and rely on strlen elision instead. If the compiler sees a string literal, be sure it will be able to determine its length for you (unless this is -O0).
https://godbolt.org/z/qsc3MKTqW
Answer: The problem is that when a non-template and a template constructor are equally good(as in your case) then the non-template version is preferred over the templated version.

For Identifier id = "identifier"; the templated constructor is an exact match(since "identifier" is an lvalue of type const char[11]) while the non-templated constructor is also any exact match(as only array to pointer decay is required). They both are equally good(ranked). And when this is the case, the non-templated version is preferred version is selected/preferred over the templated version.
Answer: The issue you're encountering is due to the fact that the non-template constructor, which takes a const char*, is considered a better match for string literals than the template constructor.
To ensure that the template constructor is preferred for string literals, you can add a constraint using std::enable_if or std::is_same.
Example of using the std::is_name in the Identifier constructor that takes a string literal:
template <size_t Len>
Identifier(const char(&str)[Len],typename std::enable_if<!std::is_same<decltype(str), const char*>::value>::type* = nullptr): Identifier(str, Len - 1);


No elliptic curves over $\mathbb{Q}$ with everywhere good reduction

Question: I'm trying to prove that there aren't any elliptic curves $E$ over $\mathbb{Q}$ with everywhere good reduction. I first suppose that $\Delta = \pm 1$ and am trying to reduce the quantities for $c_{4}, c_6, \Delta$ etc. modulo 8 in order to eventually reach a contradiction. Going off of the hint in Silverman's The Arithmetic of Elliptic Curves (exercise 8.15), I'm running into some problems trying to prove that $a_1$, i.e. the coefficient of $xy$ in Weierstrass equation of $E$, is odd. My thinking is that I need to work with
$$\Delta = \pm 1 \equiv -b_2^2b_8 - 3b_6^2 +b_2b_4b_6 \pmod{8}$$
Am I right in thinking this? I've been working with it to no avail. Any help or hints would be much appreciated!
Comment: Well, it's been a long time since I was immersed in this stuff, and believe me, I've lost a lot of of what I knew, and even that was a partial understanding. Tate's original proof involved coming up with a (universal) elliptic curve over $\Bbb Q$ that had no $\Bbb Q$-points, and I don't see that here.

Beyond that, you seem to be looking for what Silverman has called "a global minimal Weierstrass equation", but there's no reason, seems to me, that you couldn't have a good W-equation over, say $\text{Spec}(\Bbb Z_S)$, and *another* good Weierstrass equation over…
Comment: , say $\text{Spec}(\Bbb Z_{S'})$, where the two models agreed over the intersection of these two open subsets of $\text{Spec}(\Bbb Z)$. After all, the discriminant is not an invariant of the elliptic curve, just of the particular Weierstrass equation. So, if what I say is right, it's not enough to look for a $\Bbb Z$-Weierstrass equation with discriminant $\pm1$.
Comment: Why not work with the Weierstrass equation of the form $y^2=x^3+ax+b$ with discriminant $\Delta=4a^3+27b^2$?
Comment: The hint used gestured towards the form I mentioned but also: I believe the discriminant is $\Delta = -16(4A^3 + 27B^2)$ for that form of WE of E, making it congruent to 0 modulo 8 and not particularly useful? I'm sure there are other ways but I know for sure that the congruences mod 8 work. I believe I have the steps after showing that $a_1$ is odd that it leads to a contradiction but I have yet to show that it is odd if we assume that $\Delta = \pm 1$.
Comment: If that's your discriminant, then it is a multiple of $16$ so certainly never equal to $\pm1$.
Comment: Yeah true! I must have something wrong then for it to be that trivial of an exercise, which I doubt it is...
Answer: If the coefficient $a_1$ is even, then
$$b_2=a_1^2+4a_2\equiv0\pmod{4}
\qquad\text{ and }\qquad
b_4=2a_4+a_1a_3\equiv0\pmod{2}.$$
It follows that
$$\Delta=-b_2^2b_8-8b_4^3-27b_6^2+9b_2b_4b_6\equiv5b_6^2\pmod{8}.$$
This can never be congruent to $\pm1\pmod{8}$. Hence $a_1$ is odd, from which point you say you can conclude the proof.
Alternatively, as noted in the comments any elliptic curve over $\Bbb{Q}$ has a Weierstrass form
$$y^2=x^3+ax+b,$$
with discriminant $\Delta=-16(4a^3+27b^2)$, which cannot equal $\pm1$ as it is a multiple of $16$.
Comment: You need to work with a minimal model; the short Weierstrass form certainly needn't be minimal at $2$.
Comment: There's a little subtlety. The elliptic curve has the simplified Weierstrass form, but that simplification can change the reduction type at $2$ or $3$.
Comment: Exactly! It only works if the characteristic of the field your elliptic curve is over isn't 2 or 3!.


trying to add the sum of two numbers from two different button clicks

Question: imageview burger = 7.99 when clicked, imageview fries = 2.99 when clicked, I want to get the sum of both however when i click one after the other it refreshes and just adds the value inside instead of adding on to the previous value. I feel like the solution is right in front of me but i still have no idea what I am missing here.
public class ThirdActivity extends AppCompatActivity {

static double price1 = 0;
static double price2 = 0;
TextView total;
double bill;
static int but1_value = 0;
static int but1_value1 = 0;
ImageView burger;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    TextView tab = (TextView) findViewById(R.id.Table_Size);

    burger = (ImageView) findViewById(R.id.burger);

    total = (TextView) findViewById(R.id.price1);

    final ImageView fries = (ImageView) findViewById(R.id.fries);

    fries.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            price2 = price2 + 2.99;

            total.setText("Total: " + price2);

        }
    });

    burger.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            price1 = price1 + 7.99;

            total.setText("Total: " + price1);

        }
    });

Answer: In your total you are either setting price1 or price2.
You should set the addition of two, as your goal is.
total.setText("Total: " + (price1 + price2));

Answer: You have assigned a new value to the variable price1 from one place and to price2 from another place.
If you want to maintain the value use a single variable and save in it from both places, or directly add them and set the value.
Use:
total.setText("Total: " + (price1+price2));

Answer: The best solution is to create global variable total_price.
private double total_price = 0;

Update the value of total_price whenever you click on fries
total_price += price1

and for burger
total_price += price2

and then set total as
total.setText("Total: " + total_price);


Outlier removal, extremes on both ends

Question: A list of numbers and I want to remove the extremes on both ends.
The standard deviation is calculated: 26.3 (rounded to 1 decimal)
Originals
[1.1,87.8,97.2,6.8,8.1,10.8,4.9,8.1,1.9,2.8,1.2,1.2,8.7,5.8,2.1,1.9,1.4,1.2,6.6,1.4,1.3,1.5]

Each Original v.s. Standard deviation
[0.04,3.34,3.7,0.26,0.31,0.41,0.19,0.31,0.07,0.11,0.05,0.05,0.33,0.22,0.08,0.07,0.05,0.05,0.25,0.05,0.05,0.06]

For this case, I want to define the numbers of below criteria as outlier:

either greater than 3 times of standard deviation
or small than 0.05 times of standard deviation

Is this a reasonable way to consider and define outlier?
(This proposed method will be applied to lists of numbers may/not normally distributed.)
Thank you.
Comment: (1) Possible that **_no_** outliers should be removed. They may give important info about what's happening. (2) I think it's a bad idea to use SD for outlier ID because SD itself is heavily influenced by outliers. (Maybe OK to check each observations by some SD method, provided that observation is removed before SD is calculated. (3) Boxplot outliers use IQR = Q3 - Q1 to measure variability; IQR is relatively robust against outliers, so that method may generally be better. (4) Why remove outliers? Obvious data entry error? Eqpt failure? Or they just annoy you?
Comment: @BruceET, thank you for details. (3) is a brilliant point. (2) is a teaching to me. for (4), there're suspicious data entry errors (not able to trace root cause) so I want to conduct outlier removal.
Comment: Data analysis needs to know more than that you have "a list of numbers". Two values look higher on any graph worthwhile, but what you do know of their context if anything?  The two values are close to 100 and the others are much closer to 0; are they percents and if not what could high values be?
Comment: @Nick Cox, thank you for the comment. This sample is taken from a case, where human entry error and misuse of input happened.
Comment: I believe you but that doesn't really impart much flavour here.
Answer: Graphical comment. If I've correctly captured your data, here is the boxplot from R. Your title says 'both ends', but I see boxplot 'outliers' only in the upper tail. Do you have any explanation how they might have arisen.
boxplot(x, horizontal=T, col="skyblue2", pch=19)

Only two 'outliers' (no tied ones).
length(boxplot.stats(x)$out)
[1] 2

Even small samples from an exponential distribution often show 'outliers'
in the right tail, which are a natural 'feature' of exponential distributions.
Would be a mistake to remove them from the sample. The population SD below is $\sigma=12.$ The SD of all 22 observations is about 11.4 (already a slight
underestimate of $\sigma.$ The SD of the 20 'non-outliers' is only about 5.2 (serious underestimate).
set.seed(123)
y = rexp(22, 1/12)
boxplot(y, horizontal=T, col="skyblue2", pch=19 )
sd(y)
[1] 11.38477
sd(sort(y)[1:20])
[1] 5.205213

For example, certain members of the Weibull family of distributions and  members of the
Pareto family have even heavier right tails, hence more 'outliers' on the
high side.
Comment: Based on my experience, it seems to me you're altogether too willing to remove 'outliers'. Saying you have data entry errors and you're unable to find the cause seems self-contradictory. If you're talking about the equivalent of a man 12' (4m) tall, then _that's_ an outlier I'd remove. Not familiar with your data, so don't want to judge. Or if the dataset says an IQ score is 191 and the actual test paper has 91 written on it. But don't expect even honest real data to be 'tidy'--like the data in many elementary textbooks.)
Comment: thank you for the answer. The question was about to find a way also suitable for other lists, where extremely small numbers are in. for example, [0.01,0.005,2,15,12,6.8,8.1,10.8,4.9,8.1,7,2.8]
Answer: Outliers are generally defined as:

values lower than Q1 - (1.5 * IQR) on the lower end
values higher than Q3 + (1.5 * IQR) on the top end

More detail here: https://newonlinecourses.science.psu.edu/stat200/lesson/3/3.2
Comment: Not a problem at all.
Comment: thank you for your help to the question. Hope you don't mind I choose another which provide more details to the topic. :)
Comment: More wrong than right, I'd say, for a bundle of reasons. (1) Outliers might exist in many different spaces (different dimensions -- univariate, bivariate, multivariate --, categorical data too, etc.) so on that ground alone this is hardly general. (2) Precise rules of thumb for outliers miss the main point, which is that identifying outliers requires background knowledge and a statistical sense of what is expected. One size fits all criteria mislead more than they help. (3) This is a perversion of the original intent in Tukey's work, at most a criterion for flagging data points to think about.
Comment: (4) "generally defined" depends on a literature survey, and it's my reading against yours, but I wouldn't summarize general statistical advice in this way. Much better advice in the question and comments of @BruceET.

Outlier removal, extremes on both ends


I need help to achieve (as close as possible) 40G network transfer speed (~1250 MB/s x 4)

Question: I had a Gigabit network, which I upgraded recently to 40Gbps.
To do so, I replace my TPLINK switch with a Cisco Nexus 9396PX switch, which has a module with 12x 40G ports. The 40G network interconnects DELL servers with ESXi7 installed.
The problem is that I am not able to achieve the throughput speed of a 40Gbps network (~1250 MB/s x 4).

When I run iperf between two virtual machines, I get a bandwidth ranging from 6 Gbits/sec to 10 Gbits/sec. I never been able to reach the 40 Gbits bandwidth.
When I copy files with scp I am getting a transfer speed of about 110-120 MB/s which is very far from the theoretical bandwidth of 1GB/s x 4 ~ 4G/s for a 40G network.

Recently I discovered that encryption reduce transfer speed so, I set up FTP server. Transfer speed doubled with FTP. I got a transfer rate about 240-280 MB/s.
I also updated MTU to 9000 on both hosts and VMs, but transfer rates are low.
Can anybody, please, help me to identify and solve the issue?
I want 40G Network bandwidth as configured in the Switch to be reflected in Virtual machines, and the transfer speed to reflect as close as possible transfer speed of 40G network (~1250 MB/s x 4).
This is the current architecture details:

Cisco Nexus 9396PX Switch with 40G module
4 Dell PowerEdge R930 Servers  (VMware ESXi7 hosts)
NVME disks on each server
40 Gbps Mellanox NIC on  ESXi7 hosts
QSFP+ DAC cables to connect servers to switch

UPDATES
I just made 3 more tests. And I can not say there are improvements
FIRST TEST

In Switch, I checked MTU on the server interface  and noticed it was not set to Maximum. I tried to set it to 9000 but was denied. It can only be set either to 1500 or 9216. I chosed the highest value.
Still in the Switch I fixed speed to 40G/s and disabled speed auto negociation
I disabled speed auto negociation on ESXi hosts from VCenter.

Then I run the tests again. These changes did not affect the scp copy speed transfer speed between VMs (Ubuntu 22.04).
But I noticed a light improvement when downloading from FTP server (it is installed on one of the two ubuntu VMs).
SECOND TEST
I run the following tests on VMs with Windows 10 installed.

I added two Windows VM to the host and configured them with MTU set to 9000 too.
I created a shared folder for copy betwnn the two Windows VMs.

When I copy files from shared folder to local, the speed is around 600MB/s. But when I copy from local to shared folder, the copy starts at around 350MB/s and the speed decreases when it reaches ~30%.
THIRD TEST
For the last test I installed Filezilla on one the the windows VMs and dowloaded files from/to FTP server (on Ubuntu VM).

Dowload speed is around 450MB/s when the download starts and at about ~50% it decreases to ~200MB/s
Upload speed is around 200MB/s
Comment: @yaoviametepe - Yeah, I agree with Gerald, specifically the second paragraph of my answer to that other user - 40Gbps isn't what most people think it is.
Comment: Not my area of expertise, but [this answer](https://serverfault.com/a/1127314/38644) was posted yesterday and could be helpful. Especially the second paragraph.
Comment: @GeraldSchneider thank you for your answers. This is a valuable information you gave.
May be I should focus on how to achieve 1G/s of dowload speed with the one line of 10G. Because I can not even achieve this speed.
Answer: 40 gig is basically a bonded 4*10 gig connection. To benefit from the aggregated bandwidth you need multiple TCP sessions running at the same time. You'll never reach combined estimated bandwidth with just one process and a single TCP session. Some good description could be read here:
https://www.starwindsoftware.com/blog/lacp-vs-mpio-on-windows-platform-which-one-is-better-in-terms-of-redundancy-and-speed-in-this-case-2
Comment: Thank you for the answer. It helped me confirm that I am on 10Gbps link, not 40G.
I do not know if it could be related to the virtual settings or not.

The command `iperf3 -c 192.168.1.80 -P 8 -t 60` output summary is:

`...[ 19]   0.00-60.02  sec  7.15 GBytes  1.02 Gbits/sec                  receiver
[SUM]   0.00-60.00  sec  57.0 GBytes  8.16 Gbits/sec  140             sender
[SUM]   0.00-60.02  sec  56.9 GBytes  8.15 Gbits/sec                  receiver`
Comment: A 40G interface is *not* the same as four bonded 10G interfaces. On the former you can get a single flow up to 40G (with some tuning) and on the latter only up to 10G.
Comment: @Zac67 Can you please tell me what values needs to be tuned to achieve 40 Gbits/s throughput  in my VMs?


MVC Intellisense

Question: Dont have any intellisense when creating a sample MVC3 app.

Config files are fine, project compiles and works as it should when ran...
Have MVC3 & MVC4 installed, same thing with each.
Have VS2010 & VS11 installed (running fine side by side), same thing here... no intellisense.
Errors:
'ViewData' is not declared. It may be inaccessible due to its protection level.
'Url' is not declared. It may be inaccessible due to its protection level.
'Partial' is not a member of 'System.Web.WebPages.Html.HtmlHelper'.
'ActionLink' is not a member of 'System.Web.WebPages.Html.HtmlHelper'.
The 'ActionLink' error should be trageting the Mvc library should it not?
Comment: Nope, have updated Q with the error replies...
Comment: Have you restarted VS?  Sometimes I'll randomly lose IntelliSense and a restart usually takes care of it.
Comment: Restarted PC & resest settings in Import And Export tool... still the same.
Answer: You don't worry. its a reported issue with visual studio. In the latest upgrade its not there. Me also was facing the same issue.

MVC Intellisense


Is there a proper way to 'sync' branches and keep them separate?

Question: So I created a new branch from master and eventually merge the changes back, only some changes seemed to merge and it would seem I merged in one direction. Eventually I just decided to be sure master received all the 'experimental' changes then I deleted the experimental branch and made a new one. So this question is turning into a few questions suddenly:
1) How do I match up branches but keep them separate?
1a) Is that bad practice to not just make a new branch?
2) Why were the branches not the same after after one merge?
2a) Am I only supposed to call a merge on the one that I want to have all of the changes?
Answer: With respect to the practical, a merge is definitely a one-way operation. It merges one branch into another. The resulting history after merging experimental into master would look like this:
<code>- o - o - o - o - o - X (master)
   \                 /
    o - o - o - o - o (experimental)
</code>
So after one merge, master contains all of the changes made in experimental, but they are clearly not the same.
I'm a little confused about what exactly you mean by "match up but keep separate". It's common practice to make a topic branch for some specific purpose (feature, bugfix...), then merge it into everywhere that entity is needed. For example, a bugfix could belong in two old maintenance releases as well as the current master, so it'd get merged into all three.
I think this post on Junio Hamano's blog (he's the current maintainer of git) is one of the best explanations I've found of exactly when you should merge, and when you shouldn't. It should answer the bulk of your philosophical questions.
Comment: I think I see what you mean.

By match and keep separate I meant that I'd like to synchronize their code and then continue running with the "experimental" as I still have experiments to continue with but I made a wide enough change on master that it would be blasphemous to continue with being sure I had the same code.

Wonderful post, that is and will clear up a lot for me.
Comment: @jphenow: In that case, you should keep the experimental branch around even after you merge into master (e.g. `git checkout master; git merge experimental; git checkout experimental`). However, I'd caution you against using a single experimentation branch for a variety of experiments - they should really be on their own branches, so you can merge them separately.
Comment: Oh of course - I have a different branch for each experiment subject, but I looked at each branch and it seemed like something was changed on the master that would make it awfully weird to continue on the experimental without getting some of the master changes.
Comment: @jphenow: Generally, when a topic branch needs a particular change from the master branch in order to continue, the ideal workflow is to merge from the topic branch which introduced that change, rather than from master itself - this avoids merging a bunch of irrelevant stuff from master.

Is there a proper way to ‘sync’ branches and keep them separate?


Fail2ban not blocking ssh attempts

Question: I have set up fail2ban to block failed ssh attempts. I checked the rule with fail2ban-regex and and an extract from my logs, this works fine.
I also checked the logs for the "Ban" notice and the IP in question and it's there:
zgrep 'Ban.*126.96.36.199' /var/log/fail2ban.log*
/var/log/fail2ban.log:2021-08-23 01:27:19,023 fail2ban.actions        [1460]: NOTICE  [sshd] Ban 126.96.36.199

However, my ssh / auth logs still show attempts from that IP after the timestamp in question:
Aug 23 01:27:23 myhost123 sshd[4526]: message repeated 2 times: [ Failed password for root from 188.8.131.52 port 47633 ssh2]
Aug 23 01:27:23 myhost123 sshd[4526]: error: maximum authentication attempts exceeded for root from 18.104.22.168 port 47633 ssh2 [preauth]
Aug 23 01:27:23 myhost123 sshd[4526]: Disconnecting authenticating user root 220.127.116.11 port 47633: Too many authentication failures [preauth]
Aug 23 01:27:31 myhost123 sshd[4533]: message repeated 2 times: [ Failed password for root from 18.104.22.168 port 50424 ssh2]
Aug 23 01:27:31 myhost123 sshd[4533]: error: maximum authentication attempts exceeded for root from 22.214.171.124 port 50424 ssh2 [preauth]
Aug 23 01:27:31 myhost123 sshd[4533]: Disconnecting authenticating user root 18.104.22.168 port 50424: Too many authentication failures [preauth]
Aug 23 01:27:39 myhost123 sshd[4535]: error: maximum authentication attempts exceeded for root from 18.104.22.168 port 53056 ssh2 [preauth]
Aug 23 01:27:39 myhost123 sshd[4535]: Disconnecting authenticating user root 220.127.116.11 port 53056: Too many authentication failures [preauth]
Aug 23 01:27:48 myhost123 sshd[4542]: error: maximum authentication attempts exceeded for root from 18.104.22.168 port 55901 ssh2 [preauth]
Aug 23 01:27:48 myhost123 sshd[4542]: Disconnecting authenticating user root 18.104.22.168 port 55901: Too many authentication failures [preauth]
Aug 23 01:27:55 myhost123 sshd[4551]: error: maximum authentication attempts exceeded for root from 22.214.171.124 port 58908 ssh2 [preauth]
Aug 23 01:27:55 myhost123 sshd[4551]: Disconnecting authenticating user root 184.108.40.206 port 58908: Too many authentication failures [preauth]
Aug 23 01:28:03 myhost123 sshd[4565]: error: maximum authentication attempts exceeded for root from 18.104.22.168 port 61129 ssh2 [preauth]
Aug 23 01:28:03 myhost123 sshd[4565]: Disconnecting authenticating user root 22.214.171.124 port 61129: Too many authentication failures [preauth]
Aug 23 01:28:23 myhost123 sshd[4577]: error: maximum authentication attempts exceeded for invalid user admin from 184.108.40.206 port 3511 ssh2 [preauth]
Aug 23 01:29:24 myhost123 sshd[4613]: error: maximum authentication attempts exceeded for invalid user oracle from 188.8.131.52 port 24149 ssh2 [preauth]
Aug 23 01:30:07 myhost123 sshd[4641]: error: maximum authentication attempts exceeded for invalid user usuario from 18.104.22.168 port 37311 ssh2 [preauth]
Aug 23 01:30:15 myhost123 sshd[4647]: error: maximum authentication attempts exceeded for invalid user usuario from 18.104.22.168 port 39486 ssh2 [preauth]
Aug 23 01:30:58 myhost123 sshd[4684]: error: maximum authentication attempts exceeded for invalid user test from 220.127.116.11 port 52882 ssh2 [preauth]
Aug 23 01:31:33 myhost123 sshd[4699]: error: maximum authentication attempts exceeded for invalid user user from 220.127.116.11 port 64849 ssh2 [preauth]

Should fail2ban not block all requests from that IP completely? Any hints on what to check appreciated.
Comment: Check if "iptables -L -nv" returns something about f2b-ssh. Check fail2ban logs arround the ban log : there is maybe an error when trying to put the iptables in place.
Answer: Thanks to Dom's hint: the iptables limit is the root cause of the issue.
Checking with:
grep "iptables: Memory allocation problem" /var/log/fail2ban.log

egrep "failcnt|numiptent" /proc/user_beancounters

It seems I cannot change the iptables limit, only my provider can.
Comment: if your host's kernel supports `ipset` and you can use it over OpenVZ, you could switch to some of iptables-ipset action, so it is creating only one iptables chain per jail and all the IPs going to `ipset` (which is much faster and hopefully doesn't affected by such strict limits on your system as `iptables`).
Comment: This is yet another problem with OpenVZ. It is recommended to avoid OpenVZ and use a VPS with true virtualization.

Fail2ban not blocking ssh attempts


Export Forecast Plots to JPEG within a For Loop

Question: I've created a for-loop that reads individual time series files (.csv) and exports the forecast values for each time series back to csv. I wanted to also export the individual time series plots to JPEG within the for loop. I am new to R and would like to have some guidance on how to do it. Not sure if creating another for-loop within the existing for-loop is the answer.
I've tried exporting a sample plot for one time series and it worked. I couldn't figure out how to do it within the existing for-loop.
Here's the for-loop code (imports/exports forecast values from/to CSV)
setwd("wd")
 for (file in list.files(pattern = "*.csv")) {
     library(prophet)
     df <- read.csv(file)
     m <- forecast(df)
     future <- make_future_dataframe(m, periods = 90)
     out <- predict(m, future)
     write.csv(out, sprintf("out_%s.csv", file))
}

Here's the code that exports a JPEG plot
jpeg('rplot.jpg')
plot(m,forecast)
dev.off()

Edit:
setwd('wd')

files <- list.files(pattern = "\\.csv$")

for (i in seq_along(files)) {
    library(prophet)
    df <- read.csv(files[i])
    m <- prophet(df)
    future <- make_future_dataframe(m, periods = 90)
    forecast <- predict(m, future)
    out <- predict(m, future)
    write.csv(out, sprintf("out_%s.csv", files[i]))

    jpeg(paste('rplot', files[i], '.jpg'))
    plot(m, forecast)
    dev.off()
}

Comment: Your loop reads and predicts from the same file: `file.csv`. Unless some random process occurs in `forecast`, all *out* csv files would maintain the same content including attempted plot.
Comment: By the way, per R tag: please specify all non-base packages with `library()` calls.
Comment: @Parfait, the code above was 'shortened'. it works just fine and exports the forecast values. and yes `library()` should be there too. i'm more curious on the correct implementation for exporting the jpeg plots within the loop.
Comment: Once again, please edit question with exact `library` calls. Regarding plots, simply run inside loop and change the file name as below answer shows which is unclear why it doesn't work per your comment. It must be due to the single `file.csv` you use across all iterations.
Answer: You need to change filename inside the loop
setwd("wd")

files <- list.files(pattern = "\\.csv$")

for (i in seq_along(files)) {
  df <- read.csv(files[i])
  m <- forecast(df)
  future <- make_future_dataframe(m, periods = 90)
  out <- predict(m, future)
  write.csv(out, sprintf("out_%s.csv", i))

  jpeg(paste0('rplot', files[i], '.jpg'))
  plot(m, forecast)
  dev.off()
}

Comment: Simply change `sprintf("out_%s.csv", i)` to `sprintf("out_%s.csv", files[i])`. Regarding empty plots, without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), we cannot help further. Your original post reads the **same** file across all iterations where iterated files are only used for naming outputs.
Comment: Didn't work, the CSV files are now displaying out_1.csv, out_2.csv, which should have retained the original file name instead of numerical values. And the JPEG files are mostly empty, with the file name retaining the original CSVs (i.e. 'rplot forecast1.csv.jpg')
Comment: @Parfait thanks, `files[i]` worked. Also corrected the code from the original post (`file` instead of `file.csv`) and included the library call. Re-ran the whole thing and the plots are still empty. Will check out the link on reproducible example.
Comment: Actually @Reese may have kept using the unknown *forecast* object instead of *future* in `plot` call where *forecast* was used in your original non-loop code.
Comment: @Parfait, i did edit the original post showing Reese's code (with minor edits!), but I still get blank JPEGs.
Comment: Check your *m* and *future* data in each iteration as they may be empty and try printing plot in `for` loop: `print(plot(...))`. Again, all guesses since we have no reproducible example on our end. See other R questions on how they provide sample data (hint: `dput`).


Matrix equation xA=x

Question: The equation $Ax=x$ seems trivial, but how one could solve xA=x.
Assuming A is reversible, the best I can get to is $x(a^{-1}+I)=0$. However, I'm not sure what to do from here. Thank you for your help in advance!
Comment: What do you mean $xA = x$? Do dimensions even agree? If $A$ is an $m \times n$ matrix, then they agree if and only if $m = n = 1$, which is kind of boring. And um, the linear system $Ax=x$ has techniques to be solved yes, but it is not a "trivial" system in the mathematical sense.
Comment: @PatrickDaSilva, A is a square matrix, x is a vector, so the dimensions should agree. Can this be solved with matlab or mathematica?
Answer: Assuming that you work with usual column vectors and that $A$ is a square matrix, it makes sense to formulate the question on the system $x'A=x'$ that is equivalent to $A'x = x$ which is solvable by the same means as $Ax=x$.
Comment: Where $x'$ and $A'$ mean the transpose.
Answer: The point is, we usually write $Ax = x$ to mean "$A$ ($m \times n$) times $x$ ($n \times 1$) equals $x$ ($n \times 1$)". If you are looking at $xA = x$, then to make the problem "not boring" $x$ should be a row vector, so after transposing everything you can come back to the original problem $Ax=x$. The transpose of $xA$ is $(xA)^{\top} = A^{\top} x^{\top}$, so just adjust Mathematica or Matlab with commands accordingly.
Hope that helps,

Matrix equation xA=x


simple slope analsis significance testing

Question: let's say that I have this model below:
lm(academic_peformance ~ biological_sex(binary) + age + history_score + math_score+
history_score:math_score)
I want to test simple slope analysis to probe interaction (math:history)
but due to the biological sex being binary,
I was wondering what should I do to run the significance test for a simple slope analysis.
(like, I want to test the simple slope analysis with + 1 sd, -sd for math score to probe math:history interaction)
How can I conduct simple slope ?
I will plug mean of age to age, but really don't know what should I do to deal with categorical covariate.
Comment: Your question is awfully cryptic. Include more details about what you are trying to learn from the data, and you are likely to get thoughtful responses. It isn't clear to me what you mean by "simplified slope analysis" or "probe. interaction"
Answer: In your model, neither biological_sex(binary) nor age is involved in the interaction term, so each of them just provides an additive contribution to outcome regardless of the values of history_score and math_score. Thus you can choose whichever biological_sex(binary) value that you want, and the analysis of the history_score:math_score interaction will be the same. The only difference would be in the absolute values of the predicted outcomes, not in the slopes with respect to history_score and math_score.


What does (SAS) Enterprise Miner's "Bucket Transformation" do?

Question: In SAS's Enterprise Miner there is a "Bucket Transformation" which from what I can tell, is a term that is unique to Enterprise Miner.  It takes a continuous variable and groups it to become an ordinal variable with a certain number of bins.  I would like to know what it is doing and what this would be called in other software.  I am attempting to do something like this in python but I am not sure how to make "optimal buckets".
Comment: http://www.nesug.org/proceedings/nesug07/sa/sa17.pdf
Comment: What this is often called in other software is bad statistical practice.
Comment: @statsnewb, the paper referenced above by Max gives you a clear definition for what bucket transformation does. SAS support is very helpful, you might want to contact them or you could consult their documentation which is exhaustive.
Comment: @FrankHarrell what do you mean by that?
Comment: Binning categorical predictors creates artificial discontinuities and worsens the fit.  It is also arbitrary.
Comment: The SAS documentation is exhaustive.  However, from what I can tell, the Enterprise Miner documentation is not.  @Max thank you.  I am interested to see how they pick the optimal number of "buckets", any ideas on how they do this?
Answer: This question is old. If you are still interested, bucket transformation is called equal spaced bucket. If you have continuous variable with min=1 and max=100, for example, buckets=10. Then the transformation creates buckets by 1-10, 10-20,20-30... 90-100, regardless how many observations fall into each bucket. Eventually, (max-min) for each bucket is the same. Hope this helps.
Jason Xin
Answer: I can't answer what SAS's Bucket Transformation does. You'll have to read the documentation. You might want to have a look at pandas.cut or pandas.qcut though.
http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.cut.html
http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.qcut.html
Answer: 'Buckets' or 'bins' are used to transform a continuous variable into groups. For example, a model I am building might benefit by ranking subjects according to income level.
0 - $19,999k
$20k - $39,999k
.....
$100k and above
Creating buckets will allow me to do this.
-- Glenn


Azure Datalake Gen1 Permission Denied while iterating over folders

Question: I am trying to iterate over all the folders in Azure datalake Gen1 using Python SDK. I am using a service principal with an owner role assigned to it. However, when I try to access some of the folders, I get a permission denied error. I am not sure if I require any other roles as well because the owner has the highest privilege I guess.
Below is the code which I am using
<code> %pip install azure-mgmt-resource
%pip install azure-mgmt-datalake-store
%pip install azure-datalake-store

  import re
  import datetime;
  from azure.datalake.store import lib, core
  from azure.common.credentials import ServicePrincipalCredentials
  from pyspark.sql.types import StringType
  from pyspark.sql.functions import udf,split, explode,col,when,concat,array_contains,lit
  import datetime;

  def authenticate(clientId, secretId, tenantId):

      RESOURCE = 'https://datalake.azure.net/'

      tenant = tenantId

      client_id = clientId

      client_secret = secretId

      adlCreds = lib.auth(tenant_id=tenant,

                          client_secret=client_secret,

                          client_id=client_id,

                          resource=RESOURCE)

      adl = core.AzureDLFileSystem(adlCreds, store_name="xxx")

      return adl

  clientId = dbutils.secrets.get(scope = "datalakesecurity", key = "datalakeclientid")

  secretId = dbutils.secrets.get(scope = "datalakesecurity", key = "datalakecredential")

  tenantId = 'xxxxx'

  adl = authenticate(clientId, secretId, tenantId)

  path_to_look= '/'

  level1 = adl.ls(path_to_look)

  print(level1)
</code>
Comment: Hi @Ash3060, Can you share the code that you have tried?
Comment: @Venkatesan I have updated the sample code to iterate. for example in path to look when I am giving specific path it is failing. I want full access over datalake for my service principal
Answer:
Azure Datalake Gen1 Permission Denied while iterating over folders.

In Azure Data Lake Gen1, the "Owner" role has full access to the entire Data Lake account, including all folders and files. Even if you hold the Owner role, there are some folder-level permissions that may still bar you from accessing particular folders. You may also give your service principal a Contributor position.

According to this MS Document Along with the owner role you need to explicitly give the permission to read, write, execute on a folder level.

Even if have owner role on your service principle but you don't have the above permissions you will face the permission denied error.


Preciso incluir classes quando fazer herança?

Question: Seguinte.
Quando eu quero usar alguma coisa, por exemplo, no index, eu quero puxar uma classe pra fazer um objeto, certo?
Imagine que a classe esteja assim:
<?php
class Testando{
   //Meu codigo aqui
}
?>

Ai, tenho meu index:
<!DOCTYPE html>

        <meta charset="UTF-8">
        <title></title>

        <?php
             require'Testando.class.phg';
             $Teste = new Testando;
        ?>

Ai agora, tenho minha segunda class, vamos supor.. :
<?php
   //Aqui eu não precisaria de um require/include da class Testando?
   class EuTestei extends Testando {
       //Meu codigo aqui
   }
?>

Ali, onde comentei, na segunda class, não precisaria de include/require? Pôs aqui, testando, ela não da erro. Mas como ela acha essa classe?
Comment: Se este segundo arquivo também foi incluído no `index.php`, o PHP acha a classe pois ela foi carregada também no `index.php` através do primeiro `require`. Quando você faz o `require` ou `include`, para o PHP é como se todos os códigos estivessem escritos no mesmo arquivo, então a classe `Testando` estaria implementada antes da classe `EuTestei`, por isso funciona. Se no `index.php` você der `require` apenas do segundo arquivo, então sim, você precisará dar o `require` do primeiro onde você comentou.
Comment: Você está utilizando namespace? caso esteja utilizando autoload não é necessário dar o include/require..
Comment: autoload eu uso, mas não entendo, como aquela class extende a outra, sendo que não usei require.
Comment: Recomendo você a estudar e entender como funciona e pra que serve o autoload, pois ele já faz o carregamento completo de todas as classes, e onde você da um new no objeto ali já tem todas as classes do sistema carregadas.
Comment: Entendi, Anderson, se quiser por a resposta, ai marco como aceita!
Comment: Obrigado também Drik
Answer: Vamos supor que temos três arquivos: dois com as definições das classes e outro que será o código a ser executado.
Person.php
<?php

class Person
{
    public $firstname;
    public $lastname;
}

User.php
<?php

class User extends Person
{
    public $username;
    public $password;
}

Perceba que a classe User estende a classe Person. Se no arquivo index.php fizermos algo como:
<?php

require "Person.php";
require "User.php";

$user = new User();
$user->firstname = "Anderson";
$user->lastname = "Woss";

Para o PHP, o arquivo executado seria basicamente:
<?php

// require "Person.php";
class Person
{
    public $firstname;
    public $lastname;
}

// require "User.php";
class User extends Person
{
    public $username;
    public $password;
}

$user = new User();
$user->firstname = "Anderson";
$user->lastname = "Woss";

E como todas as classes estão definidas funcionará perfeitamente, mesmo que no arquivo User.php não tenhamos incluído o arquivo Person.php, pois no final, tudo foi interpretado como um arquivo só.
O que a função de autoload faz é basicamente isso: incluir todas as dependências no arquivo index.php, pois assim elas estarão definidas em todos os arquivos da aplicação - partindo da consideração que a aplicação é sempre executada sobre o arquivo index.php.


Reallocate Storage in Red Hat 6

Question: Is it possible to (safely) reallocate storage space in /home to the root filesystem?
$ df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/vg_yavin-lv_root   50G   35G   13G  74% /
tmpfs                          24G     0   24G   0% /dev/shm
/dev/sda1                     485M   64M  396M  14% /boot
/dev/mapper/vg_yavin-lv_home  478G  235M  453G   1% /home

Answer: The easiest way? Shrink the /home partition and expand /. That's how I had to do it (RHEL 6.4 has that as the default setup). Most of the instructions are located here: http://www.tcpdump.com/kb/os/linux/lvm-resizing-guide/all-pages.html
Here's the overview:
Unmount: umount /home
Resize: lvresize -L -400GB  /dev/mapper/vg_yavin-lv_home
Apply: resize2fs -p /dev/mapper/vg_yavin-lv_home
Then do it again for root:
lvresize -L +400GB  /dev/mapper/vg_yavin-lv_root
resize2fs -p /dev/mapper/vg_yavin-lv_root
Then finally remount it: mount /home - or, if for some reason this doesn't work, mount /dev/mapper/vg_yavin-lv_home /home
After, run fsck on both just to be safe. You can do this with the mounts active, although I highly recommend ending any running processes if possible. Also, it goes without saying but I highly recommend you have a good backup as anything can happen.
Comment: You're right, but LVM partitions are treated slightly differently in that shrinking does work when online (since they're not "real" partitions but logical).
Comment: Nope.  The partition/volume sizes are written to disk as they're run.
Comment: Okay, I stand corrected apparently. Some kernels don't support online shrinking. Use `umount /home` first then mount it again after resizing.
Comment: +1 for beating me to it.  But are you sure you can shrink mounted volumes?  `man resize2fs` certainly claims otherwise.
Comment: do I need to make any changes in fstab to make sure these changes remain after reboot?
Comment: I get this error:
`Filesystem at /dev/mapper/vg_yavin-lv_home is mounted on /home; on-line resizing required
On-line shrinking from 127047680 to 22190080 not supported.` when I run `resize2fs -p /dev/mapper/vg_yavin-lv_home`
Comment: @NathanC Would you edit your answer to reflect the umount /home and whatever I would need to do to remount it
Comment: All done. Should have done that before.
Comment: One other thing I was going to bring up, didn't, but really probably should have.  I believe the `resize2fs` should be done before the `lvresize`.  That way, there is a period where the filesystem is smaller than the volume.  The reverse is significantly more of an issue.  As evidenced by http://serverfault.com/questions/517336/unexpected-inconsitency-run-fsck-manually-redhat/517352#517352
Comment: @ChristopherKarel The `lvresize` has to be done first as `resize2fs` will then read the volume data that `lvresize` wrote and do what it says. If you run `resize2fs` it'll simply say there's nothing to do.
Comment: You can specify a size to `resize2fs`.  I would assume that clears up the 'nothing to do' problem.  http://linux.die.net/man/8/resize2fs  That being said, I believe resize2fs should only be run first on the shrinking portion.  Your order is correct for the growing part.
Comment: @ChristopherKarel That works on physical partitions (like sda1 and such) but not LVMs.
Comment: Fair enough.  I clearly don't work with those much.
Answer: You can do the file system resize in the same step with the inclusion of -r.
umount /home
lvreduce -rL -100G /dev/mapper/vg_yavin-lv_home
lvextend -rL +10G /dev/mapper/vg_yavin-lv_root

I'm also going to suggest that at this time you create additional filesystems for your log directories (/var/log, and /var/log/audit), at a minimum. A rogue service can start vomiting all over your filesystem. You want to segregate your logs so this doesn't impact operations. (You really don't want root to fill up..)
lvcreate -n log -L8G vg_yavin
mkfs.ext3 -L log /dev/mapper/vg_yavin-lv_log

Then, create your /etc/fstab entry and mount -a.
Also, leave a few gigs unallocated so you can grow something in a pinch without having to unmount something else.

Reallocate Storage in Red Hat 6


Complex Analysis Weierstrass Test

Question:
Hi there. I am struggling to understand the line:
$\frac{1}{2}(e^{-ny}+e^{ny}) \le e^{nr}$
Could someone please help me?
Answer: Generally, for real $t$, we have $e^t \leqslant e^{\lvert t\rvert}$ (and $\lvert e^z\rvert \leqslant e^{\lvert z\rvert}$ for $z\in\mathbb{C}$). Hence for $-r \leqslant y \leqslant r$, we have
$$\frac{1}{2}\left(e^{-ny} + e^{ny}\right) \leqslant \frac{1}{2}\left(e^{\lvert -ny\rvert} + e^{\lvert ny\rvert}\right) = \frac{1}{2}\left(e^{n\lvert y\rvert} + e^{n\lvert y\rvert}\right) = e^{n\lvert y\rvert} \leqslant e^{nr}.$$

Complex Analysis Weierstrass Test


Why such an $x_{k+1}$ is of that form?

Question: Let $p=2$, $a\in \mathbb{Z}$ such that $a\equiv 1 \pmod 8$ (notice that in particular $a$ is odd). Then for all $k\ge 1$ there exists at least one solution to the following equation : $x^2 \equiv a \pmod {2^k}$.
Here is the solution proposed :
Induction on $k$ :
$\bullet$ initialization : for $k=1$ (and even for $k=2, \ 3$) we have that : $1^2 \equiv a \pmod 2$ (mainly because $a = 1 +8l, \ l \in \mathbb{Z}$).
$\bullet$ step case : suppose that for a fixed $k \ (\ge 3$ w.l.o.g), hence there exists $x_k$ solution of the following equation : $x^2 \equiv a \pmod{2^k}$. Let us show that there exists a solution $x_{k+1}$ for the equation : $x^2 \equiv a \pmod{2^{k+1}}$.
Now the author use the trick that he wants to build such an $x_{k+1}$ of the form $x_{k+1} = x_k + 2^{k-1}\alpha$, where $\alpha \in \mathbb{Z}$ is to determine.
Finally it leads to $x_{k+1}^2 \equiv x_k^2 +2^k\alpha \equiv a + 2^k (s + \alpha) \pmod{2^{k+1}}$ with $s \in \{-1,0,1\}$. Depending on $s$, it gives $\alpha=\pm 1$ or $\alpha = 0$ and we have such the required $x_{k+1}.$
But my question is : where does the idea of searching for an $x_{k+1}$ of this form come from ?
In the case of $p>2$, $a$ a non-zero quadratic residue modulo $p$ and $k\ge 1$, I can see why we want to search $x_{k+1}$ of the form $x_{k} +p^k \beta$, $\beta \in \mathbb{Z}$ to determine. Indeed the following homomorphism : $(\mathbb{Z}/p^{k+1}\mathbb{Z})^{\times} \rightarrow (\mathbb{Z}/p^k\mathbb {Z})^{\times}$ is surjective as $p^{k} \mid p^{k+1}$ and so for $x_k \in (\mathbb{Z}/p^k\mathbb {Z})^{\times}$ its pre-image is of the form $x_k + p^k \beta \in (\mathbb{Z}/p^{k+1}\mathbb {Z})^{\times}$, with $\beta \in \mathbb{Z}$.
However for $p=2$, we cannot use the same method because depending on the value of $k$ the sub-groups are not necessary cyclic. But if you suppose $k\ge 3$, in that case you have a surjective homomorphism : $(\mathbb{Z}/2^{k+1}\mathbb{Z})^{\times} \rightarrow (\mathbb{Z}/2^k\mathbb {Z})^{\times}$ and for $x_k \in (\mathbb{Z}/2^k\mathbb {Z})^{\times}$ its pre-image is of the form $x_k + 2^k \alpha \in (\mathbb{Z}/2^{k+1}\mathbb {Z})^{\times}$,  $\alpha \in \mathbb{Z}$. But taking the square of this, you lose the $\alpha$ you want to determine so you cannot conclude as in the previous case. Even if you suppose $k\ge 4$ and consider the surjective map : $(\mathbb{Z}/2^{k+1}\mathbb{Z})^{\times} \rightarrow (\mathbb{Z}/2^{k-1}\mathbb {Z})^{\times}$ it does not work.
Thanks in advance !
Comment: Please, use descriptive titles. "Where does that trick come from?" says nothing about the subject of the question.
Answer: It is a particular case of Hensel's lemma (if there is a solution mod $p$, then there is a solution modulo $p^n, n\ge 2,$ under some assumption). The proof is also a particular case of a standard proof of that lemma.

Why such an $x_{k+1}$ is of that form?


How does a covergroup handle with an event handle sampling when enabling strobe?

Question: I am curious as to how the sampling happens in a SystemVerilog covergroup when we have an event specified and we also use type_option.strobe = 1 for said covergroup.  For example:
covergroup c @(posedge clk)
 type_option.strobe=1;

strobe implies sampling happens in the Postponed region, however the @(posedge clk) as an event is evaluated at the Active region. How does the sampling function resolve this?
I tried reading the LRM, but it wasn't very cohesive.
Answer: When type_option.strobe = 0, the default, sampling occurs immediately in whatever region the sampling event gets triggered. In some odd cases, that could mean sampling multiple times within the same time slot.
When type_option.strobe = 1, the sampling event just schedules the actual covergroup sampling to occur once in the postponed region.
Answer: From IEEE Std 1800-2017, Table 19-3—Coverage group type (static) options:

strobe=boolean
When true, all samples happen at the end of the time
slot, like the $strobe system task.

Also, section 18.104.22.168 Postponed events region:

$monitor, $strobe, and other similar events are scheduled in the
Postponed region.

Therefore, setting strobe=1 forces the sampling to occur in the Postponed region.  If strobe=0, sampling is not forced into the Postponed region.


Missing "gcc" error when trying to deploy a flask app with requirements.txt to Azure Web App from VSCode

Question: From within VSCode, using the Azure web service extension, I can deploy a Hello-World flask app successfully to my Azure web app (linux, python 3.6 stack).
When I try to deploy my real flask app (with dependencies listed in a requirements.txt) file, and I specify "pip install --upgrade -r requirements.txt" as the web app "startup command" in the Azure portal, browsing the web app page gives an "Application Error".
The log for my web app shows the startup code failing when trying to build "psutil" due to the error unable to execute 'gcc': No such file or directory.
When the above failed, I also tried using "wheels" as part of my deployment as described here (https://blogs.msdn.microsoft.com/azureossds/2015/06/29/install-native-python-modules-on-azure-web-apps-api-apps/), but that didn't seem to change anything (same 'missing gcc' error).
Is this the correct way to deploy a flask app with python library dependencies from VSCode?  I would expect that the deployment process would automatically process the requirements.txt (without having to specify the startup command) and I would expect it to be able to install the python libraries without error.
Answer: Due to you were using Azure WebApp for Linux based on Docker Container, the MSDN blog Install Python Modules on Azure App Services for Azure WebApp for Windows is not useful for your issue.
So for your issue of missing gcc error, per my experience, the solution to fix it is to configure a custom Linux container which had been pre-installed the gcc tool-chains and then to deploy your app into it from VS Code.
As references, please try to follow the documents below.

Configure a custom Linux container for Azure App Service . To install the gcc toolchains in a custom docker image via change the Docker file like install openssh as the section Enable SSH said.
Deploy to Azure using Docker from VS Code with Azure App Service extension.


Unable to download Sql Server from Microsoft

Question: i'm trying to download Sql 2012 from microsoft official website but each time i hit the "Next" buttton.

and this take me to this page

But nothing happens and this loop continues.
Answer: On the second screen you can see line. "If your download does not start after 30 second Click here". So click there and download and your problem will disappear.
Comment: try to change browser. MS page works, and allow to download these files.
Comment: wojtaszek if i click on "Click Here" it take me to the first image i have paste . where i try again and then again on second page


Table view from array index out of range

Question: I'm using an array to read data from a database, Currently I have 8 items in the array. I am trying to make a table where I have a section header. Currently I have 4 sections and I have set that properly and it works. It also works running the first time but when I try to scroll back I get an index out of range. I am using myarray[myindex] to set the cell data for each item and that is not working.
It seems that I need to break up my data into 4 sections that contains only the data for each section to let the table view control it properly. The data can contain any number of sections.
Is there a better way to do this?
I have attached a pic to describe the problem.

Thanks
Adding code on request.
  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    print("Returning Sections - > \(sections)")
    return sections //seems to work
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    print("Return number of rows in section -> \(noRowsInSection[section])")
    return noRowsInSection[section] // seems to work
}
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionHeader[section] // seems to work
}
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
    // Format for section Headers
    let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
    header.textLabel!.textColor = UIColor.blueColor()
    UIColor.blueColor()
    header.textLabel!.font = UIFont.boldSystemFontOfSize(12)
    header.textLabel!.frame = header.frame
    header.textLabel!.textAlignment = NSTextAlignment.Right
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("OurCell", forIndexPath: indexPath) as! OurTableViewCell
    print("myindex - > \(myindex) row -> \(indexPath.row)")
    cell.OurCellLabel.text = MyHouses[myindex].getAddressDetails()        // End configure houses.cell
    //cell.OurCellLabel.text = MyHouses[indexPath.row].getAddressDetails()        // End configure houses.cell
    myindex++ // PROBLEM HERE - GOES OUT OF RANGE
    return cell
}

Here I am getting data from the sqlite DB
func GetListOfHousesFromDB() {
    let docsDir = dirPaths[0]
    let databasePath = docsDir.stringByAppendingString("/newdb.db")
    if fileMgr.fileExistsAtPath(databasePath as String) {
        let houseDB = FMDatabase(path: databasePath as String)
        if houseDB.open() {
            var noRows: Int = 0
            var sql = "select count(Address) as cnt from Houses" // Define Query
            houseDB.executeStatements(sql) // Execute Query
            let results:FMResultSet? = houseDB.executeQuery(sql,withArgumentsInArray: nil) //Get results from Query
            if results?.next() == true {
                let cnt = (results?.stringForColumn("cnt"))! // Retrieve number of rows from DB
                noRows = Int(cnt)!
            }
            var i = 0
            sql = "SELECT Address, Street, City, State, Zip from Houses ORDER BY State, City, Street, Address" // Define Query
            houseDB.executeStatements(sql) // Execute Query
            let results2:FMResultSet? = houseDB.executeQuery(sql,withArgumentsInArray: nil) // Get results from Query
            while results2?.next() == true {
                MyHouses.append(newhouse())
                MyHouses[i].address = (results2?.stringForColumn("Address"))!
                MyHouses[i].street = (results2?.stringForColumn("Street"))!
                MyHouses[i].city = (results2?.stringForColumn("City"))!
                MyHouses[i].state = (results2?.stringForColumn("State"))!
                MyHouses[i].zip = (results2?.stringForColumn("Zip"))!
                print("Address -> \(i) \(MyHouses[i].getAddressDetails())")
                i++
            }
        }
        houseDB.close()
    }
}

Comment: Show us some relevant code, especially the `cellForRowAtIndexPath`, `numberOfRowsInSection` and `numberOfSectionsInTableview` methods
Answer: Based on your other post, what you need is an updated House model and updated data structure for handling data for your table view.

House - Model class

struct House {
    var address: String
    var street: String
    var city: String
    var state: String
    var zip: String

    func getAddressDetails() -> String {
        return "\(address) \(street) \(city) \(state) \(zip)"
    }

    func getCityState() -> String {
        return "\(city) - \(state)"
    }
}

Helper Class for loading data

class HouseDataHelper {

    private static let _sharedInstance = HouseDataHelper()
    var myHouses: Dictionary<String, [House]> = [:]

    private init() {
        loadHouseData()
    }

    static func sharedInstance() -> HouseDataHelper {
        return _sharedInstance
    }

    private func loadHouseData() {
        var houses = [House]()

        //Populating your actual values here. GetListOfHousesFromDB()

        //Loading dummy data for testing
        var sectionHeader = ""
        for i in 0...4 {
            sectionHeader = "Header \(i)"
            houses += [House(address: "Address1", street: "Street1", city: "City1", state: "State1", zip: "Zip1")]
            houses += [House(address: "Address2", street: "Street2", city: "City2", state: "State2", zip: "Zip2")]
            houses += [House(address: "Address3", street: "Street3", city: "City3", state: "State3", zip: "Zip3")]
            houses += [House(address: "Address4", street: "Street4", city: "City4", state: "State4", zip: "Zip4")]
            houses += [House(address: "Address5", street: "Street5", city: "City5", state: "State5", zip: "Zip5")]

            myHouses.updateValue(houses, forKey: sectionHeader)
            houses = []
        }
    }
}

Table View Controller

class TableViewController: UITableViewController {

    var houses = HouseDataHelper.sharedInstance().myHouses
    var sectionHeaders: [String] = []

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        sectionHeaders = Array(houses.keys.sort())
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return houses.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let rows = houses[sectionHeaders[section]] {
            return rows.count
        }

        return 0
    }

    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sectionHeaders[section]
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //Populate cells based on "houses"
    }
}

Comment: I'll give this a try, a few things I don't really understand but we'll see if I can get it working..... thanks!
Comment: I ended up using a multi-dimensional array[section][row]. It's working.


How should one describe that some solfege notes rhyme?

Question: How should one describe that some solfege notes rhyme or perhaps share the same role?
The two examples are do and so (I and V) and mi and ti (III and VII).
C      D      E      F      G     A     B
(I)   (II)  (III)  (IV)    (V)   (VI) (VII)
do     re     mi     fa     so    la    ti
The V is harmonically closest to the I, so it almost sounds like a resolving scale if one plays C,D,E,F,G.
Is the role of III and VII similar in that they are harmonically the 'middle' notes between I and V and V and the following I?
In other words the III and VII are harmonically the next closest notes to the I (or the V).
Is this why they have rhyming names? Is it to demonstrate the (diatonic) hierarchy of harmony?
Answer: The six solfège syllables ut, re, mi, fa, sol, and la were, as mentioned in another answer, derived from a hymn, so there's no reason to seek functional similarity between, for example, fa and la.
Ut was later replaced in Italy with do.  (France still uses ut.)  The guy who came up with this, a fellow by the name of Doni, had a bit of a hard sell, and one of the points he made was that it is the first syllable of dominus, meaning lord, but I don't see any suggestion that it was thought to be advantageous to have the same syllable there as in sol, though there is certainly such a case to be made.  (It doesn't seem that he stressed the fact that it is also the first syllable of his own name, though he does seem to have had this in mind.)
At some point in the eighteenth century, si was added, which became ti in the English-speaking world.  In this case, I think it is pretty clear that the vowel i was chosen because of the very similar melodic function of the third and seventh degrees of the major scale.
While it is true, as noted in another answer, that fa and ti are both found in the very important dominant seventh chord, there are other contexts in which ti is more closely related to mi than to fa.  In particular, mi and ti have similar melodic function because they are both a half step below their upper neighbor in the scale.  Before the invention of si, an ascending C-major scale would have been solmized ut, re, mi, fa, sol, la, mi, fa.
Indeed, it is this property of the i vowel representing a melodic (upward) leading tone that led to the choice of the same vowel to indicate upward chromatic alteration in di, ri, fi, etc.
Answer: Not directly. Solfege syllables can be modified (in moveable Do) to show chromaticism. For instances, if you raise "fa" by a half-step, it becomes "fi," or if you lower "mi" it becomes "me" (sounds like "may"). Syllables with a [ay] ending are flattened to [ah], so "re" becomes "ra."
The system works because it shows that you can't raise "mi" or "ti" because those notes are already "fa" and "do" respectively.
So there are definitely some parallels between a scale degree's function and it's solfege syllable, but I wouldn't say that the function determines why certain syllables are used.
Answer: The names of the solfege notes are derived from the hymn "Ut Queant Laxis", and as such, have no connection with their function whatsoever.  The first name, "ut", has now been replaced by "do", and "ti" didn't exist in the original hexachord (six tone) solfege system.
https://upload.wikimedia.org/wikipedia/commons/9/92/Ut_Queant_Laxis_MT.png
Comment: Because stack exchange has a significant anal population which demands clarify to the Nth degree for questions and statements they would completely understand in person. I upvote every reply ... people take valuable time on my behalf, why not be grateful.
Comment: Why did I get a downvote?  It would be helpful to know, thanks.
Answer: In terms of function FA and TI are harmonically related rather than MI and TI.
Harmonically FA and TI are respectively the chord tones 7th and 3rd of the V7 dominant seventh chord.
And functionally, as tendency tones, FA and TI resolve by half-step (in major keys) FA going down to MI and TI going up to DO giving us the progression V7 to I.
There is an interesting review of the FA-MI & TI-DO relationships in Music in the Galant Style, by Gjerdingen. While it isn't about the actual solfege syllable names (which is sort of the heart of your question) it does give a nice historic review of solfege concepts and the tonal role of the scale tones.
It is kind of interesting that DO and SOL and FA and LA verbally rhyme and are paired together in the I and IV chords, but that is just a coincidence.
Comment: @RandyZeitman, make sure you read Scott Wallace's answer and the Gjerdingen chapter. The original DO was UT... that doesn't rhyme with SOL. Also, there is the "fixed do" system, and other systems from the past, that didn't use the same syllables of today's familiar fixed DO. The point is don't attribute too much to the syllables themselves as they have changed a lot over time.
Comment: While it seems hard to believe it's a coincidence I have no evidence otherwise and Scott Wallace's reference offers an explanation for the names.

And I see I'm incorrect that III and VII are the next harmonically removed from I and V ... the next removed pair is II and VI... 1-5-2-6-3-7-4 being the note positions of the circle of harmony.
Comment: I agree that "fa" and "ti" are more closely related than "mi" and "ti." There is also a close relationship between "fi" and "ti" - in that "fi" often acts as a leading tone to "sol," but again, I think that's mostly a coincidence.
Comment: "The point is don't attribute too much to the syllables themselves as they have changed a lot over time." (Yes, perhaps because they didn't demonstrate harmony.)