Sunday, November 20, 2016

Today we continue discussing websockets. We said it facilitates duplex communication and is independent of http and we showed how both the client and the server can be both producer  and consumer.
 The frontend updates the page with the statistics on each event that an event listener on the page receives. The frontend is different from the clients that gather the statistics per machine usually with a collection tool. Each client is responsible for its own host.l
The client is in an endless loop, gathering data, publishing data and waiting for some time. The publishing is done with a post request to the server at the endpoint corresponding to the model used by the server for the client.
The server is written in django. It uses signals for creating and destroying model objects pertaining to the client statistics.  Whenever a stats needs to be saved, using signals, the server then notifies the endpoint set up by crossbar.io which dispatches a  a pubsub event by Crossbar.io with a POST request to the endpoint at path say '/notify'.
 The HTTP requests and the publisher-subscriber events on the websockets are not mutually exclusive. They are just complimentary to each other. We would have a restrictive polling mechanism if there was no pub-sub associated with the architecture.
To summarize:
Frontend:
register an event listener
Client:
publish to server
Server:
notify the subscriber
#codingexercise
We were referring to an example to find the next palindrome of a number with very large count of digits in the exercises earlier. In order to find the palindrome that is next, we need to be able to compares such numbers. Here is one way to do it which relies on the notion of a .Net comparator.
        static int compareTo(List<int> current, List<int> digits)
        {
            int ret = 0;
            if (current.Count < digits.Count) return -1;
            if (current.Count > digits.Count) return 1;
            for (int i = 0; i < current.Count && i < digits.Count; i++)
            {
                if (current[i] < digits[i])
                    return -1;
                if (current[i] > digits[i])
                    return 1;
            }
            return ret;
        }

Given a string, its value is defined as the sum of the squares of the frequencies of its letters. Find the minimum value after removing k characters

static int GetValue(string input, int k)
{
var freq = new Hashtable();
for (int I =0; I < input.Length; i++)
freq[I]++;
for (int j = 0; j < k; j++)
{
 char candidate = freq.Max();// key associated with the max value
 if (candidate)
{
    freq[candidate]--;
    if (freq[candidate] == 0)
         freq.Remove(candidate);
}
}
int val = 0;
for (int I = 0; I < freq.keys.count; i++)
{
val += freq[freq.keys[I]] ^ 2;
}
return val;
}

No comments:

Post a Comment