Tag Info

Hot answers tagged

10

f[x_] := Module[ {s = IntegerDigits@x, s1}, s1 = s /. {y__ ..} :> {y}; {s1, Length@s/Length@s1}] f@212212212212 (* {{2, 1, 2}, 4}*)


9

FYI a more elegant way to get tweets1 is tweets1 = Rule @@@ tweets; Classify automatically separates words under the hood (via StringSplit), so you don't actually need to do that yourself. Classify has a built in sentiment classifier: Classify["Sentiment", "Windows 10 why is It called windows 10 when there was no Windows 9?"] (* "Negative" *)


7

An alternative to belisarius's answer f[n_] := IntegerDigits@n /. l : {x__ ..} :> {{x}, Length@l/Length@{x}} /. {_, 1} -> False f[123123] (* {{1, 2, 3}, 2} *) f[1] (* False *)


6

Currently pattern - matcher doesn' t go inside an Association, and _Association is an exception (head test). You can note, for example, that an Association is AtomQ (although this is only a consequence of that). So, if you want to use pure patterns, you're currently out of luck. But, you can use recursive patterns. In this case: nestedAssocPattern = ...


6

Nice problem! Maybe something like this: repeatingdigits[n_Integer] := Module[{digits = IntegerDigits[n], res = False, div}, div = Divisors[Length[digits]]; Do[With[{z = Partition[digits, d]}, If[Equal @@ z, res = {z[[1]], Length[z]}; Break[]]], {d, Reverse[Most[div]]}]; res] repeatingdigits /@ {1, 11, 212212, 2122126} (* {False, {{1}, 2}, ...


5

Several demonstrative examples: list = {{1, 2, 3}, {1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}}; list /. {p___, {a___, x_, x_, b___}, q___} :> {p, {a, x, b}, q} (* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 3, 5}} *) list /. {a___, x_, x_, b___} :> {a, x, b} (* {{1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}} *) Replace[list, {a___, x_, x_, b___} :> {a, x, b}, ...


5

There is already a Mathematica construct that is like your wildcard: the pattern _. (See Blank.) I would therefore start with that replacement: sL2 = superList /. wc -> _ {{_, _, 1, _, _}, {_, _, 2, _, _}, {_, _, 3, _, _}, {_, _, 4, _, _}} Then use MatchQ, since your matching is the reverse of the normal operation: MatchQ[littleList, #] & /@ ...


5

Changing a__ and b__ to a_ and b_, respectively, j2[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> {{a}, {b}, {x}}; j2[h] gives $\left( \frac{\tau w}{\tau ^2 w^2+1}, \frac{1}{\tau ^2 w^2+1}, t w \right) $ and, the same change in OP's function j j3[q_] := q /. b_ Cos[x__] + a_ Sin[x__] :> Sqrt[a^2 + b^2] Sin[x + Pi/2 + ArcTan[b/a]]; j3[h] ...


5

Whenever you run into unexpected results with pattern matching you need to consider how the pattern object itself evaluates. For example consider these: Plus[__] Plus[_, _] Times[_, _] __ 2 _ _^2 And now your pattern: Hypergeometric2F1[_, _, _, _] (1 - _)^-_ To prevent this evaluation you can use either HoldPattern or Verbatim: ...


4

Also consider: DeleteCases[mylist, {___, Null, ___}] Or: Select[mylist, FreeQ[Null]] (* v10 operator syntax *)


4

RunnyKine's comment written up as a answer. DeleteCases[mylist, {Null, _} | {_, Null}]. This will also take care of the case where both entries are Null, so you only have to use DeleteCases once. However, since your entries appear to be all reals, you might use Cases[mylist, {_Real, _Real} But if some of your numeric values don't have head real, ...


4

Let me stay very close to your question short = {2, 3}; long = Range @ 5; par = Partition[long, Length @ short, 1]; MemberQ[par, short] True Expanding a little bit short = Range[2, 4]; long = Range [5]; par = Partition[long, Length @ short, 1]; MemberQ[par, short] True IntervalMemberQ[Interval[{1, 5}], Interval[{2, 4}]] True ...


4

Regarding the updated description of your problem while Split is very clean it is not optimal unless the overall list is relatively short. As an example consider this data: SeedRandom[0]; a = RandomChoice[{"a", "b", "c"}, 1*^7]; It begins with: {"c", "c", "b", "a", . . .} The method from your answer takes significant time: Needs["GeneralUtilities`"] ...


3

Map ReplaceAll at Level 1: # /. {a___, x_, x_, b___} :> {a, x, b} & /@ list (* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}} *) Map DeleteDuplicates at Level 1: DeleteDuplicates /@ list (* {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}} *) Alternative methods to ReplaceRepeated to get {{1, 2, 3}, {1, 3, 5}}: FixedPoint[# /. {a___, x_, x_, b___} ...


3

Here is a rough attempt at implementing what you describe, primarily using StringSplit. Fold[ Flatten @ StringSplit[##] &, StringReplace[text[[1]], "," | "*" :> ""], { StartOfLine ~~ Whitespace ... ~~ DigitCharacter ~~ Except["\n"] .. ~~ "\n", WordBoundary ~~ ("." | ";" | ":") ~~ Whitespace, "\n" } ] // StringTrim {"Since all activities ...


3

I assume you want to find an exact match, instead of just that individual members of the subset are found in the larger list. This can be quite easily be accomplished with the following: ClearAll[subsetInOrderQ] subsetInOrderQ[set_List, subset_List] := MatchQ[set, {___, Sequence @@ subset, ___}] subsetInOrderQ[{1, 2, 3, 4, 5}, {2, 3}] (* True *) ...


3

The code as below can achieve the result that you need. Clear@t; mat = RandomInteger[{1, 10}, {20, 4}]; Evaluate[t /@ Range[Length@mat]] = mat Because of (=)Set has the attribute HoldFirst, I use the function Evaluate to evaluate first before proceeding with Set.


3

Perhaps this will work for you. Given a symbol, say t, and a matrix, say m, it will define a set of indexed variables t[1], t[2], ..., t[n], bound to the respective rows of m. SetAttributes[assign, HoldFirst]; assign[name_Symbol, matrix_List] := ( Clear @ name; Do[t[i] = matrix[[i]], {i, 1, Length@matrix}] ) Generate some data. SeedRandom[42]; m ...


3

l = {0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1} LengthWhile[l[[LengthWhile[l, # != 1 &] + 1 ;;]], # == 1 &] (* 4 *) Edit: and this is dedicated to that special person: k = LengthWhile; l[[l~k~(# != 1 &) + 1 ;;]]~k~(# == 1 &) (* 4 *)


2

Pattern matching takes place on (something close to) the FullForm of the expression rather than the display form that you see. You can visualize it using TreeForm: h // TreeForm I am not sure what you are attempting but I imagine your pattern was not written with this in mind. What parts did you expect to match a__, b__, and x__? What actually ...


2

Here is a first pass at this problem. img = Import["http://i.stack.imgur.com/wwHZe.jpg"]; getHue = First[DominantColors[#, 1][[1]] ~ToColor~ Hue] &; hues = Map[getHue, ImagePartition[img, 40], {2}]; plot = Map[Hue, hues, {2}] // ArrayPlot I tried to use ClusteringComponents to further quantize these hues but I got a bad result. Edit: After a ...


2

There is new DuplicateFreeQ. So x = {{a, b, c}, {a, a, a}, {a, b, c}, {a, b, b}}; Cases[x, _?DuplicateFreeQ] (* {{a, b, c}, {a, b, c}} *)


2

Union[{1, 2, 3, 4, 5}, {2, 3}] === {1, 2, 3, 4, 5} (*True*)


2

Version 10 ClearAll[foo] foo = Or@@(SubsetQ @@ # &/@{{##},{#2, #}})& {foo[{1,2,3,4},{3,4}],foo[{2,1},{1,2,3,4}]} (* True, True *) Version 9 ClearAll[sQ, foo2] sQ = And @@ Function[{x}, MatchQ[x, Alternatives @@ #]] /@ #2 &; foo2 = Or @@ (sQ @@ # & /@ {{##}, {#2, #}}) & {foo2[{1, 2, 3, 4}, {2, 3}], foo2[{3, 1}, {1, 2, 3, 4}]} (* ...


2

Method 1 Position[Inner[If[#1 === wc, True, #1 == #2] &, #, littleList, And] & /@ superList, True] Output is the following: {{3}} Method 2 Position[Inner[Equal, #, littleList, And] & /@ superList /. Equal[wc, x_] :> True, True] Output is the following: {{3}} Function version how might I use that in the context of a function ...


2

As already pointed out by others Association objects are presently not traversed by pattern matching. See: MatchQ-ing Associations (MMA 10) difficulty abstracting the OptionValue pattern My interpretation of your question is that you want to test to see if all values in the top Association are themselves Associations, rather than testing if only one ...


2

I have tried some String manipulation functions : StringCases["342121212136" , s : (x__ ~~ (x_) ..) -> {x, s}] gives the longest cycle in the string :{{2121, 21212121}} If you want the shortest cycle : StringCases["342121212136" , s : (Shortest@x__ ~~ (x_) ..) -> {x, s}] gives : {{21, 21212121}} If you are not interested in extracting cycles ...


1

To get the longest repeating subsequence and the number of its repetitions: ClearAll[rDF] rDF[a : Repeated[Longest[x__], {2, Infinity}]] := {{x}, Length[{a}]/Length[{x}]} rDF[___] := False rDF @@@ IntegerDigits[{1, 11, 212212, 2122126, 212212212212, 2122122122125}] (* {False,{{1},2},{{2,1,2},2},False,{{2,1,2,2,1,2},2},False} *) To get the shortest ...


1

sa = SparseArray[superList, Automatic, wc]; check = littleList[[#2]] === sa[[##]] & @@@ sa["NonzeroPositions"] (* {False,False,True,False} *) Position[check, True] (* {{3}} *)


1

Another option to consider is to only (repeatedly) replace if the repeated item is an atom: rule = {a___, x_?AtomQ, x_, b___} :> {a, x, b} In action: {{1, 2, 3}, {1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}} //. rule {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 3, 5}}



Only top voted, non community-wiki answers of a minimum length are eligible