Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python+numpy+pandas Homework & Matplotlib visualisation(basic) Homework #274

Merged
merged 8 commits into from
Jan 4, 2024

Conversation

HibiscuitDeCJ
Copy link

@HibiscuitDeCJ HibiscuitDeCJ commented Jan 3, 2024

@iphysresearch
新年好!

下为python扩展作业:
Leetcode #434 Solution(in Python3):

class Solution:
    def countSegments(self, s: str) -> int:
        #初始化片段数为0
        segs=0
        #对s以i为指标,c为值进行迭代
        for i,c in enumerate(s):
            #对于第一次循环单独判断,避免i-1指标
            if i == 0 :
                #如果第一个字符不是‘ ’,说明是片段开头
                #片段数加一
                if c != ' ': segs += 1
                #‘ ’开头时跳过
                else: continue
            #后续循环中,若满足前一个是‘ ’,后一个不是‘ ’
            #说明是片段开头,片段数加一
            elif s[i-1]==' ' and c!=' ': segs +=1
            #其他情况跳过
            else: continue
        #返回片段数
        return segs

Leetcode #1869 Solution(in Python3):

class Solution:
    def checkZeroOnes(self, s: str) -> bool:
        #定义当前长度curlen并初始化为0
        curlen=0
        #初始化最长片段长度为0,maxlen[0]表示0串的长度,maxlen[1]表示1串的长度
        maxlen=[0,0]
        #对s进行迭代,使用i为指标,c为当前字符
        for i,c in enumerate(s):
            #跳过i=0时,不需要比较
            ifi>0):
                #如果当前字符与上个字符相同,连续片段当前长度就加1
                if(s[i]==s[i-1]):   curlen += 1
                #如果不连续,那么当前长度就应该为1(单个字符)
                else:   curlen = 1
            #更新最长片段,当前字符为‘0’,就更新maxlen[0];‘1’同理
            maxlen[int(c)]=max(curlen,maxlen[int(c)])
        #如果1串长于0串,返回真值,其他情况返回假植
        return maxlen[1]>maxlen[0]

Leetcode #1784 Solution(in Python3,TWO Versions):

#version1:认为形如11,111才算是one contiguous segment of ones
#比如,1001为False,1101为True,11011为False

#初始化当前字符与前一字符的差异(0-相同,1-由0到1,-1-由1到0)
diff=1
#segs记录1串的数目,初始化为0
segs=0
#在s中以索引i迭代
for i,c in enumerate(s):
    #跳过i=0,避免s[-1]的影响
    if(i>0):
        #对于当前字符,满足:1.与前一字符相等(也就是形成11,111这样的连续串)
        #2.上一个差异是1(0变为1),这里实质上是判断011的存在
        #3.当前字符是1
        if (int(s[i])-int(s[i-1])==0) and diff==1 and int(c)==1:
            #新的011出现了,所以1串数加一
            segs+=1
        #一旦超过一个1串,跳出循环
        if segs>1:break
        #每次循环结束前,都记录当前字符与前值之差
        diff=int(s[i])-int(s[i-1])
#只当segs为1时返回真值,否则返回假值
#return segs==1
print(segs==1)
#Version2 
#参考leetcode评论区
#false情况:有0隔断了1-segment,比如101 1101 11011。那么就形同于探测01序列是否存在

class Solution:
    def checkOnesSegment(self, s: str) -> bool:
        #遍历每个字符
        for i in range(len(s)):
            #跳过第一个字符,避免s[-1]的影响
            if(i>0):
                #如果01,那么1-0=1,其他情况都不等于1,所以可以探测是否有01子串存在
                if(int(s[i])-int(s[i-1])==1):
                    return False
        #在循环遍历结束后,如果没有return,则至少有一个1-segment
        return True

Leetcode #852 Solution(in Python3):

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        #当列表只有一个值时,默认为peak
        if(len(arr)==1):return 0
        #初始化左边界和右边界
        lb=0
        rb=len(arr)-1
        #当边界相差2及以上时,进行折半运算
        while rb-lb>1:
            #取中点
            m=(rb+lb)//2
            #当中点右侧递增时,说明peak在右侧,所以左边界移动到中点
            if(arr[m]<arr[m+1]):lb=m
            #当中点左侧递减(向左侧递增),说明peak在左侧,右边界移动到m
            elif(arr[m]<arr[m-1]):rb=m
            #由于列表相邻数值不会相等
            #因此m点的值必定既大于左值,又大于右值,为peak
            else:return m
            #由于1.lb、rb起始值不为peak
            #2.每一次lb、rb的更新,都经过了m的中介,而m做了peak检查
            #所以最后(lb,rb)确定的m值必定为peak的index  

