The TransCompilator

HTML chunk · 50 solved programs

Chunk 13 · #0828–#0879

Full original English source and expected stdout for fifty solved programs. Prefer plain text? Read the Markdown equivalent.

#0828 · Count Unique Characters of All Substrings of a Given String

Hard String

Keep a shared word as empty text. Place "ABA" into word. Keep a shared symbol as empty text. Keep a shared fresh of 0. Keep a shared unique of 0.

To check up to high (a whole number) with spot (a whole number). If spot is more than high, return. The other is the slice of word from spot to spot plus 1. If other matches symbol, set fresh to 0, return. Check up to high with spot plus 1.

To judge from start (a whole number) with finish (a whole number) and spot (a whole number). If spot is more than finish, return. Set symbol to the slice of word from spot to spot plus 1. Set fresh to 1. Check up to spot minus 1 with start. Check up to finish with spot plus 1. If fresh is 1, add 1 to unique. Judge from start with finish and spot plus 1.

The total is 0.
For every start from 0 to 2.
For every finish from start to 2.
Set unique to 0. Judge from start with finish and start. Add unique to total.

Show total.

Expected stdout

8

Open in Playground Standalone Page

#0829 · Consecutive Numbers Sum

Hard Math

The target is 15. The ways is 0. The length is 1.
Repeat while length times (length plus 1) divided by 2 is at most target.
The rest is length times (length minus 1) divided by 2. The start is target minus rest. If start modulo length is 0 and start divided by length is more than 0, add 1 to ways. Add 1 to length.

Show ways.

Expected stdout

4

Open in Playground Standalone Page

#0830 · Positions of Large Groups

Easy String

Keep a shared source as empty text. Place "abbxxxxzzy" into source. Keep a shared last of the last position of source. Keep a shared answers as an empty list of text. Keep a shared closing of 0.

To store from openat (a whole number) with closeat (a whole number). The line is empty text. Place "[" into line. The firsttext is format whole number openat as text. Place firsttext into line. Place "," into line. The secondtext is format whole number closeat as text. Place secondtext into line. Place "]" into line. The copy is a copy of line. Place copy into answers.

To check from spot (a whole number). The next is spot plus 1. If next is more than last, set closing to 1, return. The current is the slice of source from spot to spot plus 1. The following is the slice of source from next to next plus 1. If current does not match following, set closing to 1.

The start is 0.
For every spot from 0 to last.
Set closing to 0. Check from spot. The span is spot minus start plus 1. If closing is 1 and span is at least 3, Store from start with spot. If closing is 1, set start to spot plus 1.

For each answer in answers, show answer.

Expected stdout

[3,6]

Open in Playground Standalone Page

#0831 · Masking Personal Information

Medium String

Keep a shared source as empty text. Place "Alice@Example.com" into source. Keep a shared bigletters as empty text. Place "ABCDEFGHIJKLMNOPQRSTUVWXYZ" into bigletters. Keep a shared smallletters as empty text. Place "abcdefghijklmnopqrstuvwxyz" into smallletters. Keep a shared current as empty text. Keep a shared converted as empty text. Keep a shared softened as empty text. Keep a shared domaintext as empty text. Keep a shared line as empty text. Keep a shared digits as empty text. Keep a shared lowdigit as empty text. Place "0" into lowdigit. Keep a shared highdigit as empty text. Place "9" into highdigit. Keep a shared foundspot of 0.

To lower from spot (a whole number). If spot is more than 25, return. The bigpiece is the slice of bigletters from spot to spot plus 1. If bigpiece matches current, set converted to the slice of smallletters from spot to spot plus 1, return. Lower from spot plus 1.

To soften from spot (a whole number). If spot is more than the last position of domaintext, return. Set current to the slice of domaintext from spot to spot plus 1. Set converted to current. Lower from 0. Place converted into softened. Soften from spot plus 1.

To pad from count (a whole number) with limit (a whole number). If count is at least limit, return. Place "*" into line. Pad from count plus 1 with limit.

To findat from spot (a whole number). If spot is more than the last position of source, return. The piece is the slice of source from spot to spot plus 1. If piece matches "@", set foundspot to spot, return. Findat from spot plus 1.

To veil. Findat from 0. The person is the slice of source from 0 to foundspot. The sourcelast is the last position of source. The finish is sourcelast plus 1. The begin is foundspot plus 1. The domain is the slice of source from begin to finish. Set domaintext to domain. Set current to the slice of person from 0 to 1. Set converted to current. Lower from 0. The firstletter is converted. The lastspot is the last position of person. The localsize is lastspot plus 1. The endspot is localsize minus 1. Set current to the slice of person from endspot to localsize. Set converted to current. Lower from 0. The lastletter is converted. Set line to empty text. Place firstletter into line. Pad from 0 with localsize minus 2. Place lastletter into line. Place "@" into line. Set softened to empty text. Soften from 0. Place softened into line. Show line.

To gather digits from spot (a whole number). If spot is more than the last position of source, return. The piece is the slice of source from spot to spot plus 1. If piece comes before lowdigit, Gather digits from spot plus 1, return. If highdigit comes before piece, Gather digits from spot plus 1, return. Place piece into digits. Gather digits from spot plus 1.

To cloak. Set digits to empty text. Gather digits from 0. The lastdigit is the last position of digits. The start is lastdigit minus 3. The tail is the slice of digits from start to lastdigit plus 1. Set line to empty text. Place "***-***-" into line. Place tail into line. Show line.

The postal is 0.
For every spot from 0 to the last position of source.
The piece is the slice of source from spot to spot plus 1. If piece matches "@", set postal to 1.

If postal is 1, Veil. If postal is 0, Cloak.

Expected stdout

a***e@example.com

Open in Playground Standalone Page

#0832 · Flipping an Image

Easy Array

The image is a list of 1, 1, 0, 1, 0, 1, 0, 0, and 0. The answers is an empty list of text.
For every row from 0 to 2.
The line is empty text. Place "[" into line.
For every column from 0 to 2.
The mirror is 2 minus column. The value is item (row times 3 plus mirror) of image. The flipped is 1 minus value. The piece is format whole number flipped as text. If column is more than 0, Place "," into line. Place piece into line. If column is 2, Place "]" into line. If column is 2, The copy is a copy of line, Place copy into answers.

For each answer in answers, show answer.

Expected stdout

[1,0,0]
[0,1,0]
[1,1,1]

Open in Playground Standalone Page

#0833 · Find And Replace in String

Medium Array

Keep a shared source as empty text. Place "abcd" into source. Keep a shared indices holding 2 and 0. Keep a shared sources holding "cd" and "a". Keep a shared targets holding "ffff" and "eee". Keep a shared used holding 0 and 0. Keep a shared ordercount of 2. Keep a shared best of 0. Keep a shared pick of 0. Keep a shared cursor of 0. Keep a shared result as empty text.

To seek from slot (a whole number). If slot is at least ordercount, return.
If item slot of used is 0 and item slot of indices is less than best.
Set best to item slot of indices. Set pick to slot. Seek from slot plus 1.

