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}*)


8

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

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}, ...


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 = ...


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

Wrap the entire key in Hold, if you really want to keep things that might evaluate in the keys of an association (HoldPattern is a red herring: keys aren't patterns). Alternatively use ToString. But generally this just sounds like a dangerous and confusing game to play, to me. What exactly are you trying to do? There is probably a better way that doesn't ...


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

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] ...


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 ...


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

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

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 ...


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

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

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

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

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}}


1

If I don't missunderstand your question this could be a good candidat for ReplaceRepeated list = {1, 1, 2, 3, 3, 3, 1, 1, 7}; list //. {head___, x_, x_, tail___} :> {head, x, tail} {1, 2, 3, 1, 7} To scrutinise @ ybeltukov's excellent answer and your nice but ambiguous question matrix = {{1, 2, 3}, {1, 2, 3}, {1, 2, 2, 3}, {1, 3, 3, 5}}; matrix ...


1

As the question is posed Position[superList, littleList /. {__, a_Integer, __} :> {_, _, a, _, _}] {{3}}


1

Try this: StringSplit[ StringReplace[First@text, Shortest[(x : DigitCharacter) ~~ __ ~~ "\n"] | ")" | "(" | "*" -> ""], "." | ";" | "," | ":"] which gives the following somewhat imperfect output: {" Since all activities lectures", " seminars", " laboratories at \ Humber are conducted in English", " it is essential that all students ...



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