学习中的零碎的记录

1、python递归和非递归实现二分查找

python 复制代码
def binary_search(target, num_list):
    if len(num_list) == 0:
        return False
    left, right = 0, len(num_list)-1
    while(left < right):
        mid = (left + right)>>1
        if num_list[mid] == target:
            return True
        elif num_list[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return False

def binary_search_recursive(target, num_list):
    if len(num_list) == 0:
        return False
    mid = len(num_list) // 2
    if num_list[mid] == target:
        return True
    elif num_list[mid] < target:
        return binary_search_recursive(target, num_list[mid+1:])
    else:
        return binary_search_recursive(target, num_list[:mid])


num_list = [1,3,4,5,6,7,8,9]
print(binary_search_recursive(7, num_list))

2、撰写函数。如果文件被占用,则关闭文件句柄。

完美解决Pyinstaller 打包exe 出现PermissionError: [Errno 13] Permission denie 问题-CSDN博客

python 复制代码
import win32file
def file_occupied(file_name): # occupied:占用
    # 自己加的模块,防止出现错误:PermissionError: [Errno 13] Permission denied:exe_path
    # 如果检测到文件被占用,则关闭该文件句柄
    try:
        fp = open(file_name,'wb')
    except:
        vHandle = win32file.CreateFile(file_name, win32file.GENERIC_READ, 0, None,win32file.OPEN_EXISTING,win32file.FILE_ATTRIBUTE_NORMAL, None)
        win32file.CloseHandle(vHandle)
    else:fp.close()

3、python 使用 subprocess.run打包之后闪出弹框解决办法:

python 复制代码
result = subprocess.run(['nslookup', url], capture_output=True, text=True, shell=True)

之前没有加shell=True的时候,会出现弹框,加上这个之后再打包就没有弹框出现了。

4、LInux下使用c语言 关闭指定端口的程序

cpp 复制代码
// 结束指定端口进程
int killPortProcess(const char* targetPort) {
    // 构造Shell命令
    char command[100];
    snprintf(command, sizeof(command), "fuser -k -n tcp %s/tcp", targetPort);

    // 使用system调用执行Shell命令
    int status = system(command);

    // 检查命令执行结果
    if (status == 0) {
        printf("Processes on port %s have been terminated.\n", targetPort);
        return 0;
    } else {
        printf("No processes found on port %s.\n", targetPort);
        return -1;
    }
}
相关推荐
Yhame.5 分钟前
【使用层次序列构建二叉树(数据结构C)】
c语言·开发语言·数据结构
言之。11 分钟前
【Go语言】RPC 使用指南(初学者版)
开发语言·rpc·golang
投笔丶从戎1 小时前
Kotlin Multiplatform--01:项目结构基础
android·开发语言·kotlin
杜小暑2 小时前
动态内存管理
c语言·开发语言·动态内存管理
想不明白的过度思考者2 小时前
Java从入门到“放弃”(精通)之旅——JavaSE终篇(异常)
java·开发语言
我真的不会C2 小时前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio2 小时前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
火柴盒zhang2 小时前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
景天科技苑2 小时前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
阿让啊2 小时前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法