Difference between revisions of "Python:Accumulate Multiple Values"

From Progzoo
Jump to: navigation, search
(Created page with "{{Accumulator Tutorial}} You can use more than one accumulator in the same loop. <shell lang='py'><![CDATA[ QcQ ll = [ [1,1,1,1], [2,2,2], [1,1,1,1,1]...")
 
Line 44: Line 44:
 
<hint tease='Even numbers'>
 
<hint tease='Even numbers'>
 
You can test for even numbers using %
 
You can test for even numbers using %
  if (v % 2 == 0)
+
  if v % 2 == 0:
 
   ...
 
   ...
 
</hint>
 
</hint>
 
<prog>
 
<prog>
static String f(int [] list)
+
def f(list):
{
+
   sum = 0
   int odds = 0;
+
   count = 0
   int evens = 0;
+
   for v in list:
   for (int v : list)
+
    sum = sum + v
  {
+
    count = count + v
  }
+
   return [sum,count]
   return odds+" "+evens;
+
}
+
 
</prog>
 
</prog>
 
<answer>
 
<answer>
static String f(int [] list)
+
def f(list):
{
+
   odds = 0
   int odds = 0;
+
   evens = 0
   int evens = 0;
+
   for v in list:
   for (int v : list)
+
     if v % 2 ==0:
  {
+
       evens = evens + v
     if (v % 2 == 0)
+
     else:
    {
+
       odds = odds + v
       evens++;
+
   return [odds,evens]
    }
+
     else
+
    {
+
       odds++;
+
    }
+
  }
+
   return odds+" "+evens;
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 86: Line 76:
 
The list {1,3,5,8,6} returns "odd" because there are 3 odds and only 2 evens.
 
The list {1,3,5,8,6} returns "odd" because there are 3 odds and only 2 evens.
  
  f({1,1,1,1})            -> odd
+
  f([1,1,1,1])            -> odd
  f({1,2,3,4,5,6})        -> even
+
  f([1,2,3,4,5,6])        -> even
  f({-1,1,-1,1,-1})        -> odd
+
  f([-1,1,-1,1,-1])        -> odd
  f({1,2,101,4,-1})        -> odd
+
  f([1,2,101,4,-1])        -> odd
  f({4,2,6,1,1})          -> even   
+
  f([4,2,6,1,1])          -> even   
 
<prog>
 
<prog>
static String f(int [] list)
+
def f(list):
{
+
  sum = 0
   return "odd";
+
   count = 0
}
+
  for v in list:
 +
    sum = sum + v
 +
    count = count + v
 +
  return [sum,count]
 
</prog>
 
</prog>
 
<answer>
 
<answer>
static String f(int [] list)
+
def f(list):
{
+
   odds = 0
   int odds = 0;
+
   evens = 0
   int evens = 0;
+
   for v in list:
   for (int v : list)
+
     if v % 2 ==0:
  {
+
       evens = evens + v
     if (v % 2 == 0)
+
     else:
    {
+
       odds = odds + v
       evens++;
+
   return (odds>evens)?"odds":"evens"
    }
+
     else
+
    {
+
       odds++;
+
    }
+
  }
+
   return (odds>evens)?"odd":"even";
+
}
+
 
</answer>
 
</answer>
 
</question>
 
</question>
Line 123: Line 108:
 
*Numbers greater than 4 are large.
 
*Numbers greater than 4 are large.
 
*Other numbers are medium.
 
*Other numbers are medium.
  f({1,1,1,1})            -> 4 0 0
+
  f({1,1,1,1})            -> [4, 0, 0]
  f({1,2,3,4,5,6})        -> 1 3 2
+
  f({1,2,3,4,5,6})        -> [1, 3, 2]
  f({-1,1,-1,1,-1})        -> 5 0 0
+
  f({-1,1,-1,1,-1})        -> [5, 0, 0]
  f({1,2,101,4,-1})        -> 2 2 1
+
  f({1,2,101,4,-1})        -> [2, 2, 1]
  f({4,2,6,1,1})          -> 2 2 1  
+
  f({4,2,6,1,1})          -> [2, 2, 1]
  
 
'''Return the number of small, medium and large numbers.'''
 
'''Return the number of small, medium and large numbers.'''
 
<prog>
 
<prog>
static String f(int [] list)
+
def f(list):
{
+
   sm = 0
   int small = 0;
+
   md = 0
   int medium= 0;
+
   lg = 0
   int large = 0;
+
   for v in list:
   return small+" "+medium+" "+large;
+
    pass
}
+
  return [sm,md,lg]
 
</prog>
 
</prog>
 
<answer><![CDATA[
 
<answer><![CDATA[
static String f(int [] list)
+
def f(list):
{
+
   sm = 0
   int sm = 0;
+
   md = 0
   int md = 0;
+
   lg = 0
   int lg =0;
+
   for v in list:
   for (int v : list)
+
     if v<2:
  {
+
       sm = sm+1
     if (v <2)
+
     elsif v>4:
    {
+
       lg = lg+1
       sm++;
+
     else:
     }
+
       md = md+1
    else if (v>4)
+
   return [sm,md,lg]
    {
+
       lg++;
+
    }
+
     else
+
    {
+
       md++;
+
    }
+
  }
+
   return sm+" "+md+" "+lg;
+
}
+
 
]]></answer>
 
]]></answer>
 
</question>
 
</question>
 
{{Accumulator Tutorial}}
 
{{Accumulator Tutorial}}

Revision as of 16:32, 12 July 2017

You can use more than one accumulator in the same loop.

Find the average

Use two accumulators, sum and count. Return the two values in a string.


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

Odds and evens

Use two accumulators, odd and evens to count the number of odd numbers and the number of even numbers. Return the two numbers in a string separated by a space.


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

More odd than even?

Return the word "odd" if the number of odd numbers is bigger than the number of even numbers. Return the word "even" otherwise.

The list {1,3,5,8,6} returns "odd" because there are 3 odds and only 2 evens.

f([1,1,1,1])             -> odd
f([1,2,3,4,5,6])         -> even
f([-1,1,-1,1,-1])        -> odd
f([1,2,101,4,-1])        -> odd
f([4,2,6,1,1])           -> even  


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

Small, medium, large

  • Numbers less than 2 are small.
  • Numbers greater than 4 are large.
  • Other numbers are medium.
f({1,1,1,1})             -> [4, 0, 0]
f({1,2,3,4,5,6})         -> [1, 3, 2]
f({-1,1,-1,1,-1})        -> [5, 0, 0]
f({1,2,101,4,-1})        -> [2, 2, 1]
f({4,2,6,1,1})           -> [2, 2, 1]

Return the number of small, medium and large numbers.


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