Hot answers tagged

11

Block[{Times = Sequence}, ToExpression@ StringRiffle@ Characters@ "{ab{c{de}}f}" ] (* {a, b, {c, {d, e}}, f} *)


10

StringCases["ABCaDEFbcABCdefDEF", "ABC" ~~ Shortest[a__] ~~ "DEF" :> a] (* Out: {"a", "def"} *)


10

You can first compare two of the strings, get the longest common string, and then take the result and compare it to the third string. And keeping do it until the last string in the list will give you the longest common string for all the strings. This can be achieved using Fold, for example: ls = {"home/dir1/dir2/jmoasd.txt", ...


7

Here is a way to search from within mathematica: notebooks = Quiet@FileNames["*.nb", NotebookDirectory[], 2]; Monitor[Select[ Table[{nb, StringJoin@Select[ StringSplit[Import[nb, "Plaintext"], "\n"] , ((If[#, Print["match on:", nb]]; #) &@ StringMatchQ[#, "*NIntegrate*"]) &, 5]}, {nb,notebooks}], #[[2]] ...


7

str = "{ab{c{de}}f}"; str2 = StringReplace[str, x : LetterCharacter ~~ y : LetterCharacter :> x ~~ "," ~~ y]; Block[{Times = Sequence}, ToExpression[str2]] {a, b, {c, {d, e}}, f}


6

StringTake and Span would be useful. For example, to get the second characters: StringTake[s, 2 ;; ;; 21] (* "LLFOWJZSPLJJHNHQOQLYSOPWQOSZNTTLTOHETNJOJOODOCJFQOJ" *) To get all of the strings: StringTake[s, Array[# ;; ;; 21&, 21]] Another approach would be a combination of Characters and Part: StringJoin[Characters[s][[2 ;; ;; 21]]] (* ...


6

str = StringReplace["{ab{c{de}}f}",x : LetterCharacter :> "," <> x <> ","]; Quiet[ToExpression[str] /. Null :> Nothing] (*{a, b, {c, {d, e}}, f})*


5

Here's a simple version using SplitBy. Select[SplitBy[words, StringTake[#, 1] &], Length@# > 1 &] It groups words by the first letter, then picks groups larger than 1. So it solves the more general problem rather than strictly doing pairs. I don't have no fancy WikipediaData on my version, but it seems faster using some randomly generated ...


4

Here is an approach which does not rely on the NBImport.exe (which actually performs importing of the NB files as "Plaintext" under the hood) and performs all the operations in the Kernel only. Currently NBImport.exe contains a bug due to which it returns $Failed when have to import a NB file with non-ASCII file path. I'm also not sure how it interprets ...


4

I got faster result using listability of StringTake and Pick instead of Select words = WikipediaData["alliteration"] // TextWords; {a1, res} = AbsoluteTiming[ words // Partition[#, 2, 1] & // Select[#, StringTake[First[#], 1] == StringTake[Last[#], 1] &] &]; a1 (* 0.002902 *) {b1, res} = AbsoluteTiming[ Pick[Partition[#, ...


3

Short answer You should use RuleDelayed: "foo" /. x_String :> Head[x] Othewise, the r.h.s of your rule is computed before the match, when x is still a symbol, not bound to anything. Longer answer Let's enumerate the main steps the main evaluator goes through, when evaluating your original code. The case of Rule The head ReplaceAll is evaluated ...


3

I propose the use of ArrayComponents and Unitize: array = {{{{0, -a Sqrt[a^2 + c^2], -c Sqrt[a^2 + c^2]}, {-a Sqrt[a^2 + c^2], 0, 0}, {-c Sqrt[a^2 + c^2], 0, 0}}}, {{{0, 0, 0}, {0, -2 a^2, -2 a c}, {0, -2 a c, -2 c^2}}}, {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}}; array // ArrayComponents // Unitize {{{{0, 1, 1}, {1, 0, 0}, {1, 0, ...


2

Here is a solution written in the spirit of the answer by george2079 but NOT relying upon the buggy NBImport.exe executable. Instead it uses FrontEnd for converting Notebooks into plain text using the findings described here: findInNBFile::cntconv = "Failed to extract plain text from `1`"; findInNBFile[NBFilePath_String, stringPattern_, resPerFile_Integer: ...


2

A couple of additional solutions. The first, with FindList, is probably the simplest and quickest. Using FindList searchDir = "<NB dir>"; fnames = FileNames["*.nb", searchDir, 2]; Length@fnames sres = {#, FindList[#, {"curve"}, WordSearch -> False]} & /@ fnames; sres = Select[sres, Length[#[[2]]] > 0 &]; Grid[sres, Dividers -> All, ...


2

mat = {{{{0, -a Sqrt[a^2 + c^2], -c Sqrt[a^2 + c^2]}, {-a Sqrt[a^2 + c^2], 0, 0}, {-c Sqrt[a^2 + c^2], 0, 0}}}, {{{0, 0, 0}, {0, -2 a^2, -2 a c}, {0, -2 a c, -2 c^2}}}, {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}} You can use f1 = Replace[#, Except[0 | _List] -> 1, Infinity] &; f2 = SparseArray[SparseArray[#]["NonzeroPositions"] ...


1

@wxffles beat me to a SplitBy solution, but his version doesn't return a result limited to pairs. So, if that's the requirement, here's one that does. This method is a bit slower for pair-wise collection compared to my second solution in the OP: words = WikipediaData["alliteration"] // TextWords; SplitBy[words, ToLowerCase[StringTake[#, 1]] &] // ...


1

Let: SeedRandom[11] cromo33 = RandomInteger[1, 33]; To produce your string I would use Row and ToString: ToString @ Row @ cromo33 "001101111011110111010011001000101" For the second case IntegerString takes a third parameter that specifies length: SeedRandom[15] string = IntegerString[RandomInteger[8], 2, 4] "0101" To convert this back into ...



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