# Computer Science by Example

Welcome to Computer Science by Example! Here you can learn computer programming through a series of exercises. Each exercise is built on top of the previous applying a new concept.

To work on the exercises, you will need:

If you need help, read the Computer Science by Example book online.

If you are a lecturer/professor/instructor/teacher, you can use the collection of exercises provided here as part of your teaching material in a programming module/course. See the relevant question in the FAQ for details of how.

## Exercise Index

The exercise index is divided into volumes and chapters matching the Computer Science by Example book. After solving each of the exercises, you are invited to submit your solution to an automated online judge which will give you a score and track your progress through your very own user page. Try to get a 1/1 score before moving to the next exercise.

The online judge supports solutions in: Python, C, Haskell, C++, C#, Java, JavaScript, Lua, Ruby, Racket or Scheme.

## Volume 1: Programming and Algorithms

This first volume of exercises introduces programming and algorithms.

### 3. Programming Basics

Exercises in this chapter introduce basic programming concepts, such as: printing and reading from standard I/O, functions, number operations, conditionals and repetition. Beginner programmers can use this chapter to learn their first programming language. Experienced programmers can use this chapter to learn a new programming language.

• hello — writing to the screen
• cscx — writing to the screen
• repeat1 — reading and writing of integers
• hi1 — reading and writing of strings of characters
• age1 — reading and writing of integers and strings of characters
• swap1 — reading and writing of integers and strings of characters
• triple1 — functions and integer operations
• inc1 — functions and integer operations
• add1 — functions and integer operations
• mult1 — functions and integer operations
• box1 — functions and integer operations
• oddeven1 — conditionals (if)
• order1 — conditionals (if)
• good1 — conditionals (if-else-if)
• signum1 — conditionals (if-else-if)
• triangle1 — a geometry challenge
• factorial1 — recursion
• power1 — recursion
• fibonacci1 — recursion
• hello2 — repetition and loops
• total — repetition and loops
• countdown1 — repetition and loops
• seq1 — repetition and loops

### 4. Programming

Exercises in this chapter involve fundamental programming concepts. Some exercises are more challenging versions of exercises from previous chapter. Beginner programmers can use this chapter to improve their programming skills. Experienced programmers can use this chapter while learning a new programming language.

• repeat — repetition, loops and detecting EOF
• hi — reading from standard input
• age — reading and writing variables
• swap — reading and writing variables
• owes — reading and writing variables
• triple — functions and integer operations
• inc — functions and integer operations
• add — functions and integer operations
• mult — functions and integer operations
• pi — floating point numbers
• box — floating point numbers
• oddeven — conditionals (if)
• order — conditionals (if)
• good — conditionals (if-else-if)
• bmi — conditionals (if-else-if)
• triangle — a challenge
• calc — conditionals (case)
• factorial — recursion
• power — recursion
• fibonacci — recursion
• index-string — string indexing
• repeat-list — lists
• index-ints — list indexing
• replace — list modification

### 5. Mathematical Foundations

Exercises in this chapter involve the mathematical foundations of Computer Science: positional numeral systems, logic and boolean algebra, sets and relations. These exercises can be used both as an introduction to these topics or as additional exercises by students working through an academic module.

### 6. Algorithms

Exercises in this chapter involve the implementation of some common and basic algorithms.

• gcd — greatest common divisor
• lcm — least common multiple
• roundiv — rounded division of integers
• primes — finding primes
• cash — minimizing banknotes, change-making problem
• max-subarray — maximum subarray
• sorting — sorting arrays

### 7. Computer Foundations

Exercises in this chapter involve practical computer foundations used in system’s programming.

### Bonus exercises

Email: rudy [at] cscx [dot] org

I am interested in publishing a print book and ebook following the exercises presented here. The first 4 chapters are currently freely available online. If you are an editor and like the idea, contact me at rudy [at] cscx [dot] org.

If you find errors on the website like broken links, typos or incorrect/ambiguous exercise descriptions, feel free to mail me at rudy [at] cscx [dot] org.

The exercises here assume secondary education knowledge (High School / ages 17 and up). However, younger students who have not completed secondary education but are interested in programming may also benefit from these exercises (ages 13 and up).

If you are a lecturer/professor/instructor/teacher, you can use the collection of exercises provided here as part of your teaching material in a programming module/course. See the relevant question in the FAQ for details of how.

CScx is not the first of its kind: there are other websites with online programming exercises dating back to at least 1995. However, most online judges are focused on competitive programming problems. Here on CScx the focus is rather on introducing programming step by step with exercises of incremental difficulty.

More exercises will be added in the future, for example, on the topics of data structures and graphs. I may potentially release the exercise test sets for educational use in the future. This way, teachers, lecturers and professors teaching programming and CS modules will be able to run their own exercise sets.