---
title: "9.7 Exercises"
url: https://una-lang.org/en/learn/loops/exercises/
release: v0.4.0
edition: "0.2"
---

# 9.7 Exercises

The exercises come with the chapter's download, `09-loops.tar.gz`, which holds every package of this chapter. Each exercise starts from `exercises/N/start`: the day-to-day program with the exercise's examples added, and a stub of the function to write, whose body gives back a placeholder. `una test .` fails there until the exercise is done. Run `una check .` as you go, and `una test .` until every example passes.

## 9.7.1 Exercise 1: the demand of the plan

::::exercise
Write `demand`, the units customers ask for over the weeks of the plan, at the product's weekly sales. The start package has the stub and two examples:

```una include="exercises/1/start/src/restock.una#demand" title="exercises/1/start/src/restock.una"
--- The units customers ask for over `weeks` weeks, at the product's weekly sales.
pub demand(product: Product, weeks: Int) -> Units = 0
```

```una include="exercises/1/start/src/restock.una#L169-L175" title="exercises/1/start/src/restock.una"
example "the lamp is asked for 20 times in four weeks"
  do demand(lamp(), weeks: 4)
  expect result == 20

example "no weeks, no demand"
  do demand(lamp(), weeks: 0)
  expect result == 0
```

:::hint
*Hint:* an accumulator that starts at 0 and adds `product.weekly_sales` at each turn of `1..weeks`. The result is a `Units`, so give the accumulator that type, or the loop forgets that it is never negative.
:::

Check your answer with `una test .`. On the start package it reports:

```text include="exercises/1/start/expected/commands/test.txt#L14-L19"
example restock "the lamp is asked for 20 times in four weeks" ... FAILED
  src/restock.una:171: expect result == 20
  it is false, with `result` = `0`
example restock "no weeks, no demand" ... ok

14 examples: 13 passed, 1 failed
```

:::answer Answer (*check, run, test*; `exercises/1/answer`).
A sum over the weeks, with a `Units` accumulator:

```diff include="exercises/1/start..exercises/1/answer:src/restock.una" context="1" title="src/restock.una"
--- a/src/restock.una
+++ b/src/restock.una
@@ -85,3 +85,8 @@
 --- The units customers ask for over `weeks` weeks, at the product's weekly sales.
-pub demand(product: Product, weeks: Int) -> Units = 0
+pub demand(product: Product, weeks: Int) -> Units
+=
+  let edit total: Units = 0
+  for week in 1..weeks
+    total = total + product.weekly_sales
+  total
 
```

The empty range of 0 weeks runs no turn, so `total` keeps 0, and the second example passes with no special case.
:::
::::

## 9.7.2 Exercise 2: how many orders

::::exercise
Write `orders_placed`, the number of weeks that end with an order, and add it to the last line of each product's plan, so that it reads `20 units ordered in 2 orders, 0 weeks short`. The start package has the stub and two examples:

```una include="exercises/2/start/src/restock.una#orders_placed" title="exercises/2/start/src/restock.una"
--- How many of the `weeks` weeks end with an order.
pub orders_placed(product: Product, weeks: Int) -> Int = 0
```

```una include="exercises/2/start/src/restock.una#L169-L175" title="exercises/2/start/src/restock.una"
example "the lamp orders in two of four weeks"
  do orders_placed(lamp(), weeks: 4)
  expect result == 2

example "the desk orders every week"
  do orders_placed(desk(), weeks: 4)
  expect result == 4
```

:::hint
*Hint:* walk the weeks as `units_ordered` does, with the same `stock` accumulator, and count, in a second accumulator, the turns whose order is above 0. In `print_plan`, name the count with a `let`, as `ordered` and `short` are, and put it in the text.
:::

Check your answer with `una test .`, then `una run .` for the new totals.

:::answer Answer (*check, run, test*; `exercises/2/answer`).
A count inside the walk of the weeks, and one more value on the last line:

