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

Homework #282

Merged
merged 5 commits into from
Jan 4, 2024
Merged

Homework #282

merged 5 commits into from
Jan 4, 2024

Conversation

Momonalith
Copy link

@Momonalith Momonalith commented Jan 3, 2024

@iphysresearch 包含了Leetcode

Number of Segments in a String

s="Hello, my name is John"
#创建一个列表,并检查其中的空格个数
#对于通常的英语句子,单词数=空格数 + 1 
d=len([x for x in s if x==' '])
print(d)

Longer Contiguous Segments of Ones than Zeros

import numpy as np
import random
s=input('Please input a binary number')
a=[]
b=[]
#定义列表a与b来储存0与1的值

for i in s :
    if i=='0':
        a.append(0)
        b.append(random.randrange(1,100000))
        #当某一处为0时,用a记录这个指标处有0,同时在b处留下一个随机数,表示这里不是1
    elif i=='1':
        b.append(1)
        a.append(random.randint(100000,200000))
        #与上同理
        
a1=np.array(a)
b1=np.array(b)
#转化为ndarray,方便进行array操作

m=0
#m的值作为计数器,记录了最大的连续的0的值

#通过把相邻元素做差,生成一个新的差分array, 并检查这个array中有没有0元素存在
#m=m+1
#将差分array取代原来的array,并进行新的差分,检查有没有0元素的存在
#重复上述过程,直到最后我们得到了不包含0的差分array(写下几个会帮助更好理解这种计算方式)


#大范围随机数的使用会可以帮助避免人为原因出现的意外的0元素
while list(a1[1:]-a1[:-1]).count(0) != 0:
    a1=a1[1:]-a1[:-1]
    m+=1
if m !=0 :
    m+=1
#m=m+1 是因为最后一次循环中,m+1的操作没有执行,我们补上

#上述理由同理,不过n描述的是1元素的计数器
n=0
while list(b1[1:]-b1[:-1]).count(0) != 0:
    b1=b1[1:]-b1[:-1]
    n+=1
if n !=0 :
    n+=1

#比较并输出
print('True' if n>m else 'False')

Check if Binary String Has at Most One Segment of Ones

import numpy as np
import random
s=input('Please input a binary number, the leading number has to be one')
b=[]
#定义b储存1元素

for i in s :
    if i=='0':
        b.append(random.randrange(1,100000))
        #如果某一处有0,在b的这个位置储存一个随机数
    elif i=='1':
        b.append(1)
        #在有1元素的指标处,在b中也储存一个1

        
b1=np.array(b)
#转换为array方便进行运算

 
n=0
#n是最大连续1元素个数的计数器
#通过将相邻元素相减生成差分array,并检查其中是否含有0元素,n=n+1
#将差分array取代原有的,并进行新的差分,判断有无0元素
#重复上述过程,直到最后我们得到了不包含0的差分array
#达到不含0元素array的这个过程的迭代次数描述了最大的连续的值(写下几个会帮助更好理解这种计算方式)


#大范围随机数的使用会可以帮助避免人为原因出现的意外的0元素
while list(b1[1:]-b1[:-1]).count(0) != 0:
    b1=b1[1:]-b1[:-1]
    n+=1
if n !=0 :
    n+=1
#n=n+1 是因为最后一次循环中,m+1的操作没有执行,我们补上

#比较并输出结果
print('True' if n>1 else 'False')

Peak Index in a Mountain Array

import numpy as np
arr=np.array([0,8,9,10,100,97,5,2,1])

#构造差分array
b=arr[1:]-arr[:-1]

#过滤出正值元素, array的长度决定了上升趋势延续到哪里
index=len(b[b>0])
print(index)

Find Peak Element

import numpy as np
nums=np.array([1,2,3,1,5,4,10,9,12])

#构造差分array
diff=nums[1:]-nums[:-1]
#局部peak值在差分中的特征结构是符号(+ -)的出现,过滤出负值的指标
ind=np.where(diff<0)[0]
#考虑最后一个元素
ind=np.append(ind,len(nums)-1) if nums[-1]>nums[-2] else True
print(ind)

@iphysresearch
Copy link
Owner

Good Job!!! @Momonalith

Python homework:

  • Total questions: 108
  • Correct answers: 104
  • Score: 96.00%

Numpy homework:

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

@iphysresearch
Copy link
Owner

Good Job!!! @Momonalith

Python homework:

  • Total questions: 108
  • Correct answers: 104
  • Score: 96.00%

Numpy homework:

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

Pandas homework:

  • Total questions: 12
  • Correct answers: 11
  • Score: 91.00%

@iphysresearch
Copy link
Owner

@Momonalith
Python 基础作业再加加油哈!全做对后我给你记录成绩!

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

Good Job!!! @Momonalith

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%

@Momonalith
Copy link
Author

@iphysresearch 老师,已经修改完成

@iphysresearch
Copy link
Owner

@Momonalith
恭喜完成Python基础+拓展作业!

@iphysresearch iphysresearch merged commit 379a042 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