‹ back to the board

Wednesday, November 4

Apprenticelogic & control flow

The Ember-Hawk of Marrowfen Rookery

The falconers of Marrowfen Rookery keep an ember-hawk that is loosed each dusk to scout the bog-roads. The Rookery's perches are numbered with positive integers. Perch 1 is the falconer's glove. Wherever the hawk lands, it chooses its next perch by an unbreakable rule. The guild will pay for a written record of its flight home.

From perch n, the hawk's next perch is:

1. n / 3, if n is divisible by 3; 2. otherwise n / 2, if n is even; 3. otherwise n + 1.

The rules are checked in that order, and the first one that applies is used. The hawk keeps flying until it lands on perch 1, and then it stops.

Write hawkFlight(start), where start is a positive integer (at least 1) naming the perch the hawk is loosed from. Return a list of integers: every perch the hawk occupies, in order. The list begins with start and ends with 1. If start is 1, the hawk is already on the glove, so return a list containing only 1.

  • iterative sequence generation
  • Apprentice

examples

  1. case 1start of 7[7, 8, 4, 2, 1]. 7 is odd and not divisible by 3, so the hawk goes to 8, and then halves its way down.
  2. case 2start of 6[6, 2, 1]. 6 is even, but it is divisible by 3, and that rule is checked first.
  3. case 3start of 1[1]. The hawk is already on the glove.
reward
220 XP
signature
hawkFlight
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • already on the glove
  • odd start climbs then halves
  • divisible by three beats even
  • mixed rules from ten
  • single halving
  • single third
  • long flight from twenty-five
  • powers of three
  • thirty-five steps up first
  • from a hundred
The Ember-Hawk of Marrowfen Rookery · bitwick