Difference between revisions of "Python:Accumulate over a File"

From Progzoo
Jump to: navigation, search
 
Line 44: Line 44:
  
 
==How many big countries?==
 
==How many big countries?==
<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" 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]
 +
      region = f[1]
 +
      area = int(f[2])
 +
      pop  = int(f[3]);
 +
      gdp = int(f[4]);
 +
      if region == "Africa":
 +
        acc = acc+1
 +
print acc
 
</prog><answer>
 
</prog><answer>
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")
  public static void main(String[] argv)
+
       name = f[0]
                        throws Exception
+
      region = f[1]
  {
+
      area = int(f[2])
    BufferedReader fh =
+
       pop  = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
      gdp = int(f[4]);
    String line;
+
      if area>1500000:
    int acc = 0;
+
        acc = acc+1
    while ((line=fh.readLine())!=null)
+
print acc
    {
+
       String [] wrds = line.split("\t");
+
       int  area = Integer.parseInt(wrds[2]);
+
       if (area&gt;1500000) acc++;
+
    }
+
    System.out.println(acc);
+
  }
+
}
+
 
</answer>
 
</answer>
 
<p class="imper">Count the number of countries with an area of more than
 
<p class="imper">Count the number of countries with an area of more than
Line 78: Line 77:
  
 
==Numbering countries.==
 
==Numbering countries.==
<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" 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]
 +
      region = f[1]
 +
      area = int(f[2])
 +
      pop  = int(f[3]);
 +
      gdp = int(f[4]);
 +
      if area>1500000:
 +
        acc = acc+1
 +
print acc
 
</prog><answer>
 
</prog><answer>
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")
  public static void main(String[] argv)
+
       name = f[0]
                        throws Exception
+
      region = f[1]
{
+
      area = int(f[2])
    BufferedReader fh =
+
      pop  = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
       gdp = int(f[4]);
    String line;
+
      acc = acc+1
    int acc = 1;
+
      print "%3d %s" % (acc,name)
    while ((line=fh.readLine())!=null)
+
</answer>
    {
+
       String [] wrds = line.split("\t");
+
       String name = wrds[0];
+
       System.out.printf("%3d %s\n",acc,name);
+
      acc++;
+
    }
+
  }
+
}
+
  </answer>
+
 
<p class="imper">Produce a numbered list of countries:
 
<p class="imper">Produce a numbered list of countries:
 
<pre>
 
<pre>
Line 121: Line 118:
  
 
==Country number 42.==
 
==Country number 42.==
<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" 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]
 +
      region = f[1]
 +
      area = int(f[2])
 +
      pop  = int(f[3]);
 +
      gdp = int(f[4]);
 +
      if area>1500000:
 +
        acc = acc+1
 +
print acc
 
</prog><answer>
 
</prog><answer>
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")
  public static void main(String[] argv)
+
      name = f[0]
                        throws Exception
+
      region = f[1]
  {
+
       area = int(f[2])
    BufferedReader fh =
+
       pop  = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
      gdp = int(f[4]);
    String line;
+
       acc = acc+1
    int acc = 1;
+
       if acc==42:
    while ((line=fh.readLine())!=null)
+
          print name
    {
+
</answer>
       if (acc==42)
+
       {
+
        String [] wrds = line.split("\t");
+
        System.out.println(wrds[0]);
+
       }
+
       acc++;
+
    }
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">Print the name of country number 42
 
<p class="imper">Print the name of country number 42
 
</p>
 
</p>
Line 156: Line 150:
  
 
==Total area==
 
==Total area==
<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" 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")
  public static void main(String[] argv)
+
       name = f[0]
                        throws Exception
+
      region = f[1]
  {
+
      area = int(f[2])
    BufferedReader fh =
+
      pop  = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
      gdp = int(f[4]);
    int acc=0;
+
      if area>1500000:
    String line;
+
        acc = acc+1
    while ((line=fh.readLine())!=null)
+
print acc</prog><answer>
    {
+
fh = open("cia.txt","r");
       String [] wrds = line.split("\t");
+
acc = 0;
       int area = Integer.parseInt(wrds[2]);
+
for line in fh:
    }
+
       f = line.split("\t")
    fh.close();
+
       name = f[0]
    System.out.println(acc);
+
      region = f[1]
  }
+
      area = int(f[2])
}
+
       pop  = int(f[3]);
  </prog><answer>
+
      gdp = int(f[4]);
import java.io.BufferedReader;
+
      acc = acc+area
import java.io.FileReader;
+
print acc
public class Cia
+
</answer>
{
+
  public static void main(String[] argv)
+
                        throws Exception
+
  {
+
    BufferedReader fh =
+
    new BufferedReader(new FileReader("cia.txt"));
+
    int acc=0;
+
    String line;
+
    while ((line=fh.readLine())!=null)
+
    {
+
       String [] wrds = line.split("\t");
+
       int area = Integer.parseInt(wrds[2]);
+
       acc += area;
+
    }
+
    fh.close();
+
    System.out.println(acc);
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">
 
<p class="imper">
 
Calculate the total area of the world.
 
Calculate the total area of the world.
Line 212: Line 187:
  
 
==Total population==
 
==Total population==
<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" 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")
  public static void main(String[] argv)
+
       name = f[0]
                        throws Exception
+
      region = f[1]
  {
+
      area = int(f[2])
    BufferedReader fh =
+
      pop = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
      gdp = int(f[4]);
    int acc=0;
+
      if area>1500000:
    String line;
+
        acc = acc+1
    while ((line=fh.readLine())!=null)
+
print acc</prog><answer>
    {
+
fh = open("cia.txt","r");
       String [] wrds = line.split("\t");
+
acc = 0;
       int pop = Integer.parseInt(wrds[3]);
+
for line in fh:
    }
+
       f = line.split("\t")
    System.out.println(acc);
+
       name = f[0]
  }
+
      region = f[1]
}
+
      area = int(f[2])
  </prog><answer>
