Difference between revisions of "Python:Accumulate over a File with Multiple Variables"

From Progzoo
Jump to: navigation, search
(Created page with "{{Accumulator Tutorial}} In these examples we accumulate more than one value. We still only need one loop - we just do more work in each. ==Average population== <question xmln...")
 
Line 42: Line 42:
 
starts.
 
starts.
 
<pre>
 
<pre>
long totPop = 0;
+
totPop = 0
int count = 0;</pre>
+
count = 0</pre>
 
*Increment the accumulator inside the loop.
 
*Increment the accumulator inside the loop.
 
<pre>
 
<pre>
 
totPop += pop
 
totPop += pop
count++;</pre>
+
count+=1</pre>
 
*Use the result - in this case print it, <i>after</i> the loop.
 
*Use the result - in this case print it, <i>after</i> the loop.
<pre>    System.out.println(totPop/count);</pre>
+
<pre>    print totPop/count</pre>
 
</div>
 
</div>
 
</question>
 
</question>
  
 
==Is 3 million the median?==
 
==Is 3 million the median?==
<question xmlns:htm="http://www.w3.org/1999/xhtml" lang="java" className="Cia" copyFile="cia.txt"><prog>
+
<question xmlns:htm="http://www.w3.org/1999/xhtml" className="Cia" copyFile="cia.txt"><prog>
import java.io.BufferedReader;
+
fh = open("cia.txt","r")
import java.io.FileReader;
+
acc = 0
public class Cia
+
for line in fh:
{
+
      f = line.split("\t")
}
+
      name = f[0]
  </prog><answer>
+
      region = f[1]
import java.io.BufferedReader;
+
      area = int(f[2])
import java.io.FileReader;
+
      pop  = int(f[3])
public class Cia
+
      gdp = int(f[4])
{
+
      if area>1500000:
  public static void main(String[] argv)
+
        acc = acc+1
                        throws Exception
+
print acc
  {
+
</prog><answer>
    BufferedReader fh =
+
fh = open("cia.txt","r")
    new BufferedReader(new FileReader("cia.txt"));
+
b = 0
    int small = 0;
+
s = 0
    int large = 0;
+
for line in fh:
    String line;
+
       f = line.split("\t")
    while ((line=fh.readLine())!=null)
+
       name = f[0]
    {
+
      region = f[1]
       String [] wrds = line.split("\t");
+
      area = int(f[2])
       int pop  = Integer.parseInt(wrds[3]);
+
      pop  = int(f[3])
       if (pop&gt;3000000)
+
       gdp = int(f[4])
       {
+
       if pop>30000000:
         large++;
+
         b = b+1
      }
+
       else:
       else
+
         s = s+1
      {
+
print s,b
         small++;
+
</answer>
      }
+
    }
+
    fh.close();
+
    System.out.println(large);
+
    System.out.println(small);
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">
 
<p class="imper">
 
Show the number of countries with a population more than 3000000
 
Show the number of countries with a population more than 3000000

Revision as of 17:35, 12 July 2017

In these examples we accumulate more than one value. We still only need one loop - we just do more work in each.

Average population

Calculate the average population size for the countries of the world.

Print the total population divided by the number of countries.

Recall that there are three steps:

  • Declare and initialise your accumulators before the loop

starts.

totPop = 0
count = 0
  • Increment the accumulator inside the loop.
totPop += pop
count+=1
  • Use the result - in this case print it, after the loop.
     print totPop/count
[Font] [Default] [Show] [Resize] [History] [Profile]

Is 3 million the median?

Show the number of countries with a population more than 3000000

Show the number of countries with a population less than 3000000

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

USA and the world

Show the GDP of "United States" as a percentage of the world GDP.

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