run-length-encoding

Find longest run of consecutive zeros for each user in dataframe

╄→尐↘猪︶ㄣ 提交于 2019-12-24 01:02:00
问题 I'm looking to find the max run of consecutive zeros in a DataFrame with the result grouped by user. I'm interested in running the RLE on usage. sample input: user--day--usage A-----1------0 A-----2------0 A-----3------1 B-----1------0 B-----2------1 B-----3------0 Desired output user---longest_run a - - - - 2 b - - - - 1 mydata <- mydata[order(mydata$user, mydata$day),] user <- unique(mydata$user) d2 <- data.frame(matrix(NA, ncol = 2, nrow = length(user))) names(d2) <- c("user", "longest_no

Task from the interview. How we would solve it?

谁说我不能喝 提交于 2019-12-24 00:22:39
问题 Convert String in this way let initialString = "atttbcdddd" // result must be like this "at3bcd4" But repetition must be more than 2. For example, if we have "aa" the result will be "aa", but if we have "aaa", the result will be "a3" One more example: let str = "aahhhgggg" //result "aah3g4" My try: func encrypt(_ str: String) -> String { let char = str.components(separatedBy: "t") //must input the character var count = char.count - 1 var string = "" string.append("t\(count)") return string }

Find number of consecutive ones in binary array

独自空忆成欢 提交于 2019-12-23 03:12:44
问题 I want to find the lengths of all series of ones and zeros in a logical array in MATLAB. This is what I did: A = logical([0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1]); %// Find series of ones: csA = cumsum(A); csOnes = csA(diff([A 0]) == -1); seriesOnes = [csOnes(1) diff(csOnes)]; %// Find series of zeros (same way, using ~A) csNegA = sumsum(~A); csZeros = csNegA(diff([~A 0]) == -1); seriesZeros = [csZeros(1) diff(csZeros)]; This works, and gives seriesOnes = [4 2 5] and seriesZeros = [3 1 6]

In Place Run Length Encoding Algorithm

本秂侑毒 提交于 2019-12-22 12:40:49
问题 I encountered an interview question: Given a input String: aaaaabcddddee , convert it to a5b1c1d4e2 . One extra constraint is, this needs to be done in-place , means no extra space(array) should be used. It is guaranteed that the encoded string will always fit in the original string. In other words, string like abcde will not occur, since it will be encoded to a1b1c1d1e1 which occupies more space than the original string. One hint interviewer gave me was to traverse the string once and find

count successive occurrences of number in Prolog

て烟熏妆下的殇ゞ 提交于 2019-12-20 03:43:26
问题 Hello I am trying to make a program in Prolog that given a list it counts the occurrences of each successive element in the list as follows: count(1,[1,1,1,2,2,2,3,1,1],0,X) the result would be X=[ [1,3],[2,3],[3,1][1,2] ] aka each sublist is [element,occurrences] In my case i believe there is something wrong with the base case but I cannot solve it. Can you help me? %append an element to a list append([ ],Y,Y). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs). %c is the counter beginning with 0

count successive occurrences of number in Prolog

烂漫一生 提交于 2019-12-20 03:43:09
问题 Hello I am trying to make a program in Prolog that given a list it counts the occurrences of each successive element in the list as follows: count(1,[1,1,1,2,2,2,3,1,1],0,X) the result would be X=[ [1,3],[2,3],[3,1][1,2] ] aka each sublist is [element,occurrences] In my case i believe there is something wrong with the base case but I cannot solve it. Can you help me? %append an element to a list append([ ],Y,Y). append([X|Xs],Ys,[X|Zs]):-append(Xs,Ys,Zs). %c is the counter beginning with 0

Run Length Encoding in Matlab

吃可爱长大的小学妹 提交于 2019-12-17 05:15:39
问题 I'm very new with MatLab, I have Run Length Encoding code but it seems to not work, can you help me? I have this input : ChainCode = 11012321170701000700000700766666666666665555555544443344444333221322222322 and I want make it into RLE output : (1,2), (0,1), (1,1), (2,1), (3,1), (2,1), (1,2), (7,1), (0,1), (7,1), (0,1), (1,1), (0,3), (7,1), (0,5), (7,1), (0,2), (7,1), (6,13), (5,8), (4,4), (3,2), (4,5), (3,3), (2,2), (1,1), (3,1), (2,5), (3,1), (2,2) This is my code : lengthcode = 1; N = 1;

Element-wise array replication according to a count [duplicate]

和自甴很熟 提交于 2019-12-17 04:35:42
问题 This question already has answers here : Repeat copies of array elements: Run-length decoding in MATLAB (5 answers) Closed 5 years ago . My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size. An example of this, say I had an array v = [3 1 9 4] , I want to use rep = [2 3 1 5] to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4] . So far I'm using a

Element-wise array replication according to a count [duplicate]

南楼画角 提交于 2019-12-17 04:35:11
问题 This question already has answers here : Repeat copies of array elements: Run-length decoding in MATLAB (5 answers) Closed 5 years ago . My question is similar to this one, but I would like to replicate each element according to a count specified in a second array of the same size. An example of this, say I had an array v = [3 1 9 4] , I want to use rep = [2 3 1 5] to replicate the first element 2 times, the second three times, and so on to get [3 3 1 1 1 9 4 4 4 4 4] . So far I'm using a

OCR & OpenCV: Difference between two frames on high resolution images

微笑、不失礼 提交于 2019-12-13 18:46:02
问题 According to this post OCR: Difference between two frames, I now know how to find pixel differences between two images with OpenCV. I would like to improve this solution and use it with high resolution images (from a video) with rich content. The example above is not applicable with big images because the process is to slow (too much differences found, the "findCountours method" fills the tab with 250k elements which takes a huge time to process). My application uses a RLE decoder to decode