Tag Info

Hot answers tagged

11

You can use this to create a functionality which will fit your need the best. Here's how you can preview your input cell with c highlighted Red. CellPrint[ NotebookRead @ PreviousCell[] /. "c" -> InterpretationBox[ StyleBox["c", FontColor -> RGBColor[1, 0, 0]], c ] ] You can even evaluate such cell. General ...


10

If I understand correctly, the closest to Python's triple quotes is a TextCell. You can enter it as follows: Here I opened an inline cell after typing the input a =. This is done by pressing Ctrl-9 (or going to the Insert menu, then to Typesetting... and Start Inline Cell). In the light orange highlighted box, you can type any text you want in a natural ...


9

DictionaryLookup for each case, combined with string patterns involving ___ can be very slow. It would be faster to do it in one pass over all the words in the dictionary: With[{words = DictionaryLookup[], letters = Characters["aeiouy"]}, Select[words, Intersection[Characters[#], letters] == letters &]] This takes ~0.35 seconds on my laptop, ...


8

You can't use non-numeric data for EmpiricalDistribution (at least up to V9, and I saw nothing when I had 10.x installed that said otherwise). Deal with it as character codes: gc = ToCharacterCode@ToLowerCase@gettys; p3 = Partition[gc, 3, 1]; d = EmpiricalDistribution@p3; Probability[{a, b, c} == ToCharacterCode@"the" \[Conditioned] {a, b} == ...


8

Even though belisarius' answer is probably closest to what you want, I find that I quite often do something like this: SetAttributes[sj,HoldFirst]; sj[x_Symbol, y__] := (x = StringJoin[x, y]) Now we can type myString = "some words "; sj[myString, "more words"]; myString (* "some words more words" *) That is, instead of using infix notation lhs ...


6

I think you'll find this considerably faster... Intersection @@ (DictionaryLookup["*" <> # <> "*"] & /@Characters["aeiouy"]) and getting sneaky with letter probabilities, about five times as fast as above: Fold[Pick[#, StringFreeQ[#, #2], False] &, DictionaryLookup["*" <> First@Characters["yuioae"] <> "*"], ...


6

URLDecode and URLEncode were introduced in Mathematica 10.0: url = "https%3A%2F%2Fwww.google.co.uk%2Fimages%2Fsrpr%2Flogo4w.png"; URLDecode[url] (*"https://www.google.co.uk/images/srpr/logo4w.png"*) The symbol definition can be accessed as follows, which is scarily close to @Guesswhoitis's code above. URLDecode[url]; Unprotect[URLDecode]; ...


6

Define your own: << Notation` Notation[DoubleLongLeftRightArrow[ ParsedBoxWrapper[ RowBox[List["x_", " ", "⊕", " ", "y_", " "]]], ParsedBoxWrapper[ RowBox[List[" ", RowBox[List["x_", "=", RowBox[List["x_", "<>", "y_"]]]]]]]]] x = "caca"; x⊕"test"; x (*"cacatest"*) Edit Using the Notation palette you should see


6

To achieve customized display forms, you use either Format or MakeBoxes. Converting to string is usually a measure of last resort, and not what I would recommend here. Instead, just do this: expr = a.b + II (* ==> II + a.b *) II /: MakeBoxes[II, StandardForm] := RowBox[{"I"}] expr = a.b + II (* ==> I + a.b *) Edit: TraditionalForm, String ...


5

One way to do this using Mathematica's native string patterns is like this: StringReplace["aaabccccc", xs:((x:WordCharacter)..) :> StringPadRight[x, StringLength@xs, "x"] ] (* "axxbcxxxx" *) Here is the same replacement expressed using a RegularExpression: StringReplace["aaabccccc", RegularExpression["((\\w)\\2+)"] :> StringPadRight["$2", ...


5

Using Regexes: StringReplace["aaabccccc", RegularExpression["(\\w)(\\1+)"] :> StringJoin["$1", Array["x" &, StringLength@"$2"]]] (* "axxbcxxxx" *)


4

The given above answer is correct, however if you are not against using undocumented functions, then Internal`StringToDouble["9.0E-03"] is much faster. To demonstrate the speedup, first generate some fake data heads = ToString /@ RandomReal[{1.0000, 9.9999}, 100000, WorkingPrecision -> 5] // Quiet; exp = ConstantArray["E-", 100000]; tails = ...


3

Since you are only doing this for printing then you can simply replace II with "I" Print[ToString[ReplaceAll[expr, II -> "I"] // TraditionalForm]] (* a.b + I *)


3

expr = a.b + II; II2I[n_] := StringReplace[ToString[n], "II" -> "I"]; II2I[expr]


3

I am not sure if this is the best method, but I found this article which describes mathematica operators without predefined definitions. I used one to associate a function with the TildeTilde symbol, made by pressing esc+~+~+esc. SetAttributes[TildeTilde, HoldFirst] TildeTilde[a_Symbol, b_String] := (a = a <> b) You need to use HoldFirst attribute ...


3

Confirmed by WRI (@ilian) as bug introduced in 10.1.


3

You can directly match specific operators as: s = "abc\[CircleTimes]def" StringReplace[s, "\[CircleTimes]" -> "CircleTimes"] "abcCircleTimesdef" It seems not too unreasonable to manually create a list of all the operators you need to handle. this builds a list of rules for every possible "\[..]" character allspecial = (#[[1]] -> ...


2

This worked: StringReplace[ToString@FullForm@"\[CircleTimes]", ("\\[" ~~ Shortest[c__] ~~ "]") :> c] but there's a drawback - FullForm changes the string also in other places, for instance where a slash "\" exists, so another solution would be welcomed...


2

Aside from the fun evaluation control and numerical precision discussions happening, is this useful to accomplish the task you wanted? It does simply use string patterns to drop the appropriate zeroes. sigfigs[str_String] : = StringLength @ If[ StringContainsQ[s, "."], StringTrim[ StringReplace[s, "." -> ""], ...


1

Reading your comment about not wanting your TA's to type the quotes of a string, you could build some sort of user interface. Here's a very crude one. list = {}; DynamicModule[{f = ""}, Column[{ InputField[Dynamic[f], String], Dynamic[If[f =!= "", AppendTo[list, f]; f = ""]; list] }] ]


1

How about: Cases[DictionaryLookup["*"], x_ /; SubsetQ[Characters[x], {"a", "e", "i", "o", "u", "y"}]]


1

As a recurrence: ComputePoly[{}] := 1; ComputePoly[{0}] := x1 ComputePoly[{1}] := x2 ComputePoly[l_List] := ComputePoly[l[[1 ;; 1]]]*ComputePoly[Rest@l] ComputePoly[{0, 1, 0, 1, 1, 1, 1}] (* x1^2 x2^5 *) Of course different recurrence relationships need different implementations. As you don't mention your actual one it's very difficult to provide more ...


1

This is very similar to a common problem that many symbolic Mathematica functions share, e.g.: Solve[2*x-5==15,x] and as can be seen from the example the standard way to solve that is to pass such symbols which are to be used as formal symbols as extra argument(s). As you guessed in your comment the most direct way to implement that would be something ...


1

ClearAll[BigStringExpr]; BigStringExpr = (StartOfString ~~ anInteger : (Except["0", DigitCharacter] ~~ DigitCharacter ...) ~~ EndOfString :> ToExpression@anInteger) StringCases["1234", BigStringExpr] Head[%[[1]]] {1234} Integer



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