The TransCompilator

HTML chunk · 50 solved programs

Chunk 10 · #0637–#0699

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

#0637 · Average of Levels in Binary Tree

Easy Depth-First Search

The values is a list of 3, 9, 20, 15, and 7. The lefts is a list of 1, -1, 3, -1, and -1. The rights is a list of 2, -1, 4, -1, and -1. The queue is a list of 20 zeros. Keep a shared averages as an empty list of text.
To render with total (a whole number) with count (a whole number). The integer is total divided by count. The rest is total modulo count. The parts is an empty list of text. The headtext is format whole number integer as text. Place headtext into parts. Place "." into parts.
For every digit from 0 to 4.
Set rest to rest times 10. The piece is rest divided by count. The pad is format whole number piece as text. Place pad into parts. Set rest to rest modulo count. If digit is 4, The built is join parts with "", Place built into averages.

Set item 0 of queue to 0. The head is 0. The tail is 1. The remaining is 1. The nextcount is 0. The total is 0. The count is 0.
Repeat while remaining is more than 0.
The node is item head of queue. Add 1 to head. Subtract 1 from remaining. Increase total by item node of values. Add 1 to count. The left is item node of lefts. If left is at least 0, set item tail of queue to left, add 1 to tail, add 1 to nextcount. The right is item node of rights. If right is at least 0, set item tail of queue to right, add 1 to tail, add 1 to nextcount. If remaining is 0, render with total with count, set total to 0, set count to 0, set remaining to nextcount, set nextcount to 0.

The line is join averages with ",". Show line.

Expected stdout

3.00000,14.50000,11.00000

Open in Playground Standalone Page

#0638 · Shopping Offers

Medium Array

Keep a shared prices holding 2 and 5. Keep a shared wants holding 3 and 2. Keep a shared bundles holding 3, 0, 5, 1, 2, and 10. Keep a shared width of 3. Keep a shared deals of 2. Keep a shared best of 1000000. Keep a shared kinds of 2. Keep a shared costspot of width minus 1. Keep a shared fits of 0. Keep a shared regular of 0.
To measure from bundle (a whole number) with spot (a whole number). If spot is at least kinds, return. The amount is item (bundle times width plus spot) of bundles. If amount is more than item spot of wants, set fits to 0. Measure from bundle with spot plus 1.

To charge with spot (a whole number). If spot is at least kinds, return. The amount is item spot of wants times item spot of prices. Add amount to regular. Charge with spot plus 1.

To take from bundle (a whole number) with spot (a whole number). If spot is at least kinds, return. The amount is item (bundle times width plus spot) of bundles. Decrease item spot of wants by amount. Take from bundle with spot plus 1.

To carry from bundle (a whole number) with spot (a whole number). If spot is at least kinds, return. The amount is item (bundle times width plus spot) of bundles. Increase item spot of wants by amount. Carry from bundle with spot plus 1.

To settle with spent (a whole number). Set regular to 0. Charge with 0. The total is spent plus regular. If total is less than best, set best to total.

To explore from bundle (a whole number) with spent (a whole number). If spent is at least best, return. If bundle is at least deals, settle with spent, return. Explore from bundle plus 1 with spent. Set fits to 1. Measure from bundle with 0.
If fits is 1.
Take from bundle with 0. Explore from bundle with spent plus item (bundle times width plus costspot) of bundles. Carry from bundle with 0.

Explore from 0 with 0. Show best.

Expected stdout

14

Open in Playground Standalone Page

#0639 · Decode Ways II

Hard String

The source is "1*". The size is the number of characters in source. The table is a list of size plus 1 zeros. Set item 0 of table to 1.
For every position from 1 to size.
The current is character (position minus 1) of source. The previous is current. If position is more than 1, set previous to character (position minus 2) of source. If current is "*", set item position of table to (item position of table plus 9 times item (position minus 1) of table) modulo 1000000007. If current is between "1" and "9", set item position of table to (item position of table plus item (position minus 1) of table) modulo 1000000007. The pairways is 0. If position is more than 1 and previous is "*" and current is "*", set pairways to 15. If position is more than 1 and previous is "*" and current is between "0" and "6", set pairways to 2. If position is more than 1 and previous is "*" and current is between "7" and "9", set pairways to 1. If position is more than 1 and previous is "1" and current is "*", set pairways to 9. If position is more than 1 and previous is "2" and current is "*", set pairways to 6. If position is more than 1 and previous is "1" and current is between "0" and "9", set pairways to 1. If position is more than 1 and previous is "2" and current is between "0" and "6", set pairways to 1. If pairways is more than 0, set item position of table to (item position of table plus pairways times item (position minus 2) of table) modulo 1000000007.

Show item size of table.

Expected stdout

18

Open in Playground Standalone Page

#0640 · Solve the Equation

Medium String

Keep a shared side as empty text. Keep a shared coefficient of 0. Keep a shared constant of 0. Keep a shared collected of 0. Keep a shared after of 0. Keep a shared xnext of 0.
To finish from spot (a whole number) with total (a whole number). Set collected to total. Set after to spot.

To collect from spot (a whole number) with total (a whole number). If spot is more than the last position of side, finish from spot with total, return. The symbol is character spot of side. If symbol is not between "0" and "9", finish from spot with total, return. The digit is the digit value of symbol. Collect from spot plus 1 with total times 10 plus digit.

To peek from spot (a whole number). If spot is more than the last position of side, set xnext to 0, return. If character spot of side is "x", set xnext to 1, return. Set xnext to 0.

To score from mark (a whole number) with amount (a whole number). The folded is mark times amount. Increase coefficient by folded times xnext. The other is 1 minus xnext. Increase constant by folded times other.

To advance from spot (a whole number) with mark (a whole number). Set xnext to 1. Score from mark with 1. Inspect from spot plus 1 with mark.

To inspect from spot (a whole number) with mark (a whole number). If spot is more than the last position of side, return. The symbol is character spot of side. If symbol is "+", inspect from spot plus 1 with 1. If symbol is "-", inspect from spot plus 1 with -1. If symbol is "x", advance from spot with mark.
If symbol is between "0" and "9".
Set collected to 0. Set xnext to 0. Collect from spot with 0. Peek from after. Score from mark with collected. Inspect from after plus xnext with mark.

The equation is "x+5-3+x=6+x-2". The sides is split equation by "=". Set side to item 0 of sides. Set coefficient to 0. Set constant to 0. Inspect from 0 with 1. The leftcoefficient is coefficient. The leftconstant is constant. Set side to item 1 of sides. Set coefficient to 0. Set constant to 0. Inspect from 0 with 1. The slope is leftcoefficient minus coefficient. The offset is constant minus leftconstant. If slope is 0 and offset is 0, show "Infinite solutions". If slope is 0 and offset is not 0, show "No solution".
If slope is not 0.
The value is offset divided by slope. The answer is empty text. Place "x=" into answer. The piece is format whole number value as text. Place piece into answer. Show answer.

Expected stdout

x=2

Open in Playground Standalone Page

#0643 · Maximum Average Subarray I

Easy Array

The numbers is a list of 1, 12, -5, -6, 50, and 3. The width is 4. The last is the last position of numbers. The window is 0.
For every spot from 0 to width minus 1.
Increase window by item spot of numbers.

The best is window.
For every start from 1 to last minus width plus 1.
Decrease window by item (start minus 1) of numbers. Increase window by item (start plus width minus 1) of numbers. If window is more than best, set best to window.

The integer is best divided by width. The rest is best modulo width. The parts is an empty list of text. The headtext is format whole number integer as text. Place headtext into parts. Place "." into parts.
For every digit from 0 to 4.
Set rest to rest times 10. The piece is rest divided by width. The pad is format whole number piece as text. Place pad into parts. Set rest to rest modulo width.

The line is join parts with "". Show line.

Expected stdout

12.75000

Open in Playground Standalone Page

#0645 · Set Mismatch

Easy Array

The numbers is a list of 1, 2, 2, and 4. The count is the number of items in numbers. The tally is a list of count plus 1 zeros.
For each value in numbers.
Increase item value of tally by 1.

The repeated is 0. The absent is 0.
For every candidate from 1 to count.
If item candidate of tally is 2, set repeated to candidate. If item candidate of tally is 0, set absent to candidate.

