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

From Progzoo
Jump to: navigation, search
(Using an accumulator to add.)
Line 37: Line 37:
  
 
==Using an accumulator to count.==
 
==Using an accumulator to count.==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to count  the number of items in the list:
 
Use the accumulator to count  the number of items in the list:
 
<hint>
 
<hint>
Line 46: Line 46:
 
</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)
+
     acc = acc+1
  {
+
   return acc
     acc++;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
  
 
==Using an accumulator to multiply.==
 
==Using an accumulator to multiply.==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to find the product of items in the list:
 
Use the accumulator to find the product of items in the list:
 
<hint tease='What is the product'>
 
<hint tease='What is the product'>
Line 77: Line 69:
 
<hint tease='Why do I get zero?'>
 
<hint tease='Why do I get zero?'>
 
You must start the accumulator at 1 instead of zero.
 
You must start the accumulator at 1 instead of zero.
  int acc = 1;
+
acc = 1;
 
instead of
 
instead of
  int acc = 0;
+
acc = 0;
 
</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 = 1
   int acc = 1;
+
   for v in list:
   for (int v : list)
+
     acc = acc*v
  {
+
   return acc
     acc*=v;
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
  
 
==Using an accumulator to max.==
 
==Using an accumulator to max.==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to find the largest item in the list:
 
Use the accumulator to find the largest item in the list:
<hint>You can use the Math.max function, this takes two numbers and returns the highest.
+
<hint>You can use the max function, this takes two numbers and returns the highest.
  acc = Math.max(acc,v);
+
  acc = max(acc,v);
 
</hint>
 
</hint>
<hint>To do this right you should initialize the accumulator to Integer.MIN_VALUE</hint>
 
 
<prog>
 
<prog>
static int f(int [] list)
 
{
 
  int acc=0;
 
  for (int v : list)
 
  {
 
    acc = acc+v;
 
  }
 
  return acc;
 
}
 
 
</prog>  
 
</prog>  
 
<answer>
 
<answer>
static int f(int [] list)
+
def f(list):
{
+
   acc = None
   int acc = 0;
+
   for v in list:
   for (int v : list)
+
     acc = max(acc,v)
  {
+
   return acc
     acc = Math.max(acc,v);
+
  }
+
   return acc;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
  
 
==Count Twos.==
 
==Count Twos.==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to find the number of twos in the list:
 
Use the accumulator to find the number of twos in the list:
 
[[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]]
Line 166: Line 136:
  
 
==Count Non-Twos.==
 
==Count Non-Twos.==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to find number other than 2:
 
Use the accumulator to find number other than 2:
 
  f({1,1,1,1})            -> 4
 
  f({1,1,1,1})            -> 4
Line 204: Line 174:
  
 
==Count the big numbers==
 
==Count the big numbers==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to count all numbers larger than 1:
 
Use the accumulator to count all numbers larger than 1:
 
<prog>
 
<prog>
Line 232: Line 202:
  
 
==Add the big numbers==
 
==Add the big numbers==
<question lang="java" className="P">
+
<question lang="py" className="P">
 
Use the accumulator to sum all numbers larger than 1:
 
Use the accumulator to sum all numbers larger than 1:
 
<prog>
 
<prog>

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