Leetcode #162 Solution(in Python3):

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        #当列表只有一个值时,默认为peak
        if(len(arr)==1):return 0
        #初始化左边界和右边界
        lb=0
        rb=len(arr)-1
        #当边界相差2及以上时,进行折半运算
        while rb-lb>1:
            #取中点
            m=(rb+lb)//2
            #当中点右侧递增时,说明peak在右侧,所以左边界移动到中点
            if(arr[m]<arr[m+1]):lb=m
            #当中点左侧递减(向左侧递增),说明peak在左侧,右边界移动到m
            elif(arr[m]<arr[m-1]):rb=m
            #由于列表相邻数值不会相等
            #因此m点的值必定既大于左值,又大于右值,为peak
            else:return m
            #由于1.lb、rb起始值不为peak
            #2.每一次lb、rb的更新,都经过了m的中介,而m做了peak检查
            #所以最后(lb,rb)确定的m值必定为peak的index  

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

@HibiscuitDeCJ HibiscuitDeCJ changed the title python+numpy+pands; Homework python+numpy+pandas Homework & Matplotlib visualisation(basic) Homework Jan 3, 2024
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我算你完成Matplotlib数据可视化作业了哈!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

谢谢~🫶🫶🫶

Copy link
Owner

@iphysresearch iphysresearch Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HibiscuitDeCJ

从你的作业目录里看不出来你的名字是什么?我还没法记录成绩。。。。

是周璐-武汉大学? 回复我的时候,记得at我一下

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iphysresearch 辛苦了,我是周子力_UCAS(原来是肉眼识别的吗👀)
#274

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

周子力

对啊~ 肉眼识别。。。。

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

@HibiscuitDeCJ
你恐怕需要update branch一下才能看到基础作业的成绩哦~

@iphysresearch iphysresearch added this to the Leetcode HW finished milestone Jan 3, 2024
@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

Python homework:

  • Total questions: 108
  • Correct answers: 106
  • Score: 98.00%

Numpy homework:

  • Total questions: 10
  • Correct answers: 10
  • Score: 100.00%

Pandas homework:

  • Total questions: 12
  • Correct answers: 10
  • Score: 83.00%

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

Python homework:

  • Total questions: 108
  • Correct answers: 106
  • Score: 98.00%

Numpy homework:

  • Total questions: 10
  • Correct answers: 10
  • Score: 100.00%

Pandas homework:

  • Total questions: 12
  • Correct answers: 12
  • Score: 100.00%

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

Python homework:

  • Total questions: 108
  • Correct answers: 108
  • Score: 100.00%

Numpy homework:

  • Total questions: 10
  • Correct answers: 10
  • Score: 100.00%

Pandas homework:

  • Total questions: 12
  • Correct answers: 12
  • Score: 100.00%

Copy link
Owner

@iphysresearch iphysresearch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HibiscuitDeCJ
恭喜完成Python基础+扩展作业,以及Matplotlib数据可视化作业!

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

Python homework:

  • Total questions: 108
  • Correct answers: 108
  • Score: 100.00%

Numpy homework:

  • Total questions: 10
  • Correct answers: 10
  • Score: 100.00%

Pandas homework:

  • Total questions: 12
  • Correct answers: 12
  • Score: 100.00%

@iphysresearch
Copy link
Owner

Good Job!!! @HibiscuitDeCJ

Python homework:

  • Total questions: 108
  • Correct answers: 108
  • Score: 100.00%

Numpy homework:

  • Total questions: 10
  • Correct answers: 10
  • Score: 100.00%

Pandas homework:

  • Total questions: 12
  • Correct answers: 12
  • Score: 100.00%

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sklearn 建模作业!

Copy link
Owner

@iphysresearch iphysresearch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HibiscuitDeCJ
恭喜完成Python基础+扩展作业,Matplotlib数据可视化作业,Sklearn建模作业!

@iphysresearch iphysresearch merged commit 9e19383 into iphysresearch:homework Jan 4, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants