Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ProgrammingHero1/100-plus-python-coding-problems-with-solutions
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: sjasthi/100-plus-python-coding-problems-with-solutions
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 6 commits
  • 3 files changed
  • 1 contributor

Commits on Oct 2, 2022

  1. Created using Colaboratory

    sjasthi committed Oct 2, 2022
    Copy the full SHA
    dcbe75b View commit details
  2. Created using Colaboratory

    sjasthi committed Oct 2, 2022
    Copy the full SHA
    a069889 View commit details

Commits on May 8, 2023

  1. Copy the full SHA
    804a371 View commit details
  2. Copy the full SHA
    8a3bf3f View commit details
  3. Copy the full SHA
    857bb2f View commit details

Commits on Nov 5, 2023

  1. Created using Colaboratory

    sjasthi committed Nov 5, 2023
    Copy the full SHA
    f937fcd View commit details
Showing with 2,325 additions and 0 deletions.
  1. +650 −0 CSCI 1101_Lab7_Solution
  2. +367 −0 ch_4_while_loops.ipynb
  3. +1,308 −0 python101_2023_gas_prices_project.ipynb
650 changes: 650 additions & 0 deletions CSCI 1101_Lab7_Solution
Original file line number Diff line number Diff line change
@@ -0,0 +1,650 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true,
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/sjasthi/100-plus-python-coding-problems-with-solutions/blob/master/CSCI%201101_Lab7_Solution\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RzxTmUSMGOms"
},
"outputs": [],
"source": [
"#@title 1.First_last6\n",
"#https://codingbat.com/prob/p181624\n",
"\n",
"def first_last6(nums):\n",
" if nums[-1]==6:\n",
" return True\n",
" if nums[0]==6:\n",
" return True\n",
" else:\n",
" return False\n",
"\n"
]
},
{
"cell_type": "code",
"source": [
"#@title 1.First_last6 (one liner)\n",
"#https://codingbat.com/prob/p181624\n",
"\n",
"def first_last6(nums):\n",
" return (nums[-1] == 6) or (nums[0] == 6)\n",
"\n",
" # True or False --> True\n",
" # False or True --> True\n",
" # True or True --> True\n",
" # False or False --> False\n"
],
"metadata": {
"id": "pHCvH4NFUS2N"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 2.Same First and Last\n",
"#https://codingbat.com/prob/p179078\n",
"def same_first_last(nums):\n",
" if nums == []:\n",
" return False\n",
" if nums[-1] == nums[0]:\n",
" return True\n",
" else:\n",
" return False\n"
],
"metadata": {
"id": "E8GopYWMHZkH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 2.Same First and Last (one liner)\n",
"#https://codingbat.com/prob/p179078\n",
"# Short Circuit Evaluation\n",
"def same_first_last(nums):\n",
" #return (nums[-1] == nums[0]) and (len(nums) >=1) # will get index out of range error\n",
" return (len(nums) >=1) and (nums[-1] == nums[0]) # will work due to short-circuit evaluation\n",
"\n",
""
],
"metadata": {
"id": "aPPTYHBmX0be"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 3.make_pi\n",
"https://codingbat.com/prob/p113659\n",
"def make_pi():\n",
" return [3,1,4]\n"
],
"metadata": {
"id": "s3NPefewIgVd"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 3.make_pi (one liner)\n",
"https://codingbat.com/prob/p113659\n",
"def make_pi():\n",
" return [3,1,4]"
],
"metadata": {
"id": "bv3V0Td3vHyH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 4.common_end\n",
"#https://codingbat.com/prob/p147755\n",
"def common_end(a, b):\n",
" if a[-1] == b[-1]:\n",
" return True\n",
" if a[0] == b[0]:\n",
" return True\n",
"#if it comes here, it will return false\n",
" return False"
],
"metadata": {
"id": "wt0-ymBkJf9O"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 4.common_end (one liner)\n",
"#https://codingbat.com/prob/p147755\n",
"def common_end(a, b):\n",
" return a[-1] == b[-1] or a[0] == b[0]"
],
"metadata": {
"id": "fEcH9EcGmpKn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 5.sum3\n",
"#https://codingbat.com/prob/p191645\n",
"def sum3(nums):\n",
" nums_sum = sum(nums)\n",
" return nums_sum"
],
"metadata": {
"id": "wOTVjb5sJ9bc"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 5.sum3 (one liner)\n",
"#https://codingbat.com/prob/p191645\n",
"def sum3(nums):\n",
" return sum(nums)"
],
"metadata": {
"id": "4eo-sAvKvQVu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 6.rotate_left3\n",
"#https://codingbat.com/prob/p148661\n",
"def rotate_left3(nums):\n",
" nums_list = [nums[1], nums[2], nums[0]]\n",
" return nums_list\n",
"#how can we enhance this program so it works for input lists of any length"
],
"metadata": {
"id": "wgD1sYBtKrG7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 6.rotate_left3 (one liner)\n",
"#https://codingbat.com/prob/p148661\n",
"def rotate_left3(nums):\n",
" return [nums[1], nums[2], nums[0]]\n"
],
"metadata": {
"id": "ireqo5ANm3TO"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 7.reverse3\n",
"#https://codingbat.com/prob/p192962\n",
"def reverse3(nums):\n",
" nums.reverse()\n",
" return nums"
],
"metadata": {
"id": "q_kSri3gL-3X"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 7.reverse3 (one liner)\n",
"#https://codingbat.com/prob/p192962\n",
"def reverse3(nums):\n",
" return nums.reverse()"
],
"metadata": {
"id": "738rgw5tvY84"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 8.max_end\n",
"#https://codingbat.com/prob/p135290\n",
"def max_end3(nums):\n",
" if nums[0] > nums[-1]:\n",
" max_element = nums[0]\n",
" else:\n",
" max_element = nums[-1]\n",
" max_list = [max_element, max_element, max_element]\n",
" return max_list"
],
"metadata": {
"id": "jXa86_44MlpK"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 8.max_end (one liner)\n",
"#https://codingbat.com/prob/p135290\n",
"def max_end3(nums):\n",
" return [max(nums[0], nums[-1])] * 3"
],
"metadata": {
"id": "RqwlvL4Vn9rv"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 9.sum2\n",
"#https://codingbat.com/prob/p192589\n",
"def sum2(nums):\n",
" if len(nums) == 0:\n",
" return 0\n",
" if len(nums) < 2:\n",
" return sum(nums)\n",
" else:\n",
" return nums[0] + nums[1]\n",
"\n"
],
"metadata": {
"id": "zXYF9PsuPnc3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 9.sum2 (one liner)\n",
"#https://codingbat.com/prob/p192589\n",
"def sum2(nums):\n",
" return 0 if len(nums) == 0 else sum(nums) if len(nums) < 2 else nums[0] + nums[1]\n"
],
"metadata": {
"id": "Bdx1EqStgJf4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 10.middle_way\n",
"#https://codingbat.com/prob/p171011\n",
"def middle_way(a, b):\n",
" mid_a = a[1]\n",
" mid_b = b[1]\n",
" new_list = [mid_a, mid_b]\n",
" return new_list\n",
"\n",
"\n"
],
"metadata": {
"id": "QRn96bXuRBd3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 10.middle_way (one liner)\n",
"#https://codingbat.com/prob/p171011\n",
"def middle_way(a, b):\n",
" return [a[1], b[1]]"
],
"metadata": {
"id": "PywVAJk4vrl4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 11.make_ends\n",
"#https://codingbat.com/prob/p124806\n",
"def make_ends(nums):\n",
" end_1 = nums[0]\n",
" end_2 = nums[-1]\n",
" new_list = [end_1, end_2]\n",
" return new_list"
],
"metadata": {
"id": "Hg7K9x9KRjZr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 11.make_ends (one liner)\n",
"#https://codingbat.com/prob/p124806\n",
"def make_ends(nums):\n",
" return [nums[0], nums[-1]]"
],
"metadata": {
"id": "ME2SEq9sv4vF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 12.has23\n",
"#https://codingbat.com/prob/p177892\n",
"def has23(nums):\n",
" if 2 in nums:\n",
" return True\n",
" if 3 in nums:\n",
" return True\n",
" return False\n"
],
"metadata": {
"id": "UhNbnwaKSC5A"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 12.has23 (one liner)\n",
"#https://codingbat.com/prob/p177892\n",
"def has23(nums):\n",
" return True if 2 in nums or 3 in nums else False"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2wjvbHd7gy1Q",
"outputId": "d482756d-d203-4a75-f494-624c2bf151c5"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"x is 10\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"#@title 13.counts_even\n",
"#https://codingbat.com/prob/p189616\n",
"def count_evens(nums):\n",
" evens_count = 0\n",
" for element in nums:\n",
" if element % 2 == 0:\n",
" evens_count = evens_count + 1\n",
"\n",
" return evens_count\n"
],
"metadata": {
"id": "_skd3DuDS10J"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 13.counts_even (one liner)\n",
"#https://codingbat.com/prob/p189616\n",
"def count_evens(nums):\n",
" return len([x for x in nums if x % 2 == 0])"
],
"metadata": {
"id": "3H-1xenAiLYi"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 14.big_diff\n",
"#https://codingbat.com/prob/p184853\n",
"def big_diff(nums):\n",
" difference = max(nums) - min(nums)\n",
" return difference"
],
"metadata": {
"id": "nrXkcB8mUv-H"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 14.big_diff (one liner)\n",
"#https://codingbat.com/prob/p184853\n",
"def big_diff(nums):\n",
" return max(nums) - min(nums)"
],
"metadata": {
"id": "ctr-OvqAwJEW"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 15.centered_average\n",
"#https://codingbat.com/prob/p126968\n",
"def centered_average(nums):\n",
" nums.remove(max(nums))\n",
" nums.remove(min(nums))\n",
" nums_sum = sum(nums)\n",
" average = nums_sum / len(nums)\n",
" return average\n",
""
],
"metadata": {
"id": "cHD0AbHbVhzY"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 15.centered_average (one liner)\n",
"#https://codingbat.com/prob/p126968\n",
"def centered_average(nums):\n",
" return sum(sorted(nums)[1:-1]) / (len(nums)-2)\n",
""
],
"metadata": {
"id": "WMD0HNtnmYR4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 16.sum13\n",
"#https://codingbat.com/prob/p167025\n",
"\n",
"def sum13(nums):\n",
" i = 0\n",
" total = 0\n",
"\n",
" while i < len(nums):\n",
" current_number = nums[i]\n",
" if current_number == 13:\n",
" i = i + 2\n",
" continue\n",
" total = total + current_number\n",
" i = i + 1\n",
"\n",
" return total"
],
"metadata": {
"id": "JMRjMvciWkcy"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 16.sum13 (one liner)\n",
"#https://codingbat.com/prob/p167025\n",
"def sum13(nums):\n",
" return sum(nums[i] for i in range(len(nums)) if nums[i] != 13 and (i == 0 or nums[i-1] != 13))\n"
],
"metadata": {
"id": "C7wLmv8royLg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 17.sum67\n",
"#https://codingbat.com/prob/p108886\n",
"def sum67(nums):\n",
"\n",
" total = 0\n",
" skip_flag = False\n",
" for i in range(len(nums)):\n",
" current_number = nums[i]\n",
" if current_number == 6:\n",
" skip_flag = True\n",
" continue\n",
"\n",
" if current_number == 7:\n",
" if skip_flag == True:\n",
" skip_flag = False\n",
" continue\n",
"\n",
" if skip_flag == False:\n",
" total = total + current_number\n",
"\n",
" return total\n",
""
],
"metadata": {
"id": "AvaUGzKq26Jj"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 17.sum67 (one liner - not working)\n",
"#https://codingbat.com/prob/p108886\n",
"def sum67(nums):\n",
" return sum(nums[i] for i in range(len(nums)) if not (6 <= nums[i] <= 7) or (nums[i] == 6 and i < len(nums)-1 and nums[i+1] == 7))\n"
],
"metadata": {
"id": "qUKiTt5-qQ8X"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "yheDyenxqQ-O"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"#@title 18.has22\n",
"#https://codingbat.com/prob/p119308\n",
"def has22(nums):\n",
" length = len(nums)\n",
"\n",
" for i in range(length):\n",
" current_number = nums[i]\n",
"\n",
" if current_number == 2:\n",
" if i == length -1: # already at the end of the list\n",
" continue\n",
" i = i + 1\n",
" next_number = nums[i]\n",
" if next_number == 2:\n",
" return True\n",
"\n",
" return False"
],
"metadata": {
"id": "2WHyrTek26Wd"
},
"execution_count": null,
"outputs": []
}
]
}
367 changes: 367 additions & 0 deletions ch_4_while_loops.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,367 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPs/xaIgnXS0KLEnKKFfvRH",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/sjasthi/100-plus-python-coding-problems-with-solutions/blob/master/ch_4_while_loops.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Chapter 4: Repetition Structures\n",
"## Loops\n",
"### While loops\n"
],
"metadata": {
"id": "8CS7mRXRCHJr"
}
},
{
"cell_type": "code",
"source": [
"# we can not brute force the code to repeat a programming statement.\n",
"# we got to be smarter in doing the repititions.\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")\n",
"print(\"testing\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JAo-WnV7DQmr",
"outputId": "d4da6011-0801-4b27-b138-2f0cbdfb0bd4"
},
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n",
"testing\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Basic example of while loop\n",
"n = 5\n",
"while (n > 0) :\n",
" print(n)\n",
" n = n-1\n",
"print(\"Take Off!\")\n",
"print(n)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HSbSycexG7IK",
"outputId": "bc3028b6-a1ca-4c4e-c18d-adf9529b1bf4"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"5\n",
"4\n",
"3\n",
"2\n",
"1\n",
"Take Off!\n",
"0\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"### 4.1 The Bug Collector Problem\n",
"A bug collector collects bugs every day for five days. Write a program that keeps a running total\n",
"of the number of bugs collected during the five days. The loop should ask for the number of\n",
"bugs collected for each day, and when the loop is finished, the program should display the total\n",
"number of bugs collected."
],
"metadata": {
"id": "3HVcaXirKiMs"
}
},
{
"cell_type": "code",
"source": [
"day_count = 1\n",
"total_bugs = 0\n",
"\n",
"# while (day_count < 6): # ok\n",
"# while (day_count <= 5): # ok\n",
"# while (day_count < 5): # not ok; One-off defect\n",
" \n",
"while (day_count <= 5): \n",
" print(\"Day Number: \", day_count)\n",
" bug_count = input(\"Enter the bugs you collected: \")\n",
" bug_count = int(bug_count)\n",
" total_bugs = total_bugs + bug_count\n",
" day_count = day_count + 1\n",
" \n",
"# output\n",
"print(\"Total number of bugs collected: \", total_bugs)"
],
"metadata": {
"id": "OGEHZllNKhiU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# With minor enhancement\n",
"# FInal version for programming assignment 2.1\n",
"day_count = 1\n",
"total_bugs = 0\n",
"\n",
"# while (day_count < 6): # ok\n",
"# while (day_count <= 5): # ok\n",
"# while (day_count < 5): # not ok; One-off defect\n",
" \n",
"while (day_count <= 5): \n",
" #print(\"Day Number: \", day_count)\n",
" bug_count = input(\"Enter the bugs you collected on day \" + str(day_count) + \": \")\n",
" bug_count = int(bug_count)\n",
" total_bugs = total_bugs + bug_count\n",
" day_count = day_count + 1\n",
" \n",
"# output\n",
"print(\"Total number of bugs collected: \", total_bugs)"
],
"metadata": {
"id": "grCtpF80Nh90"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 4.2. Calories Burned\n",
"Running on a particular treadmill you burn 4.2 calories per minute. Write a program that uses a\n",
"loop to display the number of calories burned after 10, 15, 20, 25, and 30 minutes."
],
"metadata": {
"id": "HVzZrxAmPGFG"
}
},
{
"cell_type": "code",
"source": [
"# declare and initialize variables\n",
"CAL_PER_MINUTE = 4.2\n",
"starting_minute = 10\n",
"\n",
"# setup for loop\n",
"while (starting_minute < 31):\n",
" calories_burned = starting_minute * CAL_PER_MINUTE\n",
" print(\"Number of calories burned after\", starting_minute, \"minutes: \", calories_burned)\n",
" starting_minute = starting_minute + 5\n",
"\n",
"# output\n",
"print(\"Done!\")\n",
"\n"
],
"metadata": {
"id": "whepmowLPSk4"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Number of calories burned after 10 minutes: 42.0\n",
"Number of calories burned after 15 minutes: 63.0\n",
"Number of calories burned after 20 minutes: 84.0\n",
"Number of calories burned after 25 minutes: 105.0\n",
"Number of calories burned after 30 minutes: 126.0\n",
"Done!\n",
"\n",
"===============>\n",
"\n",
"Minutes Calories\n",
"10 42.0\n",
"15 63.0\n",
"20 84.0\n",
"25 105.0\n",
"30 126.0"
],
"metadata": {
"id": "2b43Zt64QVQg"
}
},
{
"cell_type": "code",
"source": [
"# declare and initialize variables\n",
"CAL_PER_MINUTE = 4.2\n",
"starting_minute = 10\n",
"\n",
"# setup for loop\n",
"while (starting_minute < 31):\n",
" calories_burned = starting_minute * CAL_PER_MINUTE\n",
" print(\"Number of calories burned after\", starting_minute, \"minutes: \", calories_burned)\n",
" starting_minute = starting_minute + 5\n",
"\n",
"# output\n",
"print(\"Done!\")\n",
"\n"
],
"metadata": {
"id": "jHILbJLVQRBn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# declare and initialize variables\n",
"CAL_PER_MINUTE = 4.2\n",
"starting_minute = 10\n",
"\n",
"# setup for loop\n",
"print(\"Minutes Calories\")\n",
"while (starting_minute < 31):\n",
" calories_burned = starting_minute * CAL_PER_MINUTE\n",
" print(starting_minute, \" \", calories_burned)\n",
" starting_minute = starting_minute + 5\n",
"\n",
"# output\n",
"print(\"Done!\")"
],
"metadata": {
"id": "JJ6rovNIQ6_B"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### 4.7. Pennies for Pay\n",
"Write a program that calculates the amount of money a person would earn over a period of time\n",
"if his or her salary is one penny the first day, two pennies the second day, and continues to\n",
"double each day. The program should ask the user for the number of days. Display a table\n",
"showing what the salary was for each day, then show the total pay at the end of the period. The\n",
"output should be displayed in a dollar amount, not the number of pennies.\n"
],
"metadata": {
"id": "BczU85UOPPia"
}
},
{
"cell_type": "code",
"source": [
"# get the input from the user\n",
"max_day = input(\"Enter the number of days: \")\n",
"max_day = int(max_day)\n",
"\n",
"\n",
"# initialize the variables\n",
"day_num = 1 # we need this to check the loop condition\n",
"pay_for_the_day = 1 # for keeping track of the pay for a given day\n",
"total_pay = 0 # need this to print the final output\n",
"\n",
"# setup the while loop\n",
"while (day_num <= max_day):\n",
" if day_num == 1 :\n",
" pay_for_the_day = 1 \n",
" else :\n",
" pay_for_the_day = pay_for_the_day * 2\n",
" print('day', day_num, ' = ' , pay_for_the_day, ' pennies')\n",
" \n",
" day_num = day_num + 1\n",
" total_pay = total_pay + pay_for_the_day\n",
" \n",
"# print the final output\n",
"print('The total pay in pennies' , total_pay)\n",
"print('The total pay in dollars' , total_pay/100)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "L_GYD0KtSN0d",
"outputId": "d43be66c-59b3-4bd3-9e47-7222ee0dcaea"
},
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Enter the number of days: 5\n",
"day 1 = 1 pennies\n",
"day 2 = 2 pennies\n",
"day 3 = 4 pennies\n",
"day 4 = 8 pennies\n",
"day 5 = 16 pennies\n",
"The total pay in pennies 31\n",
"The total pay in dollars 0.31\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "zfm8_kq3KcYY"
}
}
]
}
1,308 changes: 1,308 additions & 0 deletions python101_2023_gas_prices_project.ipynb

Large diffs are not rendered by default.