zoukankan      html  css  js  c++  java
  • (python)编程小练习

    1、将一串字符串反向输出,如将"abcd"变成“dcba”

    str1="abcd"
    print str1[::-1]
    

    2、判断是否为回文——判断用户输入的字符串是否为回文。回文是指正反拼写形式都是一样的词,譬如“racecar”。

    while(1):
        input_str = raw_input("please input your string:")
        if input_str=="quit":
            print "bye"
            break
        elif input_str==input_str[::-1]:
            print "your input is huiwen"
        else:
            print "your input is not huiwen"
    

    3、统计一段英文语句的字母个数:

    #!coding=utf-8
    txt="Failure is probably the fortification in your pole"
    count={}
    for i in txt:
        count.setdefault(i,0)#如果字典中没有key为i的键,则添加这个键,并设置该键的值为0。如果已经有这个键了,则不更改该字典
        count[i]=count[i]+1
    print count

     4、统计一段英文语句中单词的个数:

    #!coding=utf-8
    txt="Failure is probably the fortification in your pole is"
    strlist=txt.split(" ")
    countWord={}
    for i in strlist:
        countWord.setdefault(i,0)
        countWord[i]=countWord[i]+1
    print countWord
    

      

  • 相关阅读:
    Path Sum 解答
    Binary Tree Paths 解答
    Populating Next Right Pointers in Each Node 解答
    Validate Binary Search Tree 解答
    First Missing Positive
    78 Subsets
    47.Permutations 1&2
    N-Queens 1&2
    Combination Sum_DFS
    38.Count and Say
  • 原文地址:https://www.cnblogs.com/Lival/p/6204771.html
Copyright © 2011-2022 走看看