练习题5:模拟实现开闭区间
Open
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 6
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
判断一个数字,是否在若干个区间范围之中,区分开区间和闭区间。
比如
checkRanges(2,'[1,2]')输出为true。因为 2 在区间 [1,2] 中,checkRanges(2,'[1,2)')输出为false,因为2不在区间 [1,2) 中
2.区间数量无限制 checkRanges(2,'[2,5],(6,10)....')
function checkRanges(num,ranges){
function handle(num,str){
let _num=+str.replace(/\D/g,'')
if(/\[\d+/.test(str)){
return num>=_num
}
else if(/\(\d+/.test(str)){
return num>_num
}
else if(/\d+\]/.test(str)){
return num<=_num
}
else if(/\d+\)/.test(str)){
return num<_num
}
}
let _ranges=ranges.match(/\D\d+\,\d+\D/g)
let nowRange=[]
return _ranges.some(item=>{
nowRange=item.split(',')
return handle(num,nowRange[0])&&handle(num,nowRange[1])
})
}
checkRanges(2,'[1,2]')//true
checkRanges(2,'[1,2)')//false
checkRanges(5,'[1,2),[3,6]')//true
在实际开发上,很多时候是需要考虑负数和小数的,实现起来也不难,只是把正则改一下而已
function checkRanges(num,ranges){
function handle(num,str){
let _num=+str.replace(/[\[\]\(\)]/g,'')
if(/\[(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)/.test(str)){
return num>=_num
}
else if(/\((\-?(Infinity|0|([1-9]\d*))(\.\d+)?)/.test(str)){
return num>_num
}
else if(/(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)\]/.test(str)){
return num<=_num
}
else if(/(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)\)/.test(str)){
return num<_num
}
}
let _ranges=ranges.match(/\D(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)\,(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)\D/g)
let nowRange=[]
return _ranges.some(item=>{
nowRange=item.split(',')
return handle(num,nowRange[0])&&handle(num,nowRange[1])
})
}
checkRanges(2.5,'[2.5,5]')//true
checkRanges(2.5,'(2.5,5]')//false
checkRanges(-2.5,'(-3,5]')//true
checkRanges(-2.5,'(-2.5,5]')//false
checkRanges(-2.5,'(-2.6,5]')//true
checkRanges(Infinity,'[2.5,Infinity]')//true
然后看到正则写了N次,代码太长,也可以优化
function checkRanges(num,ranges){
function handle(num,range,type){
switch(type){
case '[':return num>=range;
case '(':return num>range;
case ']':return num<=range;
case ')':return num<range;
}
}
let _ranges=ranges.split(/[\]\)],[\[\(]/g)
let nowRange=[]
return _ranges.some(item=>{
nowRange=item.match(/[\(\)\[\]]|(\-?(Infinity|0|([1-9]\d*))(\.\d+)?)/g)
return handle(num,+nowRange[1],nowRange[0])&&handle(num,nowRange[2],nowRange[3])
})
}
checkRanges(2.5,'[2,2.1],[2.5,3]')//true
checkRanges(2.5,'[2,2.1],(2.5,3]')//false
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names the checkRanges function but no repository file or test. Start by locating where this exercise belongs and compare the range syntax and examples in the issue; done means checking inclusive and exclusive bounds across multiple ranges, including negative numbers, decimals, and Infinity.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100