学习中的零碎的记录

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;
    }
}
相关推荐
m0_547486661 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
MC皮蛋侠客1 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
布莱克6052 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh3 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
lzhdim3 小时前
13、JavaScript事件循环机制 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
zbyyd4 小时前
Linux 多线程:互斥锁 &amp; 信号量
linux·c语言·开发语言·网络
wuyk5554 小时前
从零吃透Modbus通信|第3章:STM32 Modbus RTU主机
服务器·开发语言·网络·数据结构·stm32·单片机·嵌入式硬件
one3125 小时前
高精度MEMS电容式倾角传感器:原理、结构与Python三维可视化
开发语言·python
程序员小八7775 小时前
Go 语言特性
开发语言·后端·golang
INGNIGHT6 小时前
1908 · 布尔表达式求值(stack单调栈&dfs)
开发语言·c++·算法