Python:Accumulate with Various Operations

From Progzoo
Jump to: navigation, search

Using an Accumulator


Concatenate

Concatenate the string array

Concatenate means stick the strings together.


[Font] [Default] [Show] [Resize] [History] [Profile]

List it

Put commas in front of each element.

Change the line to

acc = acc+","+v;


[Font] [Default] [Show] [Resize] [History] [Profile]

Concatenate with brackets

Concatenate the string array - put angle brackets around each item:

 f({"a","b","c","d"})  -> <a><b><c><d>
 f({"x","y"})          -> <x><y>
 f({"yes","but","no"}) -> <yes><but><no>


[Font] [Default] [Show] [Resize] [History] [Profile]

Reverse Concatenate

Concatenate the string array, backwards


[Font] [Default] [Show] [Resize] [History] [Profile]

Front and back

Palindrome it

f({"a","b","c","d"}) ->dcbaabcd                 
f({"x","y"})         ->yxxy                     
f({"yes","but","no"})->nobutyesyesbutno  


[Font] [Default] [Show] [Resize] [History] [Profile]

List it nicely

Put commas between the elements of the string array

  • Notice that the separator sep has is "=" for the first iteration, therafter it will always be "#".
  • The "-" is added in the return statement.
  • Replace "=", "#" and "-" with appropriate strings.


[Font] [Default] [Show] [Resize] [History] [Profile]