MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: partitioning a string

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60973] Re: partitioning a string
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Wed, 5 Oct 2005 02:28:30 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 10/4/05 at 1:24 AM, ma_sara177 at hotmail.com (Sara) wrote:

>I have tred to partition a String and iwrote this code but its take
>to time to run, and it doesnt show eny result.
>stringPartition[s_, n_] := Module[{}, emptyString = Mod[StringLength[s], n];
>    If[emptyString != 0, empt = n - emptyString];
>    str1 := " ";
>    str2 := "";
>    For[i = 1, i <= empt, i++, str2 = str2 <> str1];
>    str = s <> str2;
>    M = StringLength[str];
>    yasList = Range[1, M/n] ; 
>    For[i = 1, i <= M, i + n, For[j = 1, j <= M/n, j++,
>          yasList[[j]] = StringTake[i, j*n]
>               ];
>           ];

Generally the best way to speed up code like the above is to get rid of the For loops and use Mathematica's functional programming paradigm. The following does that and should run faster

stringPartition[s_String, n_Integer] := 
  StringJoin@@@Partition[Characters[s], n]

Note, I did not take the time to understand exactly what your code is doing. Instead, I simply created a function to partition a string using functional programming.
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: partitioning a string
  • Next by Date: Re: Partition(divid string to substring
  • Previous by thread: Re: partitioning a string
  • Next by thread: Re: partitioning a string