Python Exercise Exercises (chapater 5--python core programming)

Source: Internet
Author: User
Tags greatest common divisor

Update in ...

#!/usr/bin/env python
# Coding:utf-8
' For practise in Chapater five '


#定义一个函数, calculates and returns the two-digit flight
def product (A, B):
Return (A * b)

#根据分数输出同学的评分成绩 (A-F)
DEF score (i):
if (i > +) & (I < 100):
Return (' A ')
Elif (i >) & (I < 89):
Return (' B ')
Elif (i >) & (I < 79):
Return (' C ')
Elif (i >) & (I < 69):
Return (' D ')
Else
Return (' F ')
#print (Score (88))

#-----Practice to take the remainder-----

#1. Determine whether the year is a leap years
def estimate_leap (j): #j is the year
if ((j%4 = = 0) and (j%100! = 0)) or (j%400 = = 0):
Print ("%d is a leap year."%j)
Else
Print ("%d is a isn't leap year."%j)
Return
#print (Estimate_leap (1997))

#2. Take an amount that is less than $1, and then calculate the minimum number of coins that can be exchanged. Coins have 1,5,10,25 cents
def coin (k): #k is the amount less than $1
coin25 = Int (k/0.25)
coin10 = Int ((k-coin25*0.25)/0.1)
coin5 = Int ((k-coin25*0.25-coin10*0.1)/0.05)
coin1 = Int ((k-coin25*0.25-coin10*0.1) *100)
Return ("%.2f cent can change to at least%d coins."% (k, (coin25+coin10+coin5+coin1)))
#print (Coin (0.87))

#3.1 Use loops and arithmetic operations to find all the even numbers between 0-20
Def even_num (): #even number
For I in range (21):
If I% 2 = = 0:
Print (i),
#even_num ()

#3.2 use loops and arithmetic operations to find all odd
def odd_num ():  #odd number (uneven number)
 for i in range:
  if I% 2 = = 1:
   print (i),
#odd_num ()
  
#3.3 Write a function, Detects whether an integer can be divisible by another integer. Ask the user to enter two numbers first, and then your function to determine whether
#     has an integer relationship, and returns True and False according to the result of the Judgment
Def Exact_divi (A, b):  # Detects if integer a can be divisible by integer b, b/a
 if b% A = = 0:
  print ('%d can is divisible by%d '% (a, b))
  retur n True
 else:
  print ('%d can\ ' t is divisible by%d '% (a, b))
  return False
#a = in T (Raw_input (' first number> '))
#b = Int (raw_input (' Second number> ')
#exact_divi (A, b)



#-----Arithmetic-----
#1. A calculator program with two operands and an operator: N1 operator N2. (unfinished)
def calculate (L): #l is an expression: N1 operator N2
NUM1 = float (L.split (") [0])
num2 = float (L.split (") [2])
Opert = L.split (") [1]
# return (NUM1 Opert num2)
#print (Calculate (' 11.2-2.9 ')) #这个题目还没解决

#2. Convert and write a pair of functions to convert degrees Fahrenheit to Celsius. The conversion formula is C = (F-32) * (5/9)
Def Centi_fahren (a): #参数a为华氏度, the return value of this function is Celsius.
Return ('%.2f '% ((a-32) * (float (5)/9)))

#print (Centi_fahren (100))

#3. System limitations. Write a script to confirm the range of integers, long integers, floating-point numbers, and complex numbers that your python can handle.
Import Sys
Def num_limit ():
Print (' The limit for int:%d%d '% (-sys.maxint-1, sys.maxint))
Print (' The limit for float:%e%e '% (sys.float_info[3], sys.float_info[0]))
Print (' The limit for long:%s '% (Sys.long_info))
#num_limit ()

#4. Conversion. Write a function that converts the time represented by hours and minutes to a time that is expressed in minutes only.
def minutes (str):
Hours = Int (Str.split (': ') [0])
minutes = Int (str.split (': ') [1])
Minutes_all = hours * + minutes
Return Minutes_all
#print (minutes (' 12:50 '))

#5. Bank interest. Write a function that takes the term deposit interest rate as a parameter, assuming that the account calculates compound interest on a daily basis, calculate and return the annual return.
#工商银行定期存日款率0.35%
def interest_year_return (capital, rates): #capital is the principal, rate is the day
Capital_ori = Capital
i = 1
While True:
Capital = Capital * (1+rate) # capital = Capital * ((1+rate) **365)
i + = 1
if i = = 366:
Break
Return ((Capital-capital_ori)/capital_ori)
#print (Interest_year_return (10000, 0.000035))

#6. Greatest common divisor and least common multiple. Please calculate the greatest common divisor and least common multiple of two integers.
Def max_com_divisor (A, B): #整数a和b的最大公约数, using the Euclidean method (algorithm of division)
If A/b = = 0:
A, B = B, a
Algorithm_lst = [A, b]
i = 0
While True:
Algorithm_lst.append (Algorithm_lst[i]% algorithm_lst[i+1])
If algorithm_lst[i+2] = = 0:
return algorithm_lst[i+1]
Break
Else
i + = 1
#print Max_com_divisor (66, 242)
Def least_com_multiple (A, B): #整数a和b的最小公倍数, also using the Euclidean method (algorithm of division)
If A/b = = 0:
A, B = B, a
Algorithm_lst = [A, b]
i = 0
While True:
Algorithm_lst.append (Algorithm_lst[i]% algorithm_lst[i+1])
If algorithm_lst[i+2] = = 0:
Break
Else
i + = 1
LCM = (A * b)/algorithm_lst[i+1]
Return (LCM)
#print Least_com_multiple (12, 33)

This article is from "Cool ops" blog, please make sure to keep this source http://19901007.blog.51cto.com/10641668/1688208

Python Exercise Exercises (chapater 5--python core programming)

Related Article

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.