zoukankan      html  css  js  c++  java
  • python 刷题必备

    1、判断输入的数字是否是回文数:

    学习内容:把数字转成字符串

    1.

    def is_palindrome(n):  
        n=str(n)  
        m=n[::-1]  
        return n==m  

    2.

    tmp_str = str(n)  
    i = len(tmp_str) - 1  
    j = 0  
    while i > j :  
      if tmp_str[i] == tmp_str[j] :  
        i = i - 1   
        j = j + 1  
        pass  
      else :   
        return False  
    return True 

    如果要直接接收键盘输入的话,可以直接用raw_input()即可,这样获得的就是字符串。

    2.一行输出多个数字

    学习内容:如何一行输出多个数字

    1.

    s=[1,2,3,4,5,]
    s1='%d %d %d %d %d' %(s[0],s[1], s[2], s[3], s[4])
    print s1

    2.转换成字符串输出:

    s=[1,2,3,4,5]
    s2='%s' %(' '.join(map(str,s)))
    print s2

     3.统计列表中元素个数

    1.利用列表的count函数

    mylist = [1,2,2,2,2,3,3,3,4,4,4,4]
    myset = set(mylist)  #myset是另外一个列表,里面的内容是mylist里面的无重复项
    for item in myset:
      print("the %d has found %d" %(item,mylist.count(item)))

    2.利用字典

    List=[1,2,2,2,2,3,3,3,4,4,4,4]
    a = {}
    for i in List:
        #i是元素,不是索引,所以a[i]表示key
        a[i] = List.count(i)
    print (a)

    3.利用Counter类,Counter类的学习:http://www.cnblogs.com/shixisheng/p/7410505.html

    from collections import Counter
    Counter([1,2,2,2,2,3,3,3,4,4,4,4])
    #输出:Counter({1: 1, 2: 4, 3: 3, 4: 4})

     4.把数组组成最小的数

    题目描述

    输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。
    # -*- coding:utf-8 -*-
    class Solution:
        def PrintMinNumber(self, numbers):
            # write code here
            num_str=map(str,numbers)
            if not num_str:
                return ''
            num_str.sort(lambda x,y:cmp(x+y,y+x))
            return ''.join(num_str)

    思路:用lambda表达式写一个比较规则。

    自定义比较函数:http://www.cnblogs.com/shixisheng/p/7500407.html

     5.字符串的全排列

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

    使用递归方法:假如n-1个字符串都已经有了,那么加入第n个字符的时候,可以让n依次和前边n-1个字符进行交换。

    实例代码:假如字符串strAP=['A','B','C','D']

    # -*- coding:utf-8 -*-
    
    def SWAP(i, j):
        temp = strAP[i]
        strAP[i] = strAP[j]
        strAP[j] = temp
    
    strAP = ['A', 'B', 'C', 'D']
    result = []
    def CalAllP1(first, num):
        if first == num - 1:  # 到达最后一个元素,则退出
            pass
        else:
            for i in range(first, num):
                if i != first:  # 输出时去掉重复
                    SWAP(i, first)
                    temp = ''.join(strAP)
                    result.append(temp)
                CalAllP1(first + 1, num)  # 递归调用,全排列后面的元素
                SWAP(i, first)# 用来复位的(前边你交换了,这里再交换回来)。
    
    temp = ''.join(strAP)
    result.append(temp)
    CalAllP1(0, len(strAP))
    print result

    完整的代码:

    # -*- coding:utf-8 -*-
    class Solution:
        def __init__(self):
            self.strAP=[]
            self.result=[]
        def Permutation(self, ss):
            # write code here
            if not ss:
                return []
            self.strAP=list(ss)
            temp = ''.join(self.strAP)
            self.result.append(temp)
            self.CalAllP1(0, len(self.strAP))
            self.result=sorted(list(set(self.result)))#用set去除重复,然后再转化成list,然后再排序
            return self.result
            
            
        def SWAP(self, i, j):
            temp = self.strAP[i]
            self.strAP[i] = self.strAP[j]
            self.strAP[j] = temp
            
            
        def CalAllP1(self, first, num):
            if first == num - 1:  # 到达最后一个元素,则退出
                pass
            else:
                for i in range(first, num):
                    if i != first:  # 输出时去掉重复
                        self.SWAP(i, first)
                        temp = ''.join(self.strAP)
                        self.result.append(temp)
                    self.CalAllP1(first + 1, num)  # 递归调用,全排列后面的元素
                    self.SWAP(i, first)#用来复位的
  • 相关阅读:
    #define用法详解
    memchr函数
    aiohttp模块1 client
    asyncio标准库7 Producer/consumer
    asyncio标准库6 Threads & Subprocess
    asyncio标准库5 TCP echo client and server
    asyncio标准库4 asyncio performance
    asyncio标准库3 HTTP client example
    asyncio标准库2 Hello Clock
    asyncio标准库1 Hello World
  • 原文地址:https://www.cnblogs.com/shixisheng/p/7403003.html
Copyright © 2011-2022 走看看