EE308_Lab2

JoeyNB JoeyNB 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
The Link Your Classhttps://bbs.csdn.net/forums/MUEE308FZ
The Link of Requirement of This Assignmenthttps://bbs.csdn.net/topics/600798588
The Aim of This AssignmentCode personally & learn git and github & learn the process of writing a project & learn unit test and performance test
MU STU ID and FZU STU ID19104740_831901308

Github code

https://github.com/JoeyLee0111/Lab2.git 


PSP Form

Personal Software Process StagesEstimated Time/minutesCompleted Time/minutes
Planning1010
Estimate1010
Analysis60100
Design Spec4040
Design Review3030
Coding Standard3030
Design6060
Coding500600
Code Review Planning6060
Test90120
Test Report60100
Postmortem&Process Improvement120 120
Total10701280

Logic Design 

Main thinking.

 First step

Second step

Third step

 Fourth step

Fifth step


Process (with Code)(It's been optimized)

First, we need to get plain text.

def get_text():
    file = open("text.c", "r", encoding="UTF-8")  #Ensure normal opening
    text = file.read()
    for i in '!#$%&()+,-.:;<=>?@[\\]^_{|}~':  # Remove the punctuation
        text = text.replace(i, " ")
    file.close()
    return text

Second, we need to filt some characters and add into the list to make the next four steps easier.

def filting():
    text = get_text().replace("else if", "elseif")
    separator_word = [r'//.*', r'\/\*(?:[^\*]|\*+[^\/\*])*\*+\/', r'".*"'] # Remove // , / and '
    for i in separator_word:
        wordlist1 = re.split(i, text)
        text = ""
        for word in wordlist1:
            text = text + word
    wordlist1 = text.split()
    return wordlist1

Third, I used the traversal way to search keywords. It's a very simple solution to the first problem. Also, this method also works well for the next problem.

Fourth, I used two lists to solve this question and that'll make it more clearer.

def first_second_question():  # Number of output keywords.
    words = filting()
    keyWords = {"auto", "break", "case", "char", "const",
                "continue", "default", "do", "double", "else",
                "enum", "extern", "float", "for", "goto",
                "if", "int", "long", "register", "return",
                "short", "signed", "sizeof", "static", "struct",
                "switch", "typedef", "union", "unsigned",
                "void", "volatile", "while", "elseif"}
    number = 0
    fWords = []
    counts1 = {}
    for word in words:
        if len(word) == 1 or (word not in keyWords):
            continue
        counts1[word] = counts1.get(word, 0) + 1
        fWords.append(word)
        number = number + 1
    number = number + counts1.get("elseif", 0)
    print("total num: {}".format(number))
    num = counts1.get("switch", 0)
    print("switch num: {}".format(num))
    if num == 0:
        print("case num: {}".format(num))
        return
    list2 = []
    flag = -1
    for word in words:
        if word == "switch":
            list2.append(0)
            flag += 1
        elif word == "case":
            list2[flag] += 1
        else:
            continue
    print("case num: ", end="")
    print(" ".join(str(x) for x in list2))
    return fWords

Fifth,  I also used the traversal way to search keywords. Thinking over, I find that Q3 and Q4 can be solved together.The differences between them is else and elseif. By the 'if' , I got the result.

def three_fourth_question():
    fWords=first_second_question()
    listf = []
    num_if_else = 0
    num_if_elseif_else = 0
    for word in fWords:
        if word == "if":
            listf.append(word)
        elif word == "elseif" and listf[-1] != "elseif":
            listf.append(word)
        elif word == "else":
            if listf[-1] == "elseif":
                listf.pop()
                listf.pop()
                num_if_elseif_else += 1
            elif listf[-1] == "if":
                listf.pop()
                num_if_else += 1
    print("if-else num: {}".format(num_if_else))
    print("if-elseif-else num: {}".format(num_if_elseif_else))
    return

Run Result 


Unit Test

import timeit
def fun():
    for i in range(100000):
        a = i * i
print(timeit.timeit('fun()', 'from __main__ import fun', number=1))
import profile
def fun():
    for i in range(100000):
        a = i * i
print(profile.run('fun()'))

 


Optimization

I put all the code that was outside the method inside the method, and the average speed increased by more than 0.003 seconds.


Summary

We studied Python in the first semester of freshman year. This lab is not difficult in the code part. I think the difficult parts are optimization and unit test. Though I tried to make code run quickly, I never do so carefully about every detail. 

I learned how to use Github when I was learning Java. So it can save my time and I can improve my code.

I major in Electronic Engineering. The learning is more about hardware. I hope learn more knowleage about software by this course.

...全文

相关推荐

EE-308 LAB4 final version.7z

这是本次实验的压缩包

晨起剪桃花

产教融合实训基地与实践中心建设逻辑图-可编辑版

产教融合实训基地与实践中心建设逻辑图-可编辑版

龙猫与奥特曼

idea2025-free.zip

idea2025-free.zip

qq_45190508

sop基于灵敏度分析的有源配电网智能软开关优化配置升级1(Matlab代码实现)

内容概要:本文提出了一种基于灵敏度分析的有源配电网智能软开关(SOP)优化配置方法,旨在应对高比例分布式电源接入带来的电压波动与网络损耗加剧等问题。研究以IEEE33节点系统为算例,通过改进的灵敏度分析模型量化各节点对电压和功率流动的影响程度,进而构建SOP选址与定容的优化模型,实现降低网损、改善电压分布及提升系统运行灵活性的目标。全文详细阐述了灵敏度指标的数学推导、目标函数与约束条件的建模过程,并基于Matlab实现了完整的仿真求解流程,具备良好的工程实用性与学术参考价值。; 适合人群:电力系统及其自动化、电气工程等相关专业的硕士博士研究生、科研机构研究人员,以及从事智能配电网规划、运行与优化的工程技术人员。; 使用场景及目标:①研究分布式电源大规模接入背景下配电网的主动调控与优化策略;②指导智能软开关在实际配电网中的科学选址与容量配置;③为相关科研课题提供可复现的仿真案例,支撑学术论文撰写与算法验证。; 阅读建议:建议读者结合提供的Matlab代码与IEEE33节点标准模型进行动手实践,重点理解灵敏度指标的物理意义及其在优化模型中的应用逻辑,关注求解过程中的约束设置与算法收敛性,并可进一步拓展至SOP与储能联合配置、多时间尺度优化等复杂场景进行二次开发与研究。

荔枝科研社

Ubuntu安装Latex

关于Ubuntu安装Latex的详细记录和排错指南

Leers Ting
评论 1
收藏 已收藏
分享
返回顶部