python3随笔

1

'''
如果 a+b+c=1000,且 a^2+b^2=c^2(a,b,c 为自然数),如何求出所有a、b、c可能的组合?
'''

for a in range(1000):
    for b in range(1000):
        c = 1000 - a - b
        if c >= a and c >= b and a ** 2 + b ** 2 == c ** 2:
            print("结果:", a, b, c)

'''
结果: 0 500 500
结果: 200 375 425
结果: 375 200 425
结果: 500 0 500
'''

2

'''
给定字符串str 和一个整数k。如果str中恰好出现了连续的k个0,则将k个0删除。 
比如,给定字符串str = “0000fw300001203h000ui0000re3_0000”,k=4。
返回的结果为“fw31203h000uire3_”。
'''

the_str = '0000fw300001203h000ui0000re3_0000'
k = 4


def fun(source_str, count):
    '''
    剔除元素
    :param source_str:
    :param count:
    :return:
    '''
    rep = '0'*count
    the_str = source_str.replace(rep, '')
    return the_str


res = fun(the_str, k)
print("结果:", res)

3

'''
给定一个数组 strs,其中的数据都是字符串,给定两个字符串 str1,str2。
如果这两个字符串都在 strs数组中,就返回它们之间的最小距离;
如果其中任何一个不在里面,则返回 -1;如果两个字符串相等,则返回 0。
'''
import re


def fun(source_str, str1, str2):
    '''
    验证数据
    :param source_str:
    :param str1:
    :param str2:
    :return:
    '''
    the_source_str = str(source_str)
    res1 = re.findall(str1, the_source_str)
    res2 = re.findall(str2, the_source_str)
    # 均存在
    if res1 and res2:
        if str1 == str2:
            if len(res1) >= 2:
                return 0  # 字符相同且均存在
        else:
            index1 = source_str.index(str1)
            index2 = source_str.index(str2)
            res = abs(index2 - index1)
            return res  # 字符不同且均存在
    # 只存在一个
    if res1 or res2:
        return -1
    # 不存在
    if not res1 and not res2:
        return "不存在"


the_str = ['a', 'b', 'c', 'cc']
str1 = 'a'
str2 = 'a'
res = fun(the_str, str1, str2)
print(res)

4

'''
给定两个字符串,str1,str2,判断两个字符串中出现的字符是否全部种类一样,数量一样。
例如:
str1 = “apple”, str2 = “paple”, 返回 True;
str1 = “pear”, str2 = “bears”, 返回 False。

ps: is 与 == 区别:
is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。
'''

str1 = 'apple'
str2 = 'apple'
if str1 == str2:
    print('true')
else:
    print('false')


'''
import hashlib
>>> a = 'adfd'
>>> b = 'adfd'
>>> c = 'addf'
>>> m = hashlib.md5(a.encode('utf-8'))
>>>
>>> m.hexdigest()
'84f57b4d03744d0bf803a1c9736c2b46'
>>>
>>> m = hashlib.md5(b.encode('utf-8'))
>>> m.hexdigest()
'84f57b4d03744d0bf803a1c9736c2b46'
>>> m = hashlib.md5(c.encode('utf-8'))
>>> m.hexdigest()
'240a8254415e3c29e645fb3fb4343bc0'
>>>
'''

5

'''
给定一个列表,判断其中所有的字符是不是都只出现过一次
'''
import re

the_list = ["abc", "bcd", 'efg', 'fgh', 'ijk', 'jk']
the_str = str(the_list)
# 剔除数据
the_str = the_str.replace('[', '')
the_str = the_str.replace(']', '')
all = set(the_str)  # 所有不重复的元素
result1 = []
result2 = []
for i in all:
    res = the_str.count(i)
    if res == 1:
        result1.append(i)
print("count()结果:", result1)


for i in all:
    if i != '[' and i != ']':
        res = re.findall(i, the_str)
        if len(res) == 1:
            result2.append(i)
print("re.findall结果:", result2)

6

'''
计算斐波那契数列。具体什么是斐波那契数列,那就是0,1,1,2,3,5,8,13,21,34,55,89,144,233。
'''


def fib(max):
    '''
    斐波拉契数列
    :param max:
    :return:
    '''
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'


if __name__ == "__main__":
    res = []
    for n in fib(6):
        res.append(n)
    print(res)


7

'''
写一个方法,传入一个整型列表,计算其中不重复数字的个数并返回
'''


def fun(a):
    '''
    获取不重复的元素
    :param a:
    :return:
    '''
    aa = set(a)  # 不重复的数据集
    res = []  # 存放结果集
    for i in aa:
        if a.count(i) == 1:  # 元素出现次数为1
            res.append(i)
    return res


if __name__ == "__main__":
    a = [1, 2, 3, 5, 8, 5, 4, 1, 10, 11, 5]
    res = fun(a)
    print(res)

8

'''
随机获取一个字符串列表中的字符串,求获取一千次的情况下,各字符串被随机到的次数。
'''

__author__ = 'llf'

import random
from collections import Counter

c = Counter()
ll = ['a', 'b']

for i in range(1000):
    a = random.choice(ll)
    c[a] = c[a] + 1

print('结果:', type(c), dir(c), c)

'''
<class 'collections.Counter'> 
[
'clear', 'copy', 'elements', 'fromkeys', 'get', 'items', 
'keys', 'most_common', 'pop', 'popitem', 'setdefault', 
'subtract', 'update', 'values'
] 
'''


猜你喜欢

转载自blog.csdn.net/llf_cloud/article/details/83547142
今日推荐