pattern-matching

Unix filename wildcards in Python?

馋奶兔 提交于 2019-12-19 03:08:30
问题 How do Unix filename wildcards work from Python ? A given directory contains only subdirectories, in each of which there is (among others) one file whose name ends with a known string, say _ext . The first part of the filename always varies, so I need to get to the file by using this pattern. I wanted to do this: directory = "." listofSubDirs = [x[0] for x in os.walk(directory)] listofSubDirs = listofSubDirs[1:] #removing "." for subDirectory in listofSubDirs: fileNameToPickle = subDirectory

Case-insensitive Lua pattern-matching

徘徊边缘 提交于 2019-12-18 19:35:02
问题 I'm writing a grep utility in Lua for our mobile devices running Windows CE 6/7, but I've run into some issues implementing case-insensitive match patterns. The obvious solution of converting everything to uppercase (or lower) does not work so simply due to the character classes. The only other thing I can think of is converting the literals in the pattern itself to uppercase. Here's what I have so far: function toUpperPattern(instr) -- Check first character if string.find(instr, "^%l") then

Case-insensitive Lua pattern-matching

此生再无相见时 提交于 2019-12-18 19:34:14
问题 I'm writing a grep utility in Lua for our mobile devices running Windows CE 6/7, but I've run into some issues implementing case-insensitive match patterns. The obvious solution of converting everything to uppercase (or lower) does not work so simply due to the character classes. The only other thing I can think of is converting the literals in the pattern itself to uppercase. Here's what I have so far: function toUpperPattern(instr) -- Check first character if string.find(instr, "^%l") then

f# pattern matching with types

ぃ、小莉子 提交于 2019-12-18 19:08:24
问题 I'm trying to recursively print out all an objects properties and sub-type properties etc. My object model is as follows... type suggestedFooWidget = { value: float ; hasIncreasedSinceLastPeriod: bool ; } type firmIdentifier = { firmId: int ; firmName: string ; } type authorIdentifier = { authorId: int ; authorName: string ; firm: firmIdentifier ; } type denormalizedSuggestedFooWidgets = { id: int ; ticker: string ; direction: string ; author: authorIdentifier ; totalAbsoluteWidget:

Case classes, pattern matching and varargs

老子叫甜甜 提交于 2019-12-18 15:19:22
问题 Let's say I have such class hierarchy: abstract class Expr case class Var(name: String) extends Expr case class ExpList(listExp: List[Expr]) extends Expr Would it be better to define constructor of ExpList like this: case class ExpList(listExp: Expr*) extends Expr I would like to know, what are drawbacks/benefits of each definitions regards pattern matching? 回答1: You can have both constructors: case class ExpList(listExp: List[Expr]) extends Expr object ExpList { def apply(listExp: Expr*) =

Shouldn't “static” patterns always be static?

被刻印的时光 ゝ 提交于 2019-12-18 14:04:18
问题 I just found a bug in some code I didn't write and I'm a bit surprised: Pattern pattern = Pattern.compile("\\d{1,2}.\\d{1,2}.\\d{4}"); Matcher matcher = pattern.matcher(s); Despite the fact that this code fails badly on input data we get (because it tries to find dates in the 17.01.2011 format and gets back things like 10396/2011 and then crashed because it can't parse the date but that really ain't the point of this question ; ) I wonder: isn't one of the point of Pattern.compile to be a

Match a phrase ending in a prefix with full text search

我怕爱的太早我们不能终老 提交于 2019-12-18 13:24:59
问题 I'm looking for a way to emulate something like SELECT * FROM table WHERE attr LIKE '%text%' using a tsvector in PostgreSQL. I've created a tsvector attribute without using a dictionary. Now, a query like ... SELECT title FROM table WHERE title_tsv @@ plainto_tsquery('ph:*'); ... would return all titles like 'Physics', 'PHP', etc. But how can I create a query that returns all records where the title start with 'Zend Fram' (which should return for instance 'Zend Framework')? Of course, I could

Using variable as case pattern in Bash

江枫思渺然 提交于 2019-12-18 12:51:20
问题 I'm trying to write a Bash script that uses a variable as a pattern in a case statement. However I just cannot get it to work. Case statement: case "$1" in $test) echo "matched" ;; *) echo "didn't match" ;; esac I've tried this with assigning $test as aaa|bbb|ccc , (aaa|bbb|ccc) , [aaa,bbb,ccc] and several other combinations. I also tried these as the pattern in the case statement: @($test) , @($(echo $test)) , $($test) . Also no success. EDIT For clarity, I would like the variable to

Find the smallest period of input string in O(n)?

大兔子大兔子 提交于 2019-12-18 12:36:10
问题 Given the following problem : Definition : Let S be a string over alphabet Σ . S' is the smallest period of S if S' is the smallest string such that : S = (S')^k (S'') , where S'' is a prefix of S . If no such S' exists , then S is not periodic . Example : S = abcabcabcabca . Then abcabc is a period since S = abcabc abcabc a , but the smallest period is abc since S = abc abc abc abc a . Give an algorithm to find the smallest period of input string S or declare that S is not periodic. Hint :

PHP - How do you find duplicate value groupings in an array

为君一笑 提交于 2019-12-18 12:26:06
问题 I have an array of string values which sometimes form repeating value patterns ('a', 'b', 'c', 'd') $array = array( 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'c', 'd', ); I would like to find duplicate patterns based on the array order and group them by that same order (to maintain it). $patterns = array( array('number' => 2, 'values' => array('a', 'b', 'c', 'd')), array('number' => 1, 'values' => array('c')) array('number' => 1, 'values' => array('d')) ); Notice that [a,b], [b,c], & [c,d] are