标签:集合 air [] one tps 数学 第一个 lin [1]
地址:https://www.w3cschool.cn/codecamp/list?pename=advanced_algorithm_scripting_camp
判断电话号码算法挑战:
如果传入字符串是一个有效的美国电话号码,则返回 true.
用户可以在表单中填入一个任意有效美国电话号码. 下面是一些有效号码的例子(还有下面测试时用到的一些变体写法):
555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555
在本节中你会看见如 800-692-7753 or 8oo-six427676;laskdjf这样的字符串. 你的任务就是验证前面给出的字符串是否是有效的美国电话号码. 区号是必须有的. 如果字符串中给出了国家代码, 你必须验证其是 1.如果号码有效就返回 true ; 否则返回 false.
function telephoneCheck(str) { return /^1?\s?(\(\d{3}\)|\d{3})[-|\s]*\d{3}[-|\s]*\d{4}$/.test(str); //return /^1? ?(\d{3}|\(\d{3}\))[ -]?\d{3}[ -]?\d{4}$/.test(str); } telephoneCheck("555-555-5555");
集合交集算法挑战:
创建一个函数,接受两个或多个数组,返回所给数组的 对等差分(symmetric difference) (△ or ⊕)数组.
给出两个集合 (如集合 A = {1, 2, 3} 和集合 B = {2, 3, 4}), 而数学术语 "对等差分" 的集合就是指由所有只在两个集合其中之一的元素组成的集合(A △ B = C = {1, 4}). 对于传入的额外集合 (如 D = {2, 3}), 你应该安装前面原则求前两个集合的结果与新集合的对等差分集合 (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}).
function sym(result,...args) { for(let i=0,j=args.length;i<j;i++) { result = result.filter(a=>args[i].indexOf(a)<0).concat(args[i].filter(a=>result.indexOf(a)<0)); result = [...new Set(result)]; } return result; } sym([1, 2, 3], [5, 2, 1, 4]);
收银系统算法挑战:
设计一个收银程序 checkCashRegister() ,其把购买价格(price)作为第一个参数 , 付款金额 (cash)作为第二个参数, 和收银机中零钱 (cid) 作为第三个参数.
cid 是一个二维数组,存着当前可用的找零.
当收银机中的钱不够找零时返回字符串 "Insufficient Funds". 如果正好则返回字符串 "Closed".
否者, 返回应找回的零钱列表,且由大到小存在二维数组中.
function checkCashRegister(price, cash, cid) { //排序 //cid.sort(function(a,b){ return b[1]-a[1]}); //求和 let sum = 100 * cid.reduce(function(a,b){ return Array.isArray(a)?a[1]+b[1]:a+b[1];}) let diff =100 * (cash - price); if(sum > diff) { let result = []; let num = 0; let initMoney = 100 * cid[8][1]; let outMoney = 0; while(diff>=10000) { if(initMoney<=0) { break; } num++; outMoney += 10000; diff -= 10000; initMoney -=10000; } if(num>0) { result.push(["ONE HUNDRED",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[7][1]; outMoney = 0; while(diff>=2000) { if(initMoney<=0) { break; } num++; outMoney += 2000; diff -= 2000; initMoney -=2000; } if(num>0) { result.push(["TWENTY",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[6][1]; outMoney = 0; while(diff>=1000) { if(initMoney<=0) { break; } num++; outMoney += 1000; diff -= 1000; initMoney -=1000; } if(num>0) { result.push(["TEN",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[5][1]; outMoney = 0; while(diff>=500) { if(initMoney<=0) { break; } num++; outMoney += 500; diff -= 500; initMoney -=500; } if(num>0) { result.push(["FIVE",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[4][1]; outMoney = 0; while(diff>=100) { if(initMoney<=0) { break; } num++; outMoney += 100; diff -= 100; initMoney -=100; } if(num>0) { result.push(["ONE",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[3][1]; outMoney = 0; while(diff>= 25) { if(initMoney<=0) { break; } num++; outMoney += 25; diff -= 25; initMoney -= 25; } if(num>0) { result.push(["QUARTER",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[2][1]; outMoney = 0; while(diff>=10) { if(initMoney<=0) { break; } num++; outMoney += 10; diff -= 10; initMoney -= 10; } if(num>0) { result.push(["DIME",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[1][1]; outMoney = 0; while(diff>=5) { if(initMoney<=0) { break; } num++; outMoney += 5; diff -= 5; initMoney -= 5; } if(num>0) { result.push(["NICKEL",Number(outMoney/100)]); } num = 0; initMoney = 100 * cid[0][1]; outMoney = 0; while(diff>=1) { if(initMoney<=0) { break; } num++; outMoney += 1; diff -= 1; initMoney -=1; } console.log(diff); if(num>0) { result.push(["PENNY",Number(outMoney/100)]); } if(diff>0) { return "Insufficient Funds"; } return result; } else if(sum == diff){ return "Closed"; } else { return "Insufficient Funds"; } } checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
库存更新算法挑战:
依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的字母顺序排列.
function updateInventory(arr1, arr2) { let result = arr2; if(arr2.length == 0) { return arr1; } let arr = []; arr2.reduce(function(a,b){if(a!=undefined){arr.push(a[1]);}arr.push(b[1]);}); for(let i = 0 ,j=arr1.length;i<j;i++) { var index = arr.indexOf(arr1[i][1]); if ( index < 0 ) { result.push(arr1[i]); } else { result = result.slice(0,index).concat([[result.slice(index,index+1)[0][0]+arr1[i][0],result.slice(index,index+1)[0][1]]]).concat(result.slice(index+1,result.length)); } } result.sort(function(a,b){return b[1]<a[1]}); console.log(result); return result; } // Example inventory lists var curInv = [ [21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"] ]; var newInv = [ [2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"] ]; updateInventory(curInv, newInv);
标签:集合 air [] one tps 数学 第一个 lin [1]
原文地址:https://www.cnblogs.com/miaosj/p/10900957.html