华为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)
相关推荐
luthane26 分钟前
python 实现average mean平均数算法
开发语言·python·算法
静心问道31 分钟前
WGAN算法
深度学习·算法·机器学习
码农研究僧31 分钟前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录
Ylucius34 分钟前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
凡人的AI工具箱1 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
sleP4o1 小时前
Python操作MySQL
开发语言·python·mysql
杰九1 小时前
【算法题】46. 全排列-力扣(LeetCode)
算法·leetcode·深度优先·剪枝
manba_1 小时前
leetcode-560. 和为 K 的子数组
数据结构·算法·leetcode
liuyang-neu1 小时前
力扣 11.盛最多水的容器
算法·leetcode·职场和发展
忍界英雄1 小时前
LeetCode:2398. 预算内的最多机器人数目 双指针+单调队列,时间复杂度O(n)
算法·leetcode·机器人