Skip to content

Commit 39b4f48

Browse files
authored
Merge pull request #501 from sir-gon/feature/recursive-digit-sum
Feature/recursive digit sum
2 parents 5be7bbe + 391cd31 commit 39b4f48

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# [Recursive Digit Sum](https://www.hackerrank.com/challenges/recursive-digit-sum)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingBasic`
5+
6+
We define super digit of an integer `x` using the following rules:
7+
8+
Given an integer, we need to find the super digit of the integer.
9+
10+
- If `x` has only `1` digit, then its super digit is `x`.
11+
- Otherwise, the super digit of `x` is equal to the super digit of
12+
the sum of the digits of `x`.
13+
14+
For example, the super digit of `9875` will be calculated as:
15+
16+
```text
17+
super_digit(9875) 9+8+7+5 = 29
18+
super_digit(29) 2 + 9 = 11
19+
super_digit(11) 1 + 1 = 2
20+
super_digit(2) = 2
21+
```
22+
23+
## Example
24+
25+
`n = 9875`
26+
27+
`k = 4`
28+
29+
The number `p` is created by concatenating the string `n` `k`
30+
times so the initial `p = 9875987598759875`.
31+
32+
```text
33+
superDigit(p) = superDigit(9875987598759875)
34+
9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116
35+
superDigit(p) = superDigit(116)
36+
1+1+6 = 8
37+
superDigit(p) = superDigit(8)
38+
```
39+
40+
All of the digits of `p` sum to `116`. The digits of `116` sum to `8`.
41+
`8` is only one digit, so it is the super digit.
42+
43+
## Function Description
44+
45+
Complete the function superDigit in the editor below.
46+
It must return the calculated super digit as an integer.
47+
48+
superDigit has the following parameter(s):
49+
50+
- `string n`: a string representation of an integer
51+
- `int k`: the times to concatenate to make
52+
53+
## Returns
54+
55+
- `int`: the super digit of repeated times
56+
57+
## Input Format
58+
59+
The first line contains two space separated integers, `n` and `k`.
60+
61+
## Constraints
62+
63+
- $ 1 \leq n \leq 10^100000 $
64+
- $ 1 \leq k \leq 10^5 $
65+
66+
## Sample Input 0
67+
68+
```text
69+
148 3
70+
```
71+
72+
## Sample Output 0
73+
74+
```text
75+
3
76+
```
77+
78+
## Explanation 0
79+
80+
Here `n = 148` and `k = 3`, so `p = 148148148`.
81+
82+
```text
83+
super_digit(P) = super_digit(148148148)
84+
= super_digit(1+4+8+1+4+8+1+4+8)
85+
= super_digit(39)
86+
= super_digit(3+9)
87+
= super_digit(12)
88+
= super_digit(1+2)
89+
= super_digit(3)
90+
= 3
91+
```
92+
93+
## Sample Input 1
94+
95+
```text
96+
9875 4
97+
```
98+
99+
## Sample Output 1
100+
101+
```text
102+
8
103+
```
104+
105+
## Sample Input 2
106+
107+
```text
108+
123 3
109+
```
110+
111+
## Sample Output 2
112+
113+
```text
114+
9
115+
```
116+
117+
## Explanation 2
118+
119+
Here `n = 123` and `k = 3`, so `p = 123123123`.
120+
121+
```text
122+
super_digit(P) = super_digit(123123123)
123+
= super_digit(1+2+3+1+2+3+1+2+3)
124+
= super_digit(18)
125+
= super_digit(1+8)
126+
= super_digit(9)
127+
= 9
128+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { superDigit } from './recursive_digit_sum.js';
5+
import TEST_CASES from './recursive_digit_sum.bruteforce.testcases.json';
6+
7+
describe('recursive_digit_sum bruteforce', () => {
8+
it('superDigit test cases', () => {
9+
expect.assertions(3);
10+
11+
TEST_CASES.forEach((test) => {
12+
const answer = superDigit(test.n, test.k);
13+
14+
console.debug(`superDigit(${test.n}) solution found: ${answer}`);
15+
16+
expect(answer).toStrictEqual(test.expected);
17+
});
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"expected": 7,
4+
"k": 100000,
5+
"n": "2365978831649490136475575038877779575813226775851820912370812124502641538947920808397703549713678494683928497712437176140282589350277653479225520602813456433277417366680426198633681891184348757007292907409160353745221125354212095528784124728447770959861439390350308313917365021363541712618686942946773324003146008424205688630371656757561012224744901800726911246423272186301595490993253791386102270201965996662707215300748516732223935858816466886068592299708740453558018878677753623653080545592459765998008028026982510689469213738241205802446029154833458048894002646934119082621498341445221491190955459548371083839625590505228681017724678315572531551988758568150699821635779156685637531274097856486075649357610713833072735231599919848220063026429718137766286716343385059699133211699189933339174843625266398503099203416124466032711453854413933737536836406105991857744766344461162222670876732729171585512468615558499979720269427922798431312270483732004392503905160233457811525428432787732543799783309593536386190295516419339222642886780012683583264436427241020490358960438948951090123073035203797984302163150042110707217274102457735317367100133807782064391421012191958312396649052833396257876824943425814834615313474161638240747120342368147351931074481983318414461554116111216672594256301273113776892080967125790153125125885441941114178586071406149630777323200516190208241341822285244325578953416388462284725673478766919050744786263188733438572307443267700831425575113213359873223948072988922668251652320316884761627830570057061821492039968369341602081382603302965910382997241199808824091331180464950187035576778206683245316006405529597170652549163351875206280564448346510252775085876212617353369513619186390565654064068546863018765466029315754619416429621887091818939474391383675337791979997519954871635692656223852981330368145996344325688247632566665645262588764650823901065646663460851602833982853795901687202035893967893724362979886948663369428689585509715442019662325024294581705265808022570365351645495802686891955348084550615538750809287498241260408673517746608582833123696027380353038348218786331710334697053069152326732702246704177127367642287975998486114018970510842276024855162228267767755948030029723573646908844109049374244456580474922058250964260437721278380069311972455077102266167098465788845261016395772392904280411167763443571285141388567074805986331207454652671618663858701085276806486000014124900483188940484497173286284987124699515039831183902383877120136788492339903316093693938833730548247174799553092402671083313285813003903363625219370039413610850308966558948057932583576771492037811460439359542902624599661588624295232252616245186844975995180218889230403968087888108494214819234992007182241562844376961711438491105585390645332161544542326095869642445733823661806228697208277656426326128938546125761166960351482948539114133280810916317873360419836067625116114001377672929333638768292095236809153349426894896869446197395671771932928211109385884940274160388152027885130001908095515987519207099973668415699818805100890690896126345099249836616348702742776559368591004254298734292430313132506862931339852599450961120591169865484036943294045257801193102728400887526383477970523720630900867795111210451854194391002244150921934506879640791680297603712618076174694948300699937409787596860516266369711284028834238022724596540529491965656156574337073988564171171562036245389632730259264953520190321920347601255396137920500496074443380910841069973360586715775083998842187392360751892748665579312212647821816086677544702037289983388842777905728540745718314922321086695691823249801139552886748181144717500999021690749662561227798159566905725946118195067702032931332172088385727012140744362147720922176656204772910422189621466379287662792372374145383317294646977085291873933547226857758530880168793511465268400187943977613894365634645591604177130794105552582447385631392788652188052953414781129655671439788151513996304231024358843023105354157338564270003904673736178109502550650713167258319522545824056524791781105181639237127875356417268878249642171688744070159985867211941494396566216365392991627962886902198712613982017603237174682825814797983967054038269833008728407345912699623004623467287738296110774212005294050799218227654983820235796069236099706911525806244933683548619651925300435846989868138976944006822652462513903624200398184640627878400159480147462063679751851193719499676402496701528971830054482832320057097082360875809145428036636600549270698504540758319126452884313155162288201445118708935758033035083555929081756050975974040793712887964439001525986626643925845343840647344753344394209812309532147589135665638895095150172324875219965305745649692175878476631167339609984047545458299489650202318886162798841051758853022389219423402386031324398337411422060786064174337895040820526914615325075313448800789763344707190242881920892579588963376086146960633381215378914452523108903834607764477580583821712879080194987840450703336371649758616364805184545636558639909806705757141231728890945705281578891764357530862478284617890886615612608017861029342475376559517893073874528735689724989147963227442563141916115837740823088920018306622898786665697436694114134566032298418513134640110497915781256013658157626876007303865007938799121914067544773285391447059337068967640221461662745594510968605338966108977317081902507941847885331172633986644666002196037962414608353479436996966565895587993028389782659415971152403029518493019029205611785920255633270560095438637318885230551138363908343081523545591224134467205438637992783829259919217744713264231649621461535887435410548366408012977973203459983761967831442350892580662433295719596302715165438994052920660204857279403439598616231631708579334309451829396365207336409446940374623441420435439989058506571776629533788131454314121504595268258657172190365178279637763408587826711292793610001999656837923788319438384372260168303662843086203813103771712423574817520332644704407707584630263836497460974781867543909787940278207946350316762434685271126547340568379697114143432270663747926423410939366867323227307888313764100323948708488249291932762190879422500980573314510481474989813161818046596342997489244092851481456635241462079248335760582513488928151459281907438730238065382341691833250917737617330046824951709287812847967496061820848573006896422220098029186444659696393722864425443174804115295661452095886127784785656396807536597522937517010395385281668735072368362229248386090765688300603292905737705424037244946746445640174635429078019229973366378415273692915764515173009646632256826278792193749314522687455317800604524740119271772480341422364037622841158408693597798800732407214231715176216414119458470398433698630492023148912788371335047944731850924533486580463894436416946298869397354691924065211837844914014837056117850937923852135361875895012428779496342389871713463333419295329258828037504468306416370691252321110783303933730724981220411655523128107813025668960480700236419568269112404054054013255954297124592253625509027169128346186086879842249233969016492009074059375814396213470661585889025858173286307393463166771278528359358679107663618852904436981159408575155381452852538538161326799566605463071606829507721627760695695298663348037499346965080571845983286426728562771057921340809114888275207834276278723670038713251819678117637307797126524335451875674322135660001938501046206646725087020563845482789165114015140237476770960542212541262365674096922377749965211055083880951870947588439313087505990968967904452943561444336754539161497904651842954423690339860136920316455021978105823558793189756025331264642326253916951167641064028719315782248353333706214902160610987866723541787809188383034860801960339654982067421224921066723519523759569071993168783868197116308035921525346492767986090665094574147296651701824963729081365854249511700896706324052098265362182297874817162325372836249682033460453106454759294279011517085352648052686287835826956331856000530656816094258663224239727567999798475939204052755892731997428948369450364798425229576414764799300949556003266863462364785971588403993047829919672884685002127952124093077144535644623315342420032186819555104280110883937594970132297831017511120535205810256141119655336117669459771376005411248881994640753601606805393583940281645304757423903447751752383876356426048210439751541893763188890403792485964646286690953432929961087972625329578244854544295899075470411907779181886987220577672224785177419791954783277253421243333498197370278143247403594312138218592254174197047846302000164735966226394070001204323764164912690541799927573761616438820835021411743898829210130296149250887986491553547221899402096605456148489590866922112294624680546281250567297009605663936998939340731539740128008013832941045470722922727419033890751091173021386847464099733717633316304536930447077599861319788266450790785072377536483225439587367122393671167950836029510856465462738804587713268436992128892171509071641161165880608224322539890931445909082786991260734902410993863449929626354001121991398640542747424891140797115509495342486678389565402504229791244504867508301342798992744796454984189142033193018659744179186808244403688375727771580862592404226696538971232713170205127941869698531179001250635892043048835137445218382957976989721755706861701366347894376710532041643279157597459032202591566808167568419301491754821992741943996185368155017334681412500723160011387595363158410560488203993638466615700276165502049790282263414502568166837301414930688269694553727263733690839349714652132206285243796184219815308355634318194592022805870387606542297381666905458451054204419608774066516116961329031388338821535694456637910702328420582124442440139995352494707284230907975581024232726130673375360163257154562557607055131368534412734263401317160921075802949888891100336188281116884460133424076740001934250575466042922149519629860344218277351996828116545149383178424384811799946144322536711027462093434842033767504492071349955677051700618860129870409856113128392373584622061075886499407885141951318807519645386474",
6+
"title": "Sample Test case 7"
7+
}
8+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/recursion_and_backtracking/recursive-digit-sum.md]]
3+
*/
4+
5+
const RADIX = 10;
6+
7+
export function superDigitCompute(n) {
8+
if (n.length === 1) {
9+
return parseInt(n, RADIX);
10+
}
11+
12+
let partial = 0;
13+
for (const digit of n) {
14+
partial += parseInt(digit, RADIX);
15+
}
16+
17+
return superDigitCompute(`${partial}`);
18+
}
19+
20+
export function superDigit(n, k) {
21+
const accumulator = `${superDigitCompute(n)}`;
22+
23+
let result = '';
24+
for (let i = 0; i < k; i++) {
25+
result = `${result}${accumulator}`;
26+
}
27+
28+
return superDigitCompute(result);
29+
}
30+
31+
export default { superDigit };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { superDigit } from './recursive_digit_sum.js';
5+
import TEST_CASES from './recursive_digit_sum.testcases.json';
6+
7+
describe('recursive_digit_sum', () => {
8+
it('superDigit test cases', () => {
9+
expect.assertions(4);
10+
11+
TEST_CASES.forEach((test) => {
12+
const answer = superDigit(test.n, test.k);
13+
14+
console.debug(`superDigit(${test.n}) solution found: ${answer}`);
15+
16+
expect(answer).toStrictEqual(test.expected);
17+
});
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"n": "148",
5+
"k": 3,
6+
"expected": 3
7+
},
8+
{
9+
"title": "Sample Test case 10",
10+
"n": "9875",
11+
"k": 4,
12+
"expected": 8
13+
},
14+
{
15+
"title": "Sample Test case 11",
16+
"n": "123",
17+
"k": 3,
18+
"expected": 9
19+
},
20+
{
21+
"title": "Sample Test case 10",
22+
"n": "9875",
23+
"k": 4,
24+
"expected": 8
25+
}
26+
]

0 commit comments

Comments
 (0)