SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
To Computers Programs And Python
Exercise:
Programming Excercises
Question:11 | ISBN:978013274719 | Edition: 6

Question

(Population projection) The US Census Bureau projects population based on the following assumptions:
One birth every 7 seconds
One death every 13 seconds
One new immigrant every 45 seconds
Write a program to display the population for each of the next five years. Assume the current population is 312032486 and one year has 365 days. Hint: in Python, you can use integer division operator // to perform division. The result is an integer. For
example, 5 // 4 is 1 (not 1.25) and 10 // 4 is 2 (not 2.5).

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Python code:

print("current population: 312032486");
currentpopulation = 312032486;

#add one birth every 7 seconds; subtract one death every 13 seconds; add one immigrant every 45 seconds.
#365 days = 1 year | 1 day = 24 hours | 1 hour = 60 minutes | 1 minute = 60 seconds
#7 seconds = 1 birth | 13 seconds = 1 death | 45 seconds = 1 immigrant

#365days * 24hours * 60minutes * 60seconds = 31536000 seconds
numberOfSecondsInYear = 365 * 24 *60 * 60;

#After 1 year, number of births (7 seconds = 1 birth | plus)
birthsPerYear = numberOfSecondsInYear / 7;
print("brithsPerYear: ", int(birthsPerYear));

#After 1 year, number of deaths (13 seconds = 1 death | minus)
deathsPerYear = numberOfSecondsInYear / 13;
print("deathsPerYear: ", int(deathsPerYear));

#After 1 year, number of immigrants (45 seconds = 1 immigrant | plus)
immigrantsPerYear = numberOfSecondsInYear / 45;
print("immigrantsPerYear: ", int(immigrantsPerYear));

#Rate per year
ratePerYear = birthsPerYear - deathsPerYear + immigrantsPerYear;
print("ratePerYear: ", int(ratePerYear));

print("population after first year end :", int((currentpopulation + (1 * ratePerYear))));
print("population after second year end :",int((currentpopulation + (2 * ratePerYear))));
print("population after third year end :", int((currentpopulation + (3 * ratePerYear))));
print("population after fourth year end :",int((currentpopulation + (4 * ratePerYear))));
print("population after five year end :",  int((currentpopulation + (5 * ratePerYear))));

Output:

Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
current population: 312032486
brithsPerYear:  4505142
deathsPerYear:  2425846
immigrantsPerYear:  700800
ratePerYear:  2780096
population after first year end : 314812582
population after second year end : 317592679
population after third year end : 320372776
population after fourth year end : 323152872
population after five year end : 325932969

 

0 0

Discussions

Post the discussion to improve the above solution.