学习中的零碎的记录

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;
    }
}
相关推荐
郑州光合科技余经理13 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo12313 天前
matlab画图工具
开发语言·matlab
dustcell.13 天前
haproxy七层代理
java·开发语言·前端
norlan_jame13 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone13 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ40220549613 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月13 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_5312371713 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian13 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript
雪人不是菜鸡13 天前
简单工厂模式
开发语言·算法·c#