Difference between revisions of "Python:Accumulate with Various Operations"

From Progzoo
Jump to: navigation, search
(Created page with "{{Accumulator Tutorial}} Using an Accumulator <shell lang='py'><![CDATA[ QcQ ll = [ ["a","b","c"], ["x","y"] ] for l in ll: print "%-26s -> %-25s" % (str...")
 
Line 24: Line 24:
 
   return acc
 
   return acc
 
</prog>
 
</prog>
<answer lang="java">
+
<answer>
 
def f(list):
 
def f(list):
 
   acc = 0
 
   acc = 0
Line 34: Line 34:
  
 
==List it==
 
==List it==
<question lang='java' className="P">
+
<question>
 
<p class='imper'>Put commas in front of each element.</p>
 
<p class='imper'>Put commas in front of each element.</p>
 
Change the line to
 
Change the line to
Line 61: Line 61:
 
  f({"yes","but","no"}) -> <yes><but><no>
 
  f({"yes","but","no"}) -> <yes><but><no>
 
]]></pre>
 
]]></pre>
<prog lang="java">
+
<prog>
 
def f(list):
 
def f(list):
 
   acc = 0
 
   acc = 0
Line 97: Line 97:
  
 
==Front and back==
 
==Front and back==
<question lang='java' className="P">
+
<question>
 
<p class='imper'>Palindrome it</p>
 
<p class='imper'>Palindrome it</p>
 
  f({"a","b","c","d"}) -&gt;dcbaabcd                 
 
  f({"a","b","c","d"}) -&gt;dcbaabcd                 
Line 119: Line 119:
  
 
==List it nicely==
 
==List it nicely==
<question lang='java' className="P">
+
<question>
 
<p class='imper'>Put commas between the elements of the string array</p>
 
<p class='imper'>Put commas between the elements of the string array</p>
 
*Notice that the separator <code>sep</code> has is "=" for the first iteration, therafter it will always be "#".
 
*Notice that the separator <code>sep</code> has is "=" for the first iteration, therafter it will always be "#".

Revision as of 17:01, 12 July 2017

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]