New answers tagged

4

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


3

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


11

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


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


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


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

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[#, ...


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


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


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


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


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


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


0

StringJoin /@ Transpose[Characters @ StringPartition[s,21]]


0

The last substring does not contain 21 characters but you can use the padded form of Partition to get sublist of equal length with padding. Then it is just a Transpose and StringJoin to the result. I use "&" for padding which you can substitute for any character. StringJoin@Flatten@Transpose@Partition[Characters[s], 21, 21, {1, 1}, "&"] Hope this ...


6

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


11

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


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}



Top 50 recent answers are included