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

From Progzoo
Jump to: navigation, search
Line 19: Line 19:
 
<prog>
 
<prog>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+1
 
     acc = acc+1
Line 26: Line 26:
 
<answer>
 
<answer>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+v
 
     acc = acc+v
Line 40: Line 40:
 
<prog>
 
<prog>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+1
 
     acc = acc+1
Line 47: Line 47:
 
<answer>
 
<answer>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+","+v
 
     acc = acc+","+v
Line 63: Line 63:
 
<prog>
 
<prog>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+1
 
     acc = acc+1
Line 70: Line 70:
 
<answer><![CDATA[
 
<answer><![CDATA[
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+"<"+v+">"
 
     acc = acc+"<"+v+">"
Line 82: Line 82:
 
<prog>
 
<prog>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+1
 
     acc = acc+1
Line 89: Line 89:
 
<answer>
 
<answer>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = v+acc
 
     acc = v+acc
Line 104: Line 104:
 
<prog>
 
<prog>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = acc+1
 
     acc = acc+1
Line 111: Line 111:
 
<answer>
 
<answer>
 
def f(list):
 
def f(list):
   acc = 0
+
   acc = ""
 
   for v in list:
 
   for v in list:
 
     acc = v+acc+v
 
     acc = v+acc+v

Revision as of 17:03, 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]