zoukankan      html  css  js  c++  java
  • 函数部分相关练习题及解答

    1、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

    分析:需要计算【数字】等的个数,需首先判断该元素是不是数字或字母,需要调用元素属性判断,例子如下:

     1 str1="dadsddsfd 6 reerere0r089eeffefdfd~ewrer#"
     2 def st(string):
     3     all_sum=0
     4     space_sum=0
     5     digit_sum=0
     6     oth_sum=0
     7     for i in string:
     8         if i.isdigit():
     9             digit_sum+=1;
    10         elif i.isspace():
    11             space_sum+=1;
    12         elif i.isalpha():
    13             all_sum+=1
    14         else:
    15             oth_sum+=1;
    16     return(all_sum,space_sum,digit_sum,oth_sum)
    17 
    18 rr=st(str1)
    19 print(rr)

    2、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。

    分析:先判断传入的对象是否为字符串、列表或者元组,如果是则调用这些对象的的len方法来判断对象的长度

    def length(p):
        if isinstance(p,str)or isinstance(p.list) or isinstance(p,tuple):
            if len(p)>5:
                return "长度大于5"
            else:
                return False
        return None
    temp="hello,"
    ret=length(temp)
    print(ret)
    3、写函数,检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空格。
     1 def ContainSpace(s):
     2     if isinstance(s,str)or isinstance(s,list) or isinstance(s,tuple):
     3         for i in s:
     4             if i.isspace():
     5                 return "该对象的元素含有空格"
     6             else:
     7                 return "该对象的元素不含空格"
     8 
     9 ll=[' ',"123"]
    10 ret4=ContainSpace(ll)
    11 print(ret4)

    4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

    #分析:本例子要用到列表的截取,本例考虑到返回的仍旧为列表,存在瑕疵,若列表长度小于2,则系统报错;

    1 def split1(*s):
    2     if len(s)>2:
    3         ret=s[0:2]
    4         print(list(ret))
    5         return True
    6     else:
    7         return False;
    8 l1=[1,2,3,4,5]
    9 split1(*l1)

    5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。

    #若要查找下标所对应的元组,则应首先查找出所有的下标,考虑用for循环遍历;考虑到传入对象,所以使用动态参数

    1 def new_li(*l):
    2     result=[]
    3     for i in range(len(l)):
    4         if i%2==1:
    5             result.append(l[i])
    6     return result;
    7 ret1=new_li(1,23,45,566,77,8,9)
    8 print(ret1)
    #方法二
    def odd(*a):
        result=[]
        for k,v in enumerate(a):
            if k%2==1:
                result.append(v)
        return result
    ret2=odd(1,3,5,6,6,77,88,8)
    print(ret2)

    6、写函数,检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。

    #由于传入的对象是字典,考虑到用动态函数,接收的参数为字典格式

    
    
    1 def dic(**d):
    2     for i in d.values():
    3         if len(i)>2:
    4             ret=i[0:2]
    5             print(ret)
    6         else:
    7             return False
    8 dic(k1="Hello World",k2=[11,22,33,44])
    
    
    
     

     练习:斐波拉契数列,即后面一个数的值等于前面两个数之和

     1 #斐波拉契数列 即后一个数等于前两个数之和
     2 #分析:斐波拉契数列是一组数字,这组数字从第三个数字开始,后面的数等于前两个数之和
     3 def fib(n):
     4     result=[];
     5     a,b=0,1;
     6     while b<n:
     7         result.append(b)
     8         a,b=b,a+b
     9     return result
    10 
    11 print(fib(50))


  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/eric8899/p/5955977.html
Copyright © 2011-2022 走看看