# Tag Info

8

There is another way that is on my machine almost 500x faster then your solution. The idea is to look how Mathematica represents colored strings and use this directly. When we colorize an input string by selecting text and using the Format menu, we can create something like this Now, press Ctrl+Shift+E to see the underlying expression. ...

7

StringJoin /@ Rest @ Permutations[slist, Length @ slist] {"apple", "123", "Car", "apple123", "appleCar", "123apple", "123Car", "Carapple", "Car123", "apple123Car", "appleCar123", "123appleCar", "123Carapple", "Carapple123", "Car123apple"}

7

You at least have to provide k0 with a delayed definition, otherwise you've fallen at the first hurdle, since the original input will already have been lost. This will give you a fighting chance: k0 := 0.4 π; Now, we need to work with the (unevaluated) ownvalues of this symbol, rather than allowing its definition ever to evaluate. Since we don't know what ...

7

StringReplacePart[ # , RandomChoice[Characters@#] , {#, #} &@RandomInteger[{1, StringLength@#}] ] &@"ORANGE"

7

Use StringReplace[list, PunctuationCharacter -> " "] (* {"string1", "bla bla", "more stuff", "and more stuff"} *) PunctuationCharacter is new in version 10.3. The 10.x updates received many new functions for text processing. In older versions I would use an explicit list of possible punctuation characters, i.e. punctuation = ...

6

words = {"horse", "horses", "morse", "morses", "norse", "norses", "fox", "monses", "goose", "tool", "goal", "tools", "gothe"} I'm not very experienced with Graphs so any advice, how to make it shorter, is welcome. adjacencyMatrix = Outer[ Boole@LessEqual[1, #, 2] &@*EditDistance, words, words ]; adjacencyGraph = AdjacencyGraph[words, ...

6

This will change the first occurrence of a random char by another random char f[s_] := StringReplace[s, Rule @@ RandomChoice[Characters@s, 2], 1] f@"ORANGE" (* "ORRNGE"*)

6

I'll use a slightly modified example: str = "1) This is the first question 1)______ 2) This is the second question 2)______ ... 10) This is the tenth question 10)______" One can use either StringExpression[] or RegularExpression[] in StringCases[] for this; here's how: StringTrim /@ StringCases[str, n : DigitCharacter .. ...

6

This is more for fun, than anything else, but here's a recursive solution as short as possible: sj[n_, s_] := Sequence[s, sj[n - 1, s]]; sj[1, s_] := s; {4~sj~"0", 4~sj~"3", 4~sj~"6"} {"0", "0", "0", "0", "3", "3", "3", "3", "6", "6", "6", "6"} For the second use case: SetAttributes[sj, SequenceHold] sj[n_, {s__}] := {sj[n, Sequence@s]} 4~sj~{"1", ...

6

Note that of course none of this requires the elements of the Lists to be Strings. repeatList[lst_List, nTimes_Integer] := Join @@ ConstantArray[lst, nTimes] repeatElements[lst_List, {n_Integer}] := Join @@ Map[ConstantArray[#, n] &, lst] repeatElements[lst_List, ns : {__Integer}] /; Length@lst == Length@ns := Join @@ MapThread[ConstantArray[#1, #2] ...

6

I recommend this solution: data = {"0", "3", "4", "6", {"01", "1", "10", "102"}}; nrOfCopies = {1, 2, 3, 4, 2}; Flatten[MapThread[ConstantArray, {data, nrOfCopies}], 1] (* Out: {"0", "3", "3", "4", "4", "4", "6", "6", "6", "6", {"01", "1", "10", "102"}, {"01", "1", "10", "102"}} *) It is possible to get the exact syntax that you ask for, by adding a ...

5

Following structure could be applied to achieve the desired outcome. However, this may not be the best way to carry out the OPs request: Please see example below: Input: {Flatten[{Table["x", {3}], Table["y", {4}]}, 2]} Output: {{"x", "x", "x", "y", "y", "y", "y"}} Furthermore, the code can be repeated to reproduce the desired outcome. Reference: ...

4

Experience shows that in order to understand a text it is by far not necessary that all ist letters to be correct. Here's a little game to experiment with it. We start with this text from Wikipedia: t = "Mathematica is a symbolic mathematical computation program, \ sometimes called a computer algebra program, used in many scientific, \ engineering, ...

4

Here's a slightly more involved approach that always changes a letter, or signals an error: ClearAll[MutateString]; MutateString::nomut = "All characters in string  are the same."; MutateString[s_String] := With[{choices = DeleteDuplicates[Characters[s]]}, With[{n = RandomInteger[{1, StringLength[s]}]}, StringReplacePart[s, ...

4

I really enjoy Mathematica when I can outsource tough algorithmic decisions to their source code- I believe this is the case here. It appears as if your code is doing something expensive (searching and replacing) many different times. I propose to do it all at once. Benchmark: txt = ExampleData[{"Text", "AeneidEnglish"}]; somewords = ...

4

selector[str_String] := GroupBy[list, EditDistance[str, #] <= 2 &][True] /. str -> Style[str, Red, Bold] list = {"horse", "horses", "morse", "morses", "norse", "norses", "fox", "monses", "goose", "tool", "goal", "tools", "gothe"}; selector /@ list Alternatively, selector2[str_String] := Thread[str -> GroupBy[list, 0 < ...

3

StringCases["<em>tu tia mi tia</em>", "<em>" ~~ a__ ~~ "</em>" :> a] {"tu tia mi tia"}

2

When you used the term "my string A" in the question that is not quite the correct nomenclature. A is what is known as a named pattern. At the moment, ruleInt does not need curly brackets (although there is also no harm). ruleInt = Exp[I A___ x] -> DiracDelta[A x] Let's try your rule on the following test cases. {Exp[-4 I], Exp[I x], Exp[2 x I], ...

2

I don't know if Anna is pronouncing the umlaut correctly, but this result sounds very similar to what comes through the terminal to my tin ears: In[1]:= RunProcess[{"say", "-v", "Anna", "öffentlich"}] Out[1]= <|"ExitCode" -> 0, "StandardOutput" -> "", "StandardError" -> ""|> There's clearly something weird going on with encodings here; ...

2

To settle this: one merely needs to use ": " as a unique delimiter for StringSplit[]. list /. s_String :> Last[StringSplit[s, ": "]]

2

randomStringReverse[s_String] := StringReplacePart[s, StringReverse @ StringTake[s, #], #]& @ Sort @ RandomInteger[{1, StringLength @ s}, 2] str = "FDSRTYNHFNKHLIUHG"; newStr = randomStringReverse[str] (* "FDSRTYNHILHKNFUHG" *) And to check: MapAt[ Reverse, Transpose @ DeleteCases[Characters /@ {str, newStr} // Transpose, {a_, a_}], 1 ...

2

list = {"horse", "horses", "morse", "morses", "norse", "norses", "fox", "monses", "goose", "tool", "goal", "tools", "gothe"}; char = Characters /@ Subsets[list, {2}]; rev = Reverse /@ (SortBy[#, Length] & /@ char); com = Complement @@@ rev; pos = Position[com, a_List /; Length@a <= 2]; Map[StringJoin, Extract[rev, pos], {2}] // Sort // ...

1

Look up WordCounts: text = ExampleData[{"Text", "DeclarationOfIndependence"}]; WordCounts[text] (* <|"of" -> 79, "the" -> 76, "to" -> 64, "and" -> 55, "our" -> 25, "their" -> 20, "has" -> 20, "for" -> 19, "in" -> 18, "He" -> 18,... \> *) Or you may prefer WordCounts[ToLowerCase@text]

1

You can use StringSplit to trim and separate the words: a = ExampleData[{"Text", "DeclarationOfIndependence"}]; Sort[Counts[StringSplit[a]], Greater][[1 ;; 10]] Result: <|"of" -> 79, "the" -> 76, "to" -> 64, "and" -> 55, "our" -> 25, "has" -> 20, "their" -> 20, "for" -> 19, "He" -> 18, "in" -> 18|> Now when ...

1

list = {"there", "is", "a", "table", "on", "the", "table", "put", "it", "on", "the", "table"}; count = Tally@list {{"there", 1}, {"is", 1}, {"a", 1}, {"table", 3}, {"on", 2}, {"the", 2}, {"put", 1}, {"it", 1}} Reverse@SortBy[Last]@count // TableForm

1

str = ToString /@ {AA, BB, CC} {"AA", "BB", "CC"} should do it, but you won't see the quotation marks in the output cell, because Mathematica suppresses them in standard form output. To see them evaluate str // FullForm

1

A simple solution that excludes self-replacement. Function[{s}, StringReplacePart[s, StringTake[s, ConstantArray[#[[1]], 2]], ConstantArray[#[[2]], 2]] &@ RandomSample[Range@StringLength@s, 2]]@"ORANGE" (note you could end up with the same string in the case of repeated characters in the input)

1

To get the umlaut into the Mac OS X command line properly, you could go the route via RTF export. Here is a function that automates this: ClearAll[say]; Options[say] = {"Voice" -> "Zarvox"}; say[t_, OptionsPattern[]] := Module[{out = FileNameJoin[{\$TemporaryDirectory, "MathematicaOutput" <> StringJoin[Map[ToString, DateList[]]]}]}, ...

1

It is unclear what the question poser seeks in writing "...this shorter." Nevertheless ToExpression /@ (x = {{"0", "0", "0", "0", "3", "3", "3", "3", "6", "6", "6", "6"}, {"01", "1", "10", "102", "01", "1", "10", "102", "01", "1", "10", "102"}}) will convert each string into an expression, which you can multiply as you see fit. 4 x[[1]] 3 ...

1

I think I understand now, how to solve the question partially based on the @ MarcoB advice. Observing that the billhead ends up with "cc/g\n" let us first delete the billhead assuming that text is the freshly imported string: text1 = StringDelete[text, __ ~~ "cc/g\n"] This yields the following 16.4540 4.0763e-02 4.9548e+01 ...

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