Welcome!

CS-A1120 Programming 2

Lukas Ahrenberg and Sanna Suoranta

Department of Computer Science
Aalto University

By the time you’ve sorted out a complicated idea into little steps that even a stupid machine can deal with, you’ve certainly learned something about it yourself.

—Douglas Adams, Dirk Gently’s Holistic Detective Agency

https://presemo.aalto.fi/prog2

35c9e48f86af625bba6aa15dfb73713f-300.svg

  • Chat for questions
  • Polls and puzzles

Round 1 learning goals

  • After this week, you
    • will have refamiliarised yourself with the Scala programming language
    • can run test suits in your IDE of choice
    • have solved a few basic tasks by programming
    • should be familiar with CS-A1120 Programming 2 course information.

Programming 2

From gates to functional and parallel programming

  • Computing – one of the least understood natural phenomena
  • Programming – to harness computing for a specific purpose

The broad strokes learning outcomes

After finishing this course, you

  1. can describe the structure of a programmable computer and how it may be built using logic
  2. can construct basic computational circuit structures from simple logic gates and buses
  3. can define computational resources (time, space, parallelism, etc) and analyse the performance of basic programs
  4. can explain recursion, and is able to develop programs that employ recursion
  5. can describe the role of an interface and implement basic algorithms for common tasks (searching, sorting, etc)

… and more!

In your own words - what does this code do?

What does it compute?

 def test(m: Long) =
   var i = 1L
   var s = 0L
   while i <= m do
     s = s + i
     i = i + 1
   end while
   s
 end test

Discuss with person next to you (a couple of minutes)…

