zoukankan      html  css  js  c++  java
  • 小练手2

    1.
    """
    Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
    Suppose the following input is supplied to the program:
    Hello world!
    Then, the output should be:
    UPPER CASE 1
    LOWER CASE 9
    """
    def fun(word):
        u = 0
        l = 0
        for i in word:
            if i.isupper():
                u += 1
            elif i.islower():
                l += 1
        return print('大写{}
    小写{}'.format(u, l))
    fun('Hello world!')
    
    2.
    """
    Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
    Suppose the following input is supplied to the program:
    9
    Then, the output should be:
    11106
    """
    def fun(num):
        return print(int(num)+int(num*2)+int(num*3)+int(num*4))
    fun('9')
    
    3.
    """
    Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
    Suppose the following input is supplied to the program:
    1,2,3,4,5,6,7,8,9
    Then, the output should be:
    1,3,5,7,9
    """
    def fun(word):
        li = [i for i in word.split(',') if int(i)%2!=0]
        return print(','.join(li))
    fun('1,2,3,4,5,6,7,8,9')
    
    4.
    """
    Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:
    D 100
    W 200
    
    D means deposit while W means withdrawal.
    Suppose the following input is supplied to the program:
    D 300
    D 300
    W 200
    D 100
    Then, the output should be:
    500
    """
    def fun():
        count = 0
        while True:
            word = input('请输入操作类型和金额,以空格分割:')
            if word:
                try:
                    type = word.split(' ')[0]
                    num = word.split(' ')[1]
                except Exception as e:
                    print('格式错误!')
                    continue
                if type == 'D':
                    count += int(num)
                elif type == 'W':
                    if count - int(num) > 0:
                        count -= int(num)
                    else:
                        print('余额不足!')
                else:
                    pass
                continue
            else:
                print(count)
                break
    fun()
    
    5.
    """
    A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
    Following are the criteria for checking the password:
    1. At least 1 letter between [a-z]
    2. At least 1 number between [0-9]
    1. At least 1 letter between [A-Z]
    3. At least 1 character from [$#@]
    4. Minimum length of transaction password: 6
    5. Maximum length of transaction password: 12
    Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
    Example
    If the following passwords are given as input to the program:
    ABd1234@1,a F1#,2w3E*,2We3345
    Then, the output of the program should be:
    ABd1234@1
    """
    import re
    def fun(word):
        li = []
        for i in word.split(','):
            if re.match('^(?=.*[a-z])(?=.*[A-Z])(?=.*[$#@])(?=.*[0-9])[A-Za-z0-9$#@]{6,12}$',i):
                li.append(i)
        return print(','.join(li))
    
    fun('ABd1234@1,a F1#,2w3E*,2We3345')
    
    6.
    """
    You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:
    1: Sort based on name;
    2: Then sort based on age;
    3: Then sort by score.
    The priority is that name > age > score.
    If the following tuples are given as input to the program:
    Tom,19,80
    John,20,90
    Jony,17,91
    Jony,17,93
    Json,21,85
    Then, the output of the program should be:
    [('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
    """
    from operator import itemgetter
    def fun():
        li = []
        while True:
            word = input()
            if not word:
                break
            li.append(word)
        return print(sorted(li,key=itemgetter(0,1,2)))
    
    fun()
    
    7.
    """
    Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
    """
    def fun(n):
        for i in range(n):
            if i %7 == 0:
                yield i
    
    for i in fun(100):
        print(i)
    
    8.
    """
    A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
    UP 5
    DOWN 3
    LEFT 3
    RIGHT 2
    ¡­
    The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer.
    Example:
    If the following tuples are given as input to the program:
    UP 5
    DOWN 3
    LEFT 3
    RIGHT 2
    Then, the output of the program should be:
    2
    """
    import math
    def fun():
        zero = [0, 0]
        while True:
            word = input('')
            if word:
                type,steep = word.split(' ')[0],int(word.split(' ')[1])
                if type == 'UP':
                    zero[1] += steep
                    continue
                elif type == 'DOWN':
                    zero[1] -= steep
                    continue
                elif type == 'LEFT':
                    zero[0] -= steep
                    continue
                elif type == 'RIGHT':
                    zero[0] += steep
                    continue
                else:
                    print('格式有误,请重新输入!')
                    continue
            else:
                print(int(math.sqrt(zero[0]*zero[0]+zero[1]*zero[1])))
                break
    fun()
    
    9.
    """
    Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. 
    Suppose the following input is supplied to the program:
    New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
    Then, the output should be:
    2:2
    3.:1
    3?:1
    New:1
    Python:5
    Read:1
    and:1
    between:1
    choosing:1
    or:2
    to:1
    """
    def fun(word):
        dic = {}
        for i in word.split(' '):
            if i not in dic.keys():
                dic[i] = 1
            else:
                dic[i] += 1
        dic1 = sorted(dic.keys())
        for i in dic1:
            print(i+':'+str(dic[i]))
    fun('New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.')
    
    10.
    """
    Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0).
    Example:
    If the following n is given as input to the program:
    5
    Then, the output of the program should be:
    3.55
    In case of input data being supplied to the question, it should be assumed to be a console input
    """
    def fun(num):
        li = []
        if num>0:
            for i in range(1,num+1):
               li.append(i/(i+1))
        else:
            return print(sun(li))
        return print(round(sum(li),2))
    fun(5)
  • 相关阅读:
    MVC3分页
    ASP.NET MVC导入excel到数据库,下载文件
    使用JS获取客户端MAC地址
    关于mschart控件在mvc项目中的webform里面显示不出图片的问题
    difference between "on" and "where" when using left/right join query
    Will static methods over multithreading cause threadsafe problem?
    const VS readonly in detail
    一个编码引发js错误的问题
    分享一个基于FileSystemWatcher的文件自动备份程序
    Back/Forward and Refresh in browser
  • 原文地址:https://www.cnblogs.com/miaoweiye/p/12036479.html
Copyright © 2011-2022 走看看