To render from spot (a whole number) with slot (a whole number). If spot is less than cursor, return. The head is the slice of source from cursor to spot. Place head into result. Set cursor to spot. The pattern is item slot of sources. The patternlast is the last position of pattern. The size is patternlast plus 1. The finish is spot plus size. The piece is the slice of source from spot to finish. If piece matches pattern, Place item slot of targets into result. If piece matches pattern, Set cursor to finish. If piece does not match pattern, Place piece into result. If piece does not match pattern, Set cursor to finish.

The step is 0.
Repeat while step is less than ordercount.
Set best to 1000000. Set pick to -1. Seek from 0. The slot is pick. Set item slot of used to 1. The spot is item slot of indices. Render from spot with slot. Increase step by 1.

The endspot is the last position of source. The tail is the slice of source from cursor to endspot plus 1. Place tail into result. Show result.

Expected stdout

eeebffff

Open in Playground Standalone Page

#0834 · Sum of Distances in Tree

Hard Dynamic Programming

Keep a shared lefts holding 0, 0, 2, 2, and 2. Keep a shared rights holding 1, 2, 3, 4, and 5. Keep a shared sizes as an empty list of whole numbers. Keep a shared answers as an empty list of whole numbers. Keep a shared travel of 0. Keep a shared gain of 0.
For every place from 0 to 5.
Place 0 into sizes. Place 0 into answers.

To measure from node (a whole number) with parent (a whole number). Set item node of sizes to 1.
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Measure from second with node. If first is node and second is not parent, Set item node of sizes to item node of sizes plus item second of sizes. If second is node and first is not parent, Measure from first with node. If second is node and first is not parent, Set item node of sizes to item node of sizes plus item first of sizes.

To gather from node (a whole number) with parent (a whole number) and depth (a whole number). Increase travel by depth.
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Gather from second with node and depth plus 1. If second is node and first is not parent, Gather from first with node and depth plus 1.

To spread from node (a whole number) with parent (a whole number).
For every edge from 0 to 4.
The first is item edge of lefts. The second is item edge of rights. If first is node and second is not parent, Set gain to 6 minus 2 times item second of sizes. If first is node and second is not parent, Set item second of answers to item node of answers plus gain. If first is node and second is not parent, Spread from second with node. If second is node and first is not parent, Set gain to 6 minus 2 times item first of sizes. If second is node and first is not parent, Set item first of answers to item node of answers plus gain. If second is node and first is not parent, Spread from first with node.

Measure from 0 with -1. Set travel to 0. Gather from 0 with -1 and 0. Set item 0 of answers to travel. Spread from 0 with -1. The line is empty text.
For every node from 0 to 5.
If node is more than 0, Place "," into line. The piece is format whole number item node of answers as text. Place piece into line.

Show line.

Expected stdout

8,12,6,10,10,10

Open in Playground Standalone Page

#0835 · Image Overlap

Medium Array

Keep a shared first holding 1, 1, 0, 0, 1, 0, 0, 1, and 0. Keep a shared second holding 0, 0, 0, 0, 1, 1, 0, 0, and 1. Keep a shared best of 0.

To score from down (a whole number) with across (a whole number). The overlap is 0.
For every sourcerow from 0 to 2.
For every sourcecolumn from 0 to 2.
The sourcevalue is item (sourcerow times 3 plus sourcecolumn) of first. The targetrow is sourcerow plus down. The targetcolumn is sourcecolumn plus across.
If sourcevalue is 1 and targetrow is at least 0 and targetrow is less than 3 and targetcolumn is at least 0 and targetcolumn is less than 3.
The targetvalue is item (targetrow times 3 plus targetcolumn) of second. If targetvalue is 1, add 1 to overlap. If overlap is more than best, set best to overlap.

For every rowshift from 0 to 4.
The down is rowshift minus 2.
For every columnshift from 0 to 4.
The across is columnshift minus 2. Score from down with across.

Show best.

Expected stdout

3

Open in Playground Standalone Page

#0836 · Rectangle Overlap

Easy Math

The first is a list of 0, 0, 2, and 2. The second is a list of 1, 1, 3, and 3. The apart is 0.
For every axis from 0 to 1.
The firstlow is item axis of first. The firsthigh is item (axis plus 2) of first. The secondlow is item axis of second. The secondhigh is item (axis plus 2) of second. If firsthigh is at most secondlow, set apart to 1. If secondhigh is at most firstlow, set apart to 1.

If apart is 1, show "false". If apart is 0, show "true".

Expected stdout

true

Open in Playground Standalone Page

#0837 · New 21 Game

Medium Math

Keep a shared chances as an empty list of decimals. Keep a shared maxpoints of 0. Keep a shared faces as a decimal. Keep a shared stopping of 0. Set maxpoints to 10. Set faces to 10.0. Set stopping to 1.
For every place from 0 to 6.
Place 0.0 into chances.

To imagine from score (a whole number). The total is 0.0.
For every step from 1 to maxpoints.
The prior is score minus step. If prior is at least 0 and prior is less than stopping, The value is item prior of chances, Set total to total plus value. If step is maxpoints, Set item score of chances to total divided by faces.

Set item 0 of chances to 1.0.
For every score from 1 to 6.
Imagine from score.

The answer is 0.0.
For every score from 1 to 6.
The part is item score of chances. Add part to answer.

Show answer.

Expected stdout

0.6000

Open in Playground Standalone Page

#0838 · Push Dominoes

Medium String

The source is ".L.R...LR..L..". The last is the last position of source. The extent is 14. The rightforce is a list of 14 zeros. The leftforce is a list of 14 zeros. The verdict is a list of 14 zeros. The lastright is -1.
For every spot from 0 to last.
The symbol is the slice of source from spot to spot plus 1. If symbol matches "R", set lastright to spot. If symbol matches "L", set lastright to -1. If lastright is at least 0, set item spot of rightforce to spot minus lastright. If lastright is less than 0, set item spot of rightforce to extent.

The lastleft is -1. The spot is last.
Repeat while spot is at least 0.
The symbol is the slice of source from spot to spot plus 1. If symbol matches "L", set lastleft to spot. If symbol matches "R", set lastleft to -1. If lastleft is at least 0, set item spot of leftforce to lastleft minus spot. If lastleft is less than 0, set item spot of leftforce to extent. Decrease spot by 1.

For every spot from 0 to last.
The symbol is the slice of source from spot to spot plus 1. If symbol matches "R", set item spot of verdict to 1. If symbol matches "L", set item spot of verdict to 2.
If symbol matches ".".
The fromright is item spot of rightforce. The fromleft is item spot of leftforce. If fromright is less than fromleft, set item spot of verdict to 1. If fromleft is less than fromright, set item spot of verdict to 2.

The result is empty text.
For each state in verdict.
If state is 0, Place "." into result. If state is 1, Place "R" into result. If state is 2, Place "L" into result.

Show result.

Expected stdout

LL.RR.LLRRLL..

Open in Playground Standalone Page

#0839 · Similar String Groups

Hard Array

Keep a shared words holding "tars", "rats", "arts", and "star". Keep a shared graph as an empty list of whole numbers. Keep a shared seen as an empty list of whole numbers. Keep a shared firstword as empty text. Keep a shared secondword as empty text. Keep a shared differences of 0. Keep a shared leftspot of 0. Keep a shared rightspot of 0. Keep a shared alike of 0.
For every place from 0 to 15.
Place 0 into graph.
For every place from 0 to 3.
Place 0 into seen.

