【Edabit 算法 ★★★★★★】【两个大整数相加】Recursion: Sum of Two Numbers (With A Twist!)

2023-10-22 09:04

本文主要是介绍【Edabit 算法 ★★★★★★】【两个大整数相加】Recursion: Sum of Two Numbers (With A Twist!),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Recursion: Sum of Two Numbers (With A Twist!)

Instructions

This is an “expert” challenge!!! Why is a sum of two numbers an “expert” challenge!!! Well, the numbers can have 1000 digits or even beyond such count…

So, what’s the twist? You have to do the summation as if you’re doing it manually on a piece of paper, thus, the conversion of the numeric string to numeric literal is basically disallowed.

Examples
sum("12132000", "12171979") // "24303979"sum("4666", "544") // "5210"sum("1521512512512512515", "898989898989988998899898") // "898991420502501511412413"sum("5125515215521515", "125261616261626") // "5250776831783141"sum("6666666666666666666666666666", "99999999999999999999999") // "6666766666666666666666666665"sum("123456789123456789123456789", "987654321987654321987654329876543") // "987654445444443445444443453333332"
Notes
  • Remember how to sum two numbers ON A PAPER, such is the process.
  • Your function must run in less than 10 seconds because it has a time limit.
  • The use of Number class such as BigInt is disallowed – it will defeat the purpose and the level of difficulty associated to this challenge.
  • You are expected to solve this challenge recursively.
Solutions
const sum = (a, b,c=0) => {if(!a && !b && c == 0){return ''}let aa = (a)?(a.slice(-1)-0):0;let bb = (b)?(b.slice(-1)-0):0;let s = aa + bb + cc = s>9?1:0;return sum(a.slice(0,-1),b.slice(0,-1),c)+(s%10)
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertNotEquals:function(actual,expected){if(actual === expected){let errorMsg = `actual is ${actual},${expected} is expected,they are equals`;throw new Error(errorMsg);}}}
})();const isRecursive = src => (`${src}`.match(RegExp(`${src.name}`, 'gm'))||[]).length > +(/function/.test(src))
Test.assertNotEquals(isRecursive(sum), false, "Recursion is required!")let [actualParam, expectedParam] = [[["12132000", "12171979"], ["4666", "544"], ["1521512512512512515", "898989898989988998899898"],["5125515215521515", "125261616261626"], ["6666666666666666666666666666", "99999999999999999999999"],["123456789123456789123456789", "987654321987654321987654329876543"], ["51", "512"],["46580672134861691487886856201063433530317493541984174240640117078384844027455455145995264175402994424834479825796316174329467969102257360195385044875023188313698661902232816682563450684527972706431205", "20129647448213526330992199933412026717951269059875880213489467074335368047371342207724579931208231032969760043956811494704380198848377355718984761723730087673439394159054420344427904875384087249296946"],["9128242816391792390367394318238609154929962550133827657886034828979294413033450307173793450924762143201991300288127408763421237279633517929936847079257713141254694944681428142978110027357322312404627593110196423560326537881370897768020382035189644680256824659171348515208671339529370866296929702167647163038519576331084019822103309755374561623148508523431380245253765653509318684179663600476971689801", "5920641803160990513445202815794518152101247319199211634010324208708552138569594568355624738331704740605556159925350097568289164018471525773848461636579024644391854277092707811953956319566890527925989019562020260846251250663758330856266051985217733863782039893158278545291027890391152027767054280498870038607952519452004179810592466387736659835203110761590431605238080432136468832137768846891022675668"],["24050292702239538714424762926989391091054882494797379961190995916419743076846190252322346159955367441832937735205583340798028856059731163836333572978411075895848661770468080051146869104337532213474993926921843996913706778627924709600100860461421587774140722793995230660233453392717776973617724296276959982403528208646617679528431859423982314682036555772485961534695291544266936268924447065901465864784710200365748971482974309578528938725051499783831735126491173069337708438252812165533370751831832345511906521502270947947504198101881866181323122256768406228518806385974069302887460096561970477705646199939356606923830372891944037065847071308700107176794261922547322922787636783283829766004529060524539701495229943211611134317723328686844713489223961776582365551050940511119736023799724295560712462775", "32509558457800134082157248923945369106923458582597107662799282973299414325101867489152494482453431986091527569819907127801971978514325573048496062254539304686542784848485721075326183780905103371293505027494462848297668132430517455611475977074377557603798687767852907762254040565866554962529868705211874976201522692137999060766380670853123585546115851899681725949430620582898894763596679656710337017496618525630124192131724949516793735384953877012134805554272494110981312910720545085389502607669244051838541600298937410166860469735980721786025686375273866096353307350361733480286161315578348192126560652007559649899391482681958298876030894402469363045420883354390088168624736238621292726748213941087878476447239860986699971354976451472810499473465596980950104110861804910439253305564075282187561761831"], ["6809632763916891310120420620586174664812635530867937840217826224568347127990297761160448320170180761000408251375466103628257285208000720825374647976159883503477330018358281993325677527017557058976068333642868984291739483975621870841667306204393840341405348294943813620516550885907643492592684427984599974017532776047374095558566398217709965020793366640224373810304569301398035374905004719899959454164388545839944663454514841628001498947789419801699846006819006975473515954356883318037820103882153723720601881130291354697184471999967716687686218472392686266650318568026273517115609143992129773683289728842208471476663161973115343223846428988702456038387814984825150310010062042877806640480710525528241328472095324930106698917227353621406980530110747055257101350540514070478171412772633866196018240727864894180766177622426511795549818029880640303307250349733954619463752541929370662897223878043296358089043269919860180819045946942402216596728187295625046088616265162395417587774677163023091414012232562","8580486073000341176464481569509088314164648516198308588306341581886828084652373143824178061446100845207932260299989735098857835077503613944792671258723699144053082578718738452102088425930223680001667115358790806513480705990372645625620985201064789499679080382290229510353519900434457951354146351017356497038201676685010533295544810336326460878234636045306593283716955770214654833569407250862467764241301491075773878798755773232056371316349556337923582112418745691372603517681014116333538448075872043025194944464917374343870889032601696418177538646357828263727642286052251539150438138687143116934852022178306775340622808992992303183802309493297583818627523253611781850609208464293666564468773980578995182119812585094120227322948794646916387478317146222403428768533848504528795446827274517795896686680232498391880209937677390643332411043134407239601699748922010617022901854909440001914242164998598960486713118948710525156592829968225269277615537170861633579862039532587858207573589726753466950083211842"]], ["24303979","5210","898991420502501511412413","5250776831783141","6666766666666666666666666665","987654445444443445444443453333332","563","66710319583075217818879056134475460248268762601860054454129584152720212074826797353719844106611225457804239869753127669033848167950634715914369806598753275987138056061287237026991355559912059955728151","15048884619552782903812597134033127307031209869333039291896359037687846551603044875529418189256466883807547460213477506331710401298105043703785308715836737785646549221774135954932066346924212840330616612672216684406577788545129228624286434020407378544038864552329627060499699229920522894063983982666517201646472095783088199632695776143111221458351619285021811850491846085645787516317432447367994365469","56559851160039672796582011850934760197978341077394487623990278889719157401948057741474840642408799427924465305025490468600000834574056736884829635232950380582391446618953801126473052885242635584768498954416306845211374911058442165211576837535799145377939410561848138422487493958584331936147593001488834958605050900784616740294812530277105900228152407672167687484125912127165831032521126722611802882281328725995873163614699259095322674110005376795966540680763667180319021348973357250922873359501076397350448121801208358114364667837862587967348808632042272324872113736335802783173621412140318669832206851946916256823221855573902335941877965711169470222215145276937411091412373021905122492752743001612418177942469804198311105672699780159655212962689558757532469661912745421558989329363799577748274224606","15390118836917232486584902190095262978977284047066246428524167806455175212642670904984626381616281606208340511675455838727115120285504334770167319234883582647530412597077020445427765952947780738977735449001659790805220189965994516467288291405458629841084428677234043130870070786342101443946830779001956471055734452732384628854111208554036425899028002685530967094021525071612690208474411970762427218405690036915718542253270614860057870264138976139623428119237752666846119472037897434371358551958025766745796825595208729041055361032569413105863757118750514530377960854078525056266047282679272890618141751020515246817285970966107646407648738482000039857015338238436932160619270507171473204949484506107236510591907910024226926240176148268323368008427893277660530119074362575006966859599908383991914927408097392572646387560103902438882229073015047542908950098655965236486654396838810664811466043041895318575756388868570705975638776910627485874343724466486679668478304694983275795348266889776558364095444404"]
]
for (let i in actualParam) Test.assertEquals(sum(...actualParam[i]), expectedParam[i])

