Concept explainers
Calculate day number corresponding to a date
Program plan:
- Define the function “isLeapYear()”,
- Check whether the year is not divisible by "4",
- If it is true, return “False”.
- Otherwise, check whether the year is divisible by "100",
- If it is true, check whether the year is divisible by "400",
- If it is true, return “True”.
- Otherwise, return “False”.
- Otherwise, return “True”.
- If it is true, check whether the year is divisible by "400",
- Check whether the year is not divisible by "4",
- Define the function “verifyDate()”,
- Check whether either a month is greater than "12" or day is greater than "31",
- If it is true, return “False”.
- Otherwise, Check whether day is less than or equal to "28",
- If the condition is “True”, return “True”.
- Otherwise, check whether month is "2" and day is "29",
- If it is “True” check whether the return value from “isLeapYear()” is “False”,
- If “True”, return “False”.
- Otherwise, return “True”.
- Otherwise, check whether day is equal to “31”,
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- If “True” return “False”.
- Otherwise, return “True”.
- Otherwise, return “True”.
- If “True”, Check whether month is either "2" or "4" or "6" or "11",
- Define the function “main()”,
- Get the date from the user.
- Assign month, day, and year by splitting the date by "/" using “split()” method
- Typecast month, day, and year to “int” type.
- Check whether the value return from “verifyDate()” is “False”,
- If it is “True”, print invalid.
- Otherwise, calculate the number of day using the formula.
- Check whether the month is “2”,
- If “True”, check the value return from “isLeapYear()” method is “True”,
- If “True”, calculate the day number using the formula.
- Otherwise, calculate the day number using another formula.
- Otherwise, calculate the day number using the given formula.
- Print the numeric value for the date.
- Call the function “main()”.
This Python program accepts a date as month/ day /year format, and verifies that it is a valid date, and then calculates the corresponding day number.
Explanation of Solution
Program:
File name: “Number.py”
#Define the function isLeapYear()
def isLeapYear(y):
#Check whether the year is not divisible by "4"
if (y % 4) != 0:
#Return false
return False
#Otherwise
else:
#Check whether the year is divisible by "100"
if (y % 100) == 0:
#Check whether the year is divisible by "400"
if (y % 400) ==0:
#Return true
return True
#Otherwise
else:
#Return false
return False
#Otherwise
else:
#Return true
return True
#Define the function verifyDate()
def verifyDate(mon, d, yr):
'''Check whether either a month is greater than "12" or day is gretaer than "31"'''
if mon > 12 or d > 31:
#Return "False"
return False
#Otherwise
else:
#Check whether day is less than or equal to "28"
if d <= 28:
#Return True
return True
#Check whether month is "2" and day is "29"
elif mon == 2 and d == 29:
#Check whether the return value is false
if isLeapYear(yr) == False:
#Return "False"
return False
#Otherwise
else:
#Return "True"
return True
#Check whether day is "31"
elif d == 31:
#Check whether month is either "2" or "4" or "6" or "11"
if mon == 2 or 4 or 6 or 11:
#Return "False"
return False
#Otherwise
else:
#Return True
return True
#Otherwise
else:
#Return True
return True
#Define the function main()
def main():
#Get the date from the user
date=eval(input("Enter date"))
#Initialize the value
dayNum=0
'''Assign month, day, and year by splitting the date by "/" using split() method'''
month_Str, day_Str, year_Str = date.split("/")
#Typecast month to int type
mon = int(month_Str)
#Typecast day to int type
d = int(day_Str)
#Typecast year to int type
yr = int(year_Str)
#Check whether the return value is false
if verifyDate(mon, d, yr) == False:
#Print invalid
print("This date is invalid.")
#Otherwise
else:
#Calculate the number of a day
dayNum = 31 * (mon - 1) + d
#Check whether month is "2"
if mon == 2:
#Check whether the return value is True
if isLeapYear(yr) == True:
#Calculate the day numbber
dayNum = dayNum - (4 * (mon) + 23)//10 + 1
#Otherwise
else:
#Calculate the day number
dayNum = dayNum - (4 * (mon) + 23)//10
#Otherwise
else:
#Calculate day number
dayNum = 31 * (mon - 1) + d
#Print the day number
print("The numeric value of this date is {}.".format(dayNum))
#Call the function main()
main()
Output:
Enter date'2/28/2001'
The numeric value of this date is 56.
Additional Output:
Enter date'05/25/1885'
The numeric value of this date is 149.
Want to see more full solutions like this?
Chapter 7 Solutions
Python Programming: An Introduction to Computer Science, 3rd Ed.
- (Statistics) This is the formula for the standard normal deviate, z, used in statistical applications: z=(X)/ X is a single value. refers to an average value. refers to a standard deviation. Using this formula, you need to write a program that calculates and displays the value of the standard normal deviate when X=85.3,=80,and=4. a. For this programming problem, how many outputs are required? b. How many inputs does this problem have? c. Determine a formula for converting input items into output items. d. Test the formula written for Exercise 7c, using the data given in the problem.arrow_forward4. During each summer, John and Jessica grow vegetables in their backyard and buy seeds and fertilizer from a local nursery. The nursery carries different types of vegetable fertilizers in various bag sizes. When buying a particular fertilizer, they want to know the price of the fertilizer per pound and the cost of fertilizing per square foot. The following program prompts the user to enter the size of the fertilizer bag, in pounds, the cost of the bag, and the area, in square feet, that can be covered by the bag. The program should output the desired result. However, the program contains logic errors. Find and correct the logic errors so that the program works properly. // Logic errors. #include #include using namespace std; int main() { double costs double area; double bagsize; cout > bagsize; cout > cost; cout > area; cout << endl; cout << "The cost of the fertilizer per pound is: $" << bagsize / cost << endl; cout << "The cost of fertilizing per square foot is: $" << area / cost << endl; return 0; }arrow_forward1. Write a program that lets the user guess whether a randomly generated integer would be even or odd. The program randomly generates an integer and divides it by 2. The integer is even if the remainder is 0, otherwise odd. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.arrow_forward
- Computer Engineering labarrow_forwardSlove by pythonarrow_forwardLab 7 Pre-Lab In Java Write an algorithm that gets student exam scores from the user. Each exam score will be an integer in the range 0 to 100. Input will end when the user enters -1 as the input value. After all scores have been read, display the number of students who took the exam, the minimum score, the maximum score, the average score, and the number of As where an A is a score in the range 90-100. Make sure your average a decimal point. Only count A’s, not other grades. Note that you do not need to do any error checking on the input for this problem. This should be done as a standard sentinel-controlled while loop.arrow_forward
- Filling the Pool (Deprecated) Write a program that calculates the time neccessary to completely fill an empty pool with water. We will assume that the pool is rectangular and the depth is uniform. All input values in this program will be integers. Prompt the user to enter the pool dimensions - length, width, and depth (unit: feet) - as well as the rate at which water can be put into the pool (unit: gallons per minute). Using these values, calculate and display the time (in minutes) needed to fill the pool from completely empty to completely full. Note: you should calculate the volume of the pool (as cubic feet) and then determine the rate (in cubic feet per minute) that water can be put into the pool. Assume that there are 7.48 gallons in one cubic foot. Your program should run like the examples shown below: Enter pool dimensions Length: 10 Width: 8 Depth: 7 Water entry rate: 14 The pool will fill completely in 299.2 minutesarrow_forwardmystery_value = 5 #You may modify the lines of code above, but don't move them!#When you Submit your code, we'll change these lines to#assign different values to the variables. #Write a program that divides mystery_value by mystery_value#and prints the result. If that operation results in an#error, divide mystery_value by (mystery_value + 5) and then#print the result. If that still fails, multiply mystery_value#by 5 and print the result. You may assume one of those three#things will work.##You may not use any conditionals.# #Add your code here!arrow_forwardPython questionarrow_forward
- A complex number is a number in the form a+bi, where a and b are real numbers and i is √(-1) The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a+c) + (b+d)i (addition) a + bi − (c + di) = (a−c) + (b−d)i (subtraction) (a + bi) * (c + di) = (ac−bd) + (bc+ad)i (multiplication) (a + bi) / (c + di) = (ac+bd) / (c2+d2) + (bc−ad)i / (c2+d2) (division) You can obtain the Absolute Value for a complex number using the following formula: |a + bi| = √(a2 + b2) A Complex number can be interpreted as a point on a plane by identifying the (a,b) values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin, as shown in the example below. (1) Design a class named Complex for representing complex numbers Include…arrow_forwardBox Office: A movie theater only keeps a percentage (20%) of the revenue earned from ticket sales. The remainder goes to the movie distributor. Write a program that calculates a theater’s gross and net box office profit for a night. The program should ask for the name of the movie, and how many adult and child tickets were sold. (The price of an adult ticket is $10.00 and a child’s ticket is $6.00.) Input: the name of the movie, number of adult tickets sold, number of child tickets sold Output: as specified below Object: cin, cout, getline, format output Note: 1. Movie names contain spaces 2, the report is aligned. 3. The decimal number is displayed with 2 decimal places.arrow_forwardQ1: Write a program that reads 10 numbers and then finds the largest number and prints it and finds the smallest number and eyarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr