2021 Huawei Soft Challenge Questions_ Thinking Analysis-Real-time Update, Do More and Less (3)

2021 Huawei Soft Challenge Questions_ Thinking Analysis-Real-time Update, Do More and Less (3)



Edible suggestions for this column

  • With more and more articles, how to eat the content of this column, the author hereby makes the following suggestions, hoping to save precious time and energy for all participants:
  • The first part of this column is a preliminary exploration of ideas, suitable for friends who do not have much ideas or have a vague grasp of the competition questions. Of course, at the end of the schedule, if a friend has finished submitting and wants to review the question again, he can also review this article .
  • As for the second article, I do suggest that you can browse it in detail. It contains my main thoughts on this topic. At least compared to similar articles, I personally think that this idea is the most likely to be implemented (not talking about algorithm efficiency).
  • The next three , four , and five three articles (the following will be the case) are mainly for the code implementation of the second article. You can read the latest code as much as possible. After all, it is also written about the things that found the previous problems. It’s not the first time, I hope you forgive me~
  • Of course, the above is just my little suggestion. If you have a friend who wants to fully understand the entire mental journey of my problem-solving, it’s okay to read it one by one~

Preface

  • Things in life are always unpredictable. Fortunately, I am grateful for the support of every friend, so that I can constantly squeeze my potential in the mediocrity, and strive to live up to every expectation~
  • Recently, there is always a slack time only in the evening. I originally expected to make up the pseudo-code that you owed the day before, but when I ran the code yesterday, I realized that the input result was somewhat different from what I expected. However, it was a blessing to write only the code yesterday, but not to write the comments. After several debugging, the problem was solved, but the time was not far from midnight, so I made up a detailed note decisively, so I’m in arrears again today, let’s look at Haihan~
  • It is expected that tomorrow should be the most abundant day in the near future, and I hope to provide more information for you and add a joy to the event^-^

Updated March 15


1. Enter the code + detailed comments yesterday

# 代码解决2021华为软件挑战赛中的数据输入问题
# 所有注释为了便于说明
# 以training-1.txt中的数据为例
# !!!*****!!! 为昨日代码的出错点
# 技艺不精,私事繁忙,照顾不周,多担待!

# 服务器类
class Host:
    hostType = ''           #服务器类型
    hostCpuNum = 1          #服务器CPU数
    hostMemSize = 1         #服务器内存容量
    hostHardWareCost = 1    #服务器硬件开销
    hostDayCost = 1         #服务器每日开销
    hostXingJiaBi = 1       #服务器性价比:(硬件开销*(核数/核数+内存容量)/核数) 
    						# + (硬件开销*(内存容量/核数+内存容量)/内存容量)
    hostCoreMemBi = 1       #服务器核内比:核数/内存容量

    # 计算性价比
    # !!!认真观察的道友应该能发现,此处公式与昨日略有不同
    # 但与昨日数值结果相同,可自己细品,理解鄙人定义的性价比的意义!!!
    def xingJiaBi(self):
        self.hostXingJiaBi = ((self.hostHardWareCost * self.hostCpuNum / (self.hostCpuNum + self.hostMemSize)) / self.hostCpuNum) + ((self.hostHardWareCost * self.hostMemSize / (self.hostCpuNum + self.hostMemSize)) / self.hostMemSize)

    # 计算核内比
    def coreMemBi(self):
        self.hostCoreMemBi = self.hostCpuNum / self.hostMemSize
# 虚拟机类
class Vm:
    vmType = ''
    vmCpuNum = 1
    vmMemSize = 1
    vmDoubleOrNot = 1
    vmCoreMemBi = 1

    # 计算核内比
    def coreMemBi(self):
        self.vmCoreMemBi = self.vmCpuNum / self.vmMemSize

# 每日请求列表类
class DayRequire:
    requireNum = 0
    requireType = ''
    vmType = ''
    vmId = ''
    # !!!昨日出错重点,以前一直没发现,pyhton类中定义的列表是全局变量!!!
    # 解决方法:通过初始化方法,将列表定义为self的局部变量
    def __init__(self):
        self.addRequireList = []
        self.delRequireList = []

    # !!!增加了两个为列表赋值的函数,只为美化代码!!!
    def addRequireListAppend(self):
        self.addRequireList.append((self.requireType, self.vmType, self.vmId))

    def delRequireListAppend(self):
        self.delRequireList.append((self.requireType, self.vmId))

# 一次性读入全部文件
def readtxt(filename):
    try:
        f = open(filename, "r")
        line = f.read()
        return line
    finally:
        f.close()

# 将输入的数据处理,输出为hostListAll, vmListAll, dayListAll列表
# hostListAll, vmListAll 分别代表服务器总表与虚拟机总表
# dayListAll中存储800天的数据,每一个列表元素为一个类(oneDay)
def inputData(allData):
    flag = 0
    for i in range(len(allData)):
        if allData[i].isdigit():
            flag += 1
            # flag=1 准备处理出服务器总表
            if flag == 1:
                hostTypeNum = int(allData[i])
                hostListAll = [allData[i + j + 1] for j in range(hostTypeNum)]
            # flag=2 准备处理出虚拟机总表
            elif flag == 2:
                vmTypeNum = int(allData[i])
                vmListAll = [allData[i + j + 1] for j in range(vmTypeNum)]
            # flag=3 接收所有天的全部请求 本页代码中的小弯弯
            elif flag == 3:
                dayNum = int(allData[i])
                flag1 = 0
                dayListAll = [0 for j in range(dayNum)] #存DayRequire类
                # 遍历800日后的全部输入 可体会一下同级下的break
                for j in range(i + 1, len(allData)):
                    if allData[j].isdigit():
                        flag1 += 1
                        # 读不懂可解开下句封印 尝试观察输出
                        # print(allData[j], allData[j+1])
                        #过滤请求类数据,接收每日请求的数值 譬如第一日142
                        if flag1 <= dayNum:
                            #新建一个类 用于接收每一天的请求数据
                            oneDay = DayRequire()
                            oneDay.requireNum = int(allData[j])
                            # 读不懂可解开下句封印 尝试观察输出
                            # print(oneDay.requireNum)
                            # 下方for循环接收每一天的请求数据 存到oneDay的类中 传给dayListAll
                            for k in range(j + 1, j + oneDay.requireNum + 1):
                                # 按逗号切分每条请求
                                tmpeList = allData[k].split(',')
                                # 切分后的数组若只有两项表明为del请求 传给oneDay.delRequireList
                                if len(tmpeList) == 2:
                                    # 清除多余空格、括号
                                    oneDay.requireType = tmpeList[0].replace('(', '')
                                    oneDay.vmId = tmpeList[1].replace(')', '').replace(' ', '')
                                    oneDay.delRequireListAppend()
                                # 切分后的数组若有三项表明为add请求 传给oneDay.addRequireList
                                elif len(tmpeList) == 3:
                                    # 清除多余空格、括号
                                    oneDay.requireType = tmpeList[0].replace('(', '')
                                    oneDay.vmType = tmpeList[1].replace(' ', '')
                                    oneDay.vmId = tmpeList[2].replace(')', '').replace(' ', '')
                                    oneDay.addRequireListAppend()
                            dayListAll[flag1 - 1] = oneDay
                # 执行完flag==3中的内容后,可直接跳出最外层for循环
                break
    # 为Host类赋值,使hostListAll中每一个元素为一个Host类
    i = 0
    for host in hostListAll:
        #分割数据去除空格、括号
        host = host.split(',')
        host[0] = host[0].replace('(', '').replace(' ', '')
        host[4] = host[4].replace(')', '').replace(' ', '')

        #赋值数据
        tmpeHost = Host()
        tmpeHost.hostType = host[0]
        tmpeHost.hostCpuNum = int(host[1])
        tmpeHost.hostMemSize = int(host[2])
        tmpeHost.hostHardWareCost = int(host[3])
        tmpeHost.hostDayCost = int(host[4])
        tmpeHost.xingJiaBi()
        tmpeHost.coreMemBi()
        hostListAll[i] = tmpeHost
        i += 1
    # 为Vm类赋值,使vmListAll中每一个元素为一个Vm类
    i = 0
    for vm in vmListAll:
        #分割数据去除空格、括号
        vm = vm.split(',')
        vm[0] = vm[0].replace('(', '').replace(' ', '')
        vm[3] = vm[3].replace(')', '').replace(' ', '')

        # 赋值数据
        tmpeVm = Vm()
        tmpeVm.vmType = vm[0]
        tmpeVm.vmCpuNum = int(vm[1])
        tmpeVm.vmMemSize = int(vm[2])
        tmpeVm.vmDoubleOrNot = int(vm[3])
        tmpeVm.coreMemBi()
        vmListAll[i] = tmpeVm
        i += 1
    i = 0

    return hostListAll, vmListAll, dayListAll

def main():
    # to read standard input
    # process
    # to write standard output
    # sys.stdout.flush()

    #以training-1.txt为示例
    filename = "training-1.txt"

    #按换行分割全部数据
    allData = readtxt(filename).split('\n')
    # 根据下一条注释可以返回分割后的数据
    # print(allData[0])
    [hostListAll, vmListAll, dayListAll] = inputData(allData)

    # 根据下一条的输出,可显示第一天第一条请求
    # print(dayListAll[0].addRequireList[0])


if __name__ == "__main__":
    main()

to sum up

Ps:

  • The amount of data in this question needs to be paid attention to. Teammates feel that multi-threading or multi-process concurrency is needed to improve the efficiency of the algorithm. Please pay attention to friends~
  • If you have any good ideas, or the code of this article is not appropriate, please leave a message in the comment area~

Guess you like

Origin blog.csdn.net/weixin_44459972/article/details/114853832