Difference between revisions of "Python:Accumulate Adding and Counting"

From Progzoo
Jump to: navigation, search
 
Line 96: Line 96:
 
</hint>
 
</hint>
 
<prog>
 
<prog>
 +
def f(list):
 +
  acc = 0
 +
  for v in list:
 +
    acc = acc+v
 +
  return acc
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
Line 111: Line 116:
 
[[Using_an_Accumulator#Using_an_accumulator_to_count_on_a_condition|Count on a condition]]
 
[[Using_an_Accumulator#Using_an_accumulator_to_count_on_a_condition|Count on a condition]]
 
<prog>
 
<prog>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc=0;
+
   for v in list:
   for (int v : list)
+
     acc = acc+v
  {
+
   return acc
     acc = acc+v;
+
  }
+
   return acc;
+
}
+
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc = 0;
+
   for v in list:
   for (int v : list)
+
     if i==2:
  {
+
       acc = acc+1
     if (v==2)
+
   return acc
       acc++;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 149: Line 146:
 
</hint>
 
</hint>
 
<prog>
 
<prog>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc=0;
+
   for v in list:
   for (int v : list)
+
     acc = acc+v
  {
+
   return acc
     acc = acc+v;
+
  }
+
   return acc;
+
}
+
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc = 0;
+
   for v in list:
   for (int v : list)
+
     if v != 2:
  {
+
       acc = acc+1
     if (v!=2)
+
   return acc
       acc++;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 177: Line 166:
 
Use the accumulator to count all numbers larger than 1:
 
Use the accumulator to count all numbers larger than 1:
 
<prog>
 
<prog>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc=0;
+
   for v in list:
   for (int v : list)
+
     acc = acc+v
  {
+
   return acc
     acc = acc+v;
+
  }
+
   return acc;
+
}
+
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc = 0;
+
   for v in list:
   for (int v : list)
+
     if v>1:
  {
+
       acc = acc+1
     if (v>1)
+
   return acc
       acc++;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 205: Line 186:
 
Use the accumulator to sum all numbers larger than 1:
 
Use the accumulator to sum all numbers larger than 1:
 
<prog>
 
<prog>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc=0;
+
   for v in list:
   for (int v : list)
+
     acc = acc+v
  {
+
   return acc
     acc = acc+v;
+
  }
+
   return acc;
+
}
+
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
static int f(int [] list)
+
def f(list):
{
+
   acc = 0
   int acc = 0;
+
   for v in list:
   for (int v : list)
+
     if v>1:
  {
+
       acc = acc+v
     if (v>1)
+
   return acc
       acc+=v;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
 
{{Accumulator Tutorial}}
 
{{Accumulator Tutorial}}

Latest revision as of 16:21, 12 July 2017

Using an accumulator to add.

The variable list is an array, it holds several integers. For example it might be the list [1,2,3,4].

You can iterate over a list, every time you visit an element of the list you do something to the accumulator acc

In this example, for every member v of the list you add that member to the accumulator using the code

acc = acc + v;


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

Using an accumulator to count.

Use the accumulator to count the number of items in the list:


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

Using an accumulator to multiply.

Use the accumulator to find the product of items in the list:


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

Using an accumulator to max.

Use the accumulator to find the largest item in the list:


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

Count Twos.

Use the accumulator to find the number of twos in the list: Count on a condition


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

Count Non-Twos.

Use the accumulator to find number other than 2:

f({1,1,1,1})             -> 4
f({2,2,2})               -> 0
f({1,1,1,1,1})           -> 5
f({4,2,6,0,0})           -> 4


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

Count the big numbers

Use the accumulator to count all numbers larger than 1:


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

Add the big numbers

Use the accumulator to sum all numbers larger than 1:


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