The report is empty text. Place format whole number repeated as text into report. Place "," into report. Place format whole number absent as text into report. Show report.

Expected stdout

2,3

Open in Playground Standalone Page

#0646 · Maximum Length of Pair Chain

Medium Array

Keep a shared begins holding 1, 2, and 3. Keep a shared finishes holding 2, 3, and 4. Keep a shared last of 2. Keep a shared picked of 0.
To seek from candidate (a whole number) with chosen (a whole number). If candidate is more than last, set picked to chosen, return. If item candidate of finishes is less than item chosen of finishes, set chosen to candidate. Seek from candidate plus 1 with chosen.

To place from spot (a whole number). The pick is spot. Seek from spot plus 1 with pick. If picked is not spot, swap item spot of finishes and item picked of finishes, swap item spot of begins and item picked of begins.

For every spot from 0 to last minus 1.
Place from spot.

The count is 0. The lastend is -1000000000.
For every spot from 0 to last.
If item spot of begins is more than lastend, add 1 to count, set lastend to item spot of finishes.

Show count.

Expected stdout

2

Open in Playground Standalone Page

#0647 · Palindromic Substrings

Medium String

Keep a shared source as empty text. Place "abc" into source. Keep a shared total of 0.
To spread from left (a whole number) with right (a whole number). If left is less than 0, return. If right is more than the last position of source, return. If character left of source is not character right of source, return. Add 1 to total. Spread from left minus 1 with right plus 1.

For every center from 0 to the last position of source.
Spread from center with center. Spread from center with center plus 1.

Show total.

Expected stdout

3

Open in Playground Standalone Page

#0648 · Replace Words

Medium Array

Keep a shared roots holding "cat", "bat", and "rat". Keep a shared word as empty text. Keep a shared chosen as empty text. Keep a shared bestlen of 0. Keep a shared root as empty text. Keep a shared rootlast of 0.
To settle with limit (a whole number). The extent is rootlast plus 1. If extent is less than limit, set chosen to root, set bestlen to extent.

To examine. The wordlast is the last position of word. If rootlast is more than wordlast, return. The prefix is the slice of word from 0 to rootlast plus 1. If prefix matches root, settle with bestlen.

To scan from spot (a whole number). If spot is more than the last position of roots, return. Set root to item spot of roots. Set rootlast to the last position of root. Examine. Scan from spot plus 1.

The words is a list of "the", "cattle", "was", "rattled", "by", "the", and "battery". The answers is an empty list of text.
For each entry in words.
Set word to entry. Set chosen to entry. Set bestlen to 1000. Scan from 0. The kept is a copy of chosen. Place kept into answers.

The line is join answers with " ". Show line.

Expected stdout

the cat was rat by the bat

Open in Playground Standalone Page

#0649 · Dota2 Senate

Medium String

Keep a shared redqueue as an empty list of whole numbers. Keep a shared bluequeue as an empty list of whole numbers. Keep a shared redstart of 0. Keep a shared redend of 0. Keep a shared bluestart of 0. Keep a shared blueend of 0. Keep a shared size of 0. Keep a shared redpick of 0. Keep a shared bluepick of 0. The reds is a list of 20 zeros. The blues is a list of 20 zeros. Set redqueue to reds. Set bluequeue to blues.
To brighten from pick (a whole number). The endspot is redend. Set item endspot of redqueue to pick plus size. Add 1 to redend. Add 1 to redstart. Add 1 to bluestart.

To darken from pick (a whole number). The endspot is blueend. Set item endspot of bluequeue to pick plus size. Add 1 to blueend. Add 1 to redstart. Add 1 to bluestart.

The source is "RD". Set size to the number of characters in source.
For every seat from 0 to size minus 1.
The symbol is character seat of source. The redslot is redend. The blueslot is blueend. If symbol is "R", set item redslot of redqueue to seat, add 1 to redend. If symbol is "D", set item blueslot of bluequeue to seat, add 1 to blueend.

Repeat while redstart is less than redend and bluestart is less than blueend.
The redslot is redstart. The blueslot is bluestart. Set redpick to item redslot of redqueue. Set bluepick to item blueslot of bluequeue. If redpick is less than bluepick, brighten from redpick, otherwise darken from bluepick.

If redstart is less than redend, show "Radiant". If bluestart is less than blueend, show "Dire".

Expected stdout

Radiant

Open in Playground Standalone Page

#0650 · 2 Keys Keyboard

Medium Math

The cells is 3. The moves is 0. The divisor is 2.
Repeat while cells is more than 1.
If cells modulo divisor is 0, add divisor to moves, set cells to cells divided by divisor. If cells modulo divisor is not 0, add 1 to divisor.

Show moves.

Expected stdout

3

Open in Playground Standalone Page

#0652 · Find Duplicate Subtrees

Medium Depth-First Search

Keep a shared values holding 1, 2, 3, 4, 2, 4, and 4. Keep a shared lefts holding 1, 3, 4, -1, 6, -1, and -1. Keep a shared rights holding 2, -1, 5, -1, -1, -1, and -1. Keep a shared serial as empty text. Keep a shared keys as an empty list of text. Keep a shared found as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared pieces as an empty list of text. Keep a shared count of 0. Keep a shared tally of 0. Keep a shared earlier of 0. Keep a shared drawn as empty text.
To encode from node (a whole number). If node is less than 0, Place "#" into serial, return. Place format whole number item node of values as text into serial. Place "(" into serial. Encode from item node of lefts. Place "," into serial. Encode from item node of rights. Place ")" into serial.

To tally from node (a whole number). The amount is 0.
For every other from 0 to the last position of keys.
If item other of keys matches item node of keys, add 1 to amount. Set tally to amount.

To inspect from node (a whole number) with limit (a whole number). The seenbefore is 0.
For every prior from 0 to limit minus 1.
If item prior of keys matches item node of keys, set seenbefore to 1. Set earlier to seenbefore.

To emit from node (a whole number). If node is less than 0, Place "null" into pieces, return. The piece is format whole number item node of values as text. Place piece into pieces. Place item node of lefts into queue. Place item node of rights into queue. Add 2 to count.

To drain.
Repeat while the number of items in queue is more than 0.
Remove the last item from queue.

To reset.
Repeat while the number of items in pieces is more than 0.
Remove the last item from pieces.

To trim trailing.
Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. The tailpiece is item tail of pieces. The isnull is 0. If tailpiece matches "null", set isnull to 1. If isnull is 1, remove the last item from pieces. If isnull is 0, stop.

To render from root (a whole number). Drain. Reset. Place root into queue. Set count to 1. The head is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Emit from node.

For every node from 0 to the last position of values.
Set serial to empty text. Encode from node. The copy is a copy of serial. Place copy into keys.

For every node from 0 to the last position of keys.
Tally from node. Inspect from node with node. If tally is more than 1 and earlier is 0, Place node into found.

The output is empty text. Place "[" into output.
For each root in found.
If the length of output is more than 1, Place "," into output. Render from root. Trim trailing. Set drawn to join pieces with ",". Place "[" into output. Place drawn into output. Place "]" into output.

Place "]" into output. Show output.

Expected stdout

[[2,4],[4]]

Open in Playground Standalone Page

#0653 · Two Sum IV - Input is a BST

Easy Depth-First Search

Keep a shared values holding 5, 3, 6, 2, 4, and 7. Keep a shared lefts holding 1, 3, 5, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, -1, and -1. Keep a shared order as an empty list of whole numbers.
To gather from node (a whole number). If node is less than 0, return. Gather from item node of lefts. Place item node of values into order. Gather from item node of rights.

Gather from 0. The wanted is 9. The left is 0. The right is the last position of order. The found is 0.
Repeat while left is less than right.
The pair is item left of order plus item right of order. If pair is wanted, set found to 1, stop. If pair is less than wanted, add 1 to left. If pair is more than wanted, subtract 1 from right.

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

Expected stdout

true

Open in Playground Standalone Page

#0654 · Maximum Binary Tree

Medium Array

Keep a shared numbers holding 3, 2, 1, 6, 0, and 5. Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared pieces as an empty list of text. Keep a shared count of 0. Keep a shared made of 0. Keep a shared parent of 0. Keep a shared side of 0. Keep a shared chosen of 0.
To locate from low (a whole number) with high (a whole number). The pick is low.
For every look from low to high.
The value is item look of numbers. The current is item pick of numbers. If value is more than current, set pick to look. Set chosen to pick.

To build from low (a whole number) with high (a whole number). If low is more than high, return. Locate from low with high. The place is chosen. The slot is made. Add 1 to made. Place item place of numbers into values. Place -1 into lefts. Place -1 into rights. The holder is parent. If holder is at least 0 and side is 0, set item holder of lefts to slot. If holder is at least 0 and side is 1, set item holder of rights to slot. Set parent to slot. Set side to 0. Build from low with place minus 1. Set parent to slot. Set side to 1. Build from place plus 1 with high.

To emit from node (a whole number). If node is less than 0, Place "null" into pieces, return. The piece is format whole number item node of values as text. Place piece into pieces. Place item node of lefts into queue. Place item node of rights into queue. Add 2 to count.

Set parent to -1. Build from 0 with 5. Place 0 into queue. Set count to 1. The head is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Emit from node.

Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. The tailpiece is item tail of pieces. The isnull is 0. If tailpiece matches "null", set isnull to 1. If isnull is 1, remove the last item from pieces. If isnull is 0, stop.

The line is join pieces with ",". Show line.

Expected stdout

6,3,5,null,2,0,null,null,1

Open in Playground Standalone Page

#0655 · Print Binary Tree

Medium Depth-First Search

Keep a shared values holding 1, 2, 3, and 4. Keep a shared lefts holding 1, -1, -1, and -1. Keep a shared rights holding 2, 3, -1, and -1. Keep a shared depths as an empty list of whole numbers. Keep a shared places as an empty list of whole numbers. Keep a shared height of 0. Keep a shared tall of 0. Keep a shared foundnode of 0. Keep a shared celltext as empty text.
To measure from node (a whole number) with depth (a whole number). If node is less than 0, return. If depth is more than height, set height to depth. Measure from item node of lefts with depth plus 1. Measure from item node of rights with depth plus 1.

To mark from node (a whole number) with level (a whole number) and place (a whole number). If node is less than 0, return. Set item node of depths to level. Set item node of places to place. The steps is tall minus 2 minus level. The gap is shift 1 left by steps. The leftplace is place minus gap. The rightplace is place plus gap. Mark from item node of lefts with level plus 1 and leftplace. Mark from item node of rights with level plus 1 and rightplace.

To find at row (a whole number) with column (a whole number). The pick is -1.
For every node from 0 to the last position of values.
If item node of depths is row and item node of places is column, set pick to node. Set foundnode to pick.

To describe from node (a whole number). The piece is format whole number item node of values as text. Set celltext to piece.

Measure from 0 with 1. Set tall to height. The wide is 1. The steps is 0.
Repeat while steps is less than tall.
Set wide to wide times 2. Add 1 to steps.

Decrease wide by 1. The middle is wide minus 1. Set middle to middle divided by 2. The gap is wide plus 1. Set gap to gap divided by 4.
For every node from 0 to 3.
Place 0 into depths. Place 0 into places.

Mark from 0 with 0 and middle. The line is empty text. Place "[" into line.
For every row from 0 to tall minus 1.
If row is more than 0, Place "," into line. Place "[" into line.
For every column from 0 to wide minus 1.
If column is more than 0, Place "," into line. Place "\"" into line. Find at row with column. The celltext is empty text. If foundnode is at least 0, describe from foundnode. Place celltext into line. Place "\"" into line. If column is wide minus 1, Place "]" into line.

Place "]" into line. Show line.

Expected stdout

[["","","","1","","",""],["","2","","","","3",""],["","","4","","","",""]]

Open in Playground Standalone Page

#0657 · Robot Return to Origin

Easy String

The moves is "UD". The sideways is 0. The upward is 0. The last is the last position of moves.
For every spot from 0 to last.
The step is the slice of moves from spot to spot plus 1. If step matches "U", add 1 to upward. If step matches "D", subtract 1 from upward. If step matches "L", subtract 1 from sideways. If step matches "R", add 1 to sideways.

If sideways is 0 and upward is 0, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0658 · Find K Closest Elements

Medium Array

The values is a list of 1, 2, 3, 4, and 5. The want is 4. The aim is 3. The left is 0. The right is the last position of values.
Repeat while right minus left plus 1 is more than want.
The lowgap is item left of values minus aim. Set lowgap to the absolute value of lowgap. The highgap is item right of values minus aim. Set highgap to the absolute value of highgap. If lowgap is at most highgap, subtract 1 from right, otherwise add 1 to left.

The line is empty text.
For every spot from left to right.
If the length of line is more than 0, Place "," into line. Place format whole number item spot of values as text into line.

Show line.

Expected stdout

1,2,3,4

Open in Playground Standalone Page

#0659 · Split Array into Consecutive Subsequences

Medium Array

Keep a shared counts as an empty list of whole numbers. Keep a shared ends as an empty list of whole numbers. Keep a shared possible of 0.
To lengthen from value (a whole number).
Repeat while item value of counts is more than 0.
The onward is 0. If value is more than 0 and item (value minus 1) of ends is more than 0, set onward to 1. If onward is 1, set item (value minus 1) of ends to item (value minus 1) of ends minus 1. If onward is 1, set item value of ends to item value of ends plus 1. If onward is 1, set item value of counts to item value of counts minus 1. The fresh is 0. If onward is 0 and value is at most 7 and item (value plus 1) of counts is more than 0 and item (value plus 2) of counts is more than 0, set fresh to 1. If fresh is 1, set item value of counts to item value of counts minus 1. If fresh is 1, set item (value plus 1) of counts to item (value plus 1) of counts minus 1. If fresh is 1, set item (value plus 2) of counts to item (value plus 2) of counts minus 1. If fresh is 1, set item (value plus 2) of ends to item (value plus 2) of ends plus 1. If onward is 0 and fresh is 0, set possible to 0, set item value of counts to 0.

The numbers is a list of 1, 2, 3, 3, 4, and 5.
For every value from 0 to 9.
Place 0 into counts. Place 0 into ends.

For each value in numbers.
Increase item value of counts by 1.

Set possible to 1.
For every value from 0 to 9.
Lengthen from value. If possible is 0, stop.

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

Expected stdout

true

Open in Playground Standalone Page

#0661 · Image Smoother

Easy Array

Keep a shared grid as an empty list of whole numbers. Keep a shared average of 0.
To smooth at row (a whole number) with column (a whole number). The total is 0. The count is 0.
For every offset from 0 to 8.
The down is offset divided by 3 minus 1. The across is offset modulo 3 minus 1. The actualrow is row plus down. The actualcolumn is column plus across. The inside is 0. If actualrow is at least 0 and actualrow is less than 3 and actualcolumn is at least 0 and actualcolumn is less than 3, set inside to 1. If inside is 1, set total to total plus item (actualrow times 3 plus actualcolumn) of grid. If inside is 1, add 1 to count. If offset is 8, set average to total divided by count.

The grid is a list of 1, 1, 1, 1, 0, 1, 1, 1, and 1. The line is empty text. Place "[" into line.
For every row from 0 to 2.
If row is more than 0, Place "," into line. Place "[" into line.
For every column from 0 to 2.
If column is more than 0, Place "," into line. Smooth at row with column. Place format whole number average as text into line. If column is 2, Place "]" into line.

Place "]" into line. Show line.

Expected stdout

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

Open in Playground Standalone Page

#0662 · Maximum Width of Binary Tree

Medium Depth-First Search

Keep a shared values holding 1, 3, 2, 5, 3, and 9. Keep a shared lefts holding 1, 3, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 5, -1, -1, and -1. The queue is an empty list of whole numbers. The places is an empty list of whole numbers. Place 0 into queue. Place 0 into places. The head is 0. The count is 1. The remaining is 1. The nextcount is 0. The width is 0. The first is 1000000. The last is -1.
Repeat while head is less than count.
The node is item head of queue. The spot is item head of places. Add 1 to head. If spot is less than first, set first to spot. If spot is more than last, set last to spot. If item node of lefts is at least 0, Place item node of lefts into queue, Place spot times 2 plus 1 into places, add 1 to count, add 1 to nextcount. If item node of rights is at least 0, Place item node of rights into queue, Place spot times 2 plus 2 into places, add 1 to count, add 1 to nextcount. Subtract 1 from remaining. The span is last minus first plus 1. The ended is 0. If remaining is 0, set ended to 1. If ended is 1 and span is more than width, set width to span. If ended is 1, set first to 1000000. If ended is 1, set last to -1. If ended is 1, set remaining to nextcount. If ended is 1, set nextcount to 0.