To finish. Set alike to 0. If differences is 0, set alike to 1, return. If differences is not 2, return. The firstleft is the slice of firstword from leftspot to leftspot plus 1. The firstright is the slice of firstword from rightspot to rightspot plus 1. The secondleft is the slice of secondword from leftspot to leftspot plus 1. The secondright is the slice of secondword from rightspot to rightspot plus 1.
If firstleft matches secondright.
If firstright matches secondleft, set alike to 1.

To scan from spot (a whole number). If spot is more than the last position of firstword, Finish, return. The one is the slice of firstword from spot to spot plus 1. The two is the slice of secondword from spot to spot plus 1. If one does not match two, add 1 to differences, set rightspot to leftspot, set leftspot to spot. Scan from spot plus 1.

To check from first (a whole number) with second (a whole number). Set alike to 0. Set firstword to item first of words. Set secondword to item second of words. Set differences to 0. Set leftspot to -1. Set rightspot to -1. If firstword matches secondword, set alike to 1, return. Scan from 0.

To visit from node (a whole number). If item node of seen is 1, return. Set item node of seen to 1.
For every other from 0 to 3.
If item (node times 4 plus other) of graph is 1 and item other of seen is 0, Visit from other.

For every pair from 0 to 15.
The first is pair divided by 4. The second is pair modulo 4.
If first is less than second.
Check from first with second. If alike is 1, set item (first times 4 plus second) of graph to 1. If alike is 1, set item (second times 4 plus first) of graph to 1.

The groups is 0.
For every start from 0 to 3.
If item start of seen is 0, add 1 to groups. If item start of seen is 0, Visit from start.

Show groups.

Expected stdout

2

Open in Playground Standalone Page

#0840 · Magic Squares In Grid

Medium Array

Keep a shared grid as an empty list of whole numbers. Keep a shared rows of 0. Keep a shared columns of 0. Keep a shared total of 0. Keep a shared valid of 0. Keep a shared marks as an empty list of whole numbers. Place 4 into grid. Place 3 into grid. Place 8 into grid. Place 4 into grid. Place 9 into grid. Place 5 into grid. Place 1 into grid. Place 9 into grid. Place 2 into grid. Place 7 into grid. Place 6 into grid. Place 2 into grid. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks. Place 0 into marks.

To inspect from top (a whole number) with column (a whole number). Set valid to 1. The base is top times columns plus column. The stamp is top times columns plus column plus 1. The northwest is item base of grid. The north is item (base plus 1) of grid. The northeast is item (base plus 2) of grid. The west is item (base plus columns) of grid. The middle is item (base plus columns plus 1) of grid. The east is item (base plus columns plus 2) of grid. The southwest is item (base plus columns plus columns) of grid. The south is item (base plus columns plus columns plus 1) of grid. The southeast is item (base plus columns plus columns plus 2) of grid. If northwest is less than 1 or northwest is more than 9, return. If north is less than 1 or north is more than 9, return. If northeast is less than 1 or northeast is more than 9, return. If west is less than 1 or west is more than 9, return. If middle is less than 1 or middle is more than 9, return. If east is less than 1 or east is more than 9, return. If southwest is less than 1 or southwest is more than 9, return. If south is less than 1 or south is more than 9, return. If southeast is less than 1 or southeast is more than 9, return. If item northwest of marks is stamp, set valid to 0, otherwise set item northwest of marks to stamp. If item north of marks is stamp, set valid to 0, otherwise set item north of marks to stamp. If item northeast of marks is stamp, set valid to 0, otherwise set item northeast of marks to stamp. If item west of marks is stamp, set valid to 0, otherwise set item west of marks to stamp. If item middle of marks is stamp, set valid to 0, otherwise set item middle of marks to stamp. If item east of marks is stamp, set valid to 0, otherwise set item east of marks to stamp. If item southwest of marks is stamp, set valid to 0, otherwise set item southwest of marks to stamp. If item south of marks is stamp, set valid to 0, otherwise set item south of marks to stamp. If item southeast of marks is stamp, set valid to 0, otherwise set item southeast of marks to stamp. The firstrow is northwest plus north plus northeast. The secondrow is west plus middle plus east. The thirdrow is southwest plus south plus southeast. The firstcolumn is northwest plus west plus southwest. The secondcolumn is north plus middle plus south. The thirdcolumn is northeast plus east plus southeast. The diagonal is northwest plus middle plus southeast. The cross is northeast plus middle plus southwest. If valid is 1 and firstrow is 15 and secondrow is 15 and thirdrow is 15 and firstcolumn is 15 and secondcolumn is 15 and thirdcolumn is 15 and diagonal is 15 and cross is 15, add 1 to total.

Set rows to 3. Set columns to 4.
For every top from 0 to rows minus 3.
For every column from 0 to columns minus 3.
Inspect from top with column.

Show total.

Expected stdout

1

Open in Playground Standalone Page

#0841 · Keys and Rooms

Medium Depth-First Search

Keep a shared edges holding 1, 2, and 3. Keep a shared starts holding 0, 1, 2, and 3. Keep a shared counts holding 1, 1, 1, and 0. Keep a shared queue as an empty list of whole numbers. Keep a shared seen as an empty list of whole numbers.
For every room from 0 to 3.
Place 0 into seen.

Place 0 into queue. Set item 0 of seen to 1. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head.
For every offset from 0 to item node of counts minus 1.
The target is item (item node of starts plus offset) of edges.
If item target of seen is 0.
Set item target of seen to 1. Place target into queue. Add 1 to count.

The reached is 0.
For every room from 0 to 3.
If item room of seen is 1, add 1 to reached.

If reached is 4, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0842 · Split Array into Fibonacci Sequence

Medium String

Keep a shared digits as empty text. Keep a shared chosen as an empty list of whole numbers. Keep a shared answer as an empty list of whole numbers. Keep a shared parsed of 0. Keep a shared found of 0. Keep a shared size of 0.

To parse from start (a whole number) with finish (a whole number). Set parsed to 0.
For every spot from start to finish.
The number is the digit value of character spot of digits. Set parsed to parsed times 10 plus number.

To collect from spot (a whole number) with limit (a whole number). If spot is at least limit, return. Place item spot of chosen into answer. Collect from spot plus 1 with limit.

To extend from start (a whole number) with depth (a whole number). If found is 1, return. If start is size and depth is at least 3, Collect from 0 with depth, set found to 1. If start is size, return.
For every finish from start to size minus 1.
The lead is the digit value of character start of digits. If finish is more than start and lead is 0, stop. Parse from start with finish. If parsed is more than 2147483647, stop. The valid is 0. If depth is less than 2, set valid to 1. If depth is at least 2 and parsed is item (depth minus 1) of chosen plus item (depth minus 2) of chosen, set valid to 1.
If valid is 1.
Place parsed into chosen. Extend from finish plus 1 with depth plus 1. Remove the last item from chosen.

