初探python

发布于 2022-08-18  264 次阅读


碎碎念

最后两天!两天!

Day12

ll两点才给我发题,只能先摸会python了

摸完第一阶段,题终于来了,康了两眼,简单题!

半个小时胡完,可以继续摸python了嘿嘿嘿

--------碎碎念到此结束,下面是正文(手动分割线)--------

phase1:熟悉语句

# 请仅使用一行语句求出三个数的最小平方和
def two_of_three(x, y, z):
    return x**2+y**2+z**2-max(x,y,z)**2

# 下降阶乘
def falling(n, k):
    ans=1
    for i in range(n-k+1,n+1):
        ans=ans*i
    return ans

# 判断一个函数是否有两个或者两个连续的8
def double_eights(n):
    while n!=0:
        if n%100==88:
            return True
        n/=10
    return False

if __name__ == '__main__':
    opt=input()
    if opt=='1':
        x,y,z=map(int,input().split())
        print(two_of_three(x,y,z))
    elif opt=='2':
        x,y=map(int,input().split())
        print(falling(x,y))
    elif opt=='3':
        x=int(input())
        print(double_eights(x))

phase2:递归操作

# 编写一个递归函数skip_add,它接受一个参数 n 并返回n + n-2 + n-4 + n-6 + ... + 0。假设 n 是非负数。
def skip_add(n):
    if n>0:
        return n+skip_add(n-2)
    else:
        return 0

# GCD,给出两个正整数,求出两个正整数的最大公约数
def gcd(a, b):
    if b==0:
        return a
    else:
        return gcd(b,a%b)

# 汉诺塔
def print_move(origin, destination):
    print("Move the top disk from rod", origin, "to rod", destination)

def move_stack(n, start, end):
    assert 1 <= start <= 3 and 1 <= end <= 3 and start != end, "Bad start/end"
    if n==1:
        print_move(start,end)
    else:
        mid=6-start-end
        move_stack(n-1,start,mid)
        print_move(start,end)
        move_stack(n-1,mid,end)

if __name__ == '__main__':
    opt=input()
    if opt=='1':
        x=int(input())
        print(skip_add(x))
    elif opt=='2':
        x,y=map(int,input().split())
        print(gcd(x,y))
    elif opt=='3':
        x,y,z=map(int,input().split())
        move_stack(x,y,z)

phase3:数据抽象

# 99乘法表
def nine_nine_multiplication_table():
    return '\n'.join([' '.join([f"{j}*{i}={i*j}" for j in range(1,i+1)]) for i in range(1,10)])

# 实现函数couple,它接受两个列表并返回一个列表,其中包含两个序列的第 i 个元素耦合在一起的列表。您可以假设两个序列的长度相同。
def couple(lst1, lst2):
    assert len(lst1) == len(lst2)
    return [[lst1[i],lst2[i]] for i in range(len(lst1))]

# 计算两地距离,找出两点中离某点更近的一点
def make_city(name, lat, lon):
    return [name, lat, lon]

def get_name(city):
    return city[0]

def get_lat(city):
    return city[1]

def get_lon(city):
    return city[2]

from math import sqrt
def distance(city1, city2):
    return sqrt((get_lon(city1)-get_lon(city2))**2+(get_lat(city1)-get_lat(city2))**2)

def closer_city(lat, lon, city1, city2):
    x=make_city('x',lat,lon)
    if distance(x,city1)>distance(x,city2):
        return get_name(city2)
    else:
        return get_name(city1)

if __name__ == '__main__':
    opt=input()
    if opt=='1':
        print(nine_nine_multiplication_table())
    elif opt=='2':
        lst1 = ['c', 6]
        lst2 = ['s', '1']
        print(couple(lst1,lst2))
    elif opt=='3':
        city1 = make_city('city3', 6.5, 12)
        city2 = make_city('city4', 2.5, 15)
        print(distance(city1, city2))
    elif opt=='4':
        berkeley = make_city('Berkeley', 37.87, 112.26)
        stanford = make_city('Stanford', 34.05, 118.25)
        print(closer_city(38.33, 121.44, berkeley, stanford))
        bucharest = make_city('Bucharest', 44.43, 26.10)
        vienna = make_city('Vienna', 48.20, 16.37)
        print(closer_city(41.29, 174.78, bucharest, vienna))

phase4:高阶函数

def count_cond(condition):
    return lambda n:sum([condition(n,i) for i in range(1,n+1)])

if __name__ == '__main__':
    count_factors = count_cond(lambda n, i: n % i == 0)
    print(count_factors(2))
    print(count_factors(4))
    print(count_factors(12))
    is_prime = lambda n, i: count_factors(i) == 2
    count_primes = count_cond(is_prime)
    print(count_primes(2))
    print(count_primes(3))
    print(count_primes(4))
    print(count_primes(5))
    print(count_primes(20))

phase5:迭代生成

# 实现count,它接受一个迭代器t并返回该值x出现在 的第一个n元素中的次数t
def count(t,n,x):
    c=0
    for i in range(n):
        p=next(t)
        if x==p:
            c=c+1
    return c

# 实现生成器函数scale(it, multiplier),它产生给定迭代的元素it,按multiplier.
# 同时也希望你尝试使用yield from语句编写这个函数!
def scale(it, multiplier):
    yield from [i*multiplier for i in it]

if __name__ == '__main__':
    opt=input()
    if opt=='1':
        s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
        print(count(s, 10, 9))
        s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
        print(count(s2, 3, 10))
        s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
        print(count(s, 1, 3))
        print(count(s, 4, 2))
        print(next(s))
        s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])
        print(count(s2, 6, 6))
    elif opt=='2':
        m = scale(iter([1, 5, 2]), 5)
        print(type(m))
        print(list(m))