sublist

Haskell: test if list contains specific “sublist”

别等时光非礼了梦想. 提交于 2019-12-01 03:13:33
Is there a trick or a prelude function to test if a list contains a specific substring/sublist? xyz :: [a] -> [a] -> Bool xyz "hello world" "worl" -> True xyz [1,2,3,4,5,6,7,8,1,2,3,4,5] [7,8,1] -> True I tried to write one on my own, but this is a trivial problem and I don't want to reinvent the wheel. Use isInfixOf from Data.List. The parameters are the other way round from what you asked for --- it's usually most readable to use the function like this: "worl" `isInfixOf` "hello world" (returns True). 来源: https://stackoverflow.com/questions/8112838/haskell-test-if-list-contains-specific

.NET equivalent of Java's List.subList()?

柔情痞子 提交于 2019-12-01 02:41:40
Is there a .NET equivalent of Java's List.subList() that works on IList<T> ? using LINQ list.Skip(fromRange).Take(toRange - fromRange) For the generic List<T> , it is the GetRange(int, int) method. Edit: note that this is a shallow copy, not a 'view' on the original. I don't think C# offers that exact functionality. Edit2: as Kamarey points out, you can have a read-only view: List<int> integers = new List<int>() { 5, 6, 7, 8, 9, 10, 11, 12 }; IEnumerable<int> view = integers.Skip(2).Take(3); integers[3] = 42; foreach (int i in view ) // output The above will print 7, 42, 9. dfa GetRange is

Haskell: test if list contains specific “sublist”

走远了吗. 提交于 2019-11-30 22:38:35
问题 Is there a trick or a prelude function to test if a list contains a specific substring/sublist? xyz :: [a] -> [a] -> Bool xyz "hello world" "worl" -> True xyz [1,2,3,4,5,6,7,8,1,2,3,4,5] [7,8,1] -> True I tried to write one on my own, but this is a trivial problem and I don't want to reinvent the wheel. 回答1: Use isInfixOf from Data.List. The parameters are the other way round from what you asked for --- it's usually most readable to use the function like this: "worl" `isInfixOf` "hello world"

Combining time-series objects and lists: Package “termstrc”

吃可爱长大的小学妹 提交于 2019-11-30 15:33:06
The R package "termstrc", designed for term-structure estimation, is an incredibly useful tool, but it requires data to be set in a particularly awkward format: lists within lists. Question : What is the best way to prepare and shape data, either outside R or inside R, in order to create the repeated sublist format required to run the function "dyncouponbonds"? The "dyncouponbonds" command requires data to be set in a repeated sublist, whereby a list of bonds and time-invariant features of those bonds (let's call this "bondlist"), is appended with some time t features of those bonds (price and

Determine if all elements in a list are present and in the same order in another list

浪子不回头ぞ 提交于 2019-11-30 03:57:03
问题 How do I create a function sublist() that takes two lists, list1 and list2 , and returns True if list1 is a sublist of list2 , and False otherwise. list1 is a sublist of list2 if the numbers in list1 appear in list2 in the same order as they appear in list1 , but not necessarily consecutively. For example, >>> sublist([1, 12, 3],[25, 1, 30, 12, 3, 40]) True >>> sublist([5, 90, 2],[90, 20, 5, 2, 17]) False 回答1: Here's one way to do it in linear time (and constant space) with an iterator: def

Combining time-series objects and lists: Package “termstrc”

ぃ、小莉子 提交于 2019-11-29 23:00:57
问题 The R package "termstrc", designed for term-structure estimation, is an incredibly useful tool, but it requires data to be set in a particularly awkward format: lists within lists. Question : What is the best way to prepare and shape data, either outside R or inside R, in order to create the repeated sublist format required to run the function "dyncouponbonds"? The "dyncouponbonds" command requires data to be set in a repeated sublist, whereby a list of bonds and time-invariant features of

Checking for sublist in list

和自甴很熟 提交于 2019-11-29 12:02:49
The Question is: You are to write a function, called isSublist() , which takes two arguments ( list, sublist ) and returns 1 if sublist is a sub-list of list, and 0 otherwise. So i have my code however I get True when the sublist is not in list. Any suggestions on fixing this please? def isSublist(list, sublist): for i in range(len(list)-(len(sublist))+1): return True if sublist==list[i:i+(len(sublist))]: return False sample input: list= (0,1,2,3,4,5,6,7,8,9) isSublist(list, [1,2,3]) output: True You can break this up by getting all the slices the size of the sublist, and comparing equality:

Split a list into two sublists in all possible ways

那年仲夏 提交于 2019-11-28 21:32:16
I have a list of variable size, for example [1, 2, 3, 4] and I want to get every possible way to split this list into two: ([], [1, 2, 3, 4]) ([1], [2, 3, 4]) ([2], [1, 3, 4]) ([3], [1, 2, 4]) ([4], [1, 2, 3]) ([1, 2], [3, 4]) ([1, 3], [2, 4]) ([1, 4], [2, 3]) ([2, 3], [1, 4]) ([2, 4], [1, 3]) ([3, 4], [1, 2]) ([1, 2, 3], [4]) ([1, 2, 4], [3]) ([1, 3, 4], [2]) ([2, 3, 4], [1]) ([1, 2, 3, 4], []) I'm pretty sure this is not an unknown problem and there is probably an algorithm for this, however I could not find one. Also, this should not use any external libraries but work with simple language

Why changes in sublist are reflected in the original list?

不问归期 提交于 2019-11-28 13:46:08
I know that Collections in Java are mutable when you pass them through references. I want to know exactly what happens in memory addresses of original-list and sublist/s of it. Do the sublist and original list refer to the same object? Following is code sample reflecting changes made in sublist to main original list. List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add(1, "3"); List<String> list2 = new LinkedList<String>(list); list.addAll(list2); list2 = list.subList(2, 5); list2.clear(); //Changes are made to list System.out.println(list); As per the JavaDoc on

Nested ordered lists

。_饼干妹妹 提交于 2019-11-28 00:22:13
I need a nested list with subitem numbering, like this: 1. Item 1 1.1 - Subitem 1 1.2 - Subitem 2 1.3 - Subitem 3 1.4 - Subitem 4 1.5 - Subitem 5 2. Item 2 2.1 - Subitem 1 2.2 - Subitem 2 2.3 - Subitem 3 2.4 - Subitem 4 2.5 - Subitem 5 Well, I know I cannot achieve that with pure HTML. It would be great to use something like this and have the sublist automatically numbered: <ol> <li> Item 1 <ol> <li>Subitem 1</li> <li>Subitem 2</li> <li>Subitem 3</li> <li>Subitem 4</li> <li>Subitem 5</li> </ol> </li> <li> Item 2 <ol> <li>Subitem 1</li> <li>Subitem 2</li> <li>Subitem 3</li> <li>Subitem 4</li>