subset

Subsequence sum and GCD

丶灬走出姿态 提交于 2019-12-04 14:34:56
问题 I came across this question on a programming challenge about a month ago, but the editorial wasn't released so I am asking it here. There is an Array A of size N. Find the sum * GCD of K length subsequences of A. Example: If A = [1, 2, 3] and K = 2, {1, 2} = 3(sum) * 1(GCD) = 3 {1, 3} = 4(sum) * 1(GCD) = 4 {2, 3} = 5(sum) * 1(GCD) = 5 Ans => 3 + 4 + 5 = 12 回答1: Here it is from scratch (didn't test it thoroughly though): Let C[k, i, d] be the number of all k -length subsequences of A[1..i]

R xts object subseting xts object with multiple days of intraday data for certain hours

纵然是瞬间 提交于 2019-12-04 14:20:24
Is there a way in xts object to do the same as below but for xts object with multiple days of intraday data? The below works like a clock but for one day of data. If I pass xts from 22nd to 26th it does not. It seems like subseting intraday data in xts across multiple days is not possible to be done in one go but rather need to first split data per each day and then use this xts functionality. Is this correct? indexTZ(tdata) = "GMT" plotdata= tdata['20110822 10:00:00/20110822 12:00:00'] > plotdata= tdata['10:00:00/12:00:00'] works like swiss clock > tail(plotdata) SYMBOL EX PRICE SIZE COND

算法刷题分类

佐手、 提交于 2019-12-04 13:39:00
算法分类 分类及每个类型的经典题目: Pattern: Sliding window,滑动窗口类型 经典题目: Maximum Sum Subarray of Size K (easy) Smallest Subarray with a given sum (easy) Longest Substring with K Distinct Characters (medium) Fruits into Baskets (medium) No-repeat Substring (hard) Longest Substring with Same Letters after Replacement (hard) Longest Subarray with Ones after Replacement (hard) Pattern: two points, 双指针类型 经典题目: Pair with Target Sum (easy) Remove Duplicates (easy) Squaring a Sorted Array (easy) Triplet Sum to Zero (medium) Triplet Sum Close to Target (medium) Triplets with Smaller Sum (medium) Subarrays with Product

Given an array, how to generate all combinations of subset size k?

Deadly 提交于 2019-12-04 13:27:45
So given input = [1, 2, 3] and k=2 this would return: 1 2 1 3 2 1 2 3 3 1 3 2 This is the closest to what I am looking for, but not quite: http://algorithms.tutorialhorizon.com/print-all-combinations-of-subset-of-size-k-from-given-array/ function subsetsOfSize(a, used, startIndex, currentSize, k) { if (currentSize === k) { for (var i = 0; i < a.length; i++) { if (used[i]) console.log(a[i]); } console.log('-'); return; } if (startIndex === a.length) return; used[startIndex] = true; subsetsOfSize(a, used, startIndex+1, currentSize+1, k); used[startIndex] = false; subsetsOfSize(a, used,

R arules - subset of transactions that match a rule

谁说我不能喝 提交于 2019-12-04 12:51:11
I'm using the R package arules . I have some transactions and a rule (see below). I want the subset of transactions that break the rule. How can I do that? This is the set up: library(arules) data(Adult) summary(Adult) rules = apriori(Adult,parameter=list(support=0.2,confidence=0.8)) summary(rules) r=rules[1] I want the subset of transactions that contain the left hand side of the rule r but not the right hand side. The arules documentation doesn't have an example like this. I've tried %in% , match and subset but I can't get the syntax right. The documentation for the subset function has an

how find all groups of subsets of set A? Set partitions in Python

只愿长相守 提交于 2019-12-04 12:27:45
I want to find an algorithm that given a set A to find all groups of subsets that satisfy the following condition: x ∪ y ∪ .... z = A, where x, y, ... z ∈ Group and ∀ x,y ∈ Group: x ⊆ A, y ⊆ A, x ∩ y = ∅ = {} and ∀ x ∈ Group: x != ∅ Note: I hope to define it well, I'm not good with math symbols I made the following approach to search groups of two subsets only: from itertools import product, combinations def my_combos(A): subsets = [] for i in xrange(1, len(A)): subsets.append(list(combinations(A,i))) combos = [] for i in xrange(1, 1+len(subsets)/2): combos.extend(list(product(subsets[i-1],

Subsref with cells

折月煮酒 提交于 2019-12-04 10:37:55
This issue appeared when I was answering this question . It should be some stupid error I am doing, but I can't get what error it is… myMatrix = [22 33; 44 55] Returns: >> subsref(myMatrix, struct('type','()','subs',{{[1 2]}} ) ); ans = 22 44 While using it with cells: myCell = {2 3; 4 5} Returns: >> subsref(myCell,struct('type','{}','subs',{{[1 2]}} ) ); ans = 2 % WHATTT?? Shouldn't this be 2 and 4 Matlab?? Checking the subsref documentation , we see: See how MATLAB calls subsref for the expression: A{1:2} The syntax A{1:2} calls B = subsref(A,S) where S.type='{}' and S.subs={[1 2]}. This

Subsetting data in Python

旧时模样 提交于 2019-12-04 09:02:46
问题 I want to use the equivalent of the subset command in R for some Python code I am writing. Here is my data: col1 col2 col3 col4 col5 100002 2006 1.1 0.01 6352 100002 2006 1.2 0.84 304518 100002 2006 2 1.52 148219 100002 2007 1.1 0.01 6292 10002 2006 1.1 0.01 5968 10002 2006 1.2 0.25 104318 10002 2007 1.1 0.01 6800 10002 2007 4 2.03 25446 10002 2008 1.1 0.01 6408 I want to subset the data based on contents of col1 and col2 . (The unique values in col1 are 100002 and 10002, and in col2 are 2006

R Subset data.frame from max value of one vector and grouped by another [duplicate]

≯℡__Kan透↙ 提交于 2019-12-04 08:42:09
This question already has answers here : How to select the row with the maximum value in each group (10 answers) Closed 2 years ago . >ID<-c('A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C') >WK<-c(1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5) >NumSuccess<-c(0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 3) >Data<-data.frame(ID, WK, NumSuccess) I am trying to create a subset data.frame "Data2" based on the value in "NumSuccesses" that corresponds to the Max Value in "WK" grouped by "ID". Resulting data.frame should look like this: >ID<-c('A','B','C') >WK<-c(3, 3, 5) >NumSuccess<-c(2, 1, 3) >Data2<-data.frame(ID, WK,

Subsetting in R using OR condition with strings

巧了我就是萌 提交于 2019-12-04 08:33:20
问题 I have a data frame with about 40 columns, the second column, data[2] contains the name of the company that the rest of the row data describes. However, the names of the companies are different depending on the year (trailing 09 for 2009 data, nothing for 2010). I would like to be able to subset the data such that I can pull in both years at once. Here is an example of what I'm trying to do... subset(data, data[2] == "Company Name 09" | "Company Name", drop = T) Essentially, I'm having