Place "123456579" into digits. Set size to 9. Extend from 0 with 0. The rendered is empty text.
If found is 1.
For every spot from 0 to the last position of answer.
If spot is more than 0, place "," into rendered. Place format whole number item spot of answer as text into rendered.

If found is 1, show rendered. If found is 0, show "[]".

Expected stdout

123,456,579

Open in Playground Standalone Page

#0844 · Backspace String Compare

Easy String

The source is "ab#c", while the other is "ad#c". The cleaned is empty text. The trimmed is empty text. The kept is 0, while the spared is 0.
For every spot from 0 to 3.
The symbol is the slice of source from spot to spot plus 1. The retreat is 0. If symbol matches "#", set retreat to 1. If retreat is 1 and kept is more than 0, remove last character from cleaned, subtract 1 from kept. If retreat is 0, place symbol into cleaned, add 1 to kept.

For every spot from 0 to 3.
The symbol is the slice of other from spot to spot plus 1. The retreat is 0. If symbol matches "#", set retreat to 1. If retreat is 1 and spared is more than 0, remove last character from trimmed, subtract 1 from spared. If retreat is 0, place symbol into trimmed, add 1 to spared.

If cleaned matches trimmed, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0845 · Longest Mountain in Array

Medium Array

The heights is a list of 2, 1, 4, 7, 3, 2, 5. The last is 6. The climbing is a list of 7 zeros. Set item 0 of climbing to 1.
For every spot from 1 to last.
If item spot of heights is more than item (spot minus 1) of heights, set item spot of climbing to item (spot minus 1) of climbing plus 1, otherwise set item spot of climbing to 1.

The falling is a list of 7 zeros. Set item last of falling to 1. The spot is last minus 1.
Repeat while spot is at least 0.
If item spot of heights is more than item (spot plus 1) of heights, set item spot of falling to item (spot plus 1) of falling plus 1, otherwise set item spot of falling to 1. Subtract 1 from spot.

The best is 0.
For every spot from 0 to last.
If item spot of climbing is more than 1 and item spot of falling is more than 1.
The span is item spot of climbing plus item spot of falling minus 1. If span is more than best, set best to span.

Show best.

Expected stdout

5

Open in Playground Standalone Page

#0846 · Hand of Straights

Medium Array

Keep a shared hand holding 1, 2, 3, 6, 2, 3, 4, 7, and 8. Keep a shared used holding 0, 0, 0, 0, 0, 0, 0, 0, and 0. Keep a shared last of 0. Keep a shared hit of 0.

To seek from look (a whole number) with want (a whole number). If look is more than last, return. If item look of used is 0 and item look of hand is want, set hit to look, return. Seek from look plus 1 with want.

Set last to 8. Sort hand. The possible is 1. The group is 3.
For every spot from 0 to last.
If possible is 0, stop.
If item spot of used is 0.
The base is item spot of hand. The need is 0.
Repeat while need is less than group and possible is 1.
Set hit to -1. Seek from spot with base plus need. If hit is less than 0, set possible to 0. If hit is at least 0, set item hit of used to 1. Add 1 to need.

If possible is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0847 · Shortest Path Visiting All Nodes

Hard Dynamic Programming

The edges is a list of 1, 2, 3, 0, 0, 0. The starts is a list of 0, 3, 4, 5. The counts is a list of 3, 1, 1, 1. The seen is a list of 64 zeros. The queue is an empty list of whole numbers. The depths is an empty list of whole numbers.
For every node from 0 to 3.
The startmask is shift 1 left by node. The code is node times 16 plus startmask. Set item code of seen to 1. Place code into queue. Place 0 into depths.

The head is 0. The count is 4. The answer is -1.
Repeat while head is less than count and answer is less than 0.
The state is item head of queue. The distance is item head of depths. Add 1 to head. The node is state divided by 16. The mask is state modulo 16.
For every offset from 0 to item node of counts minus 1.
The target is item (item node of starts plus offset) of edges. The bit is shift 1 left by target. The merged is bitwise or of mask and bit. If merged is 15, set answer to distance plus 1, stop. The code is target times 16 plus merged.
If item code of seen is 0.
Set item code of seen to 1. Place code into queue. Place distance plus 1 into depths. Add 1 to count.

Show answer.

Expected stdout

4

Open in Playground Standalone Page

#0848 · Shifting Letters

Medium Array

Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared symbol as empty text. Keep a shared found of 0.

To locate from look (a whole number). The piece is the slice of alphabet from look to look plus 1. If piece matches symbol, set found to look, return. Locate from look plus 1.

The word is "abc". The shifts is a list of 3, 5, 9. The last is 2. The suffix is a list of 3 zeros. The running is 0. The spot is last.
Repeat while spot is at least 0.
Increase running by item spot of shifts. Set item spot of suffix to running. Subtract 1 from spot.

The result is empty text.
For every spot from 0 to last.
Set symbol to the slice of word from spot to spot plus 1. Set found to 0. Locate from 0. The moved is (found plus item spot of suffix) modulo 26. The letter is the slice of alphabet from moved to moved plus 1. Place letter into result.

Show result.

Expected stdout

rpl

Open in Playground Standalone Page

#0849 · Maximize Distance to Closest Person

Medium Array

The seats is a list of 1, 0, 0, 0, 1, 0, 1, while the last is 6. The nearest is a list of 7 zeros. The lastfilled is -100.
For every spot from 0 to last.
If item spot of seats is 1, set lastfilled to spot. Set item spot of nearest to spot minus lastfilled.

Set lastfilled to 100. The spot is last.
Repeat while spot is at least 0.
If item spot of seats is 1, set lastfilled to spot. The distance is lastfilled minus spot. If distance is less than item spot of nearest, set item spot of nearest to distance. Subtract 1 from spot.

The best is 0.
For every spot from 0 to last.
If item spot of seats is 0 and item spot of nearest is more than best, set best to item spot of nearest.

Show best.

Expected stdout

2

Open in Playground Standalone Page

#0850 · Rectangle Area II

Hard Array

The rects is a list of 0, 0, 2, 2, 1, 0, 2, 3, 1, 0, 3, 1. The rawx is a list of 0, 2, 1, 2, 1, 3. The rawy is a list of 0, 2, 0, 3, 0, 1. Sort rawx. Sort rawy. The xedges is an empty list of whole numbers. The yedges is an empty list of whole numbers. The recent is -1.
For each value in rawx.
If value is not recent, place value into xedges, set recent to value.

Set recent to -1.
For each value in rawy.
If value is not recent, place value into yedges, set recent to value.

The lastx is the last position of xedges. The lasty is the last position of yedges. The lastrect is the last position of rects. The entrycount is (lastrect plus 1) divided by 4. The cover is a list of 8 zeros. The area is 0.
For every xi from 0 to lastx minus 1.
The xleft is item xi of xedges. The xright is item (xi plus 1) of xedges. The width is xright minus xleft. The stamp is xi plus 1.
For every entry from 0 to entrycount minus 1.
The base is entry times 4.
For every yi from 0 to lasty minus 1.
If item base of rects is at most xleft and item (base plus 2) of rects is at least xright and item (base plus 1) of rects is at most item yi of yedges and item (base plus 3) of rects is at least item (yi plus 1) of yedges and item yi of cover is not stamp.
Set item yi of cover to stamp. The height is item (yi plus 1) of yedges minus item yi of yedges. Increase area by width times height.

