Teaching: Programming

Programming B, Java

Week 45

Example java program:

Solutions to exercises

Week 46

Solutions to exercises

Looping through a string:

Extra exercise:
Create a class called Matrix and implement the following functions:

  • public Matrix multiply(Matrix that)//Returns AxB in a new matrix object
  • public Matrix transpose()//Returns A-transpose in a new object
  • public Matrix clone()//Returns a clone of the object
  • public static Matrix identity(int m,int n)//Returns a new identity matrix of the desired dimensions

The class should implement an immutable matrix object. This means the functions should not change anything on the object it is called on, rather, it should create a new object, put the result in that object and return it.
Answer this question: Does it make sense to make the identity function static? Yes/No, why?

You don’t have to implement all methods, but start with the ones you feel most comfortable with.

You may start out with the following classes:

Week 47

Java uses camel-case (wiki) with the following conventions:

This is important because it will help you identify the different elements in a program. What can you say about this line:

Week 48

For your report, please reconsider this document:
How To: Write a Programming Report

Project1, task 5* hint:

The GO board can be modeled as a graph where pieces can be placed on vertices, and the lines represent edges. This allows you to use graph algorithms for discovering chains. Have a look at:

Breadth First Search
Depth First Search

You might get inspiration from the following scala functions. Note that this uses a single-dimension array for the vertices:



Programming A, Python

Contact me via mail for comments, suggestions and corrections:

Week 35

Extra problem:

Try solving problem 22 from ProjectEuler.net
An example solution using list comprehension (probably extra curriculum):

Extra problem 2

Guessing the gender of a name. Download the files babynames and gender.py below and place them in the same folder. Rewrite the function gender(name) to achieve a lower miss ratio for the gender of names.
babynames
gender.py

Week 36

Tips:

Installing Swampy on IMADA machines
You can use the script made by Arun by executing the following line:

Gedit is a versatile text-editor, even more so if you install a few plugins. In ubuntu, type the following in a terminal:

This gives you access to a lot of plugins, including the embedded terminal. Using this, you can right click on the embedded terminal and select change directory which navigates the terminal to the directory of the currently open document.

When the time comes for your reports, gedit also has a very nice and lightweight LaTeX plugin:

To access the installed plugins, open gedit and select edit->preferences->plugins and check the desired plugins.

When debugging your programs, it is very useful to see the line numbering in gedit. This can be enabled in edit->preferences and then check the Display line numbers box.

Extra
Exercise 3.5 from Think Python. Try to make a generic version of the function that takes two parameters, m and n and prints/draws an m x n grid.
*hint* Use recursion or for loops

Exercise 5.2 from Think Python. What is the minimal number of comparisons needed to solve the problem? (i.e. # of conditionals with a greater/smaller than comparison)

Some example solutions:
polygon.py
squares.py
squares2.py
triangle.py

Week 37

Some example solutions: 37.zip

A template for your reports (subject to change!):
dm502_template.tex
dm502_template.ps

Suggestions and improvements are more than welcome. Especially for better python markup (bold keywords,better colors, etc.)

Week 38

words.txt, use it for 9.7 in the text book.

A plethora of solutions to exercises for this week. Take a look at them, some are less straight forward.

Week 39

solutions

Week 40

Relevant Tips

Convert list of chars to string:

Sort histogram by frequency:

Alternatively using list-comprehension:

Automatic printing of object. (Good for debugging!!!)

Flatten a list of lists:

Week 41

A short “How To”, subject to change:
*updated* 12-10-2011
How To: Write a Programming Report

Questions, comments and suggestions are welcome!

1 Comment

  1. happy wheels 3 Reply

    Its like you read my mind! You appear to know a lot about this, like you wrote the book
    in it or something. I think that you could do with some pics to drive the
    message home a little bit, but instead of that, this
    is great blog. An excellent read. I will definitely be back.

Leave a Reply

Your email address will not be published. Required fields are marked *