+
      pop = int(f[3]);
import java.io.BufferedReader;
+
       gdp = int(f[4]);
import java.io.FileReader;
+
      acc = acc+pop
public class Cia
+
print acc</answer>
{
+
  public static void main(String[] argv)
+
                        throws Exception
+
  {
+
    BufferedReader fh =
+
    new BufferedReader(new FileReader("cia.txt"));
+
    long acc=0;
+
    String line;
+
    while ((line=fh.readLine())!=null)
+
    {
+
       String [] wrds = line.split("\t");
+
       int pop = Integer.parseInt(wrds[3]);
+
       acc += pop;
+
    }
+
    fh.close();
+
    System.out.println(acc);
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">
 
<p class="imper">
 
Calculate the total population of the world.
 
Calculate the total population of the world.
</p>
 
<p>
 
The answer - at a little over 6 billion will be too large for
 
an <code>int</code>. Use a <code>long</code> instead.
 
 
</p>
 
</p>
 
</question>
 
</question>
  
 
==Total GDP of Africa==
 
==Total GDP of Africa==
<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" 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")
  public static void main(String[] argv)
+
       name = f[0]
                        throws Exception
+
      region = f[1]
  {
+
       area = int(f[2])
    BufferedReader fh =
+
      pop  = int(f[3]);
    new BufferedReader(new FileReader("cia.txt"));
+
      gdp = int(f[4]);
    int acc=0;
+
      if area>1500000:
    String line;
+
        acc = acc+1
    while ((line=fh.readLine())!=null)
+
print acc</prog><answer>
    {
+
fh = open("cia.txt","r");
       String [] wrds = line.split("\t");
+
acc = 0;
       String region = wrds[1];
+
for line in fh:
       long gdp = Long.parseLong(wrds[4]);
+
       f = line.split("\t")
    }
+
       name = f[0]
    fh.close();
+
      region = f[1]
    System.out.println(acc);
+
      area = int(f[2])
  }
+
      pop  = int(f[3]);
}
+
       gdp = int(f[4]);
  </prog><answer>
+
       if region=="Africa":
import java.io.BufferedReader;
+
         acc = acc+gdp
import java.io.FileReader;
+
print acc</answer>
public class Cia
+
{
+
  public static void main(String[] argv)
+
                        throws Exception
+
  {
+
    BufferedReader fh =
+
    new BufferedReader(new FileReader("cia.txt"));
+
    long acc=0;
+
    String line;
+
    while ((line=fh.readLine())!=null)
+
    {
+
       String [] wrds = line.split("\t");
+
       String region = wrds[1];
+
       long gdp = Long.parseLong(wrds[4]);
+
       if (region.equals("Africa"))
+
      {
+
         acc += gdp;
+
      }
+
    }
+
    fh.close();
+
    System.out.println(acc);
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">
 
<p class="imper">
 
Calculate the total GDP of the countries of Africa.
 
Calculate the total GDP of the countries of Africa.
Line 320: Line 248:
  
 
==Total GDP of the 'West'.==
 
==Total GDP of the 'West'.==
<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" 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>
  {
+
fh = open("cia.txt","r");
    BufferedReader fh =
+
acc = 0;
    new BufferedReader(new FileReader("cia.txt"));
+
for line in fh:
    long acc=0;
+
       f = line.split("\t")
    String line;
+
       name = f[0]
    while ((line=fh.readLine())!=null)
+
      region = f[1]
    {
+
      area = int(f[2])
       String [] wrds = line.split("\t");
+
      pop  = int(f[3]);
       String region= wrds[1];
+
       gdp = int(f[4]);
       long gdp = Long.parseLong(wrds[4]);
+
       if region in ["North America", "Europe", "Oceania"]:
       if (region.equals("North America") ||
+
        acc = acc+gdp
          region.equals("Europe") ||
+
print acc</answer><shell lang="java" className="Cia" import="shells.xml#null"/>
          region.equals("Oceania"))
+
      {
+
        acc += gdp;
+
      }
+
    }
+
    fh.close();
+
    System.out.println(acc);
+
  }
+
}
+
  </answer><shell lang="java" className="Cia" import="shells.xml#null"/>
+
 
<p class="imper">
 
<p class="imper">
 
Calculate the total GDP of the western world.
 
Calculate the total GDP of the western world.

Latest revision as of 17:25, 12 July 2017

You may find the page on accumulating variables useful:

How many countries in Asia?

In this example we use an accumulator to count the countries in Africa.

Notice that acc shows up three times:

  • The accumulator acc is set to zero at the start.
  • Every time we encounter Africa we increment acc.
  • After the loop has completed acc contains the value we want - so we print it.

Change the program so that it counts the countries of Asia.


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

How many big countries?

Count the number of countries with an area of more than 1500000.

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

Numbering countries.

Produce a numbered list of countries:

  1 Afghanistan
  2 Albania
  3 Algeria
...
262 Zimbabwe

Use three character spaces for the number and put a single space after it.

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

Country number 42.

Print the name of country number 42

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

Total area

Calculate the total area of the world.

This time we add to the accumulator each time. We can use either of the statements below:

acc = acc + area;
acc += area;

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

Total population

Calculate the total population of the world.

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

Total GDP of Africa

Calculate the total GDP of the countries of Africa.

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

Total GDP of the 'West'.

Calculate the total GDP of the western world. Include the regions "North America", "Europe" and "Oceania" as the western world.

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