Show area.

Expected stdout

6

Open in Playground Standalone Page

#0851 · Loud and Rich

Medium Array

Keep a shared quiet holding 3, 2, 5, 4, 1, and 0. Keep a shared edges holding 1, 2, 3, 4, and 5. Keep a shared starts holding 0, 1, 3, 3, 5, and 5. Keep a shared counts holding 1, 2, 0, 2, 0, and 0. Keep a shared answers holding 0, 1, 2, 3, 4, and 5.

To settle from person (a whole number).
For every offset from 0 to item person of counts minus 1.
The source is item (item person of starts plus offset) of edges. Settle from source. The voice is item source of answers. If item voice of quiet is less than item person of quiet, set item person of answers to voice.

For every person from 0 to 5.
Settle from person.

The line is empty text.
For each value in answers.
If the length of line is more than 0, Place "," into line. Place format whole number value as text into line.

Show line.

Expected stdout

5,5,2,5,4,5

Open in Playground Standalone Page

#0852 · Peak Index in a Mountain Array

Medium Array

The heights is a list of 0, 10, 5, 2, and 1. The low is 0, while the high is the last position of heights.
Repeat while low is less than high.
The middle is (low plus high) divided by 2. If item middle of heights is less than item (middle plus 1) of heights, set low to middle plus 1. If item middle of heights is more than item (middle plus 1) of heights, set high to middle.

Show low.

Expected stdout

1

Open in Playground Standalone Page

#0853 · Car Fleet

Medium Array

The positions is a list of 10, 8, 0, 5, and 3. The speeds is a list of 2, 4, 1, 1, and 3. The target is 12. The last is the last position of positions.
For every left from 0 to last.
For every right from left plus 1 to last.
If item right of positions is more than item left of positions, swap item left of positions and item right of positions, swap item left of speeds and item right of speeds.

The leaders is an empty list of whole numbers. The paces is an empty list of whole numbers.
For every spot from 0 to last.
The here is item spot of positions. The pace is item spot of speeds. The size is the number of items in leaders. If size is 0, Place here into leaders, Place pace into paces.
If size is more than 0.
The ahead is item (size minus 1) of leaders. The cruise is item (size minus 1) of paces. The mine is (target minus here) times cruise. The theirs is (target minus ahead) times pace. If mine is more than theirs, Place here into leaders, Place pace into paces.

Show the number of items in leaders.

Expected stdout

3

Open in Playground Standalone Page

#0854 · K-Similar Strings

Hard String

Keep a shared source as empty text. Keep a shared goal as empty text. Place "abc" into source. Place "bca" into goal. Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared current as empty text. Keep a shared targetletter of 0. Keep a shared codes as an empty list of whole numbers. Keep a shared found of 0.

To locate from spot (a whole number). If spot is more than 25, return. The symbol is character spot of alphabet. If symbol is targetletter, set found to spot, return. Locate from spot plus 1.

To encode from spot (a whole number). If spot is more than the last position of current, return. Set targetletter to character spot of current. Set found to -1. Locate from 0. Place found into codes. Encode from spot plus 1.

Set current to source. Encode from 0. Set current to goal. Encode from 0.

The final is the last position of source. Keep a shared chars as an empty list of whole numbers. Keep a shared wanted as an empty list of whole numbers.
For every spot from 0 to final.
Place item spot of codes into chars.
For every spot from final plus 1 to the last position of codes.
Place item spot of codes into wanted.

Keep a shared first of 0. Keep a shared best of final plus 1.

To scan from spot (a whole number). Set first to -1.
For every index from spot to the last position of chars.
If first is -1 and item index of chars is not item index of wanted, set first to index.

To seek from spot (a whole number) with level (a whole number). If level is at least best, return. Scan from spot. The foundspot is first. If foundspot is -1, set best to level, return. Tryexact from foundspot with foundspot and level. Tryany from foundspot with foundspot and level.

To carry from first (a whole number) with index (a whole number) and level (a whole number). swap item first of chars and item index of chars. Seek from first plus 1 with level. swap item first of chars and item index of chars.

To consider from index (a whole number) with targetspot (a whole number) and level (a whole number). If item index of chars is not item targetspot of wanted, return. If item targetspot of chars is not item index of wanted, return. Carry from targetspot with index and level plus 1.

To probe from index (a whole number) with targetspot (a whole number) and level (a whole number). If item index of chars is not item targetspot of wanted, return. Carry from targetspot with index and level plus 1.

To tryexact from spot (a whole number) with targetspot (a whole number) and level (a whole number).
For every index from spot plus 1 to the last position of chars.
Consider from index with targetspot and level.

To tryany from spot (a whole number) with targetspot (a whole number) and level (a whole number).
For every index from spot plus 1 to the last position of chars.
Probe from index with targetspot and level.

Seek from 0 with 0. Show best.

Expected stdout

2

Open in Playground Standalone Page

#0856 · Score of Parentheses

Medium String

The word is "(()(()))". The last is the last position of word. The stack is an empty list of whole numbers.
For every spot from 0 to last.
The symbol is character spot of word. The size is the number of items in stack. If symbol is "(", Place 0 into stack.
If symbol is ")".
The inner is item (size minus 1) of stack. Remove the last item from stack. The left is the number of items in stack. The scored is 1. If inner is more than 0, set scored to inner times 2. If left is 0, Place scored into stack. If left is more than 0, set item (left minus 1) of stack to item (left minus 1) of stack plus scored.

Show item 0 of stack.

Expected stdout

6

Open in Playground Standalone Page

#0857 · Minimum Cost to Hire K Workers

Hard Array

The qualities is a list of 10, 20, and 5. The wages is a list of 70, 50, and 30. The wanted is 2. The last is the last position of qualities.
For every left from 0 to last.
For every right from left plus 1 to last.
If item right of wages times item left of qualities is less than item left of wages times item right of qualities, swap item left of qualities and item right of qualities, swap item left of wages and item right of wages.

Keep a shared pool holding 0 and 0. Keep a shared filled of 0. Keep a shared total of 0. Keep a shared bestnum of 0. Keep a shared bestden of 0. Keep a shared biggest of 0. Set bestden to 1.

To largest from count (a whole number). Set biggest to 0.
For every spot from 1 to count minus 1.
The local is biggest. If item spot of pool is more than item local of pool, set biggest to spot.

To record with wage (a whole number) and quality (a whole number). The big is total times wage. If bestnum is 0 or big times bestden is less than bestnum times quality, set bestnum to big, set bestden to quality.

For every spot from 0 to last.
The quality is item spot of qualities. The wage is item spot of wages. The added is 0. If filled is less than wanted, set item filled of pool to quality, add quality to total, add 1 to filled, set added to 1. If filled is wanted and added is 1, Record with wage and quality.
If added is 0 and filled is wanted.
Largest from filled. The top is biggest. The old is item top of pool. If quality is less than old, set total to total minus old plus quality, set item top of pool to quality, Record with wage and quality. If quality is at least old, Record with wage and quality.

Show bestnum divided by bestden.

Expected stdout

105

Open in Playground Standalone Page

