Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
answer
1
var str = 'bdca'
console.log(str.split(‘’).sort().join(‘’))
2
var index, res = []
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
var num = Math.floor(Math.random() * arr.length)
for(var i=num;i>=0;i--){
index = Math.floor(Math.random() * arr.length)
res = res.concat(arr[index])
arr.splice(index, 1)
}
console.log(res)
3
var str = 'adbcccde'
var k, res = {}
for(var i=0;i<str.length;i++){
k = str[i]
if(!res[k]) res[k] = 1
else res[k]++
}
res = Object.keys(res).map(function(v){
return res[v]
})
console.log(Math.max.apply(null, res))
5
var struct = {
a: {b: 5, d: 5, e: 7},
b: {c: 4},
c: {d: 8, e: 2},
d: {c: 8, e: 6},
e: {b: 3}
}
console.log('answer 1: %s', answer('a-b-c'))
console.log('answer 2: %s', answer('a-d'))
console.log('answer 3: %s', answer('a-d-c'))
console.log('answer 4: %s', answer('a-e-b-c-d'))
console.log('answer 5: %s', answer('a-e-d'))
console.log('answer 6: %s', count(['c-d-c', 'c-e-b-c']))
console.log('answer 7: %s', count(['a-b-c-d-c', 'a-d-c-d-c', 'a-d-e-b-c']))
console.log('answer 8: %s', recent('a-c'))
console.log('answer 9: %s', recent('b-b'))
console.log('answer 10: %s', count(['c-d-c', 'c-e-b-c', 'c-e-b-c-d-c', 'c-d-c-e-b-c', 'c-d-e-b-c', 'c-e-b-c-e-b-c', 'c-e-b-c-e-b-c-e-b-c'], 30))
function count(res, filter){
var n = 0
if(!filter){
res.forEach(function(v){
typeof answer(v) === 'number' && n++
})
}else{
res.forEach(function(v){
answer(v) < 30 && n++
})
}
return n
}
function recent(route){
var res = []
route = route.split('-')
if(typeof struct[route[0]][route[1]] === 'number') return struct[route[0]][route[1]]
search(route[0])
return Math.min.apply(null, res)
function search(start, tag){
var f = false
if(Object.keys(struct[start]).indexOf(route[1]) === -1){
Object.keys(struct[start]).forEach(function(k){
if(tag && tag.indexOf(tag.substring(tag.length-1, tag.length) + '-' + k) !== -1) f = true
if(!f) search(k, (tag || route[0]) + '-' + k)
})
}else res = res.concat(answer(tag + '-' + route[1]))
}
}
function answer(route){
var filter = []
route = route.split('-')
if(route.length > 2){
for(var i=0;i<route.length;i++){
if(route[i - 1] && route[i + 1] && route[i].length === 1){
filter = filter.concat(route[i - 1] + '-' + route[i])
filter = filter.concat(route[i] + '-' + route[i + 1])
}
}
for(var i=0;i<filter.length;i++){
for(var j=filter.length-1;j>=0;j--){
if(i != j && filter[i] === filter[j]) filter.splice(i, 1)
}
}
var len = 0
for(var i=0;i<filter.length;i++){
if(!struct[filter[i].split('-')[0]][filter[i].split('-')[1]]) return 'not route'
len += struct[filter[i].split('-')[0]][filter[i].split('-')[1]]
}
return len
}else return struct[route[0]][route[1]]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.