Show width.

Expected stdout

4

Open in Playground Standalone Page

#0664 · Strange Printer

Hard String

The word is "aaabbb". The size is the length of word. The table is a list of 36 zeros.
For every spot from 0 to size minus 1.
Set item (spot times size plus spot) of table to 1.

For every span from 2 to size.
For every start from 0 to size minus span.
The finish is start plus span minus 1. The opener is item ((start plus 1) times size plus finish) of table plus 1.
For every split from start plus 1 to finish.
The firstletter is the slice of word from start to start plus 1. The otherletter is the slice of word from split to split plus 1. The same is 0. If firstletter matches otherletter, set same to 1. The leftpart is 0. If split is more than start plus 1, set leftpart to item ((start plus 1) times size plus split minus 1) of table. The option is leftpart plus item (split times size plus finish) of table. If same is 1 and option is less than opener, set opener to option. If split is finish, set item (start times size plus finish) of table to opener.

Show item (size minus 1) of table.

Expected stdout

2

Open in Playground Standalone Page

#0665 · Non-decreasing Array

Medium Array

The numbers is a list of 4, 2, and 3. The last is the last position of numbers. The changes is 0.
For every spot from 1 to last.
The previous is item (spot minus 1) of numbers. The current is item spot of numbers. The drop is 0. If current is less than previous, set drop to 1. If drop is 1, add 1 to changes. The lowerok is 0. If spot is 1, set lowerok to 1. If spot is more than 1 and item (spot minus 2) of numbers is at most current, set lowerok to 1. If drop is 1 and lowerok is 1, set item (spot minus 1) of numbers to current. If drop is 1 and lowerok is 0, set item spot of numbers to previous.

If changes is at most 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0667 · Beautiful Arrangement II

Medium Array

The size is 3. The spread is 2. The low is 1. The high is size. The answer is an empty list of whole numbers.
For every step from 0 to spread minus 1.
If step modulo 2 is 0, Place low into answer, add 1 to low. If step modulo 2 is 1, Place high into answer, subtract 1 from high.

If spread modulo 2 is 0.
Repeat while low is at most high.
Place low into answer. Add 1 to low.

If spread modulo 2 is 1.
Repeat while low is at most high.
Place high into answer. Subtract 1 from high.

The line is empty text.
For each value in answer.
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

1,3,2

Open in Playground Standalone Page

#0668 · Kth Smallest Number in Multiplication Table

Hard Math

Keep a shared total of 0. Keep a shared rows of 0. Keep a shared columns of 0.
To count up to bound (a whole number). Set total to 0.
For every row from 1 to rows.
The full is bound divided by row. The take is full. If take is more than columns, set take to columns. Increase total by take.

Set rows to 3. Set columns to 3. The target is 5. The low is 1. The high is rows times columns.
Repeat while low is less than high.
The middle is low plus high. Set middle to middle divided by 2. Count up to middle. If total is at least target, set high to middle, otherwise set low to middle plus 1.

Show low.

Expected stdout

3

Open in Playground Standalone Page

#0669 · Trim a Binary Search Tree

Medium Depth-First Search

Keep a shared values holding 1, 0, and 2. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared queue as an empty list of whole numbers. Keep a shared pieces as an empty list of text. Keep a shared count of 0. Keep a shared low of 0. Keep a shared high of 0. Keep a shared trimmed of 0.
To trim from node (a whole number). If node is less than 0, set trimmed to -1, return. The here is node. If item here of values is less than low, trim from item here of rights, return. If item here of values is more than high, trim from item here of lefts, return. Trim from item here of lefts. Set item here of lefts to trimmed. Trim from item here of rights. Set item here of rights to trimmed. Set trimmed to here.

To emit from node (a whole number). If node is less than 0, Place "null" into pieces, return. The piece is format whole number item node of values as text. Place piece into pieces. Place item node of lefts into queue. Place item node of rights into queue. Add 2 to count.

Set low to 1. Set high to 2. Trim from 0. Place trimmed into queue. Set count to 1. The head is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Emit from node.

Repeat while the last position of pieces is at least 0.
The tail is the last position of pieces. The tailpiece is item tail of pieces. The isnull is 0. If tailpiece matches "null", set isnull to 1. If isnull is 1, remove the last item from pieces. If isnull is 0, stop.

The line is join pieces with ",". Show line.

Expected stdout

1,null,2

Open in Playground Standalone Page

#0670 · Maximum Swap

Medium Math

Keep a shared digits as an empty list of whole numbers. Keep a shared total of 0.
To fold. Set total to 0.
For each digit in digits.
Set total to total times 10 plus digit.

The number is 2736. The rest is number.
Repeat while rest is more than 0.
Place rest modulo 10 into digits. Set rest to rest divided by 10.

The last is the last position of digits. The half is last divided by 2.
For every left from 0 to half.
Swap item left of digits and item (last minus left) of digits.

The best is number.
For every first from 0 to last.
For every second from first plus 1 to last.
Swap item first of digits and item second of digits. Fold. If total is more than best, set best to total. Swap item first of digits and item second of digits.

Show best.

Expected stdout

7236

Open in Playground Standalone Page

#0671 · Second Minimum Node In a Binary Tree

Easy Depth-First Search

Keep a shared values holding 2, 2, 5, 5, and 7. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, 4, -1, and -1. Keep a shared base of 0. Keep a shared higher of 0.
To inspect from node (a whole number). If node is less than 0, return. The value is item node of values. If value is more than base and value is less than higher, set higher to value. Inspect from item node of lefts. Inspect from item node of rights.

Set base to item 0 of values. Set higher to 1000000. Inspect from 0. If higher is 1000000, show -1, otherwise show higher.

Expected stdout

5

Open in Playground Standalone Page

#0672 · Bulb Switcher II

Medium Math

The size is 1. The presses is 1. The patterns is 1.
For every step from 1 to size.
Set patterns to patterns times 2.

The allmask is patterns minus 1. The evenmask is 0. The oddmask is 0. The triplemask is 0.
For every spot from 0 to size minus 1.
The bit is shift 1 left by spot. If spot modulo 2 is 0, set evenmask to bitwise or of evenmask and bit. If spot modulo 2 is 1, set oddmask to bitwise or of oddmask and bit. If spot modulo 3 is 0, set triplemask to bitwise or of triplemask and bit.

The rounds is presses plus 1. The limit is rounds times patterns. The seen is an empty list of whole numbers.
For every spot from 0 to limit minus 1.
Place 0 into seen.

The queue is an empty list of whole numbers. The depths is an empty list of whole numbers. Place 0 into queue. Place 0 into depths. Set item 0 of seen to 1. The head is 0. The count is 1.
Repeat while head is less than count.
The state is item head of queue. The round is item head of depths. Add 1 to head. The flipped is exclusive or of state and allmask. The flipspot is (round plus 1) times patterns plus flipped. If round is less than presses and item flipspot of seen is 0, set item flipspot of seen to 1, Place flipped into queue, Place round plus 1 into depths, add 1 to count. The evenstate is exclusive or of state and evenmask. The evenspot is (round plus 1) times patterns plus evenstate. If round is less than presses and item evenspot of seen is 0, set item evenspot of seen to 1, Place evenstate into queue, Place round plus 1 into depths, add 1 to count. The oddstate is exclusive or of state and oddmask. The oddspot is (round plus 1) times patterns plus oddstate. If round is less than presses and item oddspot of seen is 0, set item oddspot of seen to 1, Place oddstate into queue, Place round plus 1 into depths, add 1 to count. The triplestate is exclusive or of state and triplemask. The triplespot is (round plus 1) times patterns plus triplestate. If round is less than presses and item triplespot of seen is 0, set item triplespot of seen to 1, Place triplestate into queue, Place round plus 1 into depths, add 1 to count.

