Posts

Showing posts from August 15, 2018

Find all integral coordinates given top left corner and bottom right corner of a rectangle

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 6 down vote favorite Here is the problem I am working on - There is an $N times N$ field on which missiles are being bombarded. Initially, all the cells in this field have the value $0$. There will be $M$ missiles bombarded on this field. The $i$th missile will have power $P_i$ and it will affect all the cells in region with $(A_i,B_i)$ as top-left corner and $(C_i,D_i)$ as bottom-right corner. Because of missile, value of all the cells in this rectangle will get XOR ed with $P_i$. After all the missiles have been bombarded, you have to find out values in each cell of this field. Here is what I have tried - N = 3 M = 3 missiles = [[3,3,1,3,2], [2,2,1,2,2], [3,1,1,2,3]] from itertools import product # Logic coords = for i in missiles: Pi, Ai, Bi, Ci, Di = i for j in product(*[range(Ai-1, Ci), range(Bi-1, Di)]): if coords.get(j): co

Using await to break long-running processes

Image
Clash Royale CLAN TAG #URR8PPP .everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0; up vote 2 down vote favorite I have a Node.js app / Web API that runs on an Azure app service with a single CPU. One of the functions needs to run for a long time, perhaps tens of seconds, while the server should continue to process other requests, i.e. the function should not be blocking. My idea is to use await new Promise(resolve => setTimeout(resolve, 0)); at the end of each loop cycle to requeue the microtask at the end of the queue, giving other users a chance to receive the response too. The code is processing data using the node-tfidf package like this: const Tfidf = require('node-tfidf'); const tfidf = new Tfidf(); for (let document of documents) tfidf.addDocument(document); // Break the blocking code: await new Promise(resolve => setTimeout(resolve, 0)); for (let keyword of keywords) tfidf.tfidfs(keyword, function(i, meas