#0858 · Mirror Reflection

Medium Math

The width is 4, while the height is 2. The left is width, while the right is height.
Repeat while right is more than 0.
The carry is left modulo right. Set left to right. Set right to carry.

The short is width divided by left, while the tall is height divided by left. The corner is 1. If short modulo 2 is 0, set corner to 2. If short modulo 2 is 1 and tall modulo 2 is 0, set corner to 0.

Show corner.

Expected stdout

2

Open in Playground Standalone Page

#0859 · Buddy Strings

Easy String

The source is "ab". The goal is "ba". The last is the last position of source. The valid is 1. The sizesource is the number of characters in source. The sizegoal is the number of characters in goal. If sizesource is not sizegoal, set valid to 0. The first is -1. The second is -1. The misses is 0.
For every spot from 0 to last.
The symbol is character spot of source. The wanted is character spot of goal. If symbol is not wanted and misses is 0, set first to spot. If symbol is not wanted and misses is 1, set second to spot. If symbol is not wanted, add 1 to misses.

The twin is 0.
For every left from 0 to last.
For every right from left plus 1 to last.
The leftsymbol is character left of source. The rightsymbol is character right of source. If leftsymbol is rightsymbol, set twin to 1.

The answer is 0. If valid is 1 and misses is 0, set answer to twin.
If valid is 1 and misses is 2.
The leftletter is character first of source. The rightletter is character second of goal. The backletter is character second of source. The frontletter is character first of goal. If leftletter is rightletter and backletter is frontletter, set answer to 1.

If answer is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0860 · Lemonade Change

Easy Array

The bills is a list of 5, 5, 5, 10, and 20. The fives is 0, while the tens is 0. The valid is 1.
For each bill in bills.
If bill is 5, add 1 to fives. If bill is 10 and fives is 0, set valid to 0. If bill is 10 and fives is more than 0, subtract 1 from fives, add 1 to tens. The paid is 0. If bill is 20 and tens is more than 0 and fives is more than 0, subtract 1 from tens, subtract 1 from fives, set paid to 1. If bill is 20 and paid is 0 and fives is at least 3, subtract 3 from fives, set paid to 1. If bill is 20 and paid is 0, set valid to 0.

If valid is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0861 · Score After Flipping Matrix

Medium Array

Keep a shared rows of 0. Keep a shared columns of 0. Set rows to 3. Set columns to 4. Keep a shared grid holding 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, and 0.
For every row from 0 to rows minus 1.
The start is row times columns.
If item start of grid is 0.
For every column from 0 to columns minus 1.
The cell is start plus column. The flipped is exclusive or of item cell of grid and 1. Set item cell of grid to flipped.

Keep a shared ones of 0. Keep a shared weight of 0.

To count from column (a whole number). Set ones to 0.
For every row from 0 to rows minus 1.
The cell is row times columns plus column. If item cell of grid is 1, add 1 to ones.

To weigh from place (a whole number). Set weight to 1.
For every step from 0 to place minus 1.
Set weight to weight times 2.

The total is 0.
For every column from 0 to columns minus 1.
Count from column. The taller is ones. If rows minus ones is more than taller, set taller to rows minus ones. The place is columns minus 1 minus column. Weigh from place. Increase total by taller times weight.

Show total.

Expected stdout

39

Open in Playground Standalone Page

#0862 · Shortest Subarray with Sum at Least K

Hard Array

The numbers is a list of 2, -1, and 2. The target is 3. The last is the last position of numbers. The best is last plus 2.
For every start from 0 to last.
The total is 0.
For every tail from start to last.
The value is item tail of numbers. Add value to total.
If total is at least target.
The span is tail minus start plus 1. If span is less than best, set best to span. stop.

Show best.

Expected stdout

3

Open in Playground Standalone Page

#0863 · All Nodes Distance K in Binary Tree

Medium Depth-First Search

Keep a shared lefts holding 1, 3, 5, -1, 9, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, 10, -1, -1, -1, -1, -1, and -1. Keep a shared parents as an empty list of whole numbers.
For every node from 0 to 10.
Place -2 into parents.

Keep a shared queue as an empty list of whole numbers.
For every node from 0 to 10.
Place -1 into queue.

Keep a shared seen as an empty list of whole numbers.
For every node from 0 to 10.
Place 0 into seen.

Keep a shared distance as an empty list of whole numbers.
For every node from 0 to 10.
Place 0 into distance.

Keep a shared answers as an empty list of whole numbers. Keep a shared count of 0.

To attach from node (a whole number) with parent (a whole number). If node is less than 0, return. Set item node of parents to parent. The left is item node of lefts. Attach from left with node. The right is item node of rights. Attach from right with node.

To visit from neighbor (a whole number) with depth (a whole number). If neighbor is less than 0, return. If item neighbor of seen is 1, return. Set item neighbor of seen to 1. Set item neighbor of distance to depth plus 1. The spot is count. Set item spot of queue to neighbor. Add 1 to count.

To expand from here (a whole number) with depth (a whole number). The left is item here of lefts. Visit from left with depth. The right is item here of rights. Visit from right with depth. The parent is item here of parents. Visit from parent with depth.

The values is a list of 3, 5, 1, 6, 2, 0, 8, -1, -1, 7, and 4. Attach from 0 with -1. Set item 0 of queue to 1. Set item 1 of seen to 1. Set count to 1. The head is 0.
Repeat while head is less than count.
The here is item head of queue. The depth is item here of distance. Add 1 to head. If depth is 2, Place item here of values into answers. If depth is less than 2, Expand from here with depth.

The line is empty text.
For each value in answers.
If the length of line is more than 0, Place "," into line. Place format whole number value as text into line.

Show line.

Expected stdout

7,4,1

Open in Playground Standalone Page

#0864 · Shortest Path to Get All Keys

Hard Array

Keep a shared grid holding 2, 0, 3, 0, 0, 1, 1, 1, 0, 1, 4, 0, 5, 0, and 6. Keep a shared visited as a list of 60 zeros. Keep a shared queued cells as an empty list of whole numbers. Keep a shared queued masks as an empty list of whole numbers. Keep a shared queued distances as an empty list of whole numbers. Keep a shared count of 0. Keep a shared head of 0. Keep a shared answer of 0. Keep a shared activemask of 0. Keep a shared activetravel of 0.
To visit from target (a whole number). The cell is item target of grid. If cell is 1, return. The gained is activemask. If cell is 3, set gained to bitwise or of activemask and 1. If cell is 4, set gained to bitwise or of activemask and 2. The keyone is bitwise and of activemask and 1. The keytwo is bitwise and of activemask and 2. If cell is 5 and keyone is 0, return. If cell is 6 and keytwo is 0, return. The mark is target times 4 plus gained. If item mark of visited is 1, return. Set item mark of visited to 1. Place target into queued cells. Place gained into queued masks. Place activetravel plus 1 into queued distances. Add 1 to count.