The answer is 0.
For every state from 0 to patterns minus 1.
If item (presses times patterns plus state) of seen is 1, add 1 to answer.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0673 · Number of Longest Increasing Subsequence

Medium Array

The numbers is a list of 1, 3, 5, 4, and 7. The last is the last position of numbers. The lengths is a list of 5 zeros. The counts is a list of 5 zeros.
For every spot from 0 to last.
Set item spot of lengths to 1. Set item spot of counts to 1.
For every prior from 0 to spot minus 1.
The candidate is item prior of lengths plus 1. The lower is 0. If item prior of numbers is less than item spot of numbers, set lower to 1. The raised is 0. If lower is 1 and candidate is more than item spot of lengths, set raised to 1. If raised is 1, set item spot of lengths to candidate, set item spot of counts to item prior of counts. If lower is 1 and raised is 0 and candidate is item spot of lengths, set item spot of counts to item spot of counts plus item prior of counts.

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

The total is 0.
For every spot from 0 to last.
If item spot of lengths is best, set total to total plus item spot of counts.

Show total.

Expected stdout

2

Open in Playground Standalone Page

#0674 · Longest Continuous Increasing Subsequence

Easy Array

The numbers is a list of 1, 3, 5, 4, and 7. The last is the last position of numbers. The longest is 1. The run is 1.
For every spot from 1 to last.
If item spot of numbers is more than item (spot minus 1) of numbers, add 1 to run, otherwise set run to 1. If run is more than longest, set longest to run.

Show longest.

Expected stdout

3

Open in Playground Standalone Page

#0675 · Cut Off Trees for Golf Event

Hard Array

Keep a shared grid holding 1, 2, 3, 0, 0, 4, 7, 6, and 5. Keep a shared rows of 0. Keep a shared columns of 0. Keep a shared currentrow of 0. Keep a shared currentcolumn of 0. Keep a shared travelled of 0. Set rows to 3. Set columns to 3.
To walk to targetrow (a whole number) with targetcolumn (a whole number). The queue is an empty list of whole numbers. The steps is an empty list of whole numbers. The seen is a list of 9 zeros. The start is currentrow times columns plus currentcolumn. Place start into queue. Place 0 into steps. Set item start of seen to 1. Set travelled to 0. The head is 0. The count is 1.
Repeat while head is less than count.
The cell is item head of queue. The depth is item head of steps. Add 1 to head. The row is cell divided by columns. The column is cell modulo columns. The hit is 0. If row is targetrow and column is targetcolumn, set hit to 1. If hit is 1, set travelled to depth. If hit is 1, stop. The uprow is row minus 1. The upcolumn is column. The upinside is 0. If uprow is at least 0 and uprow is less than rows and upcolumn is at least 0 and upcolumn is less than columns, set upinside to 1. The upblocked is 0. If upinside is 1 and item (uprow times columns plus upcolumn) of grid is 0, set upblocked to 1. The upseen is 0. If upinside is 1 and item (uprow times columns plus upcolumn) of seen is 1, set upseen to 1. If upinside is 1 and upblocked is 0 and upseen is 0, set item (uprow times columns plus upcolumn) of seen to 1, Place (uprow times columns plus upcolumn) into queue, Place depth plus 1 into steps, add 1 to count. The downrow is row plus 1. The downcolumn is column. The downinside is 0. If downrow is at least 0 and downrow is less than rows and downcolumn is at least 0 and downcolumn is less than columns, set downinside to 1. The downblocked is 0. If downinside is 1 and item (downrow times columns plus downcolumn) of grid is 0, set downblocked to 1. The downseen is 0. If downinside is 1 and item (downrow times columns plus downcolumn) of seen is 1, set downseen to 1. If downinside is 1 and downblocked is 0 and downseen is 0, set item (downrow times columns plus downcolumn) of seen to 1, Place (downrow times columns plus downcolumn) into queue, Place depth plus 1 into steps, add 1 to count. The leftrow is row. The leftcolumn is column minus 1. The leftinside is 0. If leftrow is at least 0 and leftrow is less than rows and leftcolumn is at least 0 and leftcolumn is less than columns, set leftinside to 1. The leftblocked is 0. If leftinside is 1 and item (leftrow times columns plus leftcolumn) of grid is 0, set leftblocked to 1. The leftseen is 0. If leftinside is 1 and item (leftrow times columns plus leftcolumn) of seen is 1, set leftseen to 1. If leftinside is 1 and leftblocked is 0 and leftseen is 0, set item (leftrow times columns plus leftcolumn) of seen to 1, Place (leftrow times columns plus leftcolumn) into queue, Place depth plus 1 into steps, add 1 to count. The rightrow is row. The rightcolumn is column plus 1. The rightinside is 0. If rightrow is at least 0 and rightrow is less than rows and rightcolumn is at least 0 and rightcolumn is less than columns, set rightinside to 1. The rightblocked is 0. If rightinside is 1 and item (rightrow times columns plus rightcolumn) of grid is 0, set rightblocked to 1. The rightseen is 0. If rightinside is 1 and item (rightrow times columns plus rightcolumn) of seen is 1, set rightseen to 1. If rightinside is 1 and rightblocked is 0 and rightseen is 0, set item (rightrow times columns plus rightcolumn) of seen to 1, Place (rightrow times columns plus rightcolumn) into queue, Place depth plus 1 into steps, add 1 to count.

The heights is an empty list of whole numbers. The treerows is an empty list of whole numbers. The treecolumns is an empty list of whole numbers.
For every spot from 0 to 8.
The height is item spot of grid.
If height is more than 1.
The treerow is spot divided by columns. The treecolumn is spot modulo columns. Place height into heights. Place treerow into treerows. Place treecolumn into treecolumns.

The treelast is the last position of heights.
For every left from 0 to treelast.
For every right from left plus 1 to treelast.
If item right of heights is less than item left of heights, swap item left of heights and item right of heights, swap item left of treerows and item right of treerows, swap item left of treecolumns and item right of treecolumns.

The total is 0.
For every tree from 0 to treelast.
Walk to item tree of treerows with item tree of treecolumns. Increase total by travelled. Set currentrow to item tree of treerows. Set currentcolumn to item tree of treecolumns.

Show total.

Expected stdout

6

Open in Playground Standalone Page

#0678 · Valid Parenthesis String

Medium String

Keep a shared source as empty text. Keep a shared possible as an empty list of whole numbers. Keep a shared fresh as an empty list of whole numbers. Keep a shared limit of 0. Place "(*))" into source.
For every slot from 0 to 102.
Place 0 into possible. Place 0 into fresh.

Set limit to the last position of source. Set item 0 of possible to 1.

To wipe from marker (a whole number).
For every balance from 0 to limit.
Set item balance of fresh to 0.

To advance from spot (a whole number).
For every balance from 0 to limit.
If item balance of possible is 1 and character spot of source is "(" and balance is less than limit, set item (balance plus 1) of fresh to 1. If item balance of possible is 1 and character spot of source is ")" and balance is more than 0, set item (balance minus 1) of fresh to 1. If item balance of possible is 1 and character spot of source is "*" and balance is less than limit, set item (balance plus 1) of fresh to 1. If item balance of possible is 1 and character spot of source is "*" and balance is more than 0, set item (balance minus 1) of fresh to 1. If item balance of possible is 1 and character spot of source is "*", set item balance of fresh to 1.

To adopt from marker (a whole number).
For every balance from 0 to limit.
Set item balance of possible to item balance of fresh.

For every spot from 0 to limit.
Wipe from 0. Advance from spot. Adopt from 0.

If item 0 of possible is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0679 · 24 Game

Hard Array

Keep a shared tops as an empty list of whole numbers. Keep a shared bottoms as an empty list of whole numbers. Keep a shared active as an empty list of whole numbers. Keep a shared found of 0. Keep a shared remaining of 0. Place 4 into tops. Place 1 into tops. Place 8 into tops. Place 7 into tops. Place 1 into bottoms. Place 1 into bottoms. Place 1 into bottoms. Place 1 into bottoms. Place 1 into active. Place 1 into active. Place 1 into active. Place 1 into active.

To count from spot (a whole number). If spot is more than 3, return. If item spot of active is 1, add 1 to remaining. Count from spot plus 1.

