Python 异步编程 事件循环(Event Loop):事件循环是asyncio的核心,它负责调度和执行异步任务。当遇到await表达式时,事件循环会暂停当前协程,转而执行其他可运行的协程。 协程(Coroutine):协程是asyncio中的基本执行单元,使用async def定义。协程可以通过await关键字挂起自身,让出控制权给事件循环。 基本操作: import asyncio async fetch_data(url): await asyncio.sleep(2) return "Data" # 主协程 async def main(): tasks = [ fetch_data("https://1.com"), fetch_data("https://2.com"), fetch_data("https://3.com") ] results = await asyncio.gather(*tasks) print(f"All results: {results}") asyncio.run(main()) GC机制、循环引用 x = [] y = [x] x.append(y) 装饰器 单例 C++ class与struct区别:使用 class 时,类中的成员默认都是 private 属性的;而使用 struct 时,结构体中的成员默认都是 public 属性的。 同理,class继承默认是 private 继承,而 struct 继承默认是 public 继承。 另外,class成员可以用模板,struct不行 内存管理 栈与堆:栈每个线程独立,没有碎片,指针向下移动,速度快;堆由程序员通过new/malloc手动管理,涉及查找内存块、分割/合并内存块、处理碎片等,速度慢。 #include <iostream> #include <chrono> void stackMemory() { auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1000000; i++) { int arr[100]; // stack } auto end = std::chrono::high_resolution_clock::now(); std::cout << "Stack Time: " << std::chrono::duration_cast<std::chrono::microseconds>(end-start)....
秋招记录
本篇大概分两部分,一部分是刷八股文,一部分是应试进度。本文不定时更新。就我找summer的体验来看,找工作真的非常痛苦,一方面是因为我面试时很容易紧张+大脑空白,另一方面是我的开发能力确实也不咋地,但是算法研发又不是那么熟悉。 八股文 又要开始背八股文了,先背背看看吧。虽然我觉得被拷打很丢人,而且背八股文真的很死板很无趣,但是也没办法,找工作就是这样的。本文不定时更新,包含被拷打的问题以及自己平时不太会的问题。 链表环的入口点或交点 使用一个快指针和慢指针,其中快指针速度是慢指针的两倍,然后快指针和慢指针会相遇在环内。假设链表里开头到环入口点长度为A,相遇处距离环的入口点长度为B,环里还剩下C,则有 $$ A + B = A + n(B+C) + B $$ 其中n为快指针转了n圈。此时有$A = n(B+C) - B = (n-1)(B+C) + C$。再搞一个head指针与慢指针一起走,它俩会相遇在环的入口点,因为head走了A,而慢指针走了A也就是转了n-1圈走了C,两者会相遇。 同理,问两个链表相交的点也一样。初始设两个指针分别指向两个链表的头,一起移动(如果到末尾了换成另一个链表的头),直到相交,两者会走在交点处走过同样的距离(A+C+B 与 B + C + A)。 快排与归并与堆排 void quicksort(int a[], int l, int r){ if(l < r){ int i = l, j = r, pivot = s[l]; while(i < j){ while(j > i && s[j] > pivot)j--; if(j > i)s[i++] = s[j]; while(j > i && s[i] < pivot)i++; if(j > i)s[j--] = s[i]; } s[i] = pivot; //别忘了这句 quicksort(a, 0, i-1); quicksort(a, i+1, r); } } void mergesort(int a[], int l, int r){ if(l >= r)return; int mid = (l + r) / 2; // l + (r - l) / 2 可以防止l + r过大溢出 mergesort(a, l, mid); mergesort(a, mid+1, r); merge(a, l, mid, r); } void merge(int a[], int l, int mid, int r){ int *temp = new int[r - l + 1]; int cur = 0; int h1 = l, h2 = mid+1; while(h1 <= mid && h2 <= r){ if(a[h1] < a[h2])temp[cur++] = a[h1++]; else temp[cur++] = a[h2++]; } while(h1 <= mid)temp[cur++] = a[h1++]; while(h2 <= r)temp[cur++] = a[h2++]; for(int i = low; i <= r; i++){ a[i] = temp[i - low]; } delete []temp; } void MaxHeap(int arr[], int now, int heap_size){ int left_son = now * 2+1, right_son = left_son + 1; while(left_son < heap_size){ int largest = right_son < heap_size && arr[left_son] < arr[right_son] ?...
两个crypto题解
这两个题目都不是数学题,是与python相关的题目。 Bruce Schneier’s Password 本题来源CryptoHack的Misc部分,分值100pt,属于简单题。题目代码如下: import numpy as np import random import re from Crypto.Util.number import isPrime from utils import listener FLAG = "crypto{????????????????????????????????????????}" SCHNEIER_FACTS = [ "If Bruce Schneier had designed the Caesar cipher, Caesar would still be alive today.", "It is impossible to hide information from Bruce Schneier. Not even by destroying it.", "Bruce Schneier can fill a knapsack in constant time without exceeding the weight.", "Bruce Schneier writes his books and essays by generating random alphanumeric text of an appropriate length and then decrypting it....
My First Post
测试博客喵! 因为发现自己记忆力大不如前了,很多看过的知识做过的题都不记得了,所以开个赛博笔记本。。(老了) 试着放一只猫猫在这里: \`*-. ) _`-. . : `. . : _ ' \ ; *` _. `*-._ `-.-' `-. ; ` `. :. . \ . \ . : .-' . ' `+.; ; ' : : ' | ; ;-. ; ' : :`-: _.`* ; [bug] .*' / .*' ; .*`- +' `*' `*-* `*-* `*-*' 试着放一只Bocchi酱在这里: