随笔分类 -  python 简单算法

摘要:def huiwen_1(x: int): # 通过字符串翻转获取 return str(x) == str(x)[::-1] print(huiwen_1(123321)) def huiwen_2(x: int): l = str(x) h = len(l) // 2 return l[:h] 阅读全文
posted @ 2020-06-14 20:30 hchan 阅读(172) 评论(0) 推荐(0)
摘要:不使用正则表达式的方式: def is_ip(ip: str) -> bool: return True if [True] * 4 == [x.isdigit() and 0 <= int(x) <= 255 for x in ip.split(".")] else False 使用正则表达式的方 阅读全文
posted @ 2020-06-02 15:57 hchan 阅读(329) 评论(0) 推荐(0)
摘要:废话不多说 直接上代码 # 用于生生一个类似于二叉树的数据 class Node: def __init__(self, value=None, left=None, right=None): self.value = value self.left = left self.right = righ 阅读全文
posted @ 2020-05-29 22:45 hchan 阅读(685) 评论(0) 推荐(0)
摘要:def sum_str(a: str, b: str) -> str: max_str, min_str = (a, b) if len(a) > len(b) else (b, a) c = 0 tmp_num = False for i in range(len(min_str)): d = i 阅读全文
posted @ 2020-05-29 19:34 hchan 阅读(1533) 评论(0) 推荐(0)
摘要:def shuchu(num): for i in range(num): for j in range(i + 1): print(j + 1, end="") print("") shuchu(8) 阅读全文
posted @ 2020-05-21 17:01 hchan 阅读(1007) 评论(0) 推荐(0)
摘要:def quick_sort(list_a: list) -> list: if len(list_a) > 0: first = list_a[0] left = quick_sort([l for l in list_a[1:] if l < first]) right = quick_sort 阅读全文
posted @ 2020-05-21 16:53 hchan 阅读(1059) 评论(0) 推荐(0)
摘要:def sum(num: int) -> int: res = 0 while num != 0: res += num % 10 num //= 10 return res print(sum(789)) 阅读全文
posted @ 2020-05-21 16:48 hchan 阅读(353) 评论(0) 推荐(0)
摘要:def cheng(a: int, b: int) -> int(): min_num = min(a, b) max_num = max(a, b) res = 0 for i in range(0, min_num): res += max_num return res print(cheng( 阅读全文
posted @ 2020-05-21 16:45 hchan 阅读(426) 评论(0) 推荐(0)
摘要:import argparse def parser(): ap = argparse.ArgumentParser() ap.add_argument("-a", "--a", required=True, help="边a的边长") ap.add_argument("-b", "--b", re 阅读全文
posted @ 2020-05-11 13:27 hchan 阅读(333) 评论(0) 推荐(0)
摘要:直接上代码 a = [1,2,5,4,6,7,390] def maopao(a): for i in range(0,len(a)-1): for j in range(0,len(a)-1-i): if a[j]>a[j+1]: a[j],a[j+1] = a[j+1],a[j] print(a 阅读全文
posted @ 2020-05-11 13:26 hchan 阅读(113) 评论(0) 推荐(0)