To settle from marker (a whole number).
For every spot from 0 to 3.
If item spot of active is 1 and item spot of bottoms is not 0 and item spot of tops is 24 times item spot of bottoms, set found to 1.

To explore from depth (a whole number). If found is 1, return. Set remaining to 0. Count from 0. If remaining is 1, settle from 0, return.
For every first from 0 to 3.
For every second from first plus 1 to 3.
If item first of active is 1 and item second of active is 1 and found is 0, attempt from first with second.

To addpair from first (a whole number) with second (a whole number). The firsttop is item first of tops. The firstbottom is item first of bottoms. The secondtop is item second of tops. The secondbottom is item second of bottoms. Set item first of tops to firsttop times secondbottom plus secondtop times firstbottom. Set item first of bottoms to firstbottom times secondbottom. If item first of bottoms is less than 0, set item first of tops to 0 minus item first of tops, set item first of bottoms to 0 minus item first of bottoms. Set item second of active to 0. Explore from 1. Set item second of active to 1. Set item first of tops to firsttop. Set item first of bottoms to firstbottom.

To cutpair from first (a whole number) with second (a whole number). The firsttop is item first of tops. The firstbottom is item first of bottoms. The secondtop is item second of tops. The secondbottom is item second of bottoms. Set item first of tops to firsttop times secondbottom minus secondtop times firstbottom. Set item first of bottoms to firstbottom times secondbottom. If item first of bottoms is less than 0, set item first of tops to 0 minus item first of tops, set item first of bottoms to 0 minus item first of bottoms. Set item second of active to 0. Explore from 1. Set item second of active to 1. Set item first of tops to firsttop. Set item first of bottoms to firstbottom.

To scalepair from first (a whole number) with second (a whole number). The firsttop is item first of tops. The firstbottom is item first of bottoms. The secondtop is item second of tops. The secondbottom is item second of bottoms. Set item first of tops to firsttop times secondtop. Set item first of bottoms to firstbottom times secondbottom. If item first of bottoms is less than 0, set item first of tops to 0 minus item first of tops, set item first of bottoms to 0 minus item first of bottoms. Set item second of active to 0. Explore from 1. Set item second of active to 1. Set item first of tops to firsttop. Set item first of bottoms to firstbottom.

To splitpair from first (a whole number) with second (a whole number). If item second of tops is 0, return. The firsttop is item first of tops. The firstbottom is item first of bottoms. The secondtop is item second of tops. The secondbottom is item second of bottoms. Set item first of tops to firsttop times secondbottom. Set item first of bottoms to firstbottom times secondtop. If item first of bottoms is less than 0, set item first of tops to 0 minus item first of tops, set item first of bottoms to 0 minus item first of bottoms. Set item second of active to 0. Explore from 1. Set item second of active to 1. Set item first of tops to firsttop. Set item first of bottoms to firstbottom.

To attempt from first (a whole number) with second (a whole number). If found is 1, return. If item first of active is 0, return. If item second of active is 0, return. If found is 0, addpair from first with second. If found is 0, cutpair from first with second. If found is 0, scalepair from first with second. If found is 0, splitpair from first with second.

Explore from 1. If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0680 · Valid Palindrome II

Easy String

Keep a shared source as empty text. Keep a shared good of 0. Keep a shared verdict of 0. Keep a shared settled of 0. Place "abca" into source.

To palm from left (a whole number) with right (a whole number).
Repeat while left is less than right.
The leftsymbol is character left of source. The rightsymbol is character right of source. If leftsymbol is not rightsymbol, set good to 0, return. Add 1 to left. Subtract 1 from right.

To resolve from first (a whole number) with last (a whole number). Set settled to 1. Set verdict to 1. Set good to 1. Palm from first plus 1 with last. If good is 1, return. Set good to 1. Palm from first with last minus 1. If good is 1, return. Set verdict to 0.

The first is 0. The last is the last position of source.
Repeat while first is less than last and settled is 0.
The leftsymbol is character first of source. The rightsymbol is character last of source. If leftsymbol is not rightsymbol, resolve from first with last. If leftsymbol is rightsymbol, add 1 to first, subtract 1 from last.

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

Expected stdout

true

Open in Playground Standalone Page

#0682 · Baseball Game

Easy Array

Keep a shared current as empty text. Keep a shared value of 0.

To read from spot (a whole number) with last (a whole number). If spot is more than last, return. The digit is the digit value of character spot of current. Set value to value times 10 plus digit. Read from spot plus 1 with last.

The operations is "5,2,C,D,+". The tokens is split operations by ",". The scores is an empty list of whole numbers. The depth is 0.
For each token in tokens.
Set current to a copy of token. The symbol is character 0 of current. If symbol is "C", remove the last item from scores, subtract 1 from depth. If symbol is "D", The doubled is item (depth minus 1) of scores times 2, Place doubled into scores, add 1 to depth. If symbol is "+", The joined is item (depth minus 1) of scores plus item (depth minus 2) of scores, Place joined into scores, add 1 to depth.
If symbol is between "0" and "9" or symbol is "-".
Set value to 0. The sign is 1. The start is 0. If symbol is "-", set sign to -1, set start to 1. The lastchar is the last position of current. Read from start with lastchar. Set value to value times sign. Place value into scores. Add 1 to depth.

The total is 0. For each score in scores, add score to total.

Show total.

Expected stdout

30

Open in Playground Standalone Page

#0684 · Redundant Connection

Medium Depth-First Search

Keep a shared parent as an empty list of whole numbers. Keep a shared rootanswer of 0.
For every node from 0 to 3.
Place node into parent.

To climb from node (a whole number). Set rootanswer to node. The cursor is rootanswer.
Repeat while item cursor of parent is not cursor.
Set rootanswer to item cursor of parent. Set cursor to rootanswer.

The edges is a list of 1, 2, 1, 3, 2, and 3. The count is 3. The found is 0. The firstanswer is 0. The secondanswer is 0.
For every edge from 0 to count minus 1.
The start is item (edge times 2) of edges. The finish is item (edge times 2 plus 1) of edges. Climb from start. The firstroot is rootanswer. Climb from finish. The secondroot is rootanswer. If found is 0 and firstroot is secondroot, set found to 1, set firstanswer to start, set secondanswer to finish. If found is 0 and firstroot is not secondroot, set item firstroot of parent to secondroot.

The line is empty text. The firstword is format whole number firstanswer as text. Place firstword into line. Place "," into line. The secondword is format whole number secondanswer as text. Place secondword into line. Show line.

Expected stdout

2,3

Open in Playground Standalone Page

#0685 · Redundant Connection II

Hard Depth-First Search

Keep a shared sources as an empty list of whole numbers. Keep a shared targets as an empty list of whole numbers. Keep a shared indegree as an empty list of whole numbers. Keep a shared parentof as an empty list of whole numbers. Keep a shared total of 0. Keep a shared removededge of 0. Keep a shared valid of 0. Keep a shared answerone of 0. Keep a shared answertwo of 0. Place 1 into sources. Place 1 into sources. Place 2 into sources. Place 2 into targets. Place 3 into targets. Place 3 into targets. Set total to 3.
For every node from 0 to total.
Place 0 into indegree. Place 0 into parentof.

To reset from marker (a whole number).
For every node from 1 to total.
Set item node of indegree to 0. Set item node of parentof to 0.

To count from edge (a whole number). If edge is removededge, return. The dest is item edge of targets. The origin is item edge of sources. Increase item dest of indegree by 1. Set item dest of parentof to origin.

To climb from node (a whole number) with steps (a whole number). If node is 1, return. If steps is more than total, set valid to 0, return. If node is less than 1, set valid to 0, return. Climb from item node of parentof with steps plus 1.

To verify from start (a whole number). Climb from start with 0.

To validate from marker (a whole number). Set valid to 1. If item 1 of indegree is not 0, set valid to 0.
For every node from 2 to total.
If item node of indegree is not 1, set valid to 0. If item node of indegree is 1, verify from node.

To check from removed (a whole number). Set removededge to removed. Reset from 0.
For every edge from 0 to total minus 1.
Count from edge. Validate from 0.

