华为OD试题二(文件目录大小、相对开音节、找最小数)

1. 文件目录大小

复制代码
题目描述:
一个文件目录的数据格式为:目录id,本目录中文件大小,(子目录id
列表)。其中目录id全局唯一,取值范围[1,200],本目录中文件大小范
围[1,1000],子目录id列表个数[0,10]

例如:1 20 (2,3)表示目录1中文件总大小是20,有两个子目录,id分	
别是2和3

现在输入一个文件系统中所有目录信息,以及待查询的目录 id ,返
回这个目录和及该目录所有子目录的大小之和。

参考代码:

python 复制代码
# 核心 递归遍历目录
# 测试数据
test_data1 = [
	"3 15 ()",
	"1 20 (2)",
	"2 10 (3)"
]
test_data2 = [
	"4 20 ()",
	"5 30 ()",
	"2 10 (4,5)",
	"1 40 ()"
]

def fun(index,data):
	# 计数器 统计文件总大小
	count = 0
	
	for line in data:
		x,y,z = line.split()
		if x == index:
			count += int(y)
			z = z[1:-1]
			if z == '':
				return count
			else:
				if ',' in z:
					index_list = z.split(',')
					for _ in index_list:
						count += fun(_)
				else:
					count += fun(z)
	return count
	
res = fun('2',test_data2)
print(res)
	

2. 相对开音节

复制代码
题目描述:
给定一个字符串,以空格为分隔符,反转每个单词中的字母,若单词
中包含如数字等其他非字母时不进行反转。
反转后计算其中含有相对开音节结构的子串个数(连续的子串中部分
字符可以重复)。

相对开音节构成的结构为辅音+元音(aeiou)+辅音(r除外)+e,常见
的单词有bike、cake等。

参考代码:

python 复制代码
# 测试数据
test_data = "!ekam a ekekac"
# 字母集
char_str = "abcdefghijklmnopqrstuvwxyz"
char_list = list(char_str)
# 开音节字母集
k_char_str = "bcdfghjklmnpqrstvwxyz"
k_char_list = list(k_char_str)

def fun(temp_str):
	# 字符分割
	temp_str_list = temp_str.split()
	# 存放新的 字符
	new_list = []
	
	for s in temp_str_list:
		# 该标志用于控制字符串是否反转
		flag = True
		for _ in s:
			if _ not in char_str:
				flag = False
				break
		if flag:
			s_list = list(s)
			s_list.reverse()
			new_list.append(''.join(s_list))
		else:
			new_list.append(s)
	
	# 统计 开音节个数
	count = 0
	for ele in new_list:
		for cur,s in enumerate(ele):
			#  开音节字母判断
			if cur <= len(ele) - 4: 
				if ele[cur] in k_char_list and ele[cur+1] in ['a','e','i','o','u'] and(ele[cur+2] in k_char_list and ele[cur+2] != 'r') and ele[cur+3] == 'e':
					count += 1

	# 打印开音节个数
	print(count)

fun(test_data)		

3. 找最小数

复制代码
题目描述:
给一个正整数NUM1,计算出新正整数NUM2,NUM2为NUM1中移
除N位数字后的结果,需要使得NUM2的值最小。
输入: 2615371
			4
输出: 131
说明: 移除 2,6,5,7 这4个数字,剩下 1、3、1 按原有顺序排列组成
131 为最小值。

算法可以参考下图:

参考代码:

python 复制代码
# 测试数据
test_data = "2615371"
# 移除的个数
num = 4

def fun(temp_str,num):
	# 字符转列表
	str_list = list(temp_str)
	# 字符串 转成 整形
	str_list = [ int(i) for i in str_list ]
	# 用于确定 要删除数的范围
	count = len(str_list) - num
	cur = -(count - 1)
	
	# 存放最后结果
	t_str = ''
	pos = -1
	for i in range(count):
		temp_list = str_list[pos+1:cur]
		if len(temp_list) == 0:
			ele = str_list[-1]
		else:
			ele = min(temp_list)
			pos = str_list.index(ele)
		t_str += str(ele)
		cur += 1
	print(t_str)
	
fun(test_data,num)
相关推荐
肖永威1 分钟前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ17 分钟前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha25 分钟前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全
abluckyboy39 分钟前
Java 实现求 n 的 n^n 次方的最后一位数字
java·python·算法
园小异1 小时前
2026年技术面试完全指南:从算法到系统设计的实战突破
算法·面试·职场和发展
m0_706653231 小时前
分布式系统安全通信
开发语言·c++·算法
喵手1 小时前
Python爬虫实战:构建各地统计局数据发布板块的自动化索引爬虫(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集数据csv导出·采集各地统计局数据发布数据·统计局数据采集
天天爱吃肉82182 小时前
跟着创意天才周杰伦学新能源汽车研发测试!3年从工程师到领域专家的成长秘籍!
数据库·python·算法·分类·汽车
alphaTao2 小时前
LeetCode 每日一题 2026/2/2-2026/2/8
算法·leetcode
m0_715575342 小时前
使用PyTorch构建你的第一个神经网络
jvm·数据库·python