这篇关于【Edabit 算法 ★★★★★★】【两个大整数相加】Recursion: Sum of Two Numbers (With A Twist!)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/260445

相关文章

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

C语言实现两个变量值交换的三种方式

《C语言实现两个变量值交换的三种方式》两个变量值的交换是编程中最常见的问题之一,以下将介绍三种变量的交换方式,其中第一种方式是最常用也是最实用的,后两种方式一般只在特殊限制下使用,需要的朋友可以参考下... 目录1.使用临时变量(推荐)2.相加和相减的方式(值较大时可能丢失数据)3.按位异或运算1.使用临时

使用C语言实现交换整数的奇数位和偶数位

《使用C语言实现交换整数的奇数位和偶数位》在C语言中,要交换一个整数的二进制位中的奇数位和偶数位,重点需要理解位操作,当我们谈论二进制位的奇数位和偶数位时,我们是指从右到左数的位置,本文给大家介绍了使... 目录一、问题描述二、解决思路三、函数实现四、宏实现五、总结一、问题描述使用C语言代码实现:将一个整

Jackson库进行JSON 序列化时遇到了无限递归(Infinite Recursion)的问题及解决方案

《Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursion)的问题及解决方案》使用Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursi... 目录解决方案‌1. 使用 @jsonIgnore 忽略一个方向的引用2. 使用 @JsonManagedR

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

java两个List的交集,并集方式

《java两个List的交集,并集方式》文章主要介绍了Java中两个List的交集和并集的处理方法,推荐使用Apache的CollectionUtils工具类,因为它简单且不会改变原有集合,同时,文章... 目录Java两个List的交集,并集方法一方法二方法三总结java两个List的交集,并集方法一

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

通俗易懂的Java常见限流算法具体实现

《通俗易懂的Java常见限流算法具体实现》:本文主要介绍Java常见限流算法具体实现的相关资料,包括漏桶算法、令牌桶算法、Nginx限流和Redis+Lua限流的实现原理和具体步骤,并比较了它们的... 目录一、漏桶算法1.漏桶算法的思想和原理2.具体实现二、令牌桶算法1.令牌桶算法流程:2.具体实现2.1

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相