Nice feedback. The following is a trivial matter.
Ceiling valiable (end1
) is not necessary. This is because the square root is always an integer for a perfect square. And if I understand this method correctly, the scope of the check is "less than or equal to the square root of n".
(I'm not familiar with JavaScript, but I can write it this way in Racket.)
Also, something I overlooked, when n=1, it returns an unintended result of (1 1 1)
, so I added a test case.
#lang racket (provide get-divs) (define (get-divs n) (if (= 1 n) (list 1) (let ([sqrt-n (sqrt n)]) (define (inner x result-min result-max) (cond [(>= x sqrt-n) (append result-min (if (integer? sqrt-n) (cons sqrt-n result-max) result-max))] [(= 0 (modulo n x)) (inner (add1 x) (append result-min (list x)) (cons (/ n x) result-max))] [else (inner (add1 x) result-min result-max)])) (inner 2 (list 1) (list n)))))
- #lang racket
- (provide get-divs)
- (define (get-divs n)
(let* ([end0 (sqrt n)][end1 (ceiling end0)])- (if (= 1 n) (list 1)
- (let ([sqrt-n (sqrt n)])
- (define (inner x result-min result-max)
- (cond
[(>= x end1) (append result-min (if (= end0 end1) (cons end1 result-max) result-max))][(= 0 (modulo n x)) (inner (add1 x)(append result-min (list x))(cons (/ n x) result-max))]- [(>= x sqrt-n) (append result-min (if (integer? sqrt-n) (cons sqrt-n result-max) result-max))]
- [(= 0 (modulo n x)) (inner (add1 x) (append result-min (list x)) (cons (/ n x) result-max))]
- [else (inner (add1 x) result-min result-max)]))
(inner 2 (list 1) (list n))))- (inner 2 (list 1) (list n)))))
#lang racket (require "solution.rkt") (require rackunit codewars/rackunit) (define (get-divs-slow n) (for/fold ([result (list)]) ([i (in-range 1 (n + 1))]) (if (= 0 (modulo n i)) (append result (list i)) result))) (run-tests (test-suite "Static Cases" (test-case "n = 200" (check-equal? (get-divs 200) (list 1 2 4 5 8 10 20 25 40 50 100 200))) (test-case "n = 560" (check-equal? (get-divs 560) (list 1 2 4 5 7 8 10 14 16 20 28 35 40 56 70 80 112 140 280 560))) (test-case "n = 755" (check-equal? (get-divs 755) (list 1 5 151 755))) (test-case "n = 1" (check-equal? (get-divs 1) (list 1))) (test-case "n = 25" (check-equal? (get-divs 25) (list 1 5 25))))) (run-tests (test-suite "More than 1000 Random Tests with challenging values up to 1000000000(1e9)" (lambda () (for/list ([i (in-range 1 1000)]) (let* ([n (random 1000 1000000000)] [res (get-divs n)] [result (get-divs-slow n)]) (test-case (format "n = ~A" n) (check-equal? result res)))))))
- #lang racket
- (require "solution.rkt")
- (require rackunit
- codewars/rackunit)
- (define (get-divs-slow n)
- (for/fold
- ([result (list)])
- ([i (in-range 1 (n + 1))])
- (if (= 0 (modulo n i)) (append result (list i)) result)))
- (run-tests
- (test-suite "Static Cases"
- (test-case "n = 200"
- (check-equal? (get-divs 200)
- (list 1 2 4 5 8 10 20 25 40 50 100 200)))
- (test-case "n = 560"
- (check-equal? (get-divs 560)
- (list 1 2 4 5 7 8 10 14 16 20 28 35 40 56 70 80 112 140 280 560)))
- (test-case "n = 755"
- (check-equal? (get-divs 755)
- (list 1 5 151 755)))
(test-case "n = 25" ; don't forget about this testcase- (test-case "n = 1"
- (check-equal? (get-divs 1) (list 1)))
- (test-case "n = 25"
- (check-equal? (get-divs 25)
- (list 1 5 25)))))
- (run-tests
- (test-suite "More than 1000 Random Tests with challenging values up to 1000000000(1e9)"
- (lambda () (for/list ([i (in-range 1 1000)])
- (let* ([n (random 1000 1000000000)]
- [res (get-divs n)]
- [result (get-divs-slow n)])
- (test-case (format "n = ~A" n) (check-equal? result res)))))))
#lang racket (provide get-divs) (define (get-divs n) (let ([sqrt-n (sqrt n)]) (define (inner x result-min result-max) (cond [(> x sqrt-n) (append result-min result-max)] [(= 0 (modulo n x)) (inner (add1 x) (append result-min (list x)) (cons (/ n x) result-max))] [else (inner (add1 x) result-min result-max)])) (inner 2 (list 1) (list n))))
function getDiv(n){let factor0 = [];let factor1 = [];let end0 = Math.sqrt(n);let end1 = Math.ceil(end0);for (let i = 1; i !== end1; ++i) {if (n % i === 0) {factor0.push(i);factor1.unshift(n / i);}}if (end0 === end1) {factor0.push(end0);}factor0.push(...factor1);return factor0;}- #lang racket
- (provide get-divs)
- (define (get-divs n)
- (let ([sqrt-n (sqrt n)])
- (define (inner x result-min result-max)
- (cond
- [(> x sqrt-n) (append result-min result-max)]
- [(= 0 (modulo n x)) (inner (add1 x)
- (append result-min (list x))
- (cons (/ n x) result-max))]
- [else (inner (add1 x) result-min result-max)]))
- (inner 2 (list 1) (list n))))
#lang racket (require "solution.rkt") (require rackunit codewars/rackunit) (define (get-divs-slow n) (for/fold ([result (list)]) ([i (in-range 1 (n + 1))]) (if (= 0 (modulo n i)) (append result (list i)) result))) (run-tests (test-suite "Static Cases" (test-case "n = 200" (check-equal? (get-divs 200) (list 1 2 4 5 8 10 20 25 40 50 100 200))) (test-case "n = 560" (check-equal? (get-divs 560) (list 1 2 4 5 7 8 10 14 16 20 28 35 40 56 70 80 112 140 280 560))) (test-case "n = 755" (check-equal? (get-divs 755) (list 1 5 151 755))))) (run-tests (test-suite "More than 1000 Random Tests with challenging values up to 1000000000(1e9)" (lambda () (for/list ([i (in-range 1 1000)]) (let* ([n (random 1000 1000000000)] [res (get-divs n)] [result (get-divs-slow n)]) (test-case (format "n = ~A" n) (check-equal? result res)))))))
var sortNumber = function(a, b){return a - b;};const Test = require("@codewars/test-compat");function getDivCheck(n){var factL= [], i;for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {if (n % i === 0) {factL.push(i);if (n / i !== i) factL.push(n / i);}}factL.sort(sortNumber);return factL;}- #lang racket
- (require "solution.rkt")
- (require rackunit
- codewars/rackunit)
describe("Static Cases", function(){it("n = 200", function(){Test.assertSimilar(getDiv(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);});it("n = 560", function(){Test.assertSimilar(getDiv(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);});it("n = 755", function(){Test.assertSimilar(getDiv(755), [ 1, 5, 151, 755 ]);});});- (define (get-divs-slow n)
- (for/fold
- ([result (list)])
- ([i (in-range 1 (n + 1))])
- (if (= 0 (modulo n i)) (append result (list i)) result)))
function randint(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min;}describe("Random Cases", function(){it("More than 1000 Random Tests with challenging values up to 1000000000(1e9)" , function(){for (var i = 0; i <= 1000; i++) {var n = randint(1000, 1000000000);var result = getDivCheck(n), res = getDiv(n);it("Testing for n = " + n.toString(), function(){Test.assertSimilar(res,result);})}})})- (run-tests
- (test-suite "Static Cases"
- (test-case "n = 200"
- (check-equal? (get-divs 200)
- (list 1 2 4 5 8 10 20 25 40 50 100 200)))
- (test-case "n = 560"
- (check-equal? (get-divs 560)
- (list 1 2 4 5 7 8 10 14 16 20 28 35 40 56 70 80 112 140 280 560)))
- (test-case "n = 755"
- (check-equal? (get-divs 755)
- (list 1 5 151 755)))))
- (run-tests
- (test-suite "More than 1000 Random Tests with challenging values up to 1000000000(1e9)"
- (lambda () (for/list ([i (in-range 1 1000)])
- (let* ([n (random 1000 1000000000)]
- [res (get-divs n)]
- [result (get-divs-slow n)])
- (test-case (format "n = ~A" n) (check-equal? result res)))))))
open BatBig_int let fib x = let rec big_fib n a b = match n with | 1 -> a + b | _ -> big_fib (Stdlib.(-) n 1) (a + b) a in match x with | 0 -> "0" | 1 -> "1" | _ -> big_fib (Stdlib.(-) x 1) (big_int_of_int 1) (big_int_of_int 0) |> string_of_big_int;;
- open BatBig_int
- let fib x =
let rec big_fib =let cache = Hashtbl.create 10000000 in(fun x ->if (equal x zero) then zeroelse if (equal x one) then oneelsebegintryHashtbl.find cache (int_of_big_int x)withNot_found ->let result = big_fib (x - one) + big_fib (x - (big_int_of_int 2)) inHashtbl.add cache (int_of_big_int x) result;resultend) inbig_fib (big_int_of_int x) |> string_of_big_int;;- let rec big_fib n a b = match n with
- | 1 -> a + b
- | _ -> big_fib (Stdlib.(-) n 1) (a + b) a in
- match x with
- | 0 -> "0"
- | 1 -> "1"
- | _ -> big_fib (Stdlib.(-) x 1) (big_int_of_int 1) (big_int_of_int 0) |> string_of_big_int;;
let project_euler_problem_1 = let rec loop i result = match i with | 0 -> result | n when i mod 3 = 0 -> loop (i - 1) (result + n) | n when i mod 5 = 0 -> loop (i - 1) (result + n) | _ -> loop (i - 1) result in loop 999 0;;
let projectEulerProblemOne = [1 .. 999] |> List.filter(fun x -> x % 3 = 0 || x % 5 = 0) |> List.sum- let project_euler_problem_1 =
- let rec loop i result = match i with
- | 0 -> result
- | n when i mod 3 = 0 -> loop (i - 1) (result + n)
- | n when i mod 5 = 0 -> loop (i - 1) (result + n)
- | _ -> loop (i - 1) result in
- loop 999 0;;
module Tests = struct open OUnit let suite = [ "Projuct euler Problem 1" >:: (fun _ -> assert_equal project_euler_problem_1 233168) ] end
module Tests = beginopen Fuchulet suite =testList "Solution" [testCase "Example Test" <|fun _ -> Assert.Equal("Project Euler - problem 1", 233168, projectEulerProblemOne)]- module Tests = struct
- open OUnit
- let suite = [
- "Projuct euler Problem 1" >::
- (fun _ -> assert_equal project_euler_problem_1 233168)
- ]
- end
memoize recursion
open BatBig_int let fib x = let rec big_fib = let cache = Hashtbl.create 10000000 in (fun x -> if (equal x zero) then zero else if (equal x one) then one else begin try Hashtbl.find cache (int_of_big_int x) with Not_found -> let result = big_fib (x - one) + big_fib (x - (big_int_of_int 2)) in Hashtbl.add cache (int_of_big_int x) result; result end) in big_fib (big_int_of_int x) |> string_of_big_int;;
module Example where- open BatBig_int
import Data.Semigroup (Endo(..),stimes)fib :: Integer -> Integerfib n = fst $ n `stimes` Endo ( \ (a,b) -> (b,a+b) ) `appEndo` (0,1)- let fib x =
- let rec big_fib =
- let cache = Hashtbl.create 10000000 in
- (fun x ->
- if (equal x zero) then zero
- else if (equal x one) then one
- else
- begin
- try
- Hashtbl.find cache (int_of_big_int x)
- with
- Not_found ->
- let result = big_fib (x - one) + big_fib (x - (big_int_of_int 2)) in
- Hashtbl.add cache (int_of_big_int x) result;
- result
- end) in
- big_fib (big_int_of_int x) |> string_of_big_int;;
module Tests = struct open OUnit let as_eq = assert_equal;; let suite = [ "tests" >:: (fun _ -> as_eq (fib 0) "0"; as_eq (fib 1) "1"; as_eq (fib 2) "1"; as_eq (fib 3) "2"; as_eq (fib 4) "3"; as_eq (fib 5) "5"; as_eq (fib 6) "8"; as_eq (fib 7) "13"; as_eq (fib 8) "21"; as_eq (fib 9) "34"; as_eq (fib 1000) "43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875"; as_eq (fib 100000) "2597406934722172416615503402127591541488048538651769658472477070395253454351127368626555677283671674475463758722307443211163839947387509103096569738218830449305228763853133492135302679278956701051276578271635608073050532200243233114383986516137827238124777453778337299916214634050054669860390862750996639366409211890125271960172105060300350586894028558103675117658251368377438684936413457338834365158775425371912410500332195991330062204363035213756525421823998690848556374080179251761629391754963458558616300762819916081109836526352995440694284206571046044903805647136346033000520852277707554446794723709030979019014860432846819857961015951001850608264919234587313399150133919932363102301864172536477136266475080133982431231703431452964181790051187957316766834979901682011849907756686456845066287392485603914047605199550066288826345877189410680370091879365001733011710028310473947456256091444932821374855573864080579813028266640270354294412104919995803131876805899186513425175959911520563155337703996941035518275274919959802257507902037798103089922984996304496255814045517000250299764322193462165366210841876745428298261398234478366581588040819003307382939500082132009374715485131027220817305432264866949630987914714362925554252624043999615326979876807510646819068792118299167964409178271868561702918102212679267401362650499784968843680975254700131004574186406448299485872551744746695651879126916993244564817673322257149314967763345846623830333820239702436859478287641875788572910710133700300094229333597292779191409212804901545976262791057055248158884051779418192905216769576608748815567860128818354354292307397810154785701328438612728620176653953444993001980062953893698550072328665131718113588661353747268458543254898113717660519461693791688442534259478126310388952047956594380715301911253964847112638900713362856910155145342332944128435722099628674611942095166100230974070996553190050815866991144544264788287264284501725332048648319457892039984893823636745618220375097348566847433887249049337031633826571760729778891798913667325190623247118037280173921572390822769228077292456662750538337500692607721059361942126892030256744356537800831830637593334502350256972906515285327194367756015666039916404882563967693079290502951488693413799125174856667074717514938979038653338139534684837808612673755438382110844897653836848318258836339917310455850905663846202501463131183108742907729262215943020429159474030610183981685506695026197376150857176119947587572212987205312060791864980361596092339594104118635168854883911918517906151156275293615849000872150192226511785315089251027528045151238603792184692121533829287136924321527332714157478829590260157195485316444794546750285840236000238344790520345108033282013803880708980734832620122795263360677366987578332625485944906021917368867786241120562109836985019729017715780112040458649153935115783499546100636635745448508241888279067531359950519206222976015376529797308588164873117308237059828489404487403932053592935976454165560795472477862029969232956138971989467942218727360512336559521133108778758228879597580320459608479024506385194174312616377510459921102486879496341706862092908893068525234805692599833377510390101316617812305114571932706629167125446512151746802548190358351688971707570677865618800822034683632101813026232996027599403579997774046244952114531588370357904483293150007246173417355805567832153454341170020258560809166294198637401514569572272836921963229511187762530753402594781448204657460288485500062806934811398276016855584079542162057543557291510641537592939022884356120792643705560062367986544382464373946972471945996555795505838034825597839682776084731530251788951718630722761103630509360074262261717363058613291544024695432904616258691774630578507674937487992329181750163484068813465534370997589353607405172909412697657593295156818624747127636468836551757018353417274662607306510451195762866349922848678780591085118985653555434958761664016447588028633629704046289097067736256584300235314749461233912068632146637087844699210427541569410912246568571204717241133378489816764096924981633421176857150311671040068175303192115415611958042570658693127276213710697472226029655524611053715554532499750843275200199214301910505362996007042963297805103066650638786268157658772683745128976850796366371059380911225428835839194121154773759981301921650952140133306070987313732926518169226845063443954056729812031546392324981793780469103793422169495229100793029949237507299325063050942813902793084134473061411643355614764093104425918481363930542369378976520526456347648318272633371512112030629233889286487949209737847861884868260804647319539200840398308008803869049557419756219293922110825766397681361044490024720948340326796768837621396744075713887292863079821849314343879778088737958896840946143415927131757836511457828935581859902923534388888846587452130838137779443636119762839036894595760120316502279857901545344747352706972851454599861422902737291131463782045516225447535356773622793648545035710208644541208984235038908770223039849380214734809687433336225449150117411751570704561050895274000206380497967960402617818664481248547269630823473377245543390519841308769781276565916764229022948181763075710255793365008152286383634493138089971785087070863632205869018938377766063006066757732427272929247421295265000706646722730009956124191409138984675224955790729398495608750456694217771551107346630456603944136235888443676215273928597072287937355966723924613827468703217858459948257514745406436460997059316120596841560473234396652457231650317792833860590388360417691428732735703986803342604670071717363573091122981306903286137122597937096605775172964528263757434075792282180744352908669606854021718597891166333863858589736209114248432178645039479195424208191626088571069110433994801473013100869848866430721216762473119618190737820766582968280796079482259549036328266578006994856825300536436674822534603705134503603152154296943991866236857638062351209884448741138600171173647632126029961408561925599707566827866778732377419444462275399909291044697716476151118672327238679208133367306181944849396607123345271856520253643621964198782752978813060080313141817069314468221189275784978281094367751540710106350553798003842219045508482239386993296926659221112742698133062300073465628498093636693049446801628553712633412620378491919498600097200836727876650786886306933418995225768314390832484886340318940194161036979843833346608676709431643653538430912157815543512852077720858098902099586449602479491970687230765687109234380719509824814473157813780080639358418756655098501321882852840184981407690738507369535377711880388528935347600930338598691608289335421147722936561907276264603726027239320991187820407067412272258120766729040071924237930330972132364184093956102995971291799828290009539147382437802779051112030954582532888721146170133440385939654047806199333224547317803407340902512130217279595753863158148810392952475410943880555098382627633127606718126171022011356181800775400227516734144169216424973175621363128588281978005788832454534581522434937268133433997710512532081478345067139835038332901313945986481820272322043341930929011907832896569222878337497354301561722829115627329468814853281922100752373626827643152685735493223028018101449649009015529248638338885664893002250974343601200814365153625369199446709711126951966725780061891215440222487564601554632812091945824653557432047644212650790655208208337976071465127508320487165271577472325887275761128357592132553934446289433258105028633583669291828566894736223508250294964065798630809614341696830467595174355313224362664207197608459024263017473392225291248366316428006552870975051997504913009859468071013602336440164400179188610853230764991714372054467823597211760465153200163085336319351589645890681722372812310320271897917951272799656053694032111242846590994556380215461316106267521633805664394318881268199494005537068697621855231858921100963441012933535733918459668197539834284696822889460076352031688922002021931318369757556962061115774305826305535862015637891246031220672933992617378379625150999935403648731423208873977968908908369996292995391977217796533421249291978383751460062054967341662833487341011097770535898066498136011395571584328308713940582535274056081011503907941688079197212933148303072638678631411038443128215994936824342998188719768637604496342597524256886188688978980888315865076262604856465004322896856149255063968811404400429503894245872382233543101078691517328333604779262727765686076177705616874050257743749983775830143856135427273838589774133526949165483929721519554793578923866762502745370104660909382449626626935321303744538892479216161188889702077910448563199514826630802879549546453583866307344423753319712279158861707289652090149848305435983200771326653407290662016775706409690183771201306823245333477966660525325490873601961480378241566071271650383582257289215708209369510995890132859490724306183325755201208090007175022022949742801823445413711916298449914722254196594682221468260644961839254249670903104007581488857971672246322887016438403908463856731164308169537326790303114583680575021119639905615169154708510459700542098571797318015564741406172334145847111268547929892443001391468289103679179216978616582489007322033591376706527676521307143985302760988478056216994659655461379174985659739227379416726495377801992098355427866179123126699374730777730569324430166839333011554515542656864937492128687049121754245967831132969248492466744261999033972825674873460201150442228780466124320183016108232183908654771042398228531316559685688005226571474428823317539456543881928624432662503345388199590085105211383124491861802624432195540433985722841341254409411771722156867086291742124053110620522842986199273629406208834754853645128123279609097213953775360023076765694208219943034648783348544492713539450224591334374664937701655605763384697062918725745426505879414630176639760457474311081556747091652708748125267159913793240527304613693961169892589808311906322510777928562071999459487700611801002296132304588294558440952496611158342804908643860880796440557763691857743754025896855927252514563404385217825890599553954627451385454452916761042969267970893580056234501918571489030418495767400819359973218711957496357095967825171096264752068890806407651445893132870767454169607107931692704285168093413311046353506242209810363216771910420786162184213763938194625697286781413636389620123976910465418956806197323148414224550071617215851321302030684176087215892702098879108938081045903397276547326416916845445627600759561367103584575649094430692452532085003091068783157561519847567569191284784654692558665111557913461272425336083635131342183905177154511228464455136016013513228948543271504760839307556100908786096663870612278690274831819331606701484957163004705262228238406266818448788374548131994380387613830128859885264201992286188208499588640888521352501457615396482647451025902530743172956899636499615707551855837165935367125448515089362904567736630035562457374779100987992499146967224041481601289530944015488942613783140087804311431741858071826185149051138744831358439067228949408258286021650288927228387426432786168690381960530155894459451808735197246008221529343980828254126128257157209350985382800738560472910941184006084485235377833503306861977724501886364070344973366473100602018128792886991861824418453968994777259482169137133647470453172979809245844361129618997595696240971845564020511432589591844724920942930301651488713079802102379065536525154780298059407529440513145807551537794861635879901158192019808879694967187448224156836463534326160242632934761634458163890163805123894184523973421841496889262398489648642093409816681494771155177009562669029850101513537599801272501241971119871526593747484778935488777815192931171431167444773882941064615028751327709474504763922874890662989841540259350834035142035136168819248238998027706666916342133424312054507359388616687691188185776118135771332483965209882085982391298606386822804754362408956522921410859852037330544625953261340234864689275060526893755148403298542086991221052597005628576707702567695300978970046408920009852106980295419699802138053295798159478289934443245491565327845223840551240445208226435420656313310702940722371552770504263482073984454889589248861397657079145414427653584572951329719091947694411910966797474262675590953832039169673494261360032263077428684105040061351052194413778158095005714526846009810352109249040027958050736436961021241137739717164869525493114805040126568351268829598413983222676377804500626507241731757395219796890754825199329259649801627068665658030178877405615167159731927320479376247375505855052839660294566992522173600874081212014209071041937598571721431338017425141582491824710905084715977249417049320254165239323233258851588893337097136310892571531417761978326033750109026284066415801371359356529278088456305951770081443994114674291850360748852366654744869928083230516815711602911836374147958492100860528981469547750812338896943152861021202736747049903930417035171342126923486700566627506229058636911882228903170510305406882096970875545329369434063981297696478031825451642178347347716471058423238594580183052756213910186997604305844068665712346869679456044155742100039179758348979935882751881524675930878928159243492197545387668305684668420775409821781247053354523194797398953320175988640281058825557698004397120538312459428957377696001857497335249965013509368925958021863811725906506436882127156815751021712900765992750370228283963962915973251173418586721023497317765969454283625519371556009143680329311962842546628403142444370648432390374906410811300792848955767243481200090309888457270907750873638873299642555050473812528975962934822878917619920725138309388288292510416837622758204081918933603653875284116785703720989718832986921927816629675844580174911809119663048187434155067790863948831489241504300476704527971283482211522202837062857314244107823792513645086677566622804977211397140621664116324756784216612961477109018826094677377686406176721484293894976671380122788941309026553511096118347012565197540807095384060916863936906673786627209429434264260402902158317345003727462588992622049877121178405563348492490326003508569099382392777297498413565614830788262363322368380709822346012274241379036473451735925215754757160934270935192901723954921426490691115271523338109124042812102893738488167358953934508930697715522989199698903885883275409044300321986834003470271220020159699371690650330547577095398748580670024491045504890061727189168031394528036165633941571334637222550477547460756055024108764382121688848916940371258901948490685379722244562009483819491532724502276218589169507405794983759821006604481996519360110261576947176202571702048684914616894068404140833587562118319210838005632144562018941505945780025318747471911604840677997765414830622179069330853875129298983009580277554145435058768984944179136535891620098725222049055183554603706533183176716110738009786625247488691476077664470147193074476302411660335671765564874440577990531996271632972009109449249216456030618827772947750764777446452586328919159107444252320082918209518021083700353881330983215894608680127954224752071924134648334963915094813097541433244209299930751481077919002346128122330161799429930618800533414550633932139339646861616416955220216447995417243171165744471364197733204899365074767844149929548073025856442942381787641506492878361767978677158510784235702640213388018875601989234056868423215585628508645525258377010620532224244987990625263484010774322488172558602233302076399933854152015343847725442917895130637050320444917797752370871958277976799686113626532291118629631164685159934660693460557545956063155830033697634000276685151293843638886090828376141157732003527565158745906567025439437931104838571313294490604926582363108949535090082673154497226396648088618041573977888472892174618974189721700770009862449653759012727015227634510874906948012210684952063002519011655963580552429180205586904259685261047412834518466736938580027700252965356366721619883672428226933950325930390994583168665542234654857020875504617520521853721567282679903418135520602999895366470106557900532129541336924472492212436324523042895188461779122338069674233980694887270587503389228395095135209123109258159006960395156367736067109050566299603571876423247920752836160805597697778756476767210521222327184821484446631261487584226092608875764331731023263768864822594691211032367737558122133470556805958008310127481673962019583598023967414489867276845869819376783757167936723213081586191045995058970991064686919463448038574143829629547131372173669836184558144505748676124322451519943362182916191468026091121793001864788050061351603144350076189213441602488091741051232290357179205497927970924502479940842696158818442616163780044759478212240873204124421169199805572649118243661921835714762891425805771871743688000324113008704819373962295017143090098476927237498875938639942530595331607891618810863505982444578942799346514915952884869757488025823353571677864826828051140885429732788197765736966005727700162592404301688659946862983717270595809808730901820120931003430058796552694788049809205484305467611034654748067290674399763612592434637719995843862812391985470202414880076880818848087892391591369463293113276849329777201646641727587259122354784480813433328050087758855264686119576962172239308693795757165821852416204341972383989932734803429262340722338155102209101262949249742423271698842023297303260161790575673111235465890298298313115123607606773968998153812286999642014609852579793691246016346088762321286205634215901479188632194659637483482564291616278532948239313229440231043277288768139550213348266388687453259281587854503890991561949632478855035090289390973718988003999026132015872678637873095678109625311008054489418857983565902063680699643165033912029944327726770869305240718416592070096139286401966725750087012218149733133695809600369751764951350040285926249203398111014953227533621844500744331562434532484217986108346261345897591234839970751854223281677187215956827243245910829019886390369784542622566912542747056097567984857136623679023878478161201477982939080513150258174523773529510165296934562786122241150783587755373348372764439838082000667214740034466322776918936967612878983488942094688102308427036452854504966759697318836044496702853190637396916357980928865719935397723495486787180416401415281489443785036291071517805285857583987711145474240156416477194116391354935466755593592608849200546384685403028080936417250583653368093407225310820844723570226809826951426162451204040711501448747856199922814664565893938488028643822313849852328452360667045805113679663751039248163336173274547275775636810977344539275827560597425160705468689657794530521602315939865780974801515414987097778078705357058008472376892422189750312758527140173117621279898744958406199843913365680297721208751934988504499713914285158032324823021340630312586072624541637765234505522051086318285359658520708173392709566445011404055106579055037417780393351658360904543047721422281816832539613634982525215232257690920254216409657452618066051777901592902884240599998882753691957540116954696152270401280857579766154722192925655963991820948894642657512288766330302133746367449217449351637104725732980832812726468187759356584218383594702792013663907689741738962252575782663990809792647011407580367850599381887184560094695833270775126181282015391041773950918244137561999937819240362469558235924171478702779448443108751901807414110290370706052085162975798361754251041642244867577350756338018895379263183389855955956527857227926155524494739363665533904528656215464288343162282921123290451842212532888101415884061619939195042230059898349966569463580186816717074818823215848647734386780911564660755175385552224428524049468033692299989300783900020690121517740696428573930196910500988278523053797637940257968953295112436166778910585557213381789089945453947915927374958600268237844486872037243488834616856290097850532497036933361942439802882364323553808208003875741710969289725499878566253048867033095150518452126944989251596392079421452606508516052325614861938282489838000815085351564642761700832096483117944401971780149213345335903336672376719229722069970766055482452247416927774637522135201716231722137632445699154022395494158227418930589911746931773776518735850032318014432883916374243795854695691221774098948611515564046609565094538115520921863711518684562543275047870530006998423140180169421109105925493596116719457630962328831271268328501760321771680400249657674186927113215573270049935709942324416387089242427584407651215572676037924765341808984312676941110313165951429479377670698881249643421933287404390485538222160837088907598277390184204138197811025854537088586701450623578513960109987476052535450100439353062072439709976445146790993381448994644609780957731953604938734950026860564555693224229691815630293922487606470873431166384205442489628760213650246991893040112513103835085621908060270866604873585849001704200923929789193938125116798421788115209259130435572321635660895603514383883939018953166274355609970015699780289236362349895374653428746875"; )] end
module ExampleSpec whereimport Test.Hspecimport Examplespec :: Specspec = doit "tests" $ dofib 0 `shouldBe` 0fib 1 `shouldBe` 1fib 2 `shouldBe` 1fib 3 `shouldBe` 2fib 4 `shouldBe` 3fib 5 `shouldBe` 5fib 6 `shouldBe` 8fib 7 `shouldBe` 13fib 8 `shouldBe` 21fib 9 `shouldBe` 34fib 1000 `shouldBe` 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875fib 100000 `shouldBe` 25974069347221724166155034021275915414880485386517696584724770703952534543511273686265556772836716744754637587223074432111638399473875091030965697382188304493052287638531334921353026792789567010512765782716356080730505322002432331143839865161378272381247774537783372999162146340500546698603908627509966393664092118901252719601721050603003505868940285581036751176582513683774386849364134573388343651587754253719124105003321959913300622043630352137565254218239986908485563740801792517616293917549634585586163007628199160811098365263529954406942842065710460449038056471363460330005208522777075544467947237090309790190148604328468198579610159510018506082649192345873133991501339199323631023018641725364771362664750801339824312317034314529641817900511879573167668349799016820118499077566864568450662873924856039140476051995500662888263458771894106803700918793650017330117100283104739474562560914449328213748555738640805798130282666402703542944121049199958031318768058991865134251759599115205631553377039969410355182752749199598022575079020377981030899229849963044962558140455170002502997643221934621653662108418767454282982613982344783665815880408190033073829395000821320093747154851310272208173054322648669496309879147143629255542526240439996153269798768075106468190687921182991679644091782718685617029181022126792674013626504997849688436809752547001310045741864064482994858725517447466956518791269169932445648176733222571493149677633458466238303338202397024368594782876418757885729107101337003000942293335972927791914092128049015459762627910570552481588840517794181929052167695766087488155678601288183543542923073978101547857013284386127286201766539534449930019800629538936985500723286651317181135886613537472684585432548981137176605194616937916884425342594781263103889520479565943807153019112539648471126389007133628569101551453423329441284357220996286746119420951661002309740709965531900508158669911445442647882872642845017253320486483194578920399848938236367456182203750973485668474338872490493370316338265717607297788917989136673251906232471180372801739215723908227692280772924566627505383375006926077210593619421268920302567443565378008318306375933345023502569729065152853271943677560156660399164048825639676930792905029514886934137991251748566670747175149389790386533381395346848378086126737554383821108448976538368483182588363399173104558509056638462025014631311831087429077292622159430204291594740306101839816855066950261973761508571761199475875722129872053120607918649803615960923395941041186351688548839119185179061511562752936158490008721501922265117853150892510275280451512386037921846921215338292871369243215273327141574788295902601571954853164447945467502858402360002383447905203451080332820138038807089807348326201227952633606773669875783326254859449060219173688677862411205621098369850197290177157801120404586491539351157834995461006366357454485082418882790675313599505192062229760153765297973085881648731173082370598284894044874039320535929359764541655607954724778620299692329561389719894679422187273605123365595211331087787582288795975803204596084790245063851941743126163775104599211024868794963417068620929088930685252348056925998333775103901013166178123051145719327066291671254465121517468025481903583516889717075706778656188008220346836321018130262329960275994035799977740462449521145315883703579044832931500072461734173558055678321534543411700202585608091662941986374015145695722728369219632295111877625307534025947814482046574602884855000628069348113982760168555840795421620575435572915106415375929390228843561207926437055600623679865443824643739469724719459965557955058380348255978396827760847315302517889517186307227611036305093600742622617173630586132915440246954329046162586917746305785076749374879923291817501634840688134655343709975893536074051729094126976575932951568186247471276364688365517570183534172746626073065104511957628663499228486787805910851189856535554349587616640164475880286336297040462890970677362565843002353147494612339120686321466370878446992104275415694109122465685712047172411333784898167640969249816334211768571503116710400681753031921154156119580425706586931272762137106974722260296555246110537155545324997508432752001992143019105053629960070429632978051030666506387862681576587726837451289768507963663710593809112254288358391941211547737599813019216509521401333060709873137329265181692268450634439540567298120315463923249817937804691037934221694952291007930299492375072993250630509428139027930841344730614116433556147640931044259184813639305423693789765205264563476483182726333715121120306292338892864879492097378478618848682608046473195392008403983080088038690495574197562192939221108257663976813610444900247209483403267967688376213967440757138872928630798218493143438797780887379588968409461434159271317578365114578289355818599029235343888888465874521308381377794436361197628390368945957601203165022798579015453447473527069728514545998614229027372911314637820455162254475353567736227936485450357102086445412089842350389087702230398493802147348096874333362254491501174117515707045610508952740002063804979679604026178186644812485472696308234733772455433905198413087697812765659167642290229481817630757102557933650081522863836344931380899717850870708636322058690189383777660630060667577324272729292474212952650007066467227300099561241914091389846752249557907293984956087504566942177715511073466304566039441362358884436762152739285970722879373559667239246138274687032178584599482575147454064364609970593161205968415604732343966524572316503177928338605903883604176914287327357039868033426046700717173635730911229813069032861371225979370966057751729645282637574340757922821807443529086696068540217185978911663338638585897362091142484321786450394791954242081916260885710691104339948014730131008698488664307212167624731196181907378207665829682807960794822595490363282665780069948568253005364366748225346037051345036031521542969439918662368576380623512098844487411386001711736476321260299614085619255997075668278667787323774194444622753999092910446977164761511186723272386792081333673061819448493966071233452718565202536436219641987827529788130600803131418170693144682211892757849782810943677515407101063505537980038422190455084822393869932969266592211127426981330623000734656284980936366930494468016285537126334126203784919194986000972008367278766507868863069334189952257683143908324848863403189401941610369798438333466086767094316436535384309121578155435128520777208580989020995864496024794919706872307656871092343807195098248144731578137800806393584187566550985013218828528401849814076907385073695353777118803885289353476009303385986916082893354211477229365619072762646037260272393209911878204070674122722581207667290400719242379303309721323641840939561029959712917998282900095391473824378027790511120309545825328887211461701334403859396540478061993332245473178034073409025121302172795957538631581488103929524754109438805550983826276331276067181261710220113561818007754002275167341441692164249731756213631285882819780057888324545345815224349372681334339977105125320814783450671398350383329013139459864818202723220433419309290119078328965692228783374973543015617228291156273294688148532819221007523736268276431526857354932230280181014496490090155292486383388856648930022509743436012008143651536253691994467097111269519667257800618912154402224875646015546328120919458246535574320476442126507906552082083379760714651275083204871652715774723258872757611283575921325539344462894332581050286335836692918285668947362235082502949640657986308096143416968304675951743553132243626642071976084590242630174733922252912483663164280065528709750519975049130098594680710136023364401644001791886108532307649917143720544678235972117604651532001630853363193515896458906817223728123103202718979179512727996560536940321112428465909945563802154613161062675216338056643943188812681994940055370686976218552318589211009634410129335357339184596681975398342846968228894600763520316889220020219313183697575569620611157743058263055358620156378912460312206729339926173783796251509999354036487314232088739779689089083699962929953919772177965334212492919783837514600620549673416628334873410110977705358980664981360113955715843283087139405825352740560810115039079416880791972129331483030726386786314110384431282159949368243429981887197686376044963425975242568861886889789808883158650762626048564650043228968561492550639688114044004295038942458723822335431010786915173283336047792627277656860761777056168740502577437499837758301438561354272738385897741335269491654839297215195547935789238667625027453701046609093824496266269353213037445388924792161611888897020779104485631995148266308028795495464535838663073444237533197122791588617072896520901498483054359832007713266534072906620167757064096901837712013068232453334779666605253254908736019614803782415660712716503835822572892157082093695109958901328594907243061833257552012080900071750220229497428018234454137119162984499147222541965946822214682606449618392542496709031040075814888579716722463228870164384039084638567311643081695373267903031145836805750211196399056151691547085104597005420985717973180155647414061723341458471112685479298924430013914682891036791792169786165824890073220335913767065276765213071439853027609884780562169946596554613791749856597392273794167264953778019920983554278661791231266993747307777305693244301668393330115545155426568649374921286870491217542459678311329692484924667442619990339728256748734602011504422287804661243201830161082321839086547710423982285313165596856880052265714744288233175394565438819286244326625033453881995900851052113831244918618026244321955404339857228413412544094117717221568670862917421240531106205228429861992736294062088347548536451281232796090972139537753600230767656942082199430346487833485444927135394502245913343746649377016556057633846970629187257454265058794146301766397604574743110815567470916527087481252671599137932405273046136939611698925898083119063225107779285620719994594877006118010022961323045882945584409524966111583428049086438608807964405577636918577437540258968554041481601289530944015488942613783140087804311431741858071826185149051138744831358439067228949408258286021650288927228387426432786168690381960530155894459451808735197246008221529343980828254126128257157209350985382800738560472910941184006084485235377833503306861977724501886364070344973366473100602018128792886991861824418453968994777259482169137133647470453172979809245844361129618997595696240971845564020511432589591844724920942930301651488713079802102379065536525154780298059407529440513145807551537794861635879901158192019808879694967187448224156836463534326160242632934761634458163890163805123894184523973421841496889262398489648642093409816681494771155177009562669029850101513537599801272501241971119871526593747484778935488777815192931171431167444773882941064615028751327709474504763922874890662989841540259350834035142035136168819248238998027706666916342133424312054507359388616687691188185776118135771332483965209882085982391298606386822804754362408956522921410859852037330544625953261340234864689275060526893755148403298542086991221052597005628576707702567695300978970046408920009852106980295419699802138053295798159478289934443245491565327845223840551240445208226435420656313310702940722371552770504263482073984454889589248861397657079145414427653584572951329719091947694411910966797474262675590953832039169673494261360032263077428684105040061351052194413778158095005714526846009810352109249040027958050736436961021241137739717164869525493114805040126568351268829598413983222676377804500626507241731757395219796890754825199329259649801627068665658030178877405615167159731927320479376247375505855052839660294566992522173600874081212014209071041937598571721431338017425141582491824710905084715977249417049320254165239323233258851588893337097136310892571531417761978326033750109026284066415801371359356529278088456305951770081443994114674291850360748852366654744869928083230516815711602911836374147958492100860528981469547750812338896943152861021202736747049903930417035171342126923486700566627506229058636911882228903170510305406882096970875545329369434063981297696478031825451642178347347716471058423238594580183052756213910186997604305844068665712346869679456044155742100039179758348979935882751881524675930878928159243492197545387668305684668420775409821781247053354523194797398953320175988640281058825557698004397120538312459428957377696001857497335249965013509368925958021863811725906506436882127156815751021712900765992750370228283963962915973251173418586721023497317765969454283625519371556009143680329311962842546628403142444370648432390374906410811300792848955767243481200090309888457270907750873638873299642555050473812528975962934822878917619920725138309388288292510416837622758204081918933603653875284116785703720989718832986921927816629675844580174911809119663048187434155067790863948831489241504300476704527971283482211522202837062857314244107823792513645086677566622804977211397140621664116324756784216612961477109018826094677377686406176721484293894976671380122788941309026553511096118347012565197540807095384060916863936906673786627209429434264260402902158317345003727462588992622049877121178405563348492490326003508569099382392777297498413565614830788262363322368380709822346012274241379036473451735925215754757160934270935192901723954921426490691115271523338109124042812102893738488167358953934508930697715522989199698903885883275409044300321986834003470271220020159699371690650330547577095398748580670024491045504890061727189168031394528036165633941571334637222550477547460756055024108764382121688848916940371258901948490685379722244562009483819491532724502276218589169507405794983759821006604481996519360110261576947176202571702048684914616894068404140833587562118319210838005632144562018941505945780025318747471911604840677997765414830622179069330853875129298983009580277554145435058768984944179136535891620098725222049055183554603706533183176716110738009786625247488691476077664470147193074476302411660335671765564874440577990531996271632972009109449249216456030618827772947750764777446452586328919159107444252320082918209518021083700353881330983215894608680127954224752071924134648334963915094813097541433244209299930751481077919002346128122330161799429930618800533414550633932139339646861616416955220216447995417243171165744471364197733204899365074767844149929548073025856442942381787641506492878361767978677158510784235702640213388018875601989234056868423215585628508645525258377010620532224244987990625263484010774322488172558602233302076399933854152015343847725442917895130637050320444917797752370871958277976799686113626532291118629631164685159934660693460557545956063155830033697634000276685151293843638886090828376141157732003527565158745906567025439437931104838571313294490604926582363108949535090082673154497226396648088618041573977888472892174618974189721700770009862449653759012727015227634510874906948012210684952063002519011655963580552429180205586904259685261047412834518466736938580027700252965356366721619883672428226933950325930390994583168665542234654857020875504617520521853721567282679903418135520602999895366470106557900532129541336924472492212436324523042895188461779122338069674233980694887270587503389228395095135209123109258159006960395156367736067109050566299603571876423247920752836160805597697778756476767210521222327184821484446631261487584226092608875764331731023263768864822594691211032367737558122133470556805958008310127481673962019583598023967414489867276845869819376783757167936723213081586191045995058970991064686919463448038574143829629547131372173669836184558144505748676124322451519943362182916191468026091121793001864788050061351603144350076189213441602488091741051232290357179205497927970924502479940842696158818442616163780044759478212240873204124421169199805572649118243661921835714762891425805771871743688000324113008704819373962295017143090098476927237498875938639942530595331607891618810863505982444578942799346514915952884869757488025823353571677864826828051140885429732788197765736966005727700162592404301688659946862983717270595809808730901820120931003430058796552694788049809205484305467611034654748067290674399763612592434637719995843862812391985470202414880076880818848087892391591369463293113276849329777201646641727587259122354784480813433328050087758855264686119576962172239308693795757165821852416204341972383989932734803429262340722338155102209101262949249742423271698842023297303260161790575673111235465890298298313115123607606773968998153812286999642014609852579793691246016346088762321286205634215901479188632194659637483482564291616278532948239313229440231043277288768139550213348266388687453259281587854503890991561949632478855035090289390973718988003999026132015872678637873095678109625311008054489418857983565902063680699643165033912029944327726770869305240718416592070096139286401966725750087012218149733133695809600369751764951350040285926249203398111014953227533621844500744331562434532484217986108346261345897591234839970751854223281677187215956827243245910829019886390369784542622566912542747056097567984857136623679023878478161201477982939080513150258174523773529510165296934562786122241150783587755373348372764439838082000667214740034466322776918936967612878983488942094688102308427036452854504966759697318836044496702853190637396916357980928865719935397723495486787180416401415281489443785036291071517805285857583987711145474240156416477194116391354935466755593592608849200546384685403028080936417250583653368093407225310820844723570226809826951426162451204040711501448747856199922814664565893938488028643822313849852328452360667045805113679663751039248163336173274547275775636810977344539275827560597425160705468689657794530521602315939865780974801515414987097778078705357058008472376892422189750312758527140173117621279898744958406199843913365680297721208751934988504499713914285158032324823021340630312586072624541637765234505522051086318285359658520708173392709566445011404055106579055037417780393351658360904543047721422281816832539613634982525215232257690920254216409657452618066051777901592902884240599998882753691957540116954696152270401280857579766154722192925655963991820948894642657512288766330302133746367449217449351637104725732980832812726468187759356584218383594702792013663907689741738962252575782663990809792647011407580367850599381887184560094695833270775126181282015391041773950918244137561999937819240362469558235924171478702779448443108751901807414110290370706052085162975798361754251041642244867577350756338018895379263183389855955956527857227926155524494739363665533904528656215464288343162282921123290451842212532888101415884061619939195042230059898349966569463580186816717074818823215848647734386780911564660755175385552224428524049468033692299989300783900020690121517740696428573930196910500988278523053797637940257968953295112436166778910585557213381789089945453947915927374958600268237844486872037243488834616856290097850532497036933361942439802882364323553808208003875741710969289725499878566253048867033095150518452126944989251596392079421452606508516052325614861938282489838000815085351564642761700832096483117944401971780149213345335903336672376719229722069970766055482452247416927774637522135201716231722137632445699154022395494158227418930589911746931773776518735850032318014432883916374243795854695691221774098948611515564046609565094538115520921863711518684562543275047870530006998423140180169421109105925493596116719457630962328831271268328501760321771680400249657674186927113215573270049935709942324416387089242427584407651215572676037924765341808984312676941110313165951429479377670698881249643421933287404390485538222160837088907598277390184204138197811025854537088586701450623578513960109987476052535450100439353062072439709976445146790993381448994644609780957731953604938734950026860564555693224229691815630293922487606470873431166384205442489628760213650246991893040112513103835085621908060270866604873585849001704200923929789193938125116798421788115209259130435572321635660895603514383883939018953166274355609970015699780289236362349895374653428746875- module Tests = struct
- open OUnit
- let as_eq = assert_equal;;
- let suite = [
- "tests" >::
- (fun _ ->
- as_eq (fib 0) "0";
- as_eq (fib 1) "1";
- as_eq (fib 2) "1";
- as_eq (fib 3) "2";
- as_eq (fib 4) "3";
- as_eq (fib 5) "5";
- as_eq (fib 6) "8";
- as_eq (fib 7) "13";
- as_eq (fib 8) "21";
- as_eq (fib 9) "34";
- as_eq (fib 1000) "43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875";
- as_eq (fib 100000) "25974069347221724166155034021275915414880485386517696584724770703952534543511273686265556772836716744754637587223074432111638399473875091030965697382188304493052287638531334921353026792789567010512765782716356080730505322002432331143839865161378272381247774537783372999162146340500546698603908627509966393664092118901252719601721050603003505868940285581036751176582513683774386849364134573388343651587754253719124105003321959913300622043630352137565254218239986908485563740801792517616293917549634585586163007628199160811098365263529954406942842065710460449038056471363460330005208522777075544467947237090309790190148604328468198579610159510018506082649192345873133991501339199323631023018641725364771362664750801339824312317034314529641817900511879573167668349799016820118499077566864568450662873924856039140476051995500662888263458771894106803700918793650017330117100283104739474562560914449328213748555738640805798130282666402703542944121049199958031318768058991865134251759599115205631553377039969410355182752749199598022575079020377981030899229849963044962558140455170002502997643221934621653662108418767454282982613982344783665815880408190033073829395000821320093747154851310272208173054322648669496309879147143629255542526240439996153269798768075106468190687921182991679644091782718685617029181022126792674013626504997849688436809752547001310045741864064482994858725517447466956518791269169932445648176733222571493149677633458466238303338202397024368594782876418757885729107101337003000942293335972927791914092128049015459762627910570552481588840517794181929052167695766087488155678601288183543542923073978101547857013284386127286201766539534449930019800629538936985500723286651317181135886613537472684585432548981137176605194616937916884425342594781263103889520479565943807153019112539648471126389007133628569101551453423329441284357220996286746119420951661002309740709965531900508158669911445442647882872642845017253320486483194578920399848938236367456182203750973485668474338872490493370316338265717607297788917989136673251906232471180372801739215723908227692280772924566627505383375006926077210593619421268920302567443565378008318306375933345023502569729065152853271943677560156660399164048825639676930792905029514886934137991251748566670747175149389790386533381395346848378086126737554383821108448976538368483182588363399173104558509056638462025014631311831087429077292622159430204291594740306101839816855066950261973761508571761199475875722129872053120607918649803615960923395941041186351688548839119185179061511562752936158490008721501922265117853150892510275280451512386037921846921215338292871369243215273327141574788295902601571954853164447945467502858402360002383447905203451080332820138038807089807348326201227952633606773669875783326254859449060219173688677862411205621098369850197290177157801120404586491539351157834995461006366357454485082418882790675313599505192062229760153765297973085881648731173082370598284894044874039320535929359764541655607954724778620299692329561389719894679422187273605123365595211331087787582288795975803204596084790245063851941743126163775104599211024868794963417068620929088930685252348056925998333775103901013166178123051145719327066291671254465121517468025481903583516889717075706778656188008220346836321018130262329960275994035799977740462449521145315883703579044832931500072461734173558055678321534543411700202585608091662941986374015145695722728369219632295111877625307534025947814482046574602884855000628069348113982760168555840795421620575435572915106415375929390228843561207926437055600623679865443824643739469724719459965557955058380348255978396827760847315302517889517186307227611036305093600742622617173630586132915440246954329046162586917746305785076749374879923291817501634840688134655343709975893536074051729094126976575932951568186247471276364688365517570183534172746626073065104511957628663499228486787805910851189856535554349587616640164475880286336297040462890970677362565843002353147494612339120686321466370878446992104275415694109122465685712047172411333784898167640969249816334211768571503116710400681753031921154156119580425706586931272762137106974722260296555246110537155545324997508432752001992143019105053629960070429632978051030666506387862681576587726837451289768507963663710593809112254288358391941211547737599813019216509521401333060709873137329265181692268450634439540567298120315463923249817937804691037934221694952291007930299492375072993250630509428139027930841344730614116433556147640931044259184813639305423693789765205264563476483182726333715121120306292338892864879492097378478618848682608046473195392008403983080088038690495574197562192939221108257663976813610444900247209483403267967688376213967440757138872928630798218493143438797780887379588968409461434159271317578365114578289355818599029235343888888465874521308381377794436361197628390368945957601203165022798579015453447473527069728514545998614229027372911314637820455162254475353567736227936485450357102086445412089842350389087702230398493802147348096874333362254491501174117515707045610508952740002063804979679604026178186644812485472696308234733772455433905198413087697812765659167642290229481817630757102557933650081522863836344931380899717850870708636322058690189383777660630060667577324272729292474212952650007066467227300099561241914091389846752249557907293984956087504566942177715511073466304566039441362358884436762152739285970722879373559667239246138274687032178584599482575147454064364609970593161205968415604732343966524572316503177928338605903883604176914287327357039868033426046700717173635730911229813069032861371225979370966057751729645282637574340757922821807443529086696068540217185978911663338638585897362091142484321786450394791954242081916260885710691104339948014730131008698488664307212167624731196181907378207665829682807960794822595490363282665780069948568253005364366748225346037051345036031521542969439918662368576380623512098844487411386001711736476321260299614085619255997075668278667787323774194444622753999092910446977164761511186723272386792081333673061819448493966071233452718565202536436219641987827529788130600803131418170693144682211892757849782810943677515407101063505537980038422190455084822393869932969266592211127426981330623000734656284980936366930494468016285537126334126203784919194986000972008367278766507868863069334189952257683143908324848863403189401941610369798438333466086767094316436535384309121578155435128520777208580989020995864496024794919706872307656871092343807195098248144731578137800806393584187566550985013218828528401849814076907385073695353777118803885289353476009303385986916082893354211477229365619072762646037260272393209911878204070674122722581207667290400719242379303309721323641840939561029959712917998282900095391473824378027790511120309545825328887211461701334403859396540478061993332245473178034073409025121302172795957538631581488103929524754109438805550983826276331276067181261710220113561818007754002275167341441692164249731756213631285882819780057888324545345815224349372681334339977105125320814783450671398350383329013139459864818202723220433419309290119078328965692228783374973543015617228291156273294688148532819221007523736268276431526857354932230280181014496490090155292486383388856648930022509743436012008143651536253691994467097111269519667257800618912154402224875646015546328120919458246535574320476442126507906552082083379760714651275083204871652715774723258872757611283575921325539344462894332581050286335836692918285668947362235082502949640657986308096143416968304675951743553132243626642071976084590242630174733922252912483663164280065528709750519975049130098594680710136023364401644001791886108532307649917143720544678235972117604651532001630853363193515896458906817223728123103202718979179512727996560536940321112428465909945563802154613161062675216338056643943188812681994940055370686976218552318589211009634410129335357339184596681975398342846968228894600763520316889220020219313183697575569620611157743058263055358620156378912460312206729339926173783796251509999354036487314232088739779689089083699962929953919772177965334212492919783837514600620549673416628334873410110977705358980664981360113955715843283087139405825352740560810115039079416880791972129331483030726386786314110384431282159949368243429981887197686376044963425975242568861886889789808883158650762626048564650043228968561492550639688114044004295038942458723822335431010786915173283336047792627277656860761777056168740502577437499837758301438561354272738385897741335269491654839297215195547935789238667625027453701046609093824496266269353213037445388924792161611888897020779104485631995148266308028795495464535838663073444237533197122791588617072896520901498483054359832007713266534072906620167757064096901837712013068232453334779666605253254908736019614803782415660712716503835822572892157082093695109958901328594907243061833257552012080900071750220229497428018234454137119162984499147222541965946822214682606449618392542496709031040075814888579716722463228870164384039084638567311643081695373267903031145836805750211196399056151691547085104597005420985717973180155647414061723341458471112685479298924430013914682891036791792169786165824890073220335913767065276765213071439853027609884780562169946596554613791749856597392273794167264953778019920983554278661791231266993747307777305693244301668393330115545155426568649374921286870491217542459678311329692484924667442619990339728256748734602011504422287804661243201830161082321839086547710423982285313165596856880052265714744288233175394565438819286244326625033453881995900851052113831244918618026244321955404339857228413412544094117717221568670862917421240531106205228429861992736294062088347548536451281232796090972139537753600230767656942082199430346487833485444927135394502245913343746649377016556057633846970629187257454265058794146301766397604574743110815567470916527087481252671599137932405273046136939611698925898083119063225107779285620719994594877006118010022961323045882945584409524966111583428049086438608807964405577636918577437540258968554041481601289530944015488942613783140087804311431741858071826185149051138744831358439067228949408258286021650288927228387426432786168690381960530155894459451808735197246008221529343980828254126128257157209350985382800738560472910941184006084485235377833503306861977724501886364070344973366473100602018128792886991861824418453968994777259482169137133647470453172979809245844361129618997595696240971845564020511432589591844724920942930301651488713079802102379065536525154780298059407529440513145807551537794861635879901158192019808879694967187448224156836463534326160242632934761634458163890163805123894184523973421841496889262398489648642093409816681494771155177009562669029850101513537599801272501241971119871526593747484778935488777815192931171431167444773882941064615028751327709474504763922874890662989841540259350834035142035136168819248238998027706666916342133424312054507359388616687691188185776118135771332483965209882085982391298606386822804754362408956522921410859852037330544625953261340234864689275060526893755148403298542086991221052597005628576707702567695300978970046408920009852106980295419699802138053295798159478289934443245491565327845223840551240445208226435420656313310702940722371552770504263482073984454889589248861397657079145414427653584572951329719091947694411910966797474262675590953832039169673494261360032263077428684105040061351052194413778158095005714526846009810352109249040027958050736436961021241137739717164869525493114805040126568351268829598413983222676377804500626507241731757395219796890754825199329259649801627068665658030178877405615167159731927320479376247375505855052839660294566992522173600874081212014209071041937598571721431338017425141582491824710905084715977249417049320254165239323233258851588893337097136310892571531417761978326033750109026284066415801371359356529278088456305951770081443994114674291850360748852366654744869928083230516815711602911836374147958492100860528981469547750812338896943152861021202736747049903930417035171342126923486700566627506229058636911882228903170510305406882096970875545329369434063981297696478031825451642178347347716471058423238594580183052756213910186997604305844068665712346869679456044155742100039179758348979935882751881524675930878928159243492197545387668305684668420775409821781247053354523194797398953320175988640281058825557698004397120538312459428957377696001857497335249965013509368925958021863811725906506436882127156815751021712900765992750370228283963962915973251173418586721023497317765969454283625519371556009143680329311962842546628403142444370648432390374906410811300792848955767243481200090309888457270907750873638873299642555050473812528975962934822878917619920725138309388288292510416837622758204081918933603653875284116785703720989718832986921927816629675844580174911809119663048187434155067790863948831489241504300476704527971283482211522202837062857314244107823792513645086677566622804977211397140621664116324756784216612961477109018826094677377686406176721484293894976671380122788941309026553511096118347012565197540807095384060916863936906673786627209429434264260402902158317345003727462588992622049877121178405563348492490326003508569099382392777297498413565614830788262363322368380709822346012274241379036473451735925215754757160934270935192901723954921426490691115271523338109124042812102893738488167358953934508930697715522989199698903885883275409044300321986834003470271220020159699371690650330547577095398748580670024491045504890061727189168031394528036165633941571334637222550477547460756055024108764382121688848916940371258901948490685379722244562009483819491532724502276218589169507405794983759821006604481996519360110261576947176202571702048684914616894068404140833587562118319210838005632144562018941505945780025318747471911604840677997765414830622179069330853875129298983009580277554145435058768984944179136535891620098725222049055183554603706533183176716110738009786625247488691476077664470147193074476302411660335671765564874440577990531996271632972009109449249216456030618827772947750764777446452586328919159107444252320082918209518021083700353881330983215894608680127954224752071924134648334963915094813097541433244209299930751481077919002346128122330161799429930618800533414550633932139339646861616416955220216447995417243171165744471364197733204899365074767844149929548073025856442942381787641506492878361767978677158510784235702640213388018875601989234056868423215585628508645525258377010620532224244987990625263484010774322488172558602233302076399933854152015343847725442917895130637050320444917797752370871958277976799686113626532291118629631164685159934660693460557545956063155830033697634000276685151293843638886090828376141157732003527565158745906567025439437931104838571313294490604926582363108949535090082673154497226396648088618041573977888472892174618974189721700770009862449653759012727015227634510874906948012210684952063002519011655963580552429180205586904259685261047412834518466736938580027700252965356366721619883672428226933950325930390994583168665542234654857020875504617520521853721567282679903418135520602999895366470106557900532129541336924472492212436324523042895188461779122338069674233980694887270587503389228395095135209123109258159006960395156367736067109050566299603571876423247920752836160805597697778756476767210521222327184821484446631261487584226092608875764331731023263768864822594691211032367737558122133470556805958008310127481673962019583598023967414489867276845869819376783757167936723213081586191045995058970991064686919463448038574143829629547131372173669836184558144505748676124322451519943362182916191468026091121793001864788050061351603144350076189213441602488091741051232290357179205497927970924502479940842696158818442616163780044759478212240873204124421169199805572649118243661921835714762891425805771871743688000324113008704819373962295017143090098476927237498875938639942530595331607891618810863505982444578942799346514915952884869757488025823353571677864826828051140885429732788197765736966005727700162592404301688659946862983717270595809808730901820120931003430058796552694788049809205484305467611034654748067290674399763612592434637719995843862812391985470202414880076880818848087892391591369463293113276849329777201646641727587259122354784480813433328050087758855264686119576962172239308693795757165821852416204341972383989932734803429262340722338155102209101262949249742423271698842023297303260161790575673111235465890298298313115123607606773968998153812286999642014609852579793691246016346088762321286205634215901479188632194659637483482564291616278532948239313229440231043277288768139550213348266388687453259281587854503890991561949632478855035090289390973718988003999026132015872678637873095678109625311008054489418857983565902063680699643165033912029944327726770869305240718416592070096139286401966725750087012218149733133695809600369751764951350040285926249203398111014953227533621844500744331562434532484217986108346261345897591234839970751854223281677187215956827243245910829019886390369784542622566912542747056097567984857136623679023878478161201477982939080513150258174523773529510165296934562786122241150783587755373348372764439838082000667214740034466322776918936967612878983488942094688102308427036452854504966759697318836044496702853190637396916357980928865719935397723495486787180416401415281489443785036291071517805285857583987711145474240156416477194116391354935466755593592608849200546384685403028080936417250583653368093407225310820844723570226809826951426162451204040711501448747856199922814664565893938488028643822313849852328452360667045805113679663751039248163336173274547275775636810977344539275827560597425160705468689657794530521602315939865780974801515414987097778078705357058008472376892422189750312758527140173117621279898744958406199843913365680297721208751934988504499713914285158032324823021340630312586072624541637765234505522051086318285359658520708173392709566445011404055106579055037417780393351658360904543047721422281816832539613634982525215232257690920254216409657452618066051777901592902884240599998882753691957540116954696152270401280857579766154722192925655963991820948894642657512288766330302133746367449217449351637104725732980832812726468187759356584218383594702792013663907689741738962252575782663990809792647011407580367850599381887184560094695833270775126181282015391041773950918244137561999937819240362469558235924171478702779448443108751901807414110290370706052085162975798361754251041642244867577350756338018895379263183389855955956527857227926155524494739363665533904528656215464288343162282921123290451842212532888101415884061619939195042230059898349966569463580186816717074818823215848647734386780911564660755175385552224428524049468033692299989300783900020690121517740696428573930196910500988278523053797637940257968953295112436166778910585557213381789089945453947915927374958600268237844486872037243488834616856290097850532497036933361942439802882364323553808208003875741710969289725499878566253048867033095150518452126944989251596392079421452606508516052325614861938282489838000815085351564642761700832096483117944401971780149213345335903336672376719229722069970766055482452247416927774637522135201716231722137632445699154022395494158227418930589911746931773776518735850032318014432883916374243795854695691221774098948611515564046609565094538115520921863711518684562543275047870530006998423140180169421109105925493596116719457630962328831271268328501760321771680400249657674186927113215573270049935709942324416387089242427584407651215572676037924765341808984312676941110313165951429479377670698881249643421933287404390485538222160837088907598277390184204138197811025854537088586701450623578513960109987476052535450100439353062072439709976445146790993381448994644609780957731953604938734950026860564555693224229691815630293922487606470873431166384205442489628760213650246991893040112513103835085621908060270866604873585849001704200923929789193938125116798421788115209259130435572321635660895603514383883939018953166274355609970015699780289236362349895374653428746875";
- )]
- end
switch function (char-upcase
-> char-downcase
-> char-upcase
....)
#lang racket (provide hellOwOrld) (define (hello-f n lst result) (if (null? lst) result (let ([char-f (if (= n 0) char-upcase char-downcase)] [n (if (= n 0) 1 0)]) (hello-f n (cdr lst) (append result (list (char-f (car lst)))))))) (define (hellOwOrld str) (list->string (hello-f 0 (filter (lambda (x) (not (char-whitespace? x))) (string->list (string-downcase str))) (list))))
- #lang racket
- (provide hellOwOrld)
(define (hello-upcase lst result)- (define (hello-f n lst result)
- (if (null? lst) result
(hello-downcase (cdr lst) (append result (list (char-upcase (car lst)))))))(define (hello-downcase lst result)(if (null? lst) result(hello-upcase (cdr lst) (append result (list (char-downcase (car lst)))))))- (let ([char-f (if (= n 0) char-upcase char-downcase)]
- [n (if (= n 0) 1 0)])
- (hello-f n (cdr lst) (append result (list (char-f (car lst))))))))
- (define (hellOwOrld str)
(list->string (hello-upcase (filter (lambda (x) (not (char-whitespace? x)))- (list->string (hello-f 0 (filter (lambda (x) (not (char-whitespace? x)))
- (string->list (string-downcase str))) (list))))
tail recursion version
let remove xs index = let rec loop n hd tl = match tl with | [] -> xs | _::tl when n = 0 -> (List.rev hd) @ tl | t::tl' -> loop (n - 1) (t :: hd) tl' in loop index [] xs;;
- let remove xs index =
let rec remove' (xs, index) =match xs with| [] -> []| x::xs' -> if index = 0 then xs' else x::(remove' (xs', index - 1))inif index < 0 then xs else remove' (xs, index);;- let rec loop n hd tl = match tl with
- | [] -> xs
- | _::tl when n = 0 -> (List.rev hd) @ tl
- | t::tl' -> loop (n - 1) (t :: hd) tl' in
- loop index [] xs;;
let char_list_of_string s = List.init (String.length s) (String.get s);; let string_of_char_list s = List.to_seq s |> String.of_seq;; let format_string_of_int x = let char_list = string_of_int x |> char_list_of_string |> List.rev in let rec loop lst work result = match (work, lst) with | (_ , []) -> (work @ result) |> string_of_char_list | (_::_::_::[], _) -> loop lst [] ((','::work) @ result) | (_, x::xs) -> loop xs (x::work) result in loop char_list [] [];;
module Format where- let char_list_of_string s = List.init (String.length s) (String.get s);;
- let string_of_char_list s = List.to_seq s |> String.of_seq;;
import Data.Bits ((.|.))import Data.Char (chr)f :: Integer -> [Char]f n = f' [chr $ (.|.) 48 $ fromIntegral $ mod n 10] (div n 10) wheref' :: [Char] -> Integer -> [Char]f' sol 0 = solf' acc n = f'' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) wheref'' :: [Char] -> Integer -> [Char]f'' sol 0 = solf'' acc n = f''' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) acc) (div n 10) wheref''' :: [Char] -> Integer -> [Char]f''' sol 0 = solf''' acc n = f' ((:) (chr $ (.|.) 48 $ fromIntegral $ mod n 10) $ (:) ',' acc) (div n 10)- let format_string_of_int x =
- let char_list = string_of_int x |> char_list_of_string |> List.rev in
- let rec loop lst work result = match (work, lst) with
- | (_ , []) -> (work @ result) |> string_of_char_list
- | (_::_::_::[], _) -> loop lst [] ((','::work) @ result)
- | (_, x::xs) -> loop xs (x::work) result in
- loop char_list [] [];;
module Tests = struct open OUnit let suite = ["format_string_of_int" >:: (fun _ -> assert_equal (format_string_of_int 100) "100"; assert_equal (format_string_of_int 1000) "1,000"; assert_equal (format_string_of_int 12345) "12,345"; assert_equal (format_string_of_int 123456789) "123,456,789"; assert_equal (format_string_of_int 10000000000) "10,000,000,000"; )] end
module FormatSpec where-- Tests can be written using Hspec http://hspec.github.io/-- Replace this with your own tests.import Test.Hspecimport Format-- `spec` of type `Spec` must existspec :: Specspec = dodescribe "Tests" $ doit "basic testing" $ do(f 100) `shouldBe` ("100" :: String)(f 1000) `shouldBe` ("1,000" :: String)(f 12345) `shouldBe` ("12,345" :: String)(f 123456789) `shouldBe` ("123,456,789" :: String)(f 10000000000) `shouldBe` ("10,000,000,000" :: String)-- the following line is optional for 8.2main = hspec spec- module Tests = struct
- open OUnit
- let suite = ["format_string_of_int"
- >:: (fun _ ->
- assert_equal (format_string_of_int 100) "100";
- assert_equal (format_string_of_int 1000) "1,000";
- assert_equal (format_string_of_int 12345) "12,345";
- assert_equal (format_string_of_int 123456789) "123,456,789";
- assert_equal (format_string_of_int 10000000000) "10,000,000,000";
- )]
- end
tail recursion version
let merge_lists a b = let rec loop a b result = match a, b with | c, [] | [], c -> (List.rev result) @ c | ax::a', bx::_ when ax < bx -> loop a' b (ax::result) | _, bx::b' -> loop a b' (bx::result) in loop a b [];;
let rec merge_lists a b =match a, b with| c, [] | [], c -> c| ax::a', bx::_ when ax < bx -> ax::(merge_lists a' b)| _, bx::b' -> bx::(merge_lists b' a);;- let merge_lists a b =
- let rec loop a b result =
- match a, b with
- | c, [] | [], c -> (List.rev result) @ c
- | ax::a', bx::_ when ax < bx -> loop a' b (ax::result)
- | _, bx::b' -> loop a b' (bx::result) in
- loop a b [];;
mutual recursion
#lang racket (provide hellOwOrld) (define (hello-upcase lst result) (if (null? lst) result (hello-downcase (cdr lst) (append result (list (char-upcase (car lst))))))) (define (hello-downcase lst result) (if (null? lst) result (hello-upcase (cdr lst) (append result (list (char-downcase (car lst))))))) (define (hellOwOrld str) (list->string (hello-upcase (filter (lambda (x) (not (char-whitespace? x))) (string->list (string-downcase str))) (list))))
public class HeLlOwOrLddddd {public static String convert(String input) {String salida = "";boolean mayus = true;for (int i=0;i<input.length();i++){if (Character.isLetter(input.charAt(i))){if (mayus){salida+=Character.toUpperCase(input.charAt(i));mayus=false;}else{salida+=Character.toLowerCase(input.charAt(i));mayus=true;}}}return salida;- #lang racket
- (provide hellOwOrld)
}}- (define (hello-upcase lst result)
- (if (null? lst) result
- (hello-downcase (cdr lst) (append result (list (char-upcase (car lst)))))))
- (define (hello-downcase lst result)
- (if (null? lst) result
- (hello-upcase (cdr lst) (append result (list (char-downcase (car lst)))))))
- (define (hellOwOrld str)
- (list->string (hello-upcase (filter (lambda (x) (not (char-whitespace? x)))
- (string->list (string-downcase str))) (list))))
#lang racket (require "solution.rkt") (require rackunit codewars/rackunit) (run-tests (test-suite "hello?" (test-case "world!" (check-equal? (hellOwOrld "Hello World") "HeLlOwOrLd")) (test-case "Qla...." (check-equal? (hellOwOrld " Ola QueAse") "OlAqUeAsE"))))
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- #lang racket
// TODO: Replace examples and use TDD development by writing your own tests- (require "solution.rkt")
- (require
- rackunit
- codewars/rackunit)
public class SolutionTest {@Testpublic void testSomething() {// assertEquals("expected", "actual");assertEquals("HeLlOwOrLd", HeLlOwOrLddddd.convert("Hello World"));assertEquals("OlAqUeAsE", HeLlOwOrLddddd.convert(" Ola QueAse"));}}- (run-tests
- (test-suite "hello?"
- (test-case "world!" (check-equal? (hellOwOrld "Hello World") "HeLlOwOrLd"))
- (test-case "Qla...." (check-equal? (hellOwOrld " Ola QueAse") "OlAqUeAsE"))))
true -> 1, false -> 0
OR:
or(2) = 1 + 1 -> true
or(1) = 1 + 0 -> true
or(1) = 0 + 1 -> true
or(0) = 0 + 0 = 0 -> false
or(x) = if x > 0 -> true
AND:
and(2) = 1 + 1 -> true
and(1) = 1 + 0 -> false
and(1) = 0 + 1 -> false
and(0) = 0 + 0 -> false
and(x) = if x > 1 (or x == 2) -> true
XOR
xor(2) = 1 + 1 -> false
xor(1) = 1 + 0 -> true
xor(1) = 0 + 1 -> true
xor(0) = 0 + 0 -> false
xor(x) = if x == 0 -> true
let int_of_bool = function | true -> 1 | false -> 0;; let (|||) x y = (int_of_bool x) + (int_of_bool y) > 0;; let (&&&) x y = (int_of_bool x) + (int_of_bool y) > 1;; let (|!|) x y = (int_of_bool x) + (int_of_bool y) = 1;;
bool Or(bool a, bool b){if(!a){if(!b){return false;}}return true;}bool Xor(bool a, bool b){return a != b;}bool And(bool a, bool b){if(a){if(b){return true;}}return false;}- let int_of_bool = function
- | true -> 1
- | false -> 0;;
- let (|||) x y = (int_of_bool x) + (int_of_bool y) > 0;;
- let (&&&) x y = (int_of_bool x) + (int_of_bool y) > 1;;
- let (|!|) x y = (int_of_bool x) + (int_of_bool y) = 1;;
module Tests = struct open OUnit let suite = [ "AND, OR, XOR" >::: ["AND" >:: (fun _ -> assert_equal (true &&& false) false; assert_equal (false &&& false) false; assert_equal (false &&& false) false; assert_equal (true &&& true) true; ); "OR" >:: (fun _ -> assert_equal (true ||| true) true; assert_equal (true ||| false) true; assert_equal (true ||| true) true; assert_equal (false ||| false) false; ); "XOR" >:: (fun _ -> assert_equal (true |!| true) false; assert_equal (true |!| false) true; assert_equal (false |!| true) true; assert_equal (false |!| false) false; )] ] end
// TODO: Replace examples and use TDD development by writing your own testsDescribe(AND_OR_XOR){It(AND){Assert::That(And(true, false), Equals(false));Assert::That(And(false, true), Equals(false));Assert::That(And(true, true), Equals(true));Assert::That(And(false, false), Equals(false));}It(OR){Assert::That(Or(true, false), Equals(true));Assert::That(Or(false, true), Equals(true));Assert::That(Or(true, true), Equals(true));Assert::That(Or(false, false), Equals(false));}It(XOR){Assert::That(Xor(true, false), Equals(true));Assert::That(Xor(false, true), Equals(true));Assert::That(Xor(true, true), Equals(false));Assert::That(Xor(false, false), Equals(false));}};- module Tests = struct
- open OUnit
- let suite = [
- "AND, OR, XOR"
- >:::
- ["AND"
- >:: (fun _ ->
- assert_equal (true &&& false) false;
- assert_equal (false &&& false) false;
- assert_equal (false &&& false) false;
- assert_equal (true &&& true) true;
- );
- "OR"
- >:: (fun _ ->
- assert_equal (true ||| true) true;
- assert_equal (true ||| false) true;
- assert_equal (true ||| true) true;
- assert_equal (false ||| false) false;
- );
- "XOR"
- >:: (fun _ ->
- assert_equal (true |!| true) false;
- assert_equal (true |!| false) true;
- assert_equal (false |!| true) true;
- assert_equal (false |!| false) false;
- )]
- ]
- end
let number_of_digits x = String.concat "" ["1"; String.make (x - 1) '0'] |> int_of_string;;
fn digits(mut n: u64) -> usize {let mut l = 1;while n >= 10 {n /= 10;l += 1;}l}- let number_of_digits x =
- String.concat "" ["1"; String.make (x - 1) '0'] |> int_of_string;;
module Tests = struct open OUnit let suite = [ "number_of_digits" >::: [ "1 -> 1" >:: (fun _ -> assert_equal (number_of_digits 1) 1); "10 -> 1000000000" >:: (fun _ -> assert_equal (number_of_digits 10) 1000000000); "18 -> 100000000000000000" >:: (fun _ -> assert_equal (number_of_digits 18) 100000000000000000); ] ] end
#[test]fn pow10() {assert_eq!(digits(0), 1);for i in 0..20 {assert_eq!(digits(POW10[i]), i+1);}assert_eq!(digits(std::u64::MAX), 20);}#[test]fn pow10_minus_1() {for i in 1..20 {assert_eq!(digits(POW10[i] - 1), i);}}#[test]fn pow10_half() {for i in 1..20 {assert_eq!(digits(POW10[i] / 2), i);}}- module Tests = struct
- open OUnit
- let suite = [
- "number_of_digits" >::: [
- "1 -> 1"
- >:: (fun _ -> assert_equal (number_of_digits 1) 1);
- "10 -> 1000000000"
- >:: (fun _ -> assert_equal (number_of_digits 10) 1000000000);
- "18 -> 100000000000000000"
- >:: (fun _ -> assert_equal (number_of_digits 18) 100000000000000000);
- ]
- ]
- end
#lang racket (provide verify-sum) (define (string->number-sum x) (apply + (map char->integer (string->list x)))) (define (verify-sum x y) (cond [(or (null? x) (null? y)) "NULL"] [(= (string->number-sum x) (string->number-sum y)) "TRUE"] [else "FALSE"]))
class Kata{public static String verifySum(String nameOne, String nameTwo) {int[] sumOfNames = new int[]{0, 0};if (nameOne == null || nameTwo == null) {return "NULL";}for (int i = 0; i < nameOne.length(); i++){sumOfNames[0] += nameOne.charAt(i);}for (int i = 0; i < nameTwo.length(); i++){sumOfNames[1] += nameTwo.charAt(i);}return sumOfNames[0] == sumOfNames[1] ? "TRUE" : "FALSE";}}- #lang racket
- (provide verify-sum)
- (define (string->number-sum x)
- (apply + (map char->integer (string->list x))))
- (define (verify-sum x y)
- (cond
- [(or (null? x) (null? y)) "NULL"]
- [(= (string->number-sum x) (string->number-sum y)) "TRUE"]
- [else "FALSE"]))
#lang racket (require "solution.rkt") (require rackunit codewars/rackunit) (run-tests (test-suite "verify-sum" (check-equal? (verify-sum "Sebastian" "Patricia") "FALSE") (check-equal? (verify-sum "Anna" "Nana") "TRUE") (check-equal? (verify-sum "John" null) "NULL")))
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;- #lang racket
- (require "solution.rkt")
- (require rackunit
- codewars/rackunit)
public class SolutionTest {@Testpublic void testName() {assertEquals("FALSE", Kata.verifySum("Sebastian", "Patricia"));assertEquals("TRUE", Kata.verifySum("Anna", "Nana"));assertEquals("NULL", Kata.verifySum("John", null));}}- (run-tests
- (test-suite "verify-sum"
- (check-equal? (verify-sum "Sebastian" "Patricia") "FALSE")
- (check-equal? (verify-sum "Anna" "Nana") "TRUE")
- (check-equal? (verify-sum "John" null) "NULL")))
#lang racket (provide flip) (define (flip xs) (define (inner xs result) (if (= (length xs) 0) result (inner (cdr xs) (append (list (if (list? (car xs)) (inner (car xs) (list)) (car xs))) result)))) (inner xs (list)))
(ns flip.core)(defn flip [xs](if (reversible? xs)(map flip (reverse xs))xs))- #lang racket
- (provide flip)
- (define (flip xs)
- (define (inner xs result)
- (if (= (length xs) 0)
- result
- (inner (cdr xs)
- (append (list
- (if (list? (car xs)) (inner (car xs) (list)) (car xs)))
- result))))
- (inner xs (list)))
#lang racket (require "solution.rkt") (require rackunit codewars/rackunit) (run-tests (test-suite "flip" (test-case "basic" (check-equal? (flip (list 1 2 3 4 5)) (list 5 4 3 2 1)) (check-equal? (flip (list 1 2 (list 3 4) 5)) (list 5 (list 4 3) 2 1)) (check-equal? (flip (list 1 2 (list 3 4 (list 5 6) 7) 8 (list 9 10))) (list (list 10 9) 8 (list 7 (list 6 5) 4 3) 2 1)))))
(ns flip.test(:require [clojure.test :refer :all][flip.core :refer [flip]]))- #lang racket
- (require "solution.rkt")
- (require rackunit
- codewars/rackunit)
(deftest flip-test(is (= (flip [1,2,3,4,5]) [5,4,3,2,1]))(is (= (flip [1,2,[3,4],5]) [5,[4,3],2,1]))(is (= (flip [1,2,[3,4,[5,6],7],8,[9,10]]) [[10,9],8,[7,[6,5],4,3],2,1])))- (run-tests
- (test-suite "flip"
- (test-case "basic"
- (check-equal? (flip (list 1 2 3 4 5)) (list 5 4 3 2 1))
- (check-equal? (flip (list 1 2 (list 3 4) 5)) (list 5 (list 4 3) 2 1))
- (check-equal? (flip (list 1 2 (list 3 4 (list 5 6) 7) 8 (list 9 10)))
- (list (list 10 9) 8 (list 7 (list 6 5) 4 3) 2 1)))))
let factorial n = let rec loop n result = match n with | 0 -> result | _ -> loop (n - 1) (result * n) in loop n 1;;
def factorial(n):# your code goes herereturn- let factorial n =
- let rec loop n result = match n with
- | 0 -> result
- | _ -> loop (n - 1) (result * n) in
- loop n 1;;
module Tests = struct open OUnit let suite = [ "Example" >::: [ "fact 0 -> 1" >:: (fun _ -> assert_equal (factorial 0) 1); "fact 1 -> 1" >:: (fun _ -> assert_equal (factorial 1) 1); "fact 3 -> 6" >:: (fun _ -> assert_equal (factorial 3) 6); "fact 5 -> 120" >:: (fun _ -> assert_equal (factorial 5) 120); "fact 6 -> 720" >:: (fun _ -> assert_equal (factorial 6) 720); ] ];; end
import codewars_test as test# TODO Write testsimport solution # or from solution import example# test.assert_equals(actual, expected, [optional] message)@test.describe("Example")def test_group():@test.it("test case")def test_case():test.assert_equals(factorial(5), 120)test.assert_equals(factorial(6), 720)test.assert_equals(factorial(3), 6)- module Tests = struct
- open OUnit
- let suite = [ "Example" >:::
- [
- "fact 0 -> 1" >:: (fun _ -> assert_equal (factorial 0) 1);
- "fact 1 -> 1" >:: (fun _ -> assert_equal (factorial 1) 1);
- "fact 3 -> 6" >:: (fun _ -> assert_equal (factorial 3) 6);
- "fact 5 -> 120" >:: (fun _ -> assert_equal (factorial 5) 120);
- "fact 6 -> 720" >:: (fun _ -> assert_equal (factorial 6) 720);
- ]
- ];;
- end