Python学习笔记(6),实践

1,定义一个函数 is_int(x),判断输入的数据是否为整数

这里的整数不仅指的是int类型的数,输入2.0也要判断为整数:

def is_int(x):
    if x==int(x) and x==float(x):
        return True
    else:
        return False

2,定义一个函数 digit_sum(n),实现n的各个位相加,如n=1234,返回10(1+2+3+4)

太笨没思路,查看hint,其中一种方法是将n转换为字符串str,将其作为每个字母分开之后,再转为数字类型 int 相加:

def digit_sum(n):
    total=0
    n=str(n)
    for word in n:
        word=int(word)
        total+=word
    return total

另一种思路:If you're looking for a challenge, try this: to get the rightmost digit of a number, you can modulo ( %) the number by 10. To remove the rightmost digit you can floor divide ( //) the number by 10. (Don't worry if you're not familiar with floor division—you can look up the documentation here. Remember, this is a challenge!)

通过除以10取模的方式,将最右的数取出来,然后将其删除,再重复(我猜是这样)。这个floor divide试了一下print 123//10=12.

智商捉鸡。。。终于稀里糊涂写好了一个:

a=1000
print 12%10
def digit_sum(n):
    i=1
    total=0
    b=len(str(n))
    while i<=b:
        print "Time: ",i
        print "len of n:",len(str(n))
        total+=n%10
        n=n//10
        print "total:",total
        
        i=i+1
        print "i: ",i
    return total
        
       
digit_sum(a)

去掉方便调试用的print:

def digit_sum(n):
    i=1
    total=0
    b=len(str(n))
    while i<=b:
        total+=n%10
        n=n//10
        i=i+1
    return total

3.阶乘

智商不够用,x=1的时候一直显示错误,只能这样了

def factorial(x):
    n=x
    total=1
    if n==1:
        return 1
    else:
        while n>1:
            total=total*x
            n-=1
            x-=1
    return total
print factorial(2)

4.判断是否是质数

每个程序都要看hint,还要改半天我也是醉了!!Remember: all numbers less than 2 are not prime numbers!

def is_prime(x):
    if x<2:
        return False
    else:
        for n in range(2,x):
            if x%n==0:
                return False
        else:
            return True

5.字符串倒序输出,比如输入‘abcd’输出‘dcba’,要求不能使用reversed函数和[::-1]

想大半天解不出来,还是靠了百度之后看到 .join的用法才恍然大悟。

def reverse(text):
    new_word=[]
    new=''
    i=0
    l=len(text)
    for i in range(l):
        new_word.append(text[l-i-1])
    return new.join(new_word)    
print reverse('abcd')

6.句子剔除元音字母之后输出

查看hint!!!使用  .join之后还算比较轻松。。。。!!!!To check to see if c is a vowel, you can do:cin"aeiouAEIOU"

def anti_vowel(text):
    result=''
    new_line=[]
    i=0
    while i<len(text):
        if text[i] not in "aeiouAEIOU":
            new_line.append(text[i])
        i+=1
    return result.join(new_line)

7.scrabble_score游戏,输入一个单词,单词的每个字母赋予不同的分值,输出这个单词总共得到的分值。注意如果输入大写字母要转换为小写再求值

一下子看蒙了题目无从下手,其实很简单:

score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, 
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, 
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, 
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, 
         "x": 8, "z": 10}

def scrabble_score(word):
    total=0
    for i in word:
        n=i.lower()
        total+=score[n]
    return total
    
print scrabble_score('word')


 

8.censor.

定义一个叫censor的函数,输入一个句子和一个单词如果检查到句子中有这个单词,将这个句子中的对应单词变为星号,且单词有几个字母就变为几个星号。

例如,

censor("this hack is wack hack", "hack")
返回  
"this **** is wack ****"
hint:use
string.split()
# and 
" ".join(list)
list中的字符可以通过   “ (空格)”.join(list)    连接成句子!!

def censor(text,word):
    line=''
    new_lst=[]
    lst=text.split()
    print lst
    for n in lst:
        print n
        if n==word:
            new_lst.append('*'*len(n))
        elif n !=word:
            new_lst.append(n)
    print new_lst
    return " ".join(new_lst)


9.列表过滤。

在列表中过滤掉重复的数字,每个数字只显示一次。

def remove_duplicates(lst):
    new_lst=[]
    lst1=lst
    for item in lst1:
        if item not in new_lst:
            new_lst.append(item)
    return new_lst
print remove_duplicates([2,2,3,4])
输出[2,3,4]

(完)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值