data-partitioning

python: Generating integer partitions

好久不见. 提交于 2019-12-18 12:29:45
问题 I need to generate all the partitions of a given integer. I found this algorithm by Jerome Kelleher for which it is stated to be the most efficient one: def accelAsc(n): a = [0 for i in range(n + 1)] k = 1 a[0] = 0 y = n - 1 while k != 0: x = a[k - 1] + 1 k -= 1 while 2*x <= y: a[k] = x y -= x k += 1 l = k + 1 while x <= y: a[k] = x a[l] = y yield a[:k + 2] x += 1 y -= 1 a[k] = x + y y = x + y - 1 yield a[:k + 1] reference: http://homepages.ed.ac.uk/jkellehe/partitions.php By the way, it is

QuickSort and Hoare Partition

梦想与她 提交于 2019-12-17 09:43:31
问题 I have a hard time translating QuickSort with Hoare partitioning into C code, and can't find out why. The code I'm using is shown below: void QuickSort(int a[],int start,int end) { int q=HoarePartition(a,start,end); if (end<=start) return; QuickSort(a,q+1,end); QuickSort(a,start,q); } int HoarePartition (int a[],int p, int r) { int x=a[p],i=p-1,j=r; while (1) { do j--; while (a[j] > x); do i++; while (a[i] < x); if (i < j) swap(&a[i],&a[j]); else return j; } } Also, I don't really get why

python equivalent of filter() getting two output lists (i.e. partition of a list)

南笙酒味 提交于 2019-12-17 04:02:09
问题 Let's say I have a list, and a filtering function. Using something like >>> filter(lambda x: x > 10, [1,4,12,7,42]) [12, 42] I can get the elements matching the criterion. Is there a function I could use that would output two lists, one of elements matching, one of the remaining elements? I could call the filter() function twice, but that's kinda ugly :) Edit: the order of elements should be conserved, and I may have identical elements multiple times. 回答1: Try this: def partition(pred,

python equivalent of filter() getting two output lists (i.e. partition of a list)

纵然是瞬间 提交于 2019-12-17 04:02:02
问题 Let's say I have a list, and a filtering function. Using something like >>> filter(lambda x: x > 10, [1,4,12,7,42]) [12, 42] I can get the elements matching the criterion. Is there a function I could use that would output two lists, one of elements matching, one of the remaining elements? I could call the filter() function twice, but that's kinda ugly :) Edit: the order of elements should be conserved, and I may have identical elements multiple times. 回答1: Try this: def partition(pred,

How to partition a vector into groups of regular, consecutive sequences?

帅比萌擦擦* 提交于 2019-12-17 02:49:28
问题 I have a vector, such as c(1, 3, 4, 5, 9, 10, 17, 29, 30) and I would like to group together the 'neighboring' elements that form a regular, consecutive sequence in a ragged vector resulting in: L1: 1 L2: 3,4,5 L3: 9,10 L4: 17 L5: 29,30 Naive code (of an ex-C programmer): partition.neighbors <- function(v) { result <<- list() #jagged array currentList <<- v[1] #current series for(i in 2:length(v)) { if(v[i] - v [i-1] == 1) { currentList <<- c(currentList, v[i]) } else { result <<- c(result,

jq: groupby and nested json arrays

旧街凉风 提交于 2019-12-13 08:29:30
问题 Let's say I have: [[1,2], [3,9], [4,2], [], []] I would like to know the scripts to get: The number of nested lists which are/are not non-empty. ie want to get: [3,2] The number of nested lists which contain or not contain number 3. ie want to get: [1,4] The number of nested lists for which the sum of the elements is/isn't less than 4 . ie want to get: [3,2] ie basic examples of nested data partition. 回答1: Since stackoverflow.com is not a coding service, I'll confine this response to the

Slice a PowerShell array into groups of smaller arrays

点点圈 提交于 2019-12-12 08:56:43
问题 I would like to convert a single array into a group of smaller arrays, based on a variable. So, 0,1,2,3,4,5,6,7,8,9 would become 0,1,2 , 3,4,5 , 6,7,8 , 9 when the size is 3. My current approach: $ids=@(0,1,2,3,4,5,6,7,8,9) $size=3 0..[math]::Round($ids.count/$size) | % { # slice first elements $x = $ids[0..($size-1)] # redefine array w/ remaining values $ids = $ids[$size..$ids.Length] # return elements (as an array, which isn't happening) $x } | % { "IDS: $($_ -Join ",")" } Produces: IDS: 0

Specifiying a selected range of data to be used in leave-one-out (jack-knife) cross-validation for use in the caret::train function

时光毁灭记忆、已成空白 提交于 2019-12-11 15:33:10
问题 This question builds on the question that I asked here: Creating data partitions over a selected range of data to be fed into caret::train function for cross-validation). The data I am working with looks like this: df <- data.frame(Effect = rep(seq(from = 0.05, to = 1, by = 0.05), each = 5), Time = rep(c(1:20,1:20), each = 5), Replicate = c(1:5)) Essentially what I would like to do is create custom partitions, like those generated by the caret::groupKFold function but for these folds to be

How do I generate set partitions of a certain size?

纵然是瞬间 提交于 2019-12-11 05:16:49
问题 I would like to generate partitions for a set in a specific way: I need to filter out all partitions which are not of size N in the process of generating these partitions. The general solution is "Generate all “unique” subsets of a set (not a powerset)". For the set S with the following subsets: [a,b,c] [a,b] [c] [d,e,f] [d,f] [e] and the following 'unique' elements: a, b, c, d, e, f the result of the function/method running with the argument N = 2 should be: [[a,b,c], [d,e,f]] While the

Number of ways to partition a number in Python

末鹿安然 提交于 2019-12-11 02:29:54
问题 I have defined a recursive function that takes a number, n , and returns a list of lists of the numbers that sum to that number (partitions): def P(n): # base case of recursion: zero is the sum of the empty list if n == 0: yield [] return for p in P(n-1): p.append(1) yield p p.pop() if p and (len(p) < 2 or p[-2] > p[-1]): p[-1] += 1 yield p I was wondering how to make the function return the number of partitions for number n . For example, P(6) would return 10 . 回答1: If you look at the