编程语言
首页 > 编程语言> > python基本功

python基本功

作者:互联网

python基本功
逻辑锻炼

# n个人排成一列,每人手里面有0~n-1个球,如果有两人手中拥有同样数量的球,返回球的数量(如果有多组只需返回任意一组即可),否则返回-1
# 例如:给定lst=[2,4,1,0,5,3,2,3]表示8人手中拥有的球的数量,返回值输出2或者3都是可行的
import random
import string
from time import sleep
def search(lst):
    n = 1
    for i in lst:
        for j in range(n, len(lst)):
            if i == lst[j]:
                print(i)
        n += 1

lst = [2, 4, 1, 0, 5, 3, 3, 3]
search(lst)
def search_char(lst):
    for i in range(len(lst)):
        char = lst[len(lst) - i-1]
        for j in range(0, len(lst) - i - 1):

            if lst[j] == char:
                print("sss:", char)
                break
search_char(lst)

2,斐波那契数列 0 1 1 2 3 5 7 12 19

# 3,斐波那契数列 0 1 1 2 3 5 7 12 19
def fib(n):
    if n == 1:
        print([0])
        return
    if n == 2:
        print([0, 1])
        return

    a, b = 0, 1
    l = []
    while a < n:  # for i in range(n)
        l.append(a)
        a, b = b, a + b
    print(l)
fib(2)

def fibo(n):
    a, b = 0, 1
    aws = []
    for i in range(n):
        aws.append(a)
        a, b = b, a + b
    return aws
print(fibo(2))

# 3 fib
def fibb():
    a,b = 0,1
    while True:
        a,b = a+b,a
        yield a
for f in fibb():
    f_li=[]
    if f > 10:
        break
    print("ssss",f)
    f_li.append(f)

3,找质数


# 找质数   后面的else是精髓
def demo(m, n):
    if m < 1 or m > n or n > 1000000:
        return -1
    t = []
    for i in range(m, n):
        for j in range(2, i):
            if i % j == 0:
                break
        else:
            t.append(i)
    return t
y = demo(3, 10)
print(y)

4,找出一个字符串中第一个不重复的字符串,并返回。如”abcacd”,第一个不重复的字符为”b”。请写一个方法实现

词频法

# 找出一个字符串中第一个不重复的字符串,并返回。如”abcacd”,第一个不重复的字符为”b”。请写一个方法实现
def search_str(str: str) -> str:
    dict_str = {}
    for i in range(len(str)):
        if str[i] in dict_str:
            dict_str[str[i]] += 1
        else:
            dict_str[str[i]] = 1
    for i in range(len(str)):
        if dict_str[str[i]] == 1:
            # return (str[i])
            print(str[i])
            print(i)
            break
search_str("abcacfd")

5,区分奇偶数,并排序,

# 奇偶两边站
# 1,空间换时间:直接两个新list
# 2,双指针,
def demo(l: list) -> list:
    n = len(l)
    a = 0
    b = n - 1
    while a < b:
        if l[a] % 2 == 0 and l[b] % 2 != 0:
            l[a], l[b] = l[b], l[a]
        else:
            if l[a] % 2 != 0:
                a += 1
            if l[b] % 2 == 0:
                b -= 1
    print(l)
    i = 0
    for i in range(n):
        if l[i] % 2 == 0:
            print(i)
            i = i
            break
    for h in range(i):
        for j in range(i - h - 1):
            if l[j] > l[j + 1]:
                l[j], l[j + 1] = l[j + 1], l[j]
    for k in range(n - i):
        for j in range(i, n - 1):
            if l[j] > l[j + 1]:
                l[j], l[j + 1] = l[j + 1], l[j]
de = [3, 4, 4, 6, 5,34,54,674,345,234, 6, 4, 1]
demo(de)
print(de)

标签:python,lst,len,range,str,print,基本功,def
来源: https://blog.csdn.net/weixin_44080239/article/details/118879709