For every forward from 0 to total minus 1.
The removed is total minus 1 minus forward. If answerone is 0, check from removed. If answerone is 0 and valid is 1, set answerone to item removed of sources, set answertwo to item removed of targets.

The line is empty text. The firstword is format whole number answerone as text. Place firstword into line. Place "," into line. The secondword is format whole number answertwo as text. Place secondword into line. Show line.

Expected stdout

2,3

Open in Playground Standalone Page

#0686 · Repeated String Match

Medium String

Keep a shared source as empty text. Keep a shared target as empty text. Keep a shared repeated as empty text. Keep a shared found of 0. Keep a shared count of 0. Place "abcd" into source. Place "cdabcdab" into target.

To seek from marker (a whole number). Set found to 0. The sourcefinal is the last position of repeated. The targetfinal is the last position of target. The final is sourcefinal minus targetfinal.
For every start from 0 to final.
The piece is the slice of repeated from start to start plus targetfinal plus 1. If piece matches target, set found to 1.

To extend from marker (a whole number). Place source into repeated. Add 1 to count. Seek from 0.

Set count to 1. Place source into repeated.
Repeat while the last position of repeated is less than the last position of target.
Place source into repeated. Add 1 to count.

Seek from 0. If found is 0, extend from 0. If found is 0, set count to -1. Show count.

Expected stdout

3

Open in Playground Standalone Page

#0687 · Longest Univalue Path

Medium Depth-First Search

Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared reach of 0. Keep a shared best of 0. Place 5 into values. Place 4 into values. Place 5 into values. Place 1 into values. Place 1 into values. Place 0 into values. Place 5 into values. Place 1 into lefts. Place 3 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place 2 into rights. Place -1 into rights. Place 6 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights.

To probe from node (a whole number). If node is less than 0, set reach to 0, return. The label is item node of values. The leftnode is item node of lefts. The rightnode is item node of rights. Set leftreach to 0. Set rightreach to 0. Probe from leftnode. Set leftreach to reach. If leftnode is at least 0 and item leftnode of values is label, add 1 to leftreach. If leftnode is at least 0 and item leftnode of values is not label, set leftreach to 0. Probe from rightnode. Set rightreach to reach. If rightnode is at least 0 and item rightnode of values is label, add 1 to rightreach. If rightnode is at least 0 and item rightnode of values is not label, set rightreach to 0. The across is leftreach plus rightreach. If across is more than best, set best to across. Set reach to leftreach. If rightreach is more than reach, set reach to rightreach.

Probe from 0. Show best.

Expected stdout

2

Open in Playground Standalone Page

#0688 · Knight Probability in Chessboard

Medium Dynamic Programming

Keep a shared counts as an empty list of whole numbers. Keep a shared fresh as an empty list of whole numbers. Keep a shared downs as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared size of 0. Keep a shared steps of 0. Place 1 into downs. Place 1 into downs. Place -1 into downs. Place -1 into downs. Place 2 into downs. Place 2 into downs. Place -2 into downs. Place -2 into downs. Place 2 into rights. Place -2 into rights. Place 2 into rights. Place -2 into rights. Place 1 into rights. Place -1 into rights. Place 1 into rights. Place -1 into rights. Set size to 3. Set steps to 2.
For every spot from 0 to 8.
Place 0 into counts. Place 0 into fresh.

Set item 0 of counts to 1.

To wipe from marker (a whole number).
For every spot from 0 to 8.
Set item spot of fresh to 0.

To adopt from marker (a whole number).
For every spot from 0 to 8.
Set item spot of counts to item spot of fresh.

To jump from cell (a whole number). The currentrow is cell divided by size. The currentcolumn is cell modulo size.
For every move from 0 to 7.
The nextrow is currentrow plus item move of downs. The nextcolumn is currentcolumn plus item move of rights.
If nextrow is at least 0 and nextrow is less than size and nextcolumn is at least 0 and nextcolumn is less than size.
The dest is nextrow times size plus nextcolumn. Increase item dest of fresh by item cell of counts.

To spread from cell (a whole number). If cell is at least 9, adopt from 0, return. If item cell of counts is more than 0, jump from cell. Spread from cell plus 1.

To begin from marker (a whole number). Wipe from 0. Spread from 0.

For every step from 1 to steps.
Begin from 0.

The numerator is 0.
For every spot from 0 to 8.
Increase numerator by item spot of counts.

The denominator is 1.
For every step from 1 to steps.
Set denominator to denominator times 8.

The headpart is numerator divided by denominator. The remainder is numerator modulo denominator. The parts is an empty list of text. The headword is format whole number headpart as text. Place headword into parts. Place "." into parts.
For every digit from 0 to 9.
Set remainder to remainder times 10. The part is remainder divided by denominator. The digword is format whole number part as text. Place digword into parts. Set remainder to remainder modulo denominator.

The answer is join parts with "".
Repeat while the last position of answer is more than 1.
The final is the last position of answer. The lastpiece is character final of answer. If lastpiece is "0", remove last character from answer. If lastpiece is not "0", stop.

The final is the last position of answer. The lastpiece is character final of answer. If lastpiece is ".", remove last character from answer. Show answer.

Expected stdout

0.0625

Open in Playground Standalone Page

#0689 · Maximum Sum of 3 Non-Overlapping Subarrays

Hard Array

The numbers is a list of 1, 2, 1, 2, 6, 7, 5, and 1. The width is 2. The last is the last position of numbers. The best is -1. The bestfirst is 0. The bestsecond is 0. The bestclosing is 0. The firstlimit is last minus width times 3 plus 1. The secondlimit is last minus width times 2 plus 1. The closinglimit is last minus width plus 1.
For every first from 0 to firstlimit.
For every second from first plus width to secondlimit.
For every closing from second plus width to closinglimit.
The sum is 0.
For every spot from first to first plus width minus 1.
Increase sum by item spot of numbers.
For every spot from second to second plus width minus 1.
Increase sum by item spot of numbers.
For every spot from closing to closing plus width minus 1.
Increase sum by item spot of numbers.
If sum is more than best.
Set best to sum. Set bestfirst to first. Set bestsecond to second. Set bestclosing to closing.

The line is empty text. The firstword is format whole number bestfirst as text. Place firstword into line. Place "," into line. The secondword is format whole number bestsecond as text. Place secondword into line. Place "," into line. The closingword is format whole number bestclosing as text. Place closingword into line. Show line.

Expected stdout

0,3,5

Open in Playground Standalone Page

#0690 · Employee Importance

Medium Array

Keep a shared identifiers as an empty list of whole numbers. Keep a shared values as an empty list of whole numbers. Keep a shared counts as an empty list of whole numbers. Keep a shared starts as an empty list of whole numbers. Keep a shared subordinates as an empty list of whole numbers. Keep a shared total of 0. Keep a shared foundindex of 0. Place 1 into identifiers. Place 2 into identifiers. Place 3 into identifiers. Place 5 into values. Place 3 into values. Place 3 into values. Place 2 into counts. Place 0 into counts. Place 0 into counts. Place 0 into starts. Place 2 into starts. Place 2 into starts. Place 2 into subordinates. Place 3 into subordinates.

To locate from spot (a whole number) with wanted (a whole number). If foundindex is at least 0, return. If spot is more than 2, return. If item spot of identifiers is wanted, set foundindex to spot, return. Locate from spot plus 1 with wanted.

To gather from spot (a whole number). Increase total by item spot of values. The degree is item spot of counts. If degree is 0, return. The base is item spot of starts.
For every offset from 0 to degree minus 1.
The childid is item (base plus offset) of subordinates. Set foundindex to -1. Locate from 0 with childid. Gather from foundindex.

Gather from 0. Show total.

Expected stdout

11

Open in Playground Standalone Page

#0691 · Stickers to Spell Word

Hard Array

Keep a shared target as empty text. Keep a shared stickers as an empty list of text. Keep a shared letters as an empty list of text. Keep a shared bits as an empty list of whole numbers. Keep a shared costs as an empty list of whole numbers. Keep a shared currentsticker as empty text. Keep a shared letter as empty text. Keep a shared nextmask of 0. Keep a shared matchpos of 0. Place "thehat" into target. Place "with" into stickers. Place "example" into stickers. Place "science" into stickers.
For every spot from 0 to the last position of target.
The piece is the slice of target from spot to spot plus 1. Place piece into letters.

