APDR

Computer Exercise 0: Practice solutions

Algorithms, Programming, and Data Representation · Module 92313 · Exercise 0

Practice · not course-graded

Exercise 0 is a practice workflow check. Use it to verify your submission setup; it is not part of the course grade. Try each task before opening the worked steps.

Task 1: Return the exact greeting

Task 01 · hello()

Try first: write the function first, then compare its returned string character for character.

Attempted it? Open the worked steps

Task 01 · hello() · worked solution

  1. The contract is a zero-argument function returning one exact string. There is no input to transform.

  2. Return the required literal; do not print it, because printing produces None as the function result.

  3. Call the function and compare the value, including capitalization, comma, and exclamation mark.

  4. The whole function:

    def hello():
        return "Hello, World!"

Task 2: Add two numbers

Task 02 · add_two_numbers(a, b)

Try first: identify the return type implied by the examples and consider negative and decimal inputs.

Attempted it? Open the worked steps

Task 02 · add_two_numbers(a, b) · worked solution

  1. The required operation is ordinary addition. Keep the parameters generic so Python preserves its numeric behavior for integers and floats.

  2. Return a + b; avoid converting to int, which would discard a fractional part.

  3. Check that add_two_numbers(2, 3) returns 5, then check a negative value and a float.

  4. The whole function:

    def add_two_numbers(a, b):
        return a + b

Task 3: Square a number

Task 03 · square(number)

Try first: express “square” as multiplication and check a negative input.

Attempted it? Open the worked steps

Task 03 · square(number) · worked solution

  1. A square is the number multiplied by itself; it is not the square root and it is nonnegative for real inputs.

  2. Return number * number.

  3. Check square(4) = 16 and square(-3) = 9.

  4. The whole function:

    def square(number):
        return number * number

Task 4: Test whether a number is even

Task 04 · is_even(number)

Try first: use the remainder after division by two.

Attempted it? Open the worked steps

Task 04 · is_even(number) · worked solution

  1. An integer is even exactly when its remainder modulo 2 is zero. Python’s modulo rule also handles negative integers consistently.

  2. Return the Boolean comparison number % 2 == 0. Do not return the strings "True" or "False".

  3. Check 4 gives True, 5 gives False, and -2 gives True.

  4. The whole function:

    def is_even(number):
        return number % 2 == 0

Task 5: Build a personalized greeting

Task 05 · greet(name)

Try first: use string formatting and preserve the supplied name exactly.

Attempted it? Open the worked steps

Task 05 · greet(name) · worked solution

  1. The output has a fixed prefix, the name, and a final exclamation mark. No punctuation is added to the name itself.

  2. Use an f-string to construct and return the full text.

  3. Check greet("Ada") returns exactly "Hello, Ada!" and does not print to the terminal.

  4. The whole function:

    def greet(name):
        return f"Hello, {name}!"

Personal worked-solution guide based on the APDR Computer Exercise 0 task sheet, rewritten in my own words. It is not an official FAU publication. Exercise 0 is practice; Exercises 1–2 are scored tasks. Use the samples and dataset from your own assigned repository where values are personalized. Made by Thanate Amornjiraporn (Focus).