pattern-matching

Detecting a specific pattern from a FFT in Arduino

北战南征 提交于 2021-02-19 05:55:07
问题 I have an FFT output from a microphone and I want to detect a specific animal's howl from that (it howls in a characteristic frequency spectrum). Is there any way to implement a pattern recognition algorithm in Arduino to do that? I already have the FFT part of it working with 128 samples @2kHz sampling rate. 回答1: lookup audio fingerprinting ... essentially you probe the frequency domain output from the FFT call and take a snapshot of the range of frequencies together with the magnitude of

Scala pattern matching: How to match on an element inside a list?

末鹿安然 提交于 2021-02-18 22:28:20
问题 Is it possible to rewrite the following code using Scala pattern matching? val ls: List[String] = ??? // some list of strings val res = if (ls.contains("foo")) FOO else if (ls.contains("bar")) BAR else SOMETHING_ELSE 回答1: You could implement this using a function like def onContains[T](xs: Seq[String], actionMappings: (String, T)*): Option[T] = { actionMappings collectFirst { case (str, v) if xs contains str => v } } And use it like this: val x = onContains(items, "foo" -> FOO, "bar" -> BAR )

Scala pattern matching: How to match on an element inside a list?

孤人 提交于 2021-02-18 22:27:06
问题 Is it possible to rewrite the following code using Scala pattern matching? val ls: List[String] = ??? // some list of strings val res = if (ls.contains("foo")) FOO else if (ls.contains("bar")) BAR else SOMETHING_ELSE 回答1: You could implement this using a function like def onContains[T](xs: Seq[String], actionMappings: (String, T)*): Option[T] = { actionMappings collectFirst { case (str, v) if xs contains str => v } } And use it like this: val x = onContains(items, "foo" -> FOO, "bar" -> BAR )

Regular expression matches more than expected

不想你离开。 提交于 2021-02-13 17:30:46
问题 Given is the following python script: text = '<?xml version="1.24" encoding="utf-8">' mu = (".??[?]?[?]", "....") for item in mu: print item,":",re.search(item, text).group() Can someone please explain why the first hit with the regex .??[?]?[?] returns <? instead of just ? . My explaination: .?? should match nothing as .? can match or not any char and the second ? makes it not greedy. [?]? can match ? or not, so nothing is good, too [?] just matches ? That should result in ? and not in <?

Regular expression matches more than expected

 ̄綄美尐妖づ 提交于 2021-02-13 17:30:42
问题 Given is the following python script: text = '<?xml version="1.24" encoding="utf-8">' mu = (".??[?]?[?]", "....") for item in mu: print item,":",re.search(item, text).group() Can someone please explain why the first hit with the regex .??[?]?[?] returns <? instead of just ? . My explaination: .?? should match nothing as .? can match or not any char and the second ? makes it not greedy. [?]? can match ? or not, so nothing is good, too [?] just matches ? That should result in ? and not in <?

Select only partial result but get total number of rows

南楼画角 提交于 2021-02-11 18:22:43
问题 I got stuck on SQL subquery selection. Right now, I have a table products: id | name | description ----+-------+---------------------- 6 | 123 | this is a + | | girl. 7 | 124 | this is a + | | girl. 8 | 125 | this is a + | | girl. 9 | 126 | this is a + | | girl. 10 | 127 | this is a + | | girl. 11 | 127 | this is a + | | girl. Isn't this? 12 | 345 | this is a cute slair 13 | ggg | this is a + | | girl 14 | shout | this is a monster 15 | haha | they are cute 16 | 123 | this is cute What I want

Pattern binding the same variable to different types sharing a trait

a 夏天 提交于 2021-02-11 14:49:10
问题 I have a question about pattern matching on values sharing some behaviour through a trait. I have an enum with two variants, each binding value of different types, where both types implement a trait. I'm trying to figure out whether it's possible to create a single pattern (of the E::VarA(x) | E::VarB(x) form) in which I bind both types to a single constant, provided I'm only interested in using the shared behaviour. An illustrative example: Playground: trait T { fn f(&self) -> usize; }

Finding consecutive text elements in list that match pattern

社会主义新天地 提交于 2021-02-11 13:30:03
问题 I am dealing with lists ls that look like the following: ls = ['y4','j8','Le1','Retx3','MNxe4','Xe4','Xf4','Xe5'] Given one such list, I am trying to find out if it contains 3 consecutive elements that match the following pattern: 3 consecutive elements starting with the letter X , and having the same length (here 3). And their ending digits may only correspond to either of the following cases: denote the ending digit of the first found element by n , then the allowed sequences of the 3

Finding consecutive text elements in list that match pattern

回眸只為那壹抹淺笑 提交于 2021-02-11 13:29:05
问题 I am dealing with lists ls that look like the following: ls = ['y4','j8','Le1','Retx3','MNxe4','Xe4','Xf4','Xe5'] Given one such list, I am trying to find out if it contains 3 consecutive elements that match the following pattern: 3 consecutive elements starting with the letter X , and having the same length (here 3). And their ending digits may only correspond to either of the following cases: denote the ending digit of the first found element by n , then the allowed sequences of the 3

Extract string between brackets

此生再无相见时 提交于 2021-02-11 12:40:25
问题 I would like to extract characters between first opening and last closing brackets in a string like match[input[name="firstname"]] considering the characters to retrieve may also contain more brackets. In this case it would get input[name="firstname"] Also, the string may contain some special characters like { # / \ ^ 回答1: This somehow awkward-looking regular expression /[^[\]]+\[[^[\]]+\]/ basically says "no brackets, then [, then no brackets, then ]". s = 'match[input[name="firstname"]]' >