```diff include="exercises/2/start..exercises/2/answer:src/restock.una" context="1" title="src/restock.una"
--- a/src/restock.una
+++ b/src/restock.una
@@ -85,3 +85,13 @@
 --- How many of the `weeks` weeks end with an order.
-pub orders_placed(product: Product, weeks: Int) -> Int = 0
+pub orders_placed(product: Product, weeks: Int) -> Int
+=
+  let edit stock: Units = product.on_hand
+  let edit orders = 0
+  for week in 1..weeks
+    stock = sell_week(stock, sales: product.weekly_sales)
+    let order = order_for(product, stock: stock)
+    if order > 0
+      orders = orders + 1
+    stock = stock + order
+  orders
 
@@ -108,3 +118,4 @@
   let short = weeks_short(product, weeks: weeks)
-  @console.print("  {ordered} units ordered, {short} weeks short")
+  let orders = orders_placed(product, weeks: weeks)
+  @console.print("  {ordered} units ordered in {orders} orders, {short} weeks short")
 
```

The stock must still be followed, since whether a week orders depends on what the weeks before left. The count is a plain `Int`, since its result needs no fact.
:::
::::

## 9.7.3 Exercise 3: the lowest stock

::::exercise
Write `lowest_stock`, the fewest units the shelf holds after a week's sales over the plan. With no weeks, it is the stock today. The stub gives back the stock today, which passes only the last of its three examples:

```una include="exercises/3/start/src/restock.una#lowest_stock" title="exercises/3/start/src/restock.una"
--- The fewest units the shelf holds after a week's sales, over `weeks` weeks: the stock
--- today when there are no weeks.
pub lowest_stock(product: Product, weeks: Int) -> Units = product.on_hand
```

```una include="exercises/3/start/src/restock.una#L170-L180" title="exercises/3/start/src/restock.una"
example "the lamp's shelf gets down to 4 units"
  do lowest_stock(lamp(), weeks: 4)
  expect result == 4

example "the desk's shelf runs empty"
  do lowest_stock(desk(), weeks: 4)
  expect result == 0

example "with no weeks, the lowest stock is the stock today"
  do lowest_stock(rug(), weeks: 0)
  expect result == 3
```

:::hint
*Hint:* two accumulators: the stock, as in `stock_after`, and the lowest value seen so far, which starts at the stock today. Look at the stock right after the week's sales, before the order arrives. The result is a `Units`, so the lowest value needs that type, and every value you assign it must be a `Units` too.
:::

Check your answer with `una test .`. On the start package it reports:

```text include="exercises/3/start/expected/commands/test.txt#L14-L22"
example restock "the lamp's shelf gets down to 4 units" ... FAILED
  src/restock.una:172: expect result == 4
  it is false, with `result` = `14`
example restock "the desk's shelf runs empty" ... FAILED
  src/restock.una:176: expect result == 0
  it is false, with `result` = `2`
example restock "with no weeks, the lowest stock is the stock today" ... ok

15 examples: 13 passed, 2 failed
```

:::answer Answer (*check, run, test*; `exercises/3/answer`).
Keep the lowest value in a `Units` accumulator, and lower it when the stock after the sales is below it:

```diff include="exercises/3/start..exercises/3/answer:src/restock.una" context="1" title="src/restock.una"
--- a/src/restock.una
+++ b/src/restock.una
@@ -86,3 +86,12 @@
 --- today when there are no weeks.
-pub lowest_stock(product: Product, weeks: Int) -> Units = product.on_hand
+pub lowest_stock(product: Product, weeks: Int) -> Units
+=
+  let edit stock: Units = product.on_hand
+  let edit lowest: Units = product.on_hand
+  for week in 1..weeks
+    stock = sell_week(stock, sales: product.weekly_sales)
+    if stock < lowest
+      lowest = stock
+    stock = stock + order_for(product, stock: stock)
+  lowest
 
```

`lowest = stock` proves itself, since `stock` is a `Units`. The check of the stock comes between the sales and the order, so the lamp's lowest is 4, the stock before its order arrives, and not 14.
:::
::::
