San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
**Assignment #3: Watering Plans**
SUGGESTED SOLUTION
"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a7852c7a-2f2f-4884-b87c-e1bd9ea74621",
"metadata": {},
"outputs": [],
"source": [
"# Constqnts\n",
"NEAR_PLAN = 1\n",
"FAR_PLAN = 2\n",
"UNITS_PER_CAN = 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b545c300-910d-495a-b73e-d34809885357",
"metadata": {},
"outputs": [],
"source": [
"def plan_name(plan):\n",
" \"\"\"\n",
" Return the name of the plan.\n",
" \"\"\"\n",
" return 'NEAR' if plan == NEAR_PLAN else 'FAR'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "771c6ad1-5446-494b-9b94-5f4eb7684b1b",
"metadata": {},
"outputs": [],
"source": [
"def print_header(plan, plant_count):\n",
" \"\"\"\n",
" Print the header for the plan and its plant count.\n",
" \"\"\"\n",
" print()\n",
" header = f'Plan {plan_name(plan)} with {plant_count} plants'\n",
" \n",
" print('='*len(header))\n",
" print(header)\n",
" print('='*len(header))\n",
" print()\n",
" print('Where Steps Water units Step-units')\n",
" print('-------- ----- ----------- ----------')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16827aca-1a3a-4cf9-bc1f-06d67c1a17c5",
"metadata": {},
"outputs": [],
"source": [
"def print_trailer(plan, steps, step_units):\n",
" \"\"\"\n",
" Print the trailer for the plan, including the calculated\n",
" numbers of steps and step-units.\n",
" \"\"\"\n",
" print()\n",
" print(f'Plan {plan_name(plan)}: Total steps = {steps}, '\n",
" f'total step units = {step_units}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fdf2b0e4-b1f1-47eb-afd8-9cd5f65e679e",
"metadata": {},
"outputs": [],
"source": [
"def print_location(where, steps, water_units, step_units):\n",
" \"\"\"\n",
" Print where you are. Include the current numbers\n",
" of steps, water units, and step-units.\n",
" \"\"\"\n",
" print(f'{where:8}{steps:6}{water_units:9}{step_units:13}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d15a6417-1cbd-4319-bade-7156b8a52dab",
"metadata": {},
"outputs": [],
"source": [
"def do_round(plan, unwatered_count, steps, step_units):\n",
" \"\"\"\n",
" Do one round of the watering plan with the current unwatered_count \n",
" of plants and the current numbers of steps and step-units.\n",
" 1. Fill the can at the faucet.\n",
" 2. NEAR_PLAN: Walk to the nearest unwatered plant.\n",
" FAR_PLAN: Walk to the farthest unwatered plant.\n",
" 3. NEAR_PLAN: Loop to water plants while walking away from the faucet.\n",
" FAR PLAN: Loop to water plants while walking back towards the faucet. \n",
" Each loop waters one plant. Keep track of the remaining water_units \n",
" and the steps and step_units. Loop until either the can is empty \n",
" or all the plants are watered.\n",
" 4. Walk back to the faucet with an empty or partially filled can.\n",
" 5. Return the new unwatered_count of plants and\n",
" the new values of current steps and step-units.\n",
" \"\"\" \n",
" # 1. Fill the can at the faucet.\n",
" water_units = UNITS_PER_CAN\n",
"\n",
" # 2. NEAR_PLAN: Walk to the nearest unwatered plant with a full can.\n",
" # FAR_PLAN: Walk to the farthest unwatered plant with a full can.\n",
" current_plant = (plant_count - unwatered_count + 1) if plan == NEAR_PLAN \\\n",
" else unwatered_count\n",
" steps += current_plant;\n",
" step_units += current_plant*water_units\n",
"\n",
" round_done = False\n",
"\n",
" # 3. Loop to water plants.\n",
" while not round_done:\n",
" print_location(f'Plant {current_plant}', steps, water_units, step_units)\n",
"\n",
" water_units -= 1\n",
" unwatered_count -= 1\n",
" round_done = (water_units == 0) or (unwatered_count == 0)\n",
"\n",
" if not round_done:\n",
" # NEAR_PLAN: Walk away from the faucet to the next unwatered plant.\n",
" # FAR_PLAN: Walk back towards the faucet to the next unwatered plant.\n",
" direction = 1 if plan == NEAR_PLAN else -1\n",
" current_plant += direction\n",
" \n",
" steps += 1\n",
" step_units += water_units\n",
" \n",
" # 4. Walk back to the faucet.\n",
" steps += current_plant\n",
" step_units += current_plant*water_units\n",
" print_location('FAUCET', steps, water_units, step_units)\n",
" \n",
" # 5. Return the new values of unwatered_count, steps, and step-units.\n",
" return unwatered_count, steps, step_units"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dae01fd7-a021-4743-b3a0-b3bf79538a9c",
"metadata": {},
"outputs": [],
"source": [
"def execute_plan(plan, plant_count):\n",
" \"\"\"\n",
" Execute the watering plan with plant_count plants.\n",
" Return the number of steps and step units.\n",
" \"\"\"\n",
" print_header(plan, plant_count)\n",
" \n",
" steps = 0\n",
" step_units = 0\n",
" unwatered_count = plant_count\n",
" \n",
" # Loop once per round until all the plants are watered.\n",
" while unwatered_count > 0:\n",
" unwatered_count, steps, step_units = \\\n",
" do_round(plan, unwatered_count, steps, step_units)\n",
" \n",
" print_trailer(plan, steps, step_units)\n",
" return steps, step_units"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2628eac6-e224-44b9-b5bb-3f1e0eaf85c2",
"metadata": {},
"outputs": [],
"source": [
"for plant_count in range(5, 11):\n",
" \n",
" # Execute the near and far plans.\n",
" near_steps, near_step_units = execute_plan(NEAR_PLAN, plant_count)\n",
" far_steps, far_step_units = execute_plan(FAR_PLAN, plant_count)\n",
" \n",
" print()\n",
" print(f'*** With {plant_count} plants, ', end='')\n",
" \n",
" difference = abs(near_step_units - far_step_units)\n",
" \n",
" # Decide which plan, if any, is better.\n",
" if near_step_units < far_step_units:\n",
" print(f'the NEAR plan is better with {difference} fewer step-units.')\n",
" elif far_step_units < near_step_units:\n",
" print(f'the FAR plan is better with {difference} fewer step-units.')\n",
" else:\n",
" print('both plans have the same number of step-units.')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "625faaec-92e6-4a2e-9bc4-4da527cdc67c",
"metadata": {},
"outputs": [],
"source": [
"# (C) Copyright 2023 by Ronald Mak"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}