Now post it in the chat (https://presemo.aalto.fi/prog2)

Try while-sum in a console

 def test(m: Long) =
   var i = 1L
   var s = 0L
   while i <= m do
     s = s + i
     i = i + 1
   end while
   s
 end test

 val NANOS_PER_SEC   = 1e9
 val test_start_time = System.nanoTime
 val x = test(4000000000L)
 val test_end_time   = System.nanoTime
 val test_duration   = test_end_time - test_start_time
 println("the test took %.2f seconds".format(test_duration/NANOS_PER_SEC))

Adds up the first four billion positive integers

Running it

the test took 1.06 seconds

(Intel Core i5-1135G7 @ 2.40GHz × 8 processor and 16 GiB of main memory)

Billions of computations per second

  • \(4 \times 10 ^ 9\) loop iterations
  • in 1.06 seconds
  • So, about \(4 \times 10^9\) loop iterations per second
  • Compare to light in vacuum: 299 792 458 m/s
  • Very roughly \(\frac{3 \times 10^8}{4 \times 10^9} = 7.5 \times 10^{-2}\) m/loop iteration
  • That is, by the time one loop iteration finishes light in vacuum travels only about 7.5 cm!
  • This is less than the distance from the screen to your nose

Computers are powerful - what else?

  • What if the data is way too large for a single computer?
  • How do you program a new computer architecture?
  • Is there anything we cannot program?
  • What resources are required for some computation?

Are there other ways to implement the function test? Better?

(https://presemo.aalto.fi/prog2)

 def test(m: Long) =
   var i = 1L
   var s = 0L
   while i <= m do
     s = s + i
     i = i + 1
   end while
   s
 end test

Are there other ways to implement test?

(https://presemo.aalto.fi/prog2)

One suggestion:

def test2(m: Long) = m * ((m + 1)/2.0)

Another:

def test3(m: Long) = (0L to m).sum

Yet another:

def test4(m: Long): Long = if m > 0 then m+test4(m-1) else 0

Or why not?

def test5(m: Long): Long = (0L to m).reduce(_ + _)

Are these "better"? Any drawbacks?

Scala recalled - Range

We can use to and until to create a Range in Scala

  • to is inclusive (the last element is included)
  • until is exclusive (the last element is excluded)
scala> (0 until 10).last
val res: Int = 9

scala> (0 to 10).last
val res: Int = 10

  • In Scala Range inherits the SeqOps trait, meaning that we can call methods such as map, fold, zip, and so on. For example, to square some values
scala> (1 to 3).map(x => x * x)
val res: IndexedSeq[Int] = Vector(1, 4, 9)

scala> Seq(1,2,3).map(x => x * x)
val res: Seq[Int] = List(1, 4, 9)

Which is why we can define the sum as

def test3(m: Long) = (0L to m).sum

(But why the 0L and not 0?)

Scala recalled - Recursion

Recursion is a way to have a function call itself

  • It needs a base case to stop
  • and a recursive step to calculate
  • More in round 8

def test4(m: Long): Long =
  if m > 0 then
    m+test4(m-1) // Recursive step
  else 0 // Base case

All programs require a physical process to execute

Antikythera-mechanism.jpg
babbage-analytic.jpg
Xeon-e7v2_die-full.jpg
Source: Wikipedia CC 2.5 Generic
Source: Wikipedia CC 2.5 SA
Intel Xeon E7 v2 die
Source: Intel Newsroom Photo Gallery

Computing is a natural phenomenon

Programming is a way to harness computing for a given purpose

Principles of programming

Independent of programming language and hardware architecture

Programming skills and understanding of computing are integral parts of scientific education

Computer science studies the phenomena of computing and the use of computing machines

Yet: understanding a program requires knowledge (example)

scala> def test(m: Long) = m * ((m + 1)/2.0)
def test(m: Long): Double

scala> test(30)
val res: Double = 465

scala>test(4000000000)
val res: Double = 8.000000002E18

Why Double?

What if?

scala> def test(m: Long) = m * ((m + 1)/2)
def test(m: Long): Long
                    
scala> test(30)
val res: Long = 450

450??

Maybe, rewrite the expression…

scala> def test(m: Long) = (m * (m + 1))/2
def test(m: Long): Long

scala> test(4000000000)
val res: Long = -1223372034854775808

-1223372034854775808???

(By the way - Double cannot handle arbitrary large numbers either; There is a type called BigInt, that you may want to try for huge numbers.)

Computer Science is a "young" area

  • The study and theory of computing really just got underway in the 20th century
  • P vs NP - is an excellent (and famous) example
    • We have incomplete knowledge on which tasks can be solved efficiently
  • P - Problems can be solved 'quickly'
  • NP - An answer can be checked 'quickly'
  • Are these set of problems the same?

PvsNP-review.png

The Status of the P Versus NP Problem by Lance Fortnow (2009)

Example of an P problem

Is the number 2026 in the following list of integers?

\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)

What would you have to do to answer this question?

Example of an NP-complete problem

Can the number 2026 be formed as a sum of the following integers?

\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)

given that you can use each at maximum one time?

What would you have to do to answer this question?

How would you write a program to do this (in general)?

It is possible to write programs to search for an answer, but, there is no known method for solving this problem efficiently in its general form (i.e. arbitrarily many numbers of arbitrary size)

It is however easy to verify whether a solution is valid

Can the number 2026 be formed as a sum of the following integers?

\( 89, 117, 153, 265, 395, 477, 516, 665, 670, 795 \)

Assume someone gave you the answer "Yes, the numbers are \(89, 117, 153, 395, 477, 795\)."

What would you have to do to answer this question?

  1. Check that each number is in the original list
  2. \(89 + 117 + 153 + 395 + 477 + 795 = 2026\)

Course contents

  • Introduction
    • 01 - Warm-up
  • Module I - The Mystery of the Computer
    • 02 - Bits and data
    • 03 - Combinational logic
    • 04 - Sequential logic
    • 05 - A programmable computer
  • Module II - Programming abstractions and analysis
    • 06 - Collections and functions
    • 07 - Efficiency
    • 08 - Recursion
  • Module III - Frontiers
    • 09 - Concurrency and parallelism
    • 10 - Machines that learn?

Course structure

  • Languages
    • Lectures in Finnish by Sanna Suoranta
    • Lectures in English by Lukas Ahrenberg
  • Course notes online (in English)
  • A+ exercises online (in English)
    • You may answer questions in English, Finnish, or Swedish
  • Workload for 5 credits (over 10 rounds)
    • 2 h / round lecture
    • 2 h / round independent study (course notes)
    • 9 h / round independent work (exercises)
    • 8 h exam (3h + preparation)

Course calendar

course-calendar-2026.svg

  • Previously recorded video material in case you miss the lecture (though lecture more up-to-date)
  • Exercise sessions: Mon-Fri (except public holidays, weekends, & exam week)

Exercises

  • Programming exercises and a few quizzes
  • Full-point deadline every week (starting Friday, February 27 at 18:00)
    • Except on exam week (deadlines visible in A+)
  • Late submissions (up to 70% of max points) allowed until May 31 for all rounds
    • Downside: no model solutions given out
  • No points given for submission after late deadline
  • Level A & B (Regular) and Level C (Challenge) exercises
    • Teaching Assistants mainly help with Level A & B exercises
    • Challenge exercises are harder, requires more time and work, but can be rewarding in the form of insights
    • It is possible to get to grade 5 level without Level C exercises

Solving exercises

  • Instructions can be found in the course note appendix
  • Template code link (zip file, sbt project) on A+ for every round
  • Submit your solutions to A+
  • We work mostly in Scala 3.3 (one round assembly programming)
  • Exercises as sbt projects
    • Visual Studio Code with the Scala Metals extension
    • IntelliJ with Scala plugin
  • Tips
    • Don't start with the exercises before you have studied the material
    • Read the A+ instructions, program code, and comments carefully
    • Most exercises come with tests. Use them!
    • Note that you only have a limited number of submissions on A+
  • Start early and Attend the exercise sessions!

Daily exercise sessions (at the start of the course)

  • Mon 16:15-18:00 (T8)
  • Tue 12:15-14:00 (T8)
  • Tue 14:15-16:00 (T8)
  • Tue 16:15-18:00 (T8)
  • Wed 12:15-14:00 (T8)
  • Wed 14:15-16:00 (T8)
  • Wed 16:15-18:00 (T8)
  • Thu 10:15-12:00 (T6)
  • Thu 12:15-14:00 (T6)
  • Thu 14:15-16:00 (T6)
  • Fri 08:15-10:00 (T6)
  • Fri 10:15-12:00 (T6)
  • Fri 12:15-14:00 (T6)
  • Fri 14:15-16:00 (T6)

  • On-campus in the CS building
  • Sign up for help using the Lab Queue (via A+)
  • The number of sessions will be reduced in May
  • Note: You will need an access token/app to get into the labs
  • Focus on the current round, but everyone is welcome!

Passing the course & Grades

  • Your grade is determined by your exercise score and exam
    • Grade 1: at least 1200 exercise points (of which at least 200 at Level B), and at least 5p on exam part I.
    • Grade 2: at least 1800 exercise points (of which at least 600 at Level B), and at least 5p on exam part I.
    • Grade 3: at least 2400 exercise points (of which at least 800 at Level B), and at least 5p on exam part I, and at least 50% on exam part II.
    • Grade 4: at least 3000 exercise points (of which at least 1000 at level B), and at least 5p on exam part I, and at least 65% on exam part II.
    • Grade 5: at least 3500 exercise points (of which at least 1300 at level B), and at least 5p on exam part I, and at least 80% on exam part II.
  • Pass the course by fulfilling at least the requirements for grade 1
  • Exam has two parts (More info on MyCo)
    • Part I has 10 "quick" questions, worth 1 point each. You will need to get at least 5 p. here
    • Part II has more in-depth questions with impact on grades 3–5.
  • Total available online exercise points is at least 6000 (Roughly 1550 A, 2450 B, 2000 C)

Possibility of bonus points

  • For every A+ round where you have provided feedback and received points for at least one exercise submission you are given 5 bonus Feedback points
  • If you provide the separate end of course feedback (published here in MyCourses), you will be awarded an extra 50 bonus points added to your total online exercise points at the time of grading

Collaboration and AI tools policy

  • The online exercises are personal and individual assignments
    • For instance, producing program code together, copying code from others, and making one's own solutions available to others (either privately or by posting to some shared or public forum) is not allowed
  • It is forbidden to use “artificial intelligence” (AI) to solve assignments in this course
    • In other words, you are not allowed to use tools that generate program code or other solutions to assignments
    • This is cheating, just like copying solutions from a human

You must submit only solutions produced by yourself. You do not have permission to enter course materials into AI tools or to otherwise distribute the materials to external parties.

That said – Please talk to each other!

  • You can, and should, of course discuss assignments and course material with each other! But, do it without the program codes related to the course exercises
  • To get help with your own code and the assignments: come to the exercise sessions!

Exam on May 21, 2026

  • You must sign up to the exam separately
    • in SISU, March 22, 2026 – May 14, 2026
  • "Pen and paper"
  • There will be a re-take exam arranged after the summer

Studying in Spring 2026

We follow the Aalto code of conduct and we maintain academic integrity!

Make sure to read and reflect on these documents.

Up-to-date information on MyCourses

157bfbd77a00a24e9e5b53d6134ef1b6-300.svg

These slides are an overview of information on MyCourses as of the start of the course (February 23, 2026).

Up-to-date information available in MyCourses.

Information & communication

Course start - checklist

  • [ ] Signed up in SiSU
  • [ ] Read course information on MyCourses
  • [ ] Have access token/app for the computer labs
  • [ ] Read the instructions
    • [ ] Able to start an IDE (in labs or on own computer)
    • [ ] Know how to work with the assignments
    • [ ] Know when/where the exercise sessions are held

Contact

Responsible Teachers
Lukas Ahrenberg (en/sv) and Sanna Suoranta (fi/en)
In person
Lectures, L01 in English (Lukas), L02 in Finnish (Sanna)
e-mail
lukas.ahrenberg@aalto.fi; sanna.suoranta@aalto.fi

Please note: Course staff will not be able to answer questions about exercises over email or direct messages. Please come to the exercise sessions if you need help!

Acknowledgements

This course is based on previous material developed by

  • Petteri Kaski,
  • Tommi Junttila,
  • Mikko Kivelä,
  • Shaofeng Jiang.