xylz,imxylz

关注后端架构、中间件、分布式和并发编程

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  111 随笔 :: 10 文章 :: 2680 评论 :: 0 Trackbacks
5-3.

Standard Type Operators. Take test score input from the user and output letter grades

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0503.py 126 2010-04-20 02:18:07Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10
11'''
12Print the score result.
13'''
14def getResultString(score):
15    if score <or score > 100return "ERROR SCORE"
16    if 90 <= score <= 100return "A"
17    if 80 <= score <= 89return "B"
18    if 70 <= score <= 79return "C"
19    if 60 <= score <= 69return "D"
20    return "F"
21
22if __name__ == '__main__':
23    '''
24    Take test score input from the user and output letter grades. 
25    '''
26    while True:
27        s = raw_input("Enter an Integer ('exit' or 'quit' for over). \n>>")
28        if s == 'exit' or s == 'quit' : break
29
30        try:
31            v=int(s)
32        except ValueError,e:
33            print "Error! Not a number. ",
34        else:
35            print v,"=>",getResultString(v)
36
5-4.

Modulus. Determine whether a given year is a leap year, using the following formula: a leap year is one that is divisible by four, but not by one hundred, unless it is also divisible by four hundred. For example, 1992, 1996, and 2000 are leap years, but 1967 and 1900 are not. The next leap year falling on a century is 2400.

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0504.py 127 2010-04-20 02:19:33Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10import sys
11import types
12
13'''
14Determine whether a given year is a leap year.
15'''
16def isleapyear(year):
17    if type(year) is not types.IntType: return False    
18    if year > 9999 or year <= 0: return False
19    if (year%400 == 0) or ((year%4==0) and (year%100 !=0)):
20        return True
21    return False
22
23if __name__ == '__main__':
24    '''
25    Print leap year.
26    '''
27    while True:
28        s = raw_input("Input a 'year' ('exit' or 'quit' for over). \n>>")
29        if s == 'exit' or s == 'quit' :sys.exit(0)
30
31        try:
32            year=int(s)
33        except ValueError,e:
34            print "Error! Not a number. ",
35        else:
36            print year,"is leap year?",isleapyear(year)
37
5-5.

Modulus. Calculate the number of basic American coins given a value less than 1 dollar. A penny is worth 1 cent, a nickel is worth 5 cents, a dime is worth 10 cents, and a quarter is worth 25 cents. It takes 100 cents to make 1 dollar. So given an amount less than 1 dollar (if using floats, convert to integers for this exercise), calculate the number of each type of coin necessary to achieve the amount, maximizing the number of larger denomination coins. For example, given $0.76, or 76 cents, the correct output would be "3 quarters and 1 penny." Output such as "76 pennies" and "2 quarters, 2 dimes, 1 nickel, and 1 penny" are not acceptable.


 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0505.py 128 2010-04-20 02:22:45Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10
11'''
12get min number of cents
13'''
14def getMinNumberOfCents(m):
15    if m < 0 or m >= 1return (-1,-1,-1,-1
16    money = round(m*100)
17    yy25 = divmod(money,25)
18    yy10 = divmod(yy25[1],10)
19    yy5 = divmod(yy10[1],5)
20    return (int(yy25[0]),int(yy10[0]),int(yy5[0]),int(yy5[1]))
21
22if __name__ == '__main__':
23    '''
24    Calculate the number of basic American coins given a value less than 1 dollar.
25    '''
26    while True:
27        s = raw_input("Input a 'money' between 0 and 1 ('exit' or 'quit' for over). \n>>")
28        if s == 'exit' or s == 'quit' :break
29
30        try:
31            money=float(s)
32        except ValueError,e:
33            print "Error! Not a number. ",
34        else:
35            print "25 cents: %d\n10 cents: %d\n 5 cents: %d\n 1 cents: %d" % getMinNumberOfCents(money)
36
5-6.

Arithmetic. Create a calculator application. Write code that will take two numbers and an operator in the format: N1 OP N2, where N1 and N2 are floating point or integer values, and OP is one of the following: +, -, *, /, %, **, representing addition, subtraction, multiplication, division, modulus/remainder, and exponentiation, respectively, and displays the result of carrying out that operation on the input operands. Hint: You may use the string split() method, but you cannot use the exal() built-in function.

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0506.py 129 2010-04-20 02:25:57Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10import sys
11
12'''
13a two number calculator
14'''
15def calculate(s):
16    for c in ('+','-','**','/','%','*'):
17        rs = s.split(c)
18        if rs and len(rs) == 2:
19            one=float(rs[0].strip())
20            two=float(rs[1].strip())
21            if c == '+':return one+two
22            if c == '-':return one-two
23            if c == '**'return one**two
24            if c == '/'
25                if two==0: raise ValueERROR,"ZERO DIVISION"
26                return one/two
27            if c == '%':return one%two
28            if c == '*':return one*two
29            raise ValueError,"ERROR EXPRESSION"
30    raise ValueError,"ERROR EXPRESSION"
31            
32
33if __name__ == '__main__':
34    '''
35    Write code that will take two numbers and an operator in the format: N1 OP N2.
36    '''
37    while True:
38        s = raw_input("Input a string expression ('exit' or 'quit' for over). \n>>")
39        if s == 'exit' or s == 'quit' :break
40
41        try:
42            v = calculate(s)
43        except ValueError,e:
44            print "Error! Not a expression. ",
45        else:
46            print "%s = %s" % (s,v)
47
5-17.

*Random Numbers. Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 231-1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0517.py 130 2010-04-20 02:29:51Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10import sys
11import random
12
13'''
14a random demo
15'''
16def randomAndSort(m,max):
17    ret = []
18    for i in range(m):
19        ret.append(random.randint(0,max))
20    ret.sort()
21    return ret
22
23if __name__ == '__main__':
24    '''
25    Read up on the random module and do the following problem: Generate a list of a random number (1 < N <= 100) of random numbers (0 <= n <= 2e31-1). Then randomly select a set of these numbers (1 <= N <= 100), sort them, and display this subset.
26    '''
27    print randomAndSort(100,sys.maxint)
28


©2009-2014 IMXYLZ |求贤若渴
posted on 2010-04-20 10:31 imxylz 阅读(16797) 评论(0)  编辑  收藏 所属分类: Python

只有注册用户登录后才能发表评论。


网站导航:
 

©2009-2014 IMXYLZ