Set item 0 of visited to 1. Place 0 into queued cells. Place 0 into queued masks. Place 0 into queued distances. Set count to 1. Set head to 0. Set answer to -1. The columns is 5. The rows is 3. The lastrow is rows minus 1. The lastcolumn is columns minus 1.
Repeat while head is less than count.
The cursor is head. The spot is item cursor of queued cells. Set activemask to item cursor of queued masks. Set activetravel to item cursor of queued distances. Add 1 to head. If activemask is 3, set answer to activetravel, stop. The line is spot divided by columns. The column is spot modulo columns. If line is more than 0, Visit from spot minus columns. If line is less than lastrow, Visit from spot plus columns. If column is more than 0, Visit from spot minus 1. If column is less than lastcolumn, Visit from spot plus 1.

Show answer.

Expected stdout

8

Open in Playground Standalone Page

#0865 · Smallest Subtree with all the Deepest Nodes

Medium Depth-First Search

Keep a shared values holding 3, 5, 1, 6, 2, 0, 8, -1, -1, 7, and 4. Keep a shared lefts holding 1, 3, 5, -1, 9, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, 10, -1, -1, -1, -1, -1, and -1. Keep a shared heights as an empty list of whole numbers. Keep a shared answer of 0. Keep a shared order as an empty list of whole numbers.
To descend from node (a whole number). The left is item node of lefts. The right is item node of rights. The leftheight is 0. The rightheight is 0. If left is at least 0, set leftheight to item left of heights. If right is at least 0, set rightheight to item right of heights. If leftheight is equal to rightheight, set answer to node, return. If leftheight is more than rightheight, Descend from left, return. Descend from right.

To gather from node (a whole number). If node is less than 0, return. Place node into order. Gather from item node of lefts. Gather from item node of rights.

The last is the last position of values.
For every node from 0 to last.
Place 0 into heights.

For every step from 0 to last.
The node is last minus step. The leftheight is 0. The rightheight is 0. The left is item node of lefts. The right is item node of rights. If left is at least 0, set leftheight to item left of heights. If right is at least 0, set rightheight to item right of heights. The tall is leftheight. If rightheight is more than tall, set tall to rightheight. Set item node of heights to tall plus 1.

Descend from 0. Gather from answer. For each node in order, show item node of values.

Expected stdout

2
7
4

Open in Playground Standalone Page

#0866 · Prime Palindrome

Medium Math

Keep a shared candidate of 0. Keep a shared prime of 1. Keep a shared flipped of 0. Keep a shared arrived of 0.
To probe from divisor (a whole number). If divisor times divisor is more than candidate, return. If candidate modulo divisor is 0, set prime to 0, return. Probe from divisor plus 1.

To flip from value (a whole number). If value is 0, return. Set flipped to flipped times 10 plus value modulo 10. Flip from value divided by 10.

Set candidate to 13.
Repeat while arrived is 0.
Set prime to 1. Probe from 2. Set flipped to 0. Flip from candidate. If prime is 1 and flipped is candidate, set arrived to 1. If arrived is 0, add 1 to candidate.

Show candidate.

Expected stdout

101

Open in Playground Standalone Page

#0867 · Transpose Matrix

Easy Array

The grid is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The rows is 3. The columns is 3. The lastrow is rows minus 1. The lastcolumn is columns minus 1.
For every column from 0 to lastcolumn.
For every line from 0 to lastrow.
Show item (line times columns plus column) of grid.

Expected stdout

1
4
7
2
5
8
3
6
9

Open in Playground Standalone Page

#0868 · Binary Gap

Easy Bit Manipulation

The value is 22. The working is value. The best is 0. The previous is -1. The spot is 0.
Repeat while working is more than 0.
The low is bitwise and of working and 1. If low is 1 and previous is at least 0 and spot minus previous is more than best, set best to spot minus previous. If low is 1, set previous to spot. Set working to shift working right by 1. Add 1 to spot.

Show best.

Expected stdout

2

Open in Playground Standalone Page

#0869 · Reordered Power of 2

Medium Math

Keep a shared basecounts as an empty list of whole numbers. Keep a shared testcounts as an empty list of whole numbers. Keep a shared same of 0.
To mark base from value (a whole number). Repeat while value is more than 0, set digit to value modulo 10, set item digit of basecounts to item digit of basecounts plus 1, set value to value divided by 10.

To mark test from value (a whole number). Repeat while value is more than 0, set digit to value modulo 10, set item digit of testcounts to item digit of testcounts plus 1, set value to value divided by 10.

To wipe from spot (a whole number). If spot is more than 9, return. Set item spot of testcounts to 0. Wipe from spot plus 1.

To compare from spot (a whole number). If spot is more than 9, return. If item spot of basecounts is not item spot of testcounts, set same to 0, return. Compare from spot plus 1.

For every spot from 0 to 9, Place 0 into basecounts. For every spot from 0 to 9, Place 0 into testcounts. The number is 10. Mark base from number. The power is 1. The reached is 0.
Repeat while power is at most 1000000000.
Set same to 1. Wipe from 0. Mark test from power. Compare from 0. If same is 1, set reached to 1, stop. Set power to power times 2.

If reached is 1, show true, otherwise show false.

Expected stdout

false

Open in Playground Standalone Page

#0870 · Advantage Shuffle

Medium Array

Keep a shared numbers holding 2, 7, 11, and 15. Keep a shared aims holding 1, 10, 4, and 11. Keep a shared used holding 0, 0, 0, and 0. Keep a shared answers holding 0, 0, 0, and 0. Keep a shared pick of 0.
To seek from spot (a whole number) with aim (a whole number). If spot is more than the last position of numbers, return. If item spot of used is 1, seek from spot plus 1 with aim, return. If item spot of numbers is more than aim, set pick to spot, return. Seek from spot plus 1 with aim.

To spare from spot (a whole number). If spot is more than the last position of numbers, return. If item spot of used is 0, set pick to spot, return. Spare from spot plus 1.

Sort numbers. The place is 0.
For each aim in aims.
Set pick to -1. Seek from 0 with aim. If pick is equal to -1, Spare from 0. The chosen is pick. Set item chosen of used to 1. Set item place of answers to item chosen of numbers. Add 1 to place.

For each value in answers, show value.

Expected stdout

2
11
7
15

Open in Playground Standalone Page

#0871 · Minimum Number of Refueling Stops

Hard Array

Keep a shared places holding 10, 20, 30, and 60. Keep a shared fuels holding 60, 30, 30, and 40. Keep a shared reach holding 0, 0, 0, 0, and 0.

To expand from spot (a whole number).
If spot is less than 0, return. The position is item spot of places. The raised is item spot of reach plus item spot of fuels.
If item spot of reach is at least position and raised is more than item (spot plus 1) of reach, set item (spot plus 1) of reach to raised. Expand from spot minus 1.

Set item 0 of reach to 10.
For every station from 0 to 3.
Expand from station.

The answer is -1.
For every stops from 0 to 4.
If item stops of reach is at least 100 and answer is -1, set answer to stops.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0872 · Leaf-Similar Trees

Easy Depth-First Search