Place 1 into bits.
For every step from 1 to 6.
The ending is the last position of bits. The prior is item ending of bits. The doubled is prior times 2. Place doubled into bits.

For every spot from 0 to 63.
Place 999 into costs.

Set item 0 of costs to 0.

To find from spot (a whole number). If matchpos is at least 0, return. If spot is more than the last position of letters, return. The raised is item spot of bits. The bit is bitwise and of nextmask and raised. If bit is not 0, find from spot plus 1, return. If item spot of letters matches letter, set matchpos to spot, return. Find from spot plus 1.

To paste from marker (a whole number). The final is the last position of currentsticker.
For every spot from 0 to final.
Set letter to the slice of currentsticker from spot to spot plus 1. Set matchpos to -1. Find from 0. The slot is matchpos. If slot is at least 0, set nextmask to nextmask plus item slot of bits.

To apply from mask (a whole number).
For every index from 0 to the last position of stickers.
Set currentsticker to item index of stickers. Set nextmask to mask. Paste from index. The start is mask. The candidate is item start of costs plus 1. The slot is nextmask. If item slot of costs is more than candidate, set item slot of costs to candidate.

For every mask from 0 to 63.
If item mask of costs is less than 999, apply from mask.

Show item 63 of costs.

Expected stdout

3

Open in Playground Standalone Page

#0692 · Top K Frequent Words

Medium Array

Keep a shared words as an empty list of text. Keep a shared distinct as an empty list of text. Keep a shared frequencies as an empty list of whole numbers. Keep a shared chosen as an empty list of whole numbers. Keep a shared wanted as empty text. Keep a shared foundindex of 0. Keep a shared bestindex of 0. Place "i" into words. Place "love" into words. Place "leetcode" into words. Place "i" into words. Place "love" into words. Place "coding" into words.

To locate from spot (a whole number). If foundindex is at least 0, return. If spot is more than the last position of distinct, return. If item spot of distinct matches wanted, set foundindex to spot, return. Locate from spot plus 1.

To consider from spot (a whole number). If item spot of chosen is 1, return. If bestindex is less than 0, set bestindex to spot, return. The candidatecount is item spot of frequencies. The local is bestindex. The bestcount is item local of frequencies. If candidatecount is more than bestcount, set bestindex to spot, return. If candidatecount is less than bestcount, return. The localbest is bestindex. If item spot of distinct comes before item localbest of distinct, set bestindex to spot.

To choose from marker (a whole number). Set bestindex to -1.
For every spot from 0 to the last position of distinct.
Consider from spot.

For each word in words.
Set wanted to word. Set foundindex to -1. Locate from 0. If foundindex is at least 0, The local is foundindex, set item local of frequencies to item local of frequencies plus 1. If foundindex is less than 0, Place word into distinct, Place 1 into frequencies.

For every spot from 0 to the last position of distinct.
Place 0 into chosen.

For every pick from 1 to 2.
Choose from 0. The local is bestindex. Show item local of distinct. Set item local of chosen to 1.

Expected stdout

i
love

Open in Playground Standalone Page

#0693 · Binary Number with Alternating Bits

Easy Bit Manipulation

The value is 5. The current is value. The previous is current modulo 2. Set current to current divided by 2. The good is 1.
Repeat while current is more than 0.
The bit is current modulo 2. If bit is previous, set good to 0. Set previous to bit. Set current to current divided by 2.

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

Expected stdout

true

Open in Playground Standalone Page

#0695 · Max Area of Island

Medium Array

Keep a shared cells as an empty list of whole numbers. Keep a shared visited as an empty list of whole numbers. Keep a shared area of 0. Keep a shared best of 0. The picture is "0010000100000,0000000111000,0110100000000,0100110010100,0100110011100,0000000000100,0000000111000,0000000110000". The rows is split picture by ",".
For each band in rows.
For every column from 0 to 12.
The digit is the digit value of character column of band. Place digit into cells.

For every spot from 0 to 103.
Place 0 into visited.

To sink from node (a whole number). If node is less than 0, return. If item node of visited is 1, return. If item node of cells is 0, return. Set item node of visited to 1. Add 1 to area. The rowspot is node divided by 13. The columnspot is node modulo 13. If rowspot is more than 0, sink from node minus 13. If rowspot is less than 7, sink from node plus 13. If columnspot is more than 0, sink from node minus 1. If columnspot is less than 12, sink from node plus 1.

To probe from node (a whole number). Set area to 0. Sink from node. If area is more than best, set best to area.

For every node from 0 to 103.
If item node of cells is 1 and item node of visited is 0, probe from node.

Show best.

Expected stdout

6

Open in Playground Standalone Page

#0696 · Count Binary Substrings

Easy String

The source is "00110011". The last is the last position of source. The previous is 0. The current is 1. The total is 0.
For every spot from 1 to last.
The rightletter is character spot of source. The leftletter is character (spot minus 1) of source. If rightletter is leftletter, add 1 to current. If rightletter is not leftletter, The joined is the smaller of previous and current, add joined to total, set previous to current, set current to 1.

The extra is the smaller of previous and current. Add extra to total. Show total.

Expected stdout

6

Open in Playground Standalone Page

#0697 · Degree of an Array

Easy Array

The numbers is a list of 1, 2, 2, 3, and 1. The counts is a list of 100 zeros. The firsts is an empty list of whole numbers. The lasts is an empty list of whole numbers.
For every spot from 0 to 99.
Place -1 into firsts. Place -1 into lasts.

The last is the last position of numbers.
For every spot from 0 to last.
The value is item spot of numbers. Increase item value of counts by 1. If item value of firsts is -1, set item value of firsts to spot. Set item value of lasts to spot.

The degree is 0.
For every value from 0 to 99.
If item value of counts is more than degree, set degree to item value of counts.

The best is 1000000.
For every value from 0 to 99.
If item value of counts is degree.
The width is item value of lasts minus item value of firsts plus 1. If width is less than best, set best to width.

Show best.

Expected stdout

2

Open in Playground Standalone Page

#0698 · Partition to K Equal Sum Subsets

Medium Array

Keep a shared numbers as an empty list of whole numbers. Keep a shared groups as an empty list of whole numbers. Keep a shared count of 0. Keep a shared bins of 0. Keep a shared target of 0. Keep a shared found of 0. Place 4 into numbers. Place 3 into numbers. Place 2 into numbers. Place 3 into numbers. Place 5 into numbers. Place 2 into numbers. Place 1 into numbers. Set count to 7. Set bins to 4. The total is 0.
For each value in numbers.
Increase total by value.

Set target to total divided by bins. Sort numbers.
For every group from 0 to bins minus 1.
Place 0 into groups.

To trygroup from spot (a whole number) with group (a whole number). If found is 1, return. The here is count minus 1 minus spot. The value is item here of numbers. If item group of groups plus value is more than target, return. Increase item group of groups by value. Fill from spot plus 1. Decrease item group of groups by value.

To fill from spot (a whole number). If found is 1, return. If spot is at least count, set found to 1, return.
For every group from 0 to bins minus 1.
Trygroup from spot with group.

Fill from 0. If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0699 · Falling Squares

Hard Array

Keep a shared lefts as an empty list of whole numbers. Keep a shared sizes as an empty list of whole numbers. Keep a shared tops as an empty list of whole numbers. Keep a shared base of 0. Keep a shared candidate of 0. Place 1 into lefts. Place 2 into lefts. Place 6 into lefts. Place 2 into sizes. Place 3 into sizes. Place 1 into sizes.

To measure from block (a whole number) at opening (a whole number) with closing (a whole number). Set base to 0.
For every before from 0 to block minus 1.
The otherleft is item before of lefts. The otherright is otherleft plus item before of sizes.
If otherleft is less than closing and opening is less than otherright.
Set candidate to item before of tops. If candidate is more than base, set base to candidate.

The last is the last position of lefts. The best is 0.
For every block from 0 to last.
The front is item block of lefts. The back is front plus item block of sizes. Measure from block at front with back. The height is base plus item block of sizes. Place height into tops. If height is more than best, set best to height. Show best.

Expected stdout

2
5
5

Open in Playground Standalone Page