Computer Programming


Computer Engineering

New technology is common, new thinking is rare - Sir Peter Blake

ECOO Programming Competition

  • Board-wide, all high schools in HCDSB are invited to compete
  • Annual competition, top three finishing teams move onto regionals at York University
  • Written in team of up to 4
  • 3 hours to solve 4 problems
  • One computer only
  • Bring as much code or printed material you like with you
  • Below are past competitions for practice, and helper code at the bottom

*** I STRONGLY recommend that you write the ECOO competition in Python ***

Python Primer

Files

- Open a file


f = open("DATA12.txt")
		

- You have two options for reading from a file


#1 - all the lines at once into a list
f = open("DATA12.txt")
lines = f.readlines()
f.close()


#loop through the lines list one line at a time
for line in lines:
	print (line)

# OR

#pop the first line in the list, keep going until there's nothing more to pop
while True:
	if len(lines) == 0:
		break
		
line = lines.pop(0)
print (line)
		

#2 - one line at a time
f = open("input.txt")

while True:
	line = f.readline()

	if line == "":
		break

	print (line)

f.close()
		

Lists


#You can loop through a list in two ways:
#1 - itemized
numbers = [10, 45, 23, 56, 20]
for num in numbers:
    print (num)

#2 - range
numbers = [10, 45, 23, 56, 20]
for i in range(len(numbers)):
    print (numbers[i])


#Don't forget the very important list manipulation functions:

numbers = [10, 45, 23, 45, 20]

print (len(numbers))  #5
print (numbers[2])    #23

print (numbers.count(45))   #2
print (numbers.count(33))   #0

print (numbers.index(20))   #4
print (numbers.index(45))   #1
#print (numbers.index(17))  #CRASH (17 is not in the list)

numbers.sort()   #sorts the list
print (numbers)  #[10, 20, 23, 45, 45]

numbers.remove(23)   #removes 23 from the list
print (numbers)      #[10, 45, 45, 20]
#numbers.remove(17)  #CRASH (17 is not in the list)

num = numbers.pop(0)  #pops slot 0 from the list, stores result in num
print (num)           #10
print (numbers)       #[20, 45, 45]

numbers.append(55)  #adds 55 to the end of the list
print (numbers)     #[20, 45, 45, 55]

if 55 in numbers:   #check if item 55 is in the numbers list
    print ("yes")   #yes!
else:
    print ("no")

rstrip


#Sometimes (often) strings have \n at the end of them (it's invisible,
#it tells python that a new line has started).  It's often beneficial
#to get rid of it because otherwise a string match may not work
line = line.rstrip()

split


#If your input line consists of multiple pieces of information separated
#by some delimiter, you can break tthem up into a list, like this (it
#gets rid of the \n and creates a list with 10 in slot 0, 30 in slot 2, etc)
line = "10 30 1 45"
line = line.rstrip().split(" ")

Decimal places


#Sometimes you need to display values to a specific decimal point.
num = 3.362718
print ("%0.3f" %num)
print ("%0.2f" %num)

Substrings


#Substrings in python are simple
x = "abcdefghijklmnop"

print (x[7:12])
#hijkl (from 7 to 12)

print (x[:5])
#abcde (from start to 5)

print (x[10:])
#klmnop (from 10 to end)

print (x[:-1])
#abcdefghijklmno (from start to one minus end

Java Primer


import java.io.*;
import java.text.*;

public class Primer
{

	public static void main (String[] args) throws IOException
	{

		//If you want to read from a file don't forget the following line of code:
		BufferedReader input = new BufferedReader (new FileReader ("DATA10.txt"));
		//And don't forget to put "throws IOException" at the end of the main class.

		//split - breaks up a string into an array of string based on a delimeter.

		//Here, the delimeter is a space (" ")
		String test1 = "Hello there my name is George.";
		String[] arrayOfWords = test1.split (" ");
		for (int i = 0 ; i < arrayOfWords.length ; i++)
		{
			System.out.println (arrayOfWords [i]);
		}
		System.out.println ();

		//Here, the delimeter is a comma (",")
		String test2 = "Red,Orange,Blue,Yellow";
		String[] arrayOfWords2 = test2.split (",");
		for (int i = 0 ; i < arrayOfWords2.length ; i++)
		{
			System.out.println (arrayOfWords2 [i]);
		}
		System.out.println ();

		//Here, the delimeter is a comma followed by a space(", ")
		String test3 = "Ham, Turkey, Tuna, Beef";
		String[] arrayOfWords3 = test3.split (", ");
		for (int i = 0 ; i < arrayOfWords3.length ; i++)
		{
			System.out.println (arrayOfWords3 [i]);
		}
		System.out.println ();

		//Here, the delimeter is a space but the numbers are doubled and dispalyed
		String test4 = "5 12 7 24";
		String[] arrayOfWords4 = test4.split (" ");
		for (int i = 0 ; i < arrayOfWords4.length ; i++)
		{
			System.out.println (Integer.parseInt (arrayOfWords4 [i]) * 2);
		}
		System.out.println ();

		//If you wanted to read 5 lines from a file:
		for (int i = 1 ; i <= 5 ; i++)
		{
			String line = input.readLine ();
		}

		//If you wanted to read 5 lines from a file and splitk each line up:
		for (int i = 1 ; i <= 5 ; i++)
		{
			String[] line = input.readLine ().split (" ");
		}

		//If you wanted to format a number:
		double num = 56.33472;
		DecimalFormat df = new DecimalFormat (".000");
		System.out.println (df.format(num));

		double num2 = 0.33472;
		DecimalFormat df2 = new DecimalFormat (".000");
		System.out.println (df2.format(num2));

		double num3 = 0.33472;
		DecimalFormat df3 = new DecimalFormat ("0.000");
		System.out.println (df3.format(num3));

	}

}