Python core programming version 2 Chapter 5 Exercise 4

Source: Internet
Author: User

I will share with you the answer to my core Python programming.
Because it is not from official resources, it is your own exercises. It may be wrong or not the best solution.

5-13.
Conversion. Write a function to convert the time represented by hours and minutes to the time represented by minutes.
[Answer]
The Code is as follows:
Def conversion (a, B ):
Return a * 60 + B

Time = raw_input ('Please input the time in HH: MM format :...')
T = time. split (':')
Print conversion (int (t [0]), int (t [1])

5-14.
Bank interest. Write a function with the time deposit rate as the parameter. If the account calculates compound interest on a daily basis, calculate and return the annual rate of return.
[Answer]
The Code is as follows:
DayInterestRate = float (raw_input ('Please input the day rate :...'))
Print (1. + dayInterestRate) ** 365-1

5-15.
The maximum common approx. Calculate the maximum common divisor and least common multiple of two integer types.
Background Knowledge]
The answer to this question uses the more-phase reduction technique, also known as the "equivalent algorithm", to calculate the maximum common divisor of two numbers, while the minimum common multiples of two numbers are their product divided by the maximum common divisor.
Least Common Multiple (LCM)
Greatest Common Divisor (GCD) or Highest Common Factor (HCF)
[Answer]
The Code is as follows:
Def GCD (a, B ):
I = 0
If (a % 2 = 0) and (B % 2 = 0 ):
C = a/2
D = B/2
I = I + 1
Else:
C =
D = B
While c! = D:
If c> d: c = c-d
Elif c <d: d = d-c
Else: return c * (2 ** I)
Return c * (2 ** I)

Def LCM (a, B ):
Return a * B/GCD (a, B)

Print GCD (4044,908 8)
Print LCM (4044,908 8)

[Reference] Algorithm for Finding the maximum public approx.
Http://blog.csdn.net/cauwtj/archive/2009/04/02/4043388.aspx
Four Methods for quickly finding the least common multiple
Http://www.hmtyxx.com/jiaoyu/ShowArticle.asp? ArticleID = 1414

Keywords: Pyhon core programming Second Edition answer unofficial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.