Find minimal weight of a tree

I am trying to find an algorithm, which can find the minimal total weight of a given tree. I am given a tree and a weight of all nodes (each node can have a different weight). For example in this graph, where each node has weight 1: tree with weights Then I am given a…

Discover more

Intersection of 2 polyhedra using intersectn() R

I have 2 polyhedra generated by the following extreme points: df1 = structure(c(3, 5, 8, 6), .Dim = c(2L, 2L)) df2 = structure(c(2, 4, 9, 7), .Dim = c(2L, 2L)) Those points, taken as constraint inequalities, create a space. I would like to select the hull formed by the intersection of those 2 polyhedra. The…

Discover more

MS SQL: is it possible to solve this task in SQL without T/PL sql?

I was trying a task “calculate minimum number of rooms required to host all the meetings”. Result should be “4”. Table meetings is like this: . I was trying with inner join on m2.end > m1.start and m2.start < m1.end and m1.id<>m2.id to get collisions of meetings, in other case I tried with oposite conditions…

Discover more

Fill missing with 0 if all values are 0 with R

I’m trying to fill missing value with R. If all other value is 0, then I want to fill missing with 0. An example is shown below. In this data, All value in c column except NA is 0. So, I want to fill Na with 0. set.seed(1000) a<-rnorm(10) b<-rnorm(10) c<-rep(0,10) c[c(2,4,8)]<-NA test<-cbind(a,b,c) a b…

Discover more

How to remove all combination with the same element in subarray from array?

I have this array [[1,2,3],[4,1,6],[1,3,2],[1,2,4],[3,1,2],[4,6,1],[9,9,9]] and I want a function that do this: [[1,2,3],[4,1,6],[1,2,4],[9,9,9]]. This function removes all subarray with the same value. I thought about a filter like this .filter(el => el.filter(value => array2.includes(value))) But I don’t think it will work. PS: I use node.js EDIT: Weirdly, many of your answer work PERFECTLY with…

Discover more

Applying a function to each element of a group in R data frame

I am trying to find the first non-NA element of column w in each group and then construct a new variable which starts from the index of that non-NA element and follows this law of motion: k_{it+1}=k_{it}+s_{it+1}-s{it}. i denotes the group and t is time. k_{i1} comes from the first non-NA element of column w.…

Discover more

Pandas apply() with multiple arguments in multiprocessing

I have a big data set made of a million records (which is represented in the following snippet as big_df with just 5 rows) and I would like to use multiprocessing when invoking apply function which is calling classify function. The following is a working version simply invoking the function classify using apply: import pandas…

Discover more

Codility – CountNonDivisible (Python, better performance)

The exercise in question is: You are given an array A consisting of N integers. For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors. For example, consider integer…

Discover more

Find words that startswith() and endswith() in a DataFrame column

Having a dataFrame column hosting a compound name (school name), I am interested to go over each word of the school name and find its position with respect all the dataFrame column. I used the following approach: Create another column which is a column and iterate for each word using startswith(word) and endswith(word) for each…

Discover more

SQL sort order in Japanese breaks when text includes non-Japanese characters

It seems that Japanese sorting “breaks” when the text contains non-japanese text, even when forcing any possible collation after the sort part of the query. I would like to know if this is a known phenomenon, and what a solution could be. In the end I’m look for a kana type insensitive, case sensitive sorting,…

Discover more