05、算法练习

1、判断一个字符串有多少个数字

有多少个字母
有多少个空格
有多少个其他字符
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2018/4/11 19:19
# @Author   : cjl
# @Name     : test8.py
'''
判断一个字符串有多少个数字
有多少个字母
有多少个空格
有多少个其他字符
'''
alpha,dig,space,other = 0, 0, 0, 0

while 1:
    strings = input("Please input a string(quit will be exit):  ")
    if strings.strip() == "quit":
        exit(1)
    for i in strings:
        if i.isdigit():
            dig += 1
        elif i.isspace():
            space += 1
        elif i.isalpha():
            alpha += 1
        else:
            other += 1
    print (alpha,space,dig,other)

思路:

  使用for循环取出每个字符使用字符串方法做判断,然后分别统计累加对应的数量。最后打印结果。

  

2、ABCD*9=DCBA,A=?,B=?,C=?,D=?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2018/4/11 19:36
# @Author   : cjl
# @Name     : test9.py
'''
ABCD*9=DCBA,A=?,B=?,C=?,D=? 答案: a=1,b=0,c=8,d=9.
'''
for A in range(1,10):
    for B in range(0, 10):
        for C in range(0, 10):
            for D in range(1,10):
                if ((1000* A + 100*B + 10*C + D)*9 == 1000*D + 100*C + 10*B + A ):
                    print("A = {0}".format(A))
                    print("B = {0}".format(B))
                    print("C = {0}".format(C))
                    print("D = {0}".format(D))
                    print("{0}{1}{2}{3}x9={3}{2}{1}{0}".format(A, B, C, D))

思路:

  for循环其实比较简单,但是写程序前对算法要绝对重视。以此为例说明算法的重要性,9*D=A,而A又在1到9,那么可以确定A的值为9,D=1。可以大幅度降低性能消耗。

3、所谓九宫格问题就是在一个行数列数相等切都为奇数的方阵中,每行数字之和,每列数字之和,两个对角线数字之和都相等。

九宫格
  1-9
  -------------
  | A | B | C |
  | D | E | F |
  | G | H | I |
  -------------
  所有的横竖斜线加起来都等于15
  A 1-9
  B 1-9 除A
  C 1-9 除A,B

 

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time     : 2018/4/11 19:36
# @Author   : cjl
# @Name     : test9.py
'''
九宫格
1-9
                -------------
                | A | B | C |
                | D | E | F |
                | G | H | I |
                -------------
所有的横竖斜线加起来都等于15
A  1-9
B  1-9 除A
C  1-9 除A,B
'''

number = list()
for i in range(1, 10):
    number.append(i)
print(number)


count = 1
for A in number:
    a = number.copy()
    a.remove(A)
    for B in a:
        b = a.copy()
        b.remove(B)
        for C in b:
            c = b.copy()
            c.remove(C)
            for D in c:
                d = c.copy()
                d.remove(D)
                for E in d:
                    e = d.copy()
                    e.remove(E)
                    for F in e:
                        f = e.copy()
                        f.remove(F)
                        for G in f:
                            g = f.copy()
                            g.remove(G)
                            for H in g:
                                h = g.copy()
                                h.remove(H)
                                for I in h:
                                    if (A+B+C == D+E+F == G+H+I == A+D+G == B+E+H == C+F+I == A+E+I == G+E+C == 15):
                                        print('''
                                        第{9}种例子
                                        -------------
                                        | {0} | {1} | {2} |
                                        | {3} | {4} | {5} |
                                        | {6} | {7} | {8} |
                                        -------------'''.format(A,B,C,D,E,F,G,H,I,count))
                                        count += 1

思路:

  其实除了这种穷举法不断去试,还可以使用递归函数进行计算,这是一个典型的递归用例。

 

 

4、题目1:阶乘之和:1!+2!+3!+。。。。+100!

'''
0! +1! +2! + 3! +4! + 5! + n!
1 + 1 + 2 + 6 + …… + n*(n-1)*(n-2)……*1
'''

def jc(n):
    result = 1
    if n == 0:
        return result
    else:
        for i in range(1, n+1):
            result *= i
        return result


n = input("Plese inpurt number n:")
count = 0
for i in range(0, int(n)+1):
    count += jc(i)

print("count = {0}".format(count))

思路:

  使用函数和for循环,使用函数求的每一个数的阶乘,然后套用for循环相加累积求和。

5、编码解码

在计算机内存中,统一使用Unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为UTF-8编码。

用记事本编辑的时候,从文件读取的UTF-8字符被转换为Unicode字符到内存里,编辑完成后,保存的时候再把Unicode转换为UTF-8保存到文件:

rw-file-utf-8

浏览网页的时候,服务器会把动态生成的Unicode内容转换为UTF-8再传输到浏览器:

web-utf-8

所以你看到很多网页的源码上会有类似<meta charset="UTF-8" />的信息,表示该网页正是用的UTF-8编码。

 

 

编码:支持中文的编码utf-8, gbk, gb2312

>>> 'ABC'.encode('ascii')
b'ABC'
>>> '中文'.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'
>>> '中文'.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

解码:

>>> b'ABC'.decode('ascii')
'ABC'
>>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
'中文'

注意:

  不写python代码排头,就会报错。

    代码文件被执行时就会出错,就是编码出了问题。python默认将代码文件内容当作asci编码处理,但asci编码中不存在中文,因此抛出异常。 解决问题之道就是要让python知道文件中使用的是什么编码形式,对于中文,可以用的常见编码有utf-8,gbk和gb2312等。只需在代码文件的最前端添加如下: #-*- coding:utf-8 -*-

posted @ 2018-04-11 21:01  有人喜欢蓝  阅读(215)  评论(0编辑  收藏  举报