Keep a shared values holding 3, 5, 1, 6, 2, 9, 8, -1, -1, 7, and 4. Keep a shared lefts holding 1, 3, 5, -1, 9, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, -1, 10, -1, -1, -1, -1, -1, and -1. Keep a shared secondvalues holding 3, 5, 1, 6, 7, 4, 2, -1, -1, -1, -1, -1, -1, 9, and 8. Keep a shared secondlefts holding 1, 3, 5, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, and -1. Keep a shared secondrights holding 2, 4, 6, -1, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, and -1. Keep a shared firstleaves as an empty list of whole numbers. Keep a shared otherleaves as an empty list of whole numbers.
To gather first from node (a whole number). If node is less than 0, return. If item node of lefts is less than 0 and item node of rights is less than 0, Place item node of values into firstleaves, return. Gather first from item node of lefts. Gather first from item node of rights.

To gather other from node (a whole number). If node is less than 0, return. If item node of secondlefts is less than 0 and item node of secondrights is less than 0, Place item node of secondvalues into otherleaves, return. Gather other from item node of secondlefts. Gather other from item node of secondrights.

Gather first from 0. Gather other from 0. The same is 1. The firstcount is the number of items in firstleaves. The othercount is the number of items in otherleaves. If firstcount is not othercount, set same to 0.
For every spot from 0 to the last position of firstleaves.
If item spot of firstleaves is not item spot of otherleaves, set same to 0.

If same is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0873 · Length of Longest Fibonacci Subsequence

Medium Array

Keep a shared numbers holding 1, 2, 3, 4, 5, 6, 7, and 8. Keep a shared located of -1. Keep a shared best of 0. Keep a shared tally of 2.
To locate from goal (a whole number) with spot (a whole number). If spot is more than the last position of numbers, return. If item spot of numbers is goal, set located to spot, return. Locate from goal with spot plus 1.

To collect from first (a whole number) with second (a whole number). If tally is more than best, set best to tally. The wanted is item first of numbers plus item second of numbers. Set located to -1. Locate from wanted with second plus 1. If located is at least 0, set tally to tally plus 1, Collect from second with located.

The last is the last position of numbers.
For every first from 0 to last.
For every second from first plus 1 to last.
Set tally to 2. Collect from first with second.

If best is less than 3, set best to 0. Show best.

Expected stdout

5

Open in Playground Standalone Page

#0874 · Walking Robot Simulation

Medium Array

Keep a shared stepx holding 0, 1, 0, and -1. Keep a shared stepy holding 1, 0, -1, and 0. Keep a shared barrierx as an empty list of whole numbers. Keep a shared barriery as an empty list of whole numbers. Keep a shared east of 0. Keep a shared north of 0. Keep a shared dir of 0. Keep a shared best of 0. Keep a shared probex of 0. Keep a shared probey of 0. Keep a shared blocked of 0.
To obstruct from spot (a whole number). If spot is more than the last position of barrierx, return. If item spot of barrierx is probex and item spot of barriery is probey, set blocked to 1, return. Obstruct from spot plus 1.

To advance from remaining (a whole number). If remaining is 0, return. The way is dir. The nextx is east plus item way of stepx. The nexty is north plus item way of stepy. Set probex to nextx. Set probey to nexty. Set blocked to 0. Obstruct from 0. If blocked is 1, return. Set east to nextx. Set north to nexty. The reach is east times east plus north times north. If reach is more than best, set best to reach. Advance from remaining minus 1.

The commands is a list of 4, -1, and 3.
For each command in commands.
If command is equal to -1, set dir to dir plus 1 modulo 4. If command is equal to -2, set dir to dir plus 3 modulo 4. If command is more than 0, Advance from command.

Show best.

Expected stdout

25

Open in Playground Standalone Page

#0875 · Koko Eating Bananas

Medium Array

Keep a shared piles holding 3, 6, 7, and 11. Keep a shared hours of 0.
To count from spot (a whole number) with speed (a whole number). If spot is more than the last position of piles, return. The pile is item spot of piles. The needed is (pile plus speed minus 1) divided by speed. Add needed to hours. Count from spot plus 1 with speed.

The limit is 8. The low is 1. The high is 11. The answer is 11.
Repeat while low is at most high.
The middle is the midpoint of low and high. Set hours to 0. Count from 0 with middle. If hours is at most limit, set answer to middle, set high to middle minus 1, otherwise set low to middle plus 1.

Show answer.

Expected stdout

4

Open in Playground Standalone Page

#0876 · Middle of the Linked List

Easy Two Pointers

The values is a list of 1, 2, 3, 4, and 5. The last is the last position of values. The middle is 0. The fast is 0.
Repeat while fast is less than last.
Increase fast by 2. Add 1 to middle.

For every spot from middle to last.
Show item spot of values.

Expected stdout

3
4
5

Open in Playground Standalone Page

#0877 · Stone Game

Medium Array

The piles is a list of 5, 3, 4, and 5. The size is 4. The table is a list of 16 zeros.
For every span from 1 to size.
For every start from 0 to size minus span.
The finish is start plus span minus 1. If start is finish, set item (start times size plus start) of table to item start of piles.
If start is not finish.
The leftindex is (start plus 1) times size plus finish. The rightindex is start times size plus finish minus 1. The left is item start of piles minus item leftindex of table. The right is item finish of piles minus item rightindex of table. If left is more than right, set item (start times size plus finish) of table to left, otherwise set item (start times size plus finish) of table to right.

The outcome is item (size minus 1) of table. If outcome is at least 0, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0878 · Nth Magical Number

Hard Math

Keep a shared first of 0. Keep a shared second of 0. Keep a shared rank of 0. Keep a shared leftvalue of 0. Keep a shared rightvalue of 0. Keep a shared divisor of 0. Keep a shared multiple of 0. Keep a shared counted of 0. Keep a shared answer of 0. Set first to 2. Set second to 3. Set rank to 4.
To reduce. If rightvalue is 0, set divisor to leftvalue, return. The remainder is leftvalue modulo rightvalue. Set leftvalue to rightvalue. Set rightvalue to remainder. Reduce.

To tally with spot (a whole number). Set counted to spot divided by first plus spot divided by second minus spot divided by multiple.

To seek from low (a whole number) with high (a whole number). If low is at least high, set answer to low, return. The middle is (low plus high) divided by 2. Tally with middle. If counted is at least rank, seek from low with middle, return. If counted is less than rank, seek from middle plus 1 with high.

Set leftvalue to first. Set rightvalue to second. Reduce. Set multiple to first divided by divisor times second. Seek from 1 with first times rank. Show answer.

Expected stdout

6

Open in Playground Standalone Page

#0879 · Profitable Schemes

Hard Array

The members is a list of 2, 3, and 5. The gains is a list of 6, 7, and 8. The people is 10. The target is 5. The width is target plus 1. The rows is (people plus 1) times width. The table is a list of 264 zeros. Set item 0 of table to 1.
For every crime from 0 to 2.
The cost is item crime of members. The gain is item crime of gains.
For every used from 0 to people.
For every earned from 0 to target.
The here is crime times rows plus used times width plus earned. The next is here plus rows. Increase item next of table by item here of table.
If used plus cost is at most people.
The reached is earned plus gain. If reached is more than target, set reached to target. The moved is next plus cost times width plus reached minus earned. Increase item moved of table by item here of table.

The total is 0.
For every used from 0 to people.
Increase total by item (3 times rows plus used times width plus target) of table.

Show total.

Expected stdout

7

Open in Playground Standalone Page