华为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)
相关推荐
AC赳赳老秦5 小时前
OpenClaw email技能:批量发送邮件、自动回复,高效处理工作邮件
运维·人工智能·python·django·自动化·deepseek·openclaw
zhaoshuzhaoshu5 小时前
Python 语法之数据结构详细解析
python
hetao17338375 小时前
2026-04-09~12 hetao1733837 的刷题记录
c++·算法
6Hzlia6 小时前
【Hot 100 刷题计划】 LeetCode 136. 只出现一次的数字 | C++ 哈希表&异或基础解法
c++·算法·leetcode
AI问答工程师6 小时前
Meta Muse Spark 的"思维压缩"到底是什么?我用 Python 复现了核心思路(附代码)
人工智能·python
MWWZ6 小时前
最近的一些软件更新
opencv·算法·计算机视觉
CoovallyAIHub6 小时前
视频理解新范式:Agent不再被动看视频,LensWalk让它自己决定看哪里
算法·架构·github
CoovallyAIHub6 小时前
斯坦福丨AirVLA:将地面机械臂模型迁移至无人机实现空中抓取,成功率从23%提升至50%
算法·架构·github
zfan5207 小时前
python对Excel数据处理(1)
python·excel·pandas
小饕7 小时前
我从零搭建 RAG 学到的 10 件事
python