{
  "id": "P178",
  "slug": "fuel-economy",
  "key": "P178-fuel-economy",
  "title": "Fuel economy",
  "summary": "Two cars and a round trip: the van gaining 4 km/L saves more fuel than the sedan gaining 20, and 30 out and 60 back averages 40, not 45.",
  "entry": "main.eml",
  "ui": "terminal",
  "readme": "# P178 - Fuel economy\n\nA household's two cars and a round trip, and the scale that adds up.\n\n- `main.eml` - the menu, the cars, which swap saves more, the round trip,\n  a swap of your own and a trip of your own\n- `fuel.eml` - the cars, litres a year, litres saved, litres per 100 km,\n  the trip, and the average speed over two equal legs\n\nHow each part works:\n\n- Fuel used is distance divided by kilometres per litre; litres are kept\n  in tenths, from exact integer arithmetic rounded once.\n- As in the corpus case `the-van-gaining-four-saved-more-than-the-hybrid-gaining-twenty`,\n  each car drives 10,000 km a year: the van goes from 8 km/L to 12, the\n  sedan from 20 to 40. A gain of 20 is five times a gain of 4, and the\n  van's swap saves 416.7 litres a year to the hybrid's 250.0: a gain counts\n  most where km/L is lowest. In litres per 100 km, the scale that adds up,\n  the van gains 4.2 and the sedan 2.5.\n- As in `the-average-speed-was-the-mean-of-the-legs`, 60 km out at 30 km/h\n  and back at 60 is 120 km in 3 hours, 40.0 km/h, where the mean of the\n  speeds is 45.0: the slow leg takes longer. The average is never above\n  the mean, and equal only at one speed.\n\nWhat is checked: menu choices 1 to 6; 1 to 100000 km a year; 1 to 99 km\nper litre; speeds of 1 to 200 km/h. An empty answer cancels.\n\nSessions: `sessions/basic.in` gives:\n- the cars, the swaps and the round trip;\n- swaps of your own: 6 to 7 km/L over 15,000 km, and 30 to 25 over 10,000\n  (more fuel);\n- trips at 30 and 60, at 100 and 101 (the same to a tenth) and at 50 and\n  50.\n\n`sessions/bad-input.in` gives:\n- menu choices 0 and x;\n- km a year: a cancel, 0, 100001 and x; km/L: a cancel, 0, 100 and x,\n  twice; then 8 to 8 km/L;\n- speeds: a cancel, 0, 201 and x, then a cancel and 0 for the way back;\n- 1 and 200 km/h.\n\nBuilt on the verified corpus cases `the-van-gaining-four-saved-more-than-the-hybrid-gaining-twenty`\n(two cars and which replacement saves more fuel) and\n`the-average-speed-was-the-mean-of-the-legs` (30 km/h out, 60 back over the\nsame road).\n",
  "modules": [
    {
      "name": "main.eml",
      "eml": "# P178 fuel economy: two cars and a round trip - the cars, which swap saves\n# more fuel, the trip's average speed, a swap of your own and a trip of\n# your own.\nimport fuel\n\ndef plain(s):\n    0 => i\n    len(s) => j\n    while i < j and s[i] == \" \":\n        i + 1 => i\n    while j > i and s[j - 1] == \" \":\n        j - 1 => j\n    return s[i:j]\n\ndef pad(s, n):\n    s => out\n    while len(out) < n:\n        out + \" \" => out\n    return out\n\ndef lpad(s, n):\n    s => out\n    while len(out) < n:\n        \" \" + out => out\n    return out\n\ndef digits(t):\n    for c in t:\n        if c < \"0\" or c > \"9\":\n            return False\n    return len(t) > 0\n\ndef commas(n):\n    str(n) => s\n    \"\" => out\n    0 => k\n    len(s) => i\n    while i > 0:\n        if k == 3:\n            \",\" + out => out\n            0 => k\n        s[i - 1] + out => out\n        k + 1 => k\n        i - 1 => i\n    return out\n\ndef t1(t):\n    # tenths, t >= 0\n    return commas(int((t - t % 10) / 10)) + \".\" + str(t % 10)\n\ndef show_cars():\n    \"Two cars, each driven 10,000 km a year; the household can afford to replace one:\" ^0\n    for c in fuel.cars():\n        (\"  \" + pad(c[0], 7) + lpad(str(c[2]), 2) + \" km/L now, \" + lpad(str(c[3]), 2) + \" with its replacement: a gain of \" + str(c[3] - c[2]) + \" km/L\") ^0\n\ndef show_swaps():\n    \"Fuel a year, now and after each swap:\" ^0\n    (\"  \" + pad(\"car\", 7) + lpad(\"now\", 10) + lpad(\"after\", 10) + lpad(\"saved\", 10) + \"   L/100 km, now and after\") ^0\n    [] => s\n    for c in fuel.cars():\n        fuel.saved(c[1], c[2], c[3]) => v\n        s + [v] => s\n        (\"  \" + pad(c[0], 7) + lpad(t1(fuel.litres(c[1], c[2])) + \" L\", 10) + lpad(t1(fuel.litres(c[1], c[3])) + \" L\", 10) + lpad(t1(v) + \" L\", 10) + \"   \" + t1(fuel.per_100(c[2])) + \" and \" + t1(fuel.per_100(c[3]))) ^0\n    (\"A gain of 20 km/L is five times a gain of 4, and the van's swap saves \" + t1(s[0]) + \" litres a year to the hybrid's \" + t1(s[1]) + \". Fuel used is distance divided by km/L, so a gain counts most where km/L is lowest; in litres per 100 km the van gains \" + t1(fuel.tenths_of(100 * 4, 8 * 12)) + \" and the sedan \" + t1(fuel.tenths_of(100 * 20, 20 * 40)) + \".\") ^0\n\ndef show_trip():\n    fuel.trip() => t\n    (str(t[0]) + \" km out at \" + str(t[1]) + \" km/h and \" + str(t[0]) + \" km back at \" + str(t[2]) + \" km/h: \" + str(int(t[0] / t[1])) + \" hours out and \" + str(int(t[0] / t[2])) + \" back.\") ^0\n    (\"The mean of the two speeds is \" + t1(fuel.tenths_of(t[1] + t[2], 2)) + \" km/h. The trip's average is \" + str(2 * t[0]) + \" km in \" + str(int(t[0] / t[1]) + int(t[0] / t[2])) + \" hours, \" + t1(fuel.average(t[1], t[2])) + \" km/h: the slow leg takes longer, so it counts for more of the time.\") ^0\n\ndef number(prompt, lo, hi):\n    plain(input(prompt + \" (\" + str(lo) + \" to \" + str(hi) + \")> \")) => t\n    if t == \"\":\n        \"Cancelled.\" ^0\n        return 0 - 1\n    if len(t) > len(str(hi)) or not digits(t) or int(t) < lo or int(t) > hi:\n        (\"Type a whole number from \" + str(lo) + \" to \" + str(hi) + \".\") ^0\n        return 0 - 1\n    return int(t)\n\ndef own_swap():\n    number(\"km a year\", 1, 100000) => km\n    if km < 0:\n        return\n    number(\"km per litre now\", 1, 99) => a\n    if a < 0:\n        return\n    number(\"km per litre after\", 1, 99) => b\n    if b < 0:\n        return\n    (\"At \" + commas(km) + \" km a year: \" + t1(fuel.litres(km, a)) + \" litres now, \" + t1(fuel.litres(km, b)) + \" after; \" + t1(fuel.per_100(a)) + \" and \" + t1(fuel.per_100(b)) + \" litres per 100 km.\") ^0\n    if b > a:\n        (\"The swap saves \" + t1(fuel.saved(km, a, b)) + \" litres a year.\") ^0\n    elif b < a:\n        (\"The swap uses \" + t1(fuel.saved(km, b, a)) + \" litres a year more.\") ^0\n    else:\n        \"The swap changes nothing.\" ^0\n\ndef own_trip():\n    number(\"speed out, km/h\", 1, 200) => a\n    if a < 0:\n        return\n    number(\"speed back, km/h\", 1, 200) => b\n    if b < 0:\n        return\n    (\"Two legs of the same length at \" + str(a) + \" and \" + str(b) + \" km/h: the mean of the speeds is \" + t1(fuel.tenths_of(a + b, 2)) + \", the average speed \" + t1(fuel.average(a, b)) + \".\") ^0\n    if a == b:\n        \"At one speed they are the same.\" ^0\n    elif fuel.tenths_of(a + b, 2) == fuel.average(a, b):\n        \"Rounded to a tenth they look the same; the average is still the lower, as it always is: more of the time is spent on the slower leg.\" ^0\n    else:\n        \"The average is the lower, always: more of the time is spent on the slower leg.\" ^0\n\n\"== Fuel economy ==\" ^0\n\"Two cars, a round trip, and the scale that adds up.\" ^0\nTrue => running\nwhile running:\n    \"\" ^0\n    \"1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\" ^0\n    plain(input(\"choice> \")) => choice\n    if choice == \"1\":\n        show_cars()\n    elif choice == \"2\":\n        show_swaps()\n    elif choice == \"3\":\n        show_trip()\n    elif choice == \"4\":\n        own_swap()\n    elif choice == \"5\":\n        own_trip()\n    elif choice == \"6\":\n        False => running\n    else:\n        \"Pick a number from 1 to 6.\" ^0\n\"Bye.\" ^0\n",
      "python": "import fuel\n\ndef plain(s):\n    i = 0\n    j = len(s)\n    while i < j and s[i] == \" \":\n        i = i + 1\n    while j > i and s[j - 1] == \" \":\n        j = j - 1\n    return s[i:j]\n\ndef pad(s, n):\n    out = s\n    while len(out) < n:\n        out = out + \" \"\n    return out\n\ndef lpad(s, n):\n    out = s\n    while len(out) < n:\n        out = \" \" + out\n    return out\n\ndef digits(t):\n    for c in t:\n        if c < \"0\" or c > \"9\":\n            return False\n    return len(t) > 0\n\ndef commas(n):\n    s = str(n)\n    out = \"\"\n    k = 0\n    i = len(s)\n    while i > 0:\n        if k == 3:\n            out = \",\" + out\n            k = 0\n        out = s[i - 1] + out\n        k = k + 1\n        i = i - 1\n    return out\n\ndef t1(t):\n    return commas(int((t - t % 10) / 10)) + \".\" + str(t % 10)\n\ndef show_cars():\n    print(\"Two cars, each driven 10,000 km a year; the household can afford to replace one:\")\n    for c in fuel.cars():\n        print(\"  \" + pad(c[0], 7) + lpad(str(c[2]), 2) + \" km/L now, \" + lpad(str(c[3]), 2) + \" with its replacement: a gain of \" + str(c[3] - c[2]) + \" km/L\")\n\ndef show_swaps():\n    print(\"Fuel a year, now and after each swap:\")\n    print(\"  \" + pad(\"car\", 7) + lpad(\"now\", 10) + lpad(\"after\", 10) + lpad(\"saved\", 10) + \"   L/100 km, now and after\")\n    s = []\n    for c in fuel.cars():\n        v = fuel.saved(c[1], c[2], c[3])\n        s = s + [v]\n        print(\"  \" + pad(c[0], 7) + lpad(t1(fuel.litres(c[1], c[2])) + \" L\", 10) + lpad(t1(fuel.litres(c[1], c[3])) + \" L\", 10) + lpad(t1(v) + \" L\", 10) + \"   \" + t1(fuel.per_100(c[2])) + \" and \" + t1(fuel.per_100(c[3])))\n    print(\"A gain of 20 km/L is five times a gain of 4, and the van's swap saves \" + t1(s[0]) + \" litres a year to the hybrid's \" + t1(s[1]) + \". Fuel used is distance divided by km/L, so a gain counts most where km/L is lowest; in litres per 100 km the van gains \" + t1(fuel.tenths_of(100 * 4, 8 * 12)) + \" and the sedan \" + t1(fuel.tenths_of(100 * 20, 20 * 40)) + \".\")\n\ndef show_trip():\n    t = fuel.trip()\n    print(str(t[0]) + \" km out at \" + str(t[1]) + \" km/h and \" + str(t[0]) + \" km back at \" + str(t[2]) + \" km/h: \" + str(int(t[0] / t[1])) + \" hours out and \" + str(int(t[0] / t[2])) + \" back.\")\n    print(\"The mean of the two speeds is \" + t1(fuel.tenths_of(t[1] + t[2], 2)) + \" km/h. The trip's average is \" + str(2 * t[0]) + \" km in \" + str(int(t[0] / t[1]) + int(t[0] / t[2])) + \" hours, \" + t1(fuel.average(t[1], t[2])) + \" km/h: the slow leg takes longer, so it counts for more of the time.\")\n\ndef number(prompt, lo, hi):\n    t = plain(input(prompt + \" (\" + str(lo) + \" to \" + str(hi) + \")> \"))\n    if t == \"\":\n        print(\"Cancelled.\")\n        return 0 - 1\n    if len(t) > len(str(hi)) or not digits(t) or int(t) < lo or int(t) > hi:\n        print(\"Type a whole number from \" + str(lo) + \" to \" + str(hi) + \".\")\n        return 0 - 1\n    return int(t)\n\ndef own_swap():\n    km = number(\"km a year\", 1, 100000)\n    if km < 0:\n        return\n    a = number(\"km per litre now\", 1, 99)\n    if a < 0:\n        return\n    b = number(\"km per litre after\", 1, 99)\n    if b < 0:\n        return\n    print(\"At \" + commas(km) + \" km a year: \" + t1(fuel.litres(km, a)) + \" litres now, \" + t1(fuel.litres(km, b)) + \" after; \" + t1(fuel.per_100(a)) + \" and \" + t1(fuel.per_100(b)) + \" litres per 100 km.\")\n    if b > a:\n        print(\"The swap saves \" + t1(fuel.saved(km, a, b)) + \" litres a year.\")\n    elif b < a:\n        print(\"The swap uses \" + t1(fuel.saved(km, b, a)) + \" litres a year more.\")\n    else:\n        print(\"The swap changes nothing.\")\n\ndef own_trip():\n    a = number(\"speed out, km/h\", 1, 200)\n    if a < 0:\n        return\n    b = number(\"speed back, km/h\", 1, 200)\n    if b < 0:\n        return\n    print(\"Two legs of the same length at \" + str(a) + \" and \" + str(b) + \" km/h: the mean of the speeds is \" + t1(fuel.tenths_of(a + b, 2)) + \", the average speed \" + t1(fuel.average(a, b)) + \".\")\n    if a == b:\n        print(\"At one speed they are the same.\")\n    elif fuel.tenths_of(a + b, 2) == fuel.average(a, b):\n        print(\"Rounded to a tenth they look the same; the average is still the lower, as it always is: more of the time is spent on the slower leg.\")\n    else:\n        print(\"The average is the lower, always: more of the time is spent on the slower leg.\")\n\nprint(\"== Fuel economy ==\")\nprint(\"Two cars, a round trip, and the scale that adds up.\")\nrunning = True\nwhile running:\n    print(\"\")\n    print(\"1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\")\n    choice = plain(input(\"choice> \"))\n    if choice == \"1\":\n        show_cars()\n    elif choice == \"2\":\n        show_swaps()\n    elif choice == \"3\":\n        show_trip()\n    elif choice == \"4\":\n        own_swap()\n    elif choice == \"5\":\n        own_trip()\n    elif choice == \"6\":\n        running = False\n    else:\n        print(\"Pick a number from 1 to 6.\")\nprint(\"Bye.\")\n"
    },
    {
      "name": "fuel.eml",
      "eml": "# P178 fuel economy - a household's two cars and a round trip. As in the\n# corpus case the-van-gaining-four-saved-more-than-the-hybrid-gaining-twenty,\n# fuel used is distance divided by kilometres per litre, so a gain in km/L\n# saves the most where km/L is lowest: litres per 100 km is the scale that\n# adds up. And as in the-average-speed-was-the-mean-of-the-legs, the average\n# speed of a trip is its distance over its time, and over two equal legs that\n# is not the mean of the two speeds. Litres are kept in tenths, rounded\n# half-up, from exact integer arithmetic.\n\ndef cars():\n    # [name, km a year, km per litre now, km per litre of its replacement]\n    return [[\"van\", 10000, 8, 12], [\"sedan\", 10000, 20, 40]]\n\ndef tenths_of(num, den):\n    # num / den in tenths, rounded half-up\n    num * 20 + den => y\n    return int((y - y % (2 * den)) / (2 * den))\n\ndef litres(km, kmpl):\n    # litres a year, in tenths\n    return tenths_of(km, kmpl)\n\ndef saved(km, before, after):\n    # litres saved a year, in tenths: the exact difference, rounded once\n    return tenths_of(km * (after - before), before * after)\n\ndef per_100(kmpl):\n    # litres per 100 km, in tenths\n    return tenths_of(100, kmpl)\n\ndef trip():\n    # [km each way, speed out, speed back]\n    return [60, 30, 60]\n\ndef average(a, b):\n    # the average speed over two legs of equal length, in tenths of a km/h\n    return tenths_of(2 * a * b, a + b)\n",
      "python": "def cars():\n    return [[\"van\", 10000, 8, 12], [\"sedan\", 10000, 20, 40]]\n\ndef tenths_of(num, den):\n    y = num * 20 + den\n    return int((y - y % (2 * den)) / (2 * den))\n\ndef litres(km, kmpl):\n    return tenths_of(km, kmpl)\n\ndef saved(km, before, after):\n    return tenths_of(km * (after - before), before * after)\n\ndef per_100(kmpl):\n    return tenths_of(100, kmpl)\n\ndef trip():\n    return [60, 30, 60]\n\ndef average(a, b):\n    return tenths_of(2 * a * b, a + b)\n"
    }
  ],
  "sessions": [
    {
      "name": "bad-input",
      "input": "0\nx\n4\n\n4\n0\n4\n100001\n4\nx\n4\n100\n\n4\n100\n0\n4\n100\n100\n4\n100\nx\n4\n100\n8\n\n4\n100\n8\n0\n4\n100\n8\nx\n4\n100\n8\n8\n5\n\n5\n0\n5\n201\n5\nx\n5\n40\n\n5\n40\n0\n5\n1\n200\n6\n",
      "screen": "== Fuel economy ==\nTwo cars, a round trip, and the scale that adds up.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 0\nPick a number from 1 to 6.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> x\nPick a number from 1 to 6.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> \nCancelled.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 0\nType a whole number from 1 to 100000.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100001\nType a whole number from 1 to 100000.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> x\nType a whole number from 1 to 100000.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> \nCancelled.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 0\nType a whole number from 1 to 99.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 100\nType a whole number from 1 to 99.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> x\nType a whole number from 1 to 99.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 8\nkm per litre after (1 to 99)> \nCancelled.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 8\nkm per litre after (1 to 99)> 0\nType a whole number from 1 to 99.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 8\nkm per litre after (1 to 99)> x\nType a whole number from 1 to 99.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 100\nkm per litre now (1 to 99)> 8\nkm per litre after (1 to 99)> 8\nAt 100 km a year: 12.5 litres now, 12.5 after; 12.5 and 12.5 litres per 100 km.\nThe swap changes nothing.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> \nCancelled.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 0\nType a whole number from 1 to 200.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 201\nType a whole number from 1 to 200.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> x\nType a whole number from 1 to 200.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 40\nspeed back, km/h (1 to 200)> \nCancelled.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 40\nspeed back, km/h (1 to 200)> 0\nType a whole number from 1 to 200.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 1\nspeed back, km/h (1 to 200)> 200\nTwo legs of the same length at 1 and 200 km/h: the mean of the speeds is 100.5, the average speed 2.0.\nThe average is the lower, always: more of the time is spent on the slower leg.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 6\nBye.\n",
      "interpreter": "equal"
    },
    {
      "name": "basic",
      "input": "1\n2\n3\n4\n15000\n6\n7\n4\n10000\n30\n25\n5\n30\n60\n5\n100\n101\n5\n50\n50\n6\n",
      "screen": "== Fuel economy ==\nTwo cars, a round trip, and the scale that adds up.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 1\nTwo cars, each driven 10,000 km a year; the household can afford to replace one:\n  van     8 km/L now, 12 with its replacement: a gain of 4 km/L\n  sedan  20 km/L now, 40 with its replacement: a gain of 20 km/L\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 2\nFuel a year, now and after each swap:\n  car           now     after     saved   L/100 km, now and after\n  van     1,250.0 L   833.3 L   416.7 L   12.5 and 8.3\n  sedan     500.0 L   250.0 L   250.0 L   5.0 and 2.5\nA gain of 20 km/L is five times a gain of 4, and the van's swap saves 416.7 litres a year to the hybrid's 250.0. Fuel used is distance divided by km/L, so a gain counts most where km/L is lowest; in litres per 100 km the van gains 4.2 and the sedan 2.5.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 3\n60 km out at 30 km/h and 60 km back at 60 km/h: 2 hours out and 1 back.\nThe mean of the two speeds is 45.0 km/h. The trip's average is 120 km in 3 hours, 40.0 km/h: the slow leg takes longer, so it counts for more of the time.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 15000\nkm per litre now (1 to 99)> 6\nkm per litre after (1 to 99)> 7\nAt 15,000 km a year: 2,500.0 litres now, 2,142.9 after; 16.7 and 14.3 litres per 100 km.\nThe swap saves 357.1 litres a year.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 4\nkm a year (1 to 100000)> 10000\nkm per litre now (1 to 99)> 30\nkm per litre after (1 to 99)> 25\nAt 10,000 km a year: 333.3 litres now, 400.0 after; 3.3 and 4.0 litres per 100 km.\nThe swap uses 66.7 litres a year more.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 30\nspeed back, km/h (1 to 200)> 60\nTwo legs of the same length at 30 and 60 km/h: the mean of the speeds is 45.0, the average speed 40.0.\nThe average is the lower, always: more of the time is spent on the slower leg.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 100\nspeed back, km/h (1 to 200)> 101\nTwo legs of the same length at 100 and 101 km/h: the mean of the speeds is 100.5, the average speed 100.5.\nRounded to a tenth they look the same; the average is still the lower, as it always is: more of the time is spent on the slower leg.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 5\nspeed out, km/h (1 to 200)> 50\nspeed back, km/h (1 to 200)> 50\nTwo legs of the same length at 50 and 50 km/h: the mean of the speeds is 50.0, the average speed 50.0.\nAt one speed they are the same.\n\n1) the cars  2) which swap saves more  3) the round trip  4) a swap of your own  5) a trip of your own  6) quit\nchoice> 6\nBye.\n",
      "interpreter": "equal"
    }
  ],
  "builtOn": [
    {
      "slug": "the-van-gaining-four-saved-more-than-the-hybrid-gaining-twenty",
      "caseId": "986-the-van-gaining-four-saved-more-than-the-hybrid-gaining-twenty",
      "title": "The van gaining four saved more than the hybrid gaining twenty"
    },
    {
      "slug": "the-average-speed-was-the-mean-of-the-legs",
      "caseId": "792-the-average-speed-was-the-mean-of-the-legs",
      "title": "The average speed was the mean of the legs"
    }
  ],
  "updated": "2026-10-19"
}
