The TransCompilator

HTML chunk · 50 solved programs

Chunk 19 · #1247–#1316

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

#1247 · Minimum Swaps to Make Strings Equal

Medium String

The first is "xx". The second is "yy". The leftovers is 0. The rightovers is 0.
For every spot from 0 to the last position of first.
The left is character spot of first. The right is character spot of second. If left is "x" and right is "y", add 1 to leftovers. If left is "y" and right is "x", add 1 to rightovers.

The mismatch is leftovers plus rightovers. If mismatch modulo 2 is 1, show -1.
If mismatch modulo 2 is 0.
The swaps is leftovers divided by 2 plus rightovers divided by 2 plus (leftovers modulo 2) times 2. Show swaps.

Expected stdout

1

Open in Playground Standalone Page

#1248 · Count Number of Nice Subarrays

Medium Array

The readings is a list of 1, 1, 2, 1, and 1. The wanted is 3. The seen is a list of 6 zeros. Set item 0 of seen to 1. The odds is 0. The ways is 0.
For each value in readings.
If value modulo 2 is 1, add 1 to odds. If odds is at least wanted, The prior is item (odds minus wanted) of seen, Set ways to ways plus prior. Increase item odds of seen by 1.

Show ways.

Expected stdout

2

Open in Playground Standalone Page

#1249 · Minimum Remove to Make Valid Parentheses

Medium String

The text is "lee(t(c)o)de)". The keeps is a list of 13 zeros. The stack is an empty list of whole numbers.
For every spot from 0 to 12.
Set item spot of keeps to 1. The symbol is character spot of text. The depth is the number of items in stack. If symbol is "(", Place spot into stack. If symbol is ")" and depth is more than 0, remove the last item from stack. If symbol is ")" and depth is 0, set item spot of keeps to 0.

For every spot from 0 to the last position of stack.
Set item (item spot of stack) of keeps to 0.

The letters is an empty list of text.
For every spot from 0 to 12.
The piece is the slice of text from spot to spot plus 1. If item spot of keeps is 1, Place piece into letters.

The result is join letters with "". Show result.

Expected stdout

lee(t(c)o)de

Open in Playground Standalone Page

#1250 · Check If It Is a Good Array

Hard Array

The values is a list of 12, 5, 7, and 23. Keep a shared common of 0.

To Reduce from first (a whole number) with second (a whole number). The low is the absolute value of first. The high is the absolute value of second. If high is 0, set common to low, return. Reduce from high with low modulo high.

Set common to item 0 of values.
For every spot from 1 to 3.
If common is 1, stop. Reduce from common with item spot of values.

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

Expected stdout

true

Open in Playground Standalone Page

#1252 · Cells with Odd Values in a Matrix

Easy Array

The rows is 2, while the columns is 3. The pairs is a list of 0, 1, 1, and 1. The size is rows times columns. The cells is a list of size zeros.
For every which from 0 to 1.
The rowspot is item (which times 2) of pairs. The colspot is item (which times 2 plus 1) of pairs.
For every column from 0 to columns minus 1.
Increase item (rowspot times columns plus column) of cells by 1.
For every band from 0 to rows minus 1.
Increase item (band times columns plus colspot) of cells by 1.

The odds is 0.
For each value in cells.
If value modulo 2 is 1, add 1 to odds.

Show odds.

Expected stdout

6

Open in Playground Standalone Page

#1253 · Reconstruct a 2-Row Binary Matrix

Medium Array

The upper is 2, while the lower is 1. The sums is a list of 1, 1, and 1. The tops is an empty list of whole numbers. The bottoms is an empty list of whole numbers.
For each amount in sums.
If amount is 2, Place 1 into tops, Place 1 into bottoms.
If amount is 1.
If upper is more than 0, Place 1 into tops, Place 0 into bottoms, subtract 1 from upper, otherwise Place 0 into tops, Place 1 into bottoms, subtract 1 from lower.

The topline is empty text. The bottomline is empty text.
For every spot from 0 to the last position of tops.
If spot is more than 0, Place "," into topline. Place format whole number item spot of tops as text into topline. If spot is more than 0, Place "," into bottomline. Place format whole number item spot of bottoms as text into bottomline.

Show topline. Show bottomline.

Expected stdout

1,1,0
0,0,1

Open in Playground Standalone Page

#1254 · Number of Closed Islands

Medium Array

Keep a shared cells holding 1, 1, 1, 1, 0, 1, 1, 1, and 1. Keep a shared visited as a list of 9 zeros. Keep a shared islands of 0. Keep a shared levels of 0. Keep a shared spans of 0. Set levels to 3. Set spans to 3.

To sink from node (a whole number). If node is less than 0, return. If node is at least 9, return. If item node of cells is not 0, return. If item node of visited is 1, return. Set item node of visited to 1. The band is node divided by spans. The file is node modulo spans. If band is more than 0, sink from node minus spans. If band is less than levels minus 1, sink from node plus spans. If file is more than 0, sink from node minus 1. If file is less than spans minus 1, sink from node plus 1.

To probe from node (a whole number). If node is less than 0, return. If node is at least 9, return. If item node of cells is not 0, return. If item node of visited is 1, return. Set item node of visited to 1. The band is node divided by spans. The file is node modulo spans. If band is more than 0, probe from node minus spans. If band is less than levels minus 1, probe from node plus spans. If file is more than 0, probe from node minus 1. If file is less than spans minus 1, probe from node plus 1.

For every node from 0 to 8.
The band is node divided by spans. The file is node modulo spans. If band is 0 and item node of cells is 0, sink from node. If band is levels minus 1 and item node of cells is 0, sink from node. If file is 0 and item node of cells is 0, sink from node. If file is spans minus 1 and item node of cells is 0, sink from node.

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

Show islands.

Expected stdout

1

Open in Playground Standalone Page

#1255 · Maximum Score Words Formed by Letters

Hard Array

Keep a shared needed as a list of 26 zeros. Keep a shared available holding 2, 0, 1, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, and 0. Keep a shared wordcounts holding 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, and 0. Keep a shared wordscores holding 8, 10, 11, and 8. Keep a shared weights holding 1, 2, 4, and 8. Keep a shared total of 0. Keep a shared best of 0. Keep a shared fit of 1.
To reset from letter (a whole number). If letter is more than 25, return. Set item letter of needed to 0. Reset from letter plus 1.

To addcounts from letter (a whole number) with base (a whole number). If letter is more than 25, return. Increase item letter of needed by item (base plus letter) of wordcounts. Addcounts from letter plus 1 with base.

To include from word (a whole number). The base is word times 26. Addcounts from 0 with base. Increase total by item word of wordscores.

To measure from mask (a whole number) with word (a whole number). If word is more than 3, return. The bit is bitwise and of mask and item word of weights. If bit is not 0, include from word. Measure from mask with word plus 1.

To inspect from letter (a whole number). If letter is more than 25 and fit is 1 and total is more than best, set best to total. If letter is more than 25, return. If item letter of needed is more than item letter of available, set fit to 0. Inspect from letter plus 1.

For every mask from 0 to 15.
Set total to 0. Set fit to 1. Reset from 0. Measure from mask with 0. Inspect from 0.

Show best.

Expected stdout

19

Open in Playground Standalone Page

#1260 · Shift 2D Grid

Easy Array

The grid is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The rows is 3, while the columns is 3. The steps is 1. The total is rows times columns. The turn is steps modulo total. The flat is an empty list of whole numbers. For each value in grid, Place value into flat. The moved is a list of total zeros.
For every spot from 0 to total minus 1.
The target is (spot plus turn) modulo total. Set item target of moved to item spot of flat.

For each value in moved, show value.

Expected stdout

9
1
2
3
4
5
6
7
8

Open in Playground Standalone Page

#1262 · Greatest Sum Divisible by Three

Medium Array

Keep a shared table holding 0, -1000000, and -1000000. Keep a shared fresh as a list of 3 zeros. The values is a list of 3, 6, 5, 1, and 8.

To settle from value (a whole number). The leftover is value modulo 3.
For every rest from 0 to 2.
The before is (rest minus leftover plus 3) modulo 3. The candidate is item rest of table. If item before of table is more than -1000000, set candidate to the larger of candidate and item before of table plus value. Set item rest of fresh to candidate.

To replace.
For every rest from 0 to 2.
Set item rest of table to item rest of fresh.

For each value in values.
Settle from value. Replace.

Show item 0 of table.

Expected stdout

18

Open in Playground Standalone Page

#1263 · Minimum Moves to Move a Box to Their Target Location

Hard Array

Keep a shared grid holding 1, 1, 1, 1, 1, 1, 0, 0, 4, 1, 1, 0, 0, 0, 1, 1, 0, 2, 0, 1, 1, 0, 3, 0, and 1. Keep a shared seen as an empty list of whole numbers. Keep a shared distance as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers.
For every state from 0 to 624.
Place 0 into seen. Place 0 into distance. Place 0 into queue.

Keep a shared count of 0.

To expand from state (a whole number) with offset (a whole number).
The boxcell is state divided by 25. The playercell is state modulo 25. The nextcell is playercell plus offset.
If item nextcell of grid is 1, return.
If nextcell is boxcell and item (boxcell plus offset) of grid is 1, return.
The newboxcell is boxcell. The newplayercell is nextcell.
If nextcell is boxcell, set newboxcell to boxcell plus offset, set newplayercell to boxcell.
The freshstate is newboxcell times 25 plus newplayercell.
If item freshstate of seen is 1, return.
Set item freshstate of seen to 1. Set item freshstate of distance to item state of distance plus 1. The slot is count. Set item slot of queue to freshstate. Add 1 to count.

The start is 447. Set item 447 of seen to 1. Set item 0 of queue to start. Set count to 1. The head is 0. The answer is -1.
Repeat while head is less than count and answer is less than 0.
The state is item head of queue. Add 1 to head. The boxcell is state divided by 25. If boxcell is 8, set answer to item state of distance.
If boxcell is not 8.
The playercell is state modulo 25. The band is playercell divided by 5. The file is playercell modulo 5. If band is more than 0, Expand from state with -5. If band is less than 4, Expand from state with 5. If file is more than 0, Expand from state with -1. If file is less than 4, Expand from state with 1.

Show answer.

Expected stdout

5

Open in Playground Standalone Page

#1266 · Minimum Time Visiting All Points

Easy Array

The xs is a list of 1, 3, and -1. The ys is a list of 1, 4, and 0. The last is the last position of xs. The total is 0.
For every spot from 1 to last.
The east is item spot of xs minus item (spot minus 1) of xs. The north is item spot of ys minus item (spot minus 1) of ys. Set east to the absolute value of east. Set north to the absolute value of north. The stride is the larger of east and north. Add stride to total.

Show total.

Expected stdout

7

Open in Playground Standalone Page

#1267 · Count Servers that Communicate

Medium Array

The grid is a list of 1, 0, 1, and 1. The rows is 2, while the columns is 2. The rowtotals is a list of 2 zeros. The coltotals is a list of 2 zeros.
For every row from 0 to 1.
For every column from 0 to 1.
If item (row times columns plus column) of grid is 1, set item row of rowtotals to item row of rowtotals plus 1, set item column of coltotals to item column of coltotals plus 1.

The linked is 0.
For every row from 0 to 1.
For every column from 0 to 1.
If item (row times columns plus column) of grid is 1 and item row of rowtotals is more than 1, add 1 to linked. If item (row times columns plus column) of grid is 1 and item row of rowtotals is 1 and item column of coltotals is more than 1, add 1 to linked.

Show linked.

Expected stdout

3

Open in Playground Standalone Page

#1268 · Search Suggestions System

Medium Array

Keep a shared wordslist holding "mobile", "moneypot", "monitor", "mouse", and "mousepad".
For every left from 0 to 4.
For every right from left plus 1 to 4.
If item right of wordslist comes before item left of wordslist, swap item left of wordslist and item right of wordslist.

Keep a shared pickscount of 0. Keep a shared opening as empty text. Keep a shared line as empty text. Keep a shared same of 0.

To match from index (a whole number). Set same to 1. The word is item index of wordslist. The span is the length of opening.
For every part from 0 to span minus 1.
If character part of word is not character part of opening, set same to 0.

To collect. Set line to empty text. Set pickscount to 0.
For every index from 0 to the last position of wordslist.
The word is item index of wordslist.
If pickscount is less than 3.
If the length of word is at least the length of opening.
Match from index.
If same is 1.
If pickscount is more than 0, Place "," into line. Place word into line. Add 1 to pickscount.

The sought is "mouse".
For every spot from 0 to the last position of sought.
The piece is the slice of sought from spot to spot plus 1. Place piece into opening. Collect. Show line.

Expected stdout

mobile,moneypot,monitor
mobile,moneypot,monitor
mouse,mousepad
mouse,mousepad
mouse,mousepad

Open in Playground Standalone Page

#1269 · Number of Ways to Stay in the Same Place After Some Steps

Hard Dynamic Programming

The steps is 3, while the places is 2. The reach is the smaller of steps plus 1 and places. The width is reach plus 2. The table is a list of width zeros. The fresh is a list of width zeros. Set item 1 of table to 1.
For every stride from 1 to steps.
For every spot from 1 to reach.
If stride modulo 2 is 1, set item spot of fresh to (item spot of table plus item (spot minus 1) of table plus item (spot plus 1) of table) modulo 1000000007. If stride modulo 2 is 0, set item spot of table to (item spot of fresh plus item (spot minus 1) of fresh plus item (spot plus 1) of fresh) modulo 1000000007.

If steps modulo 2 is 1, show item 1 of fresh, otherwise show item 1 of table.

Expected stdout

4

Open in Playground Standalone Page

#1275 · Find Winner on a Tic Tac Toe Game

Easy Array

The moves is a list of 0, 0, 2, 0, 1, 1, 2, 1, 2, 2, 1, 0, 0, 1, 0, 2, and 1, 2. The board is a list of 9 zeros. The winner is 0. The turn is 0.
Repeat while turn is at most 8 and winner is 0.
The row is item (turn times 2) of moves. The column is item (turn times 2 plus 1) of moves. The mark is 1. If turn modulo 2 is 1, set mark to 2. Set item (row times 3 plus column) of board to mark. If item 0 of board is item 1 of board and item 1 of board is item 2 of board and item 0 of board is not 0, set winner to item 0 of board. If item 3 of board is item 4 of board and item 4 of board is item 5 of board and item 3 of board is not 0, set winner to item 3 of board. If item 6 of board is item 7 of board and item 7 of board is item 8 of board and item 6 of board is not 0, set winner to item 6 of board. If item 0 of board is item 3 of board and item 3 of board is item 6 of board and item 0 of board is not 0, set winner to item 0 of board. If item 1 of board is item 4 of board and item 4 of board is item 7 of board and item 1 of board is not 0, set winner to item 1 of board. If item 2 of board is item 5 of board and item 5 of board is item 8 of board and item 2 of board is not 0, set winner to item 2 of board. If item 0 of board is item 4 of board and item 4 of board is item 8 of board and item 0 of board is not 0, set winner to item 0 of board. If item 2 of board is item 4 of board and item 4 of board is item 6 of board and item 2 of board is not 0, set winner to item 2 of board. Add 1 to turn.

If winner is 1, show "A". If winner is 2, show "B". If winner is 0 and turn is 9, show "Draw". If winner is 0 and turn is less than 9, show "Pending".

Expected stdout

A

Open in Playground Standalone Page

#1276 · Number of Burgers with No Waste of Ingredients

Medium Math

The tomato is 16, while the cheese is 7. The difference is tomato minus cheese times 2.
If difference modulo 2 is 0 and difference is at least 0.
The jumbo is difference divided by 2. The small is cheese minus jumbo. Show jumbo. Show small.

If difference modulo 2 is not 0 or difference is less than 0, show "none".

Expected stdout

1
6

Open in Playground Standalone Page

#1277 · Count Square Submatrices with All Ones

Medium Array

The matrix is a list of 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, and 1. The rows is 3, while the columns is 4. The table is a list of 12 zeros. The total is 0.
For every row from 0 to rows minus 1.
For every column from 0 to columns minus 1.
The index is row times columns plus column.
If item index of matrix is 1.
The above is 0. The left is 0. The corner is 0. If row is more than 0, set above to item (index minus columns) of table. If column is more than 0, set left to item (index minus 1) of table. If row is more than 0 and column is more than 0, set corner to item (index minus columns minus 1) of table. The small is the smaller of above and left. Set small to the smaller of small and corner. Set item index of table to small plus 1. Increase total by item index of table.

Show total.

Expected stdout

15

Open in Playground Standalone Page

#1278 · Palindrome Partitioning III

Hard String

The word is "abc". The parts is 2. Keep a shared size of 3. Keep a shared width of size plus 1. Keep a shared costs as an empty list of whole numbers. Keep a shared scores as an empty list of whole numbers.
For every spot from 0 to 8.
Place 0 into costs.

For every spot from 0 to 11.
Place 1000000 into scores.

Set item 0 of scores to 0.
For every start from 0 to size minus 1.
For every finish from start to size minus 1.
The left is start. The right is finish.
Repeat while left is less than right.
If character left of word is not character right of word, Set item (start times size plus finish) of costs to item (start times size plus finish) of costs plus 1. Add 1 to left. Subtract 1 from right.

To measure from cuts (a whole number) with finish (a whole number). The least is 1000000.
For every start from 0 to finish minus 1.
The previous is item ((cuts minus 1) times width plus start) of scores. The change is item (start times size plus finish minus 1) of costs. The candidate is previous plus change. If candidate is less than least, set least to candidate. Set item (cuts times width plus finish) of scores to least.

For every cuts from 1 to parts.
For every finish from 1 to size.
Measure from cuts with finish.

Show item (parts times width plus size) of scores.

Expected stdout

1

Open in Playground Standalone Page

#1281 · Subtract the Product and Sum of Digits of an Integer

Easy Math

The source is 234. The value is source. The product is 1. The sum is 0.
Repeat while value is more than 0.
The digit is value modulo 10. Set product to product times digit. Add digit to sum. Set value to value divided by 10.

The answer is product minus sum. Show answer.

Expected stdout

15

Open in Playground Standalone Page

#1282 · Group the People Given the Group Size They Belong To

Medium Array

Keep a shared sizes holding 3, 3, 3, 3, 3, 1, and 3. Keep a shared people of 7. Keep a shared bucket as a list of 7 zeros. Keep a shared line as empty text. Keep a shared count of 0.

To gather from target (a whole number). Set count to 0.
For every person from 0 to people minus 1.
If item person of sizes is target, Set item count of bucket to person, add 1 to count.

To render from start (a whole number) with target (a whole number). Set line to empty text.
For every offset from 0 to target minus 1.
The spot is start plus offset. If offset is more than 0, Place "," into line. Place format whole number item spot of bucket as text into line.

For every target from 1 to people.
Gather from target.
If count is more than 0.
The start is 0.
Repeat while start is less than count.
Render from start with target. Show line. Add target to start.

Expected stdout

5
0,1,2
3,4,6

Open in Playground Standalone Page

#1283 · Find the Smallest Divisor Given a Threshold

Medium Array

Keep a shared values holding 1, 2, 5, and 9. Keep a shared total of 0. The limit is 6.

To measure from divisor (a whole number). Set total to 0.
For each value in values.
The share is value divided by divisor. If value modulo divisor is not 0, add 1 to share. Add share to total.

The low is 1. The high is 0. For each value in values, if value is more than high, set high to value. The answer is high.
Repeat while low is at most high.
The middle is the midpoint of low and high. Measure from middle. If total is at most limit, set answer to middle, set high to middle minus 1. If total is more than limit, set low to middle plus 1.

Show answer.

Expected stdout

5

Open in Playground Standalone Page

#1284 · Minimum Number of Flips to Convert Binary Matrix to Zero Matrix

Hard Array

The matrix is a list of 0, 0, 0, and 1. The rows is 2, while the columns is 2. The weights is a list of 1, 2, 4, and 8. The patterns is a list of 4 zeros.
For every cell from 0 to 3.
The band is cell divided by columns. The file is cell modulo columns. The pattern is item cell of weights. If band is more than 0, set pattern to pattern plus item (cell minus columns) of weights. If band is less than rows minus 1, set pattern to pattern plus item (cell plus columns) of weights. If file is more than 0, set pattern to pattern plus item (cell minus 1) of weights. If file is less than columns minus 1, set pattern to pattern plus item (cell plus 1) of weights. Set item cell of patterns to pattern.

The seen is a list of 16 zeros. The distance is a list of 16 zeros. The queue is a list of 16 zeros. The start is 8. Set item start of seen to 1. Set item 0 of queue to start. The count is 1. The head is 0. The answer is -1.
Repeat while head is less than count and answer is less than 0.
The state is item head of queue. Add 1 to head. If state is 0, set answer to item state of distance.
If state is not 0.
For every cell from 0 to 3.
The next is exclusive or of state and item cell of patterns.
If item next of seen is 0.
Set item next of seen to 1. Set item next of distance to item state of distance plus 1. Set item count of queue to next. Add 1 to count.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#1287 · Element Appearing More Than 25% In Sorted Array

Easy Array

The values is a list of 1, 2, 2, 6, 6, 6, 6, 7, and 10. The last is the last position of values. The total is last plus 1. The current is item 0 of values. The run is 0. The answer is 0.
For each value in values.
If value is current, add 1 to run, otherwise set current to value, set run to 1. If answer is 0 and run times 4 is more than total, set answer to value.

Show answer.

Expected stdout

6

Open in Playground Standalone Page

#1288 · Remove Covered Intervals

Medium Array

The starts is a list of 1, 3, and 2. The ends is a list of 4, 6, and 8. The last is the last position of starts. The answer is 0.
For every leftspot from 0 to last.
The covered is 0.
For every rightspot from 0 to last.
If leftspot is not rightspot and item rightspot of starts is at most item leftspot of starts and item leftspot of ends is at most item rightspot of ends, set covered to 1. If rightspot is last and covered is 0, add 1 to answer.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#1289 · Minimum Falling Path Sum II

Hard Array

The cells is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The scores is a list of 9 zeros. For every column from 0 to 2, set item column of scores to item column of cells.
For every level from 1 to 2.
For every column from 0 to 2.
The index is level times 3 plus column. The best is 1000000.
For every prior from 0 to 2.
The above is (level minus 1) times 3 plus prior. The candidate is item above of scores plus item index of cells. If prior is not column and candidate is less than best, set best to candidate. If prior is 2, set item index of scores to best.

The answer is item 6 of scores.
For every column from 1 to 2.
If item (6 plus column) of scores is less than answer, set answer to item (6 plus column) of scores.

Show answer.

Expected stdout

13

Open in Playground Standalone Page

#1290 · Convert Binary Number in a Linked List to Integer

Easy Math

The values is a list of 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, and 0. The nexts is a list of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and -1. The answer is 0. The node is 0.
Repeat while node is at least 0.
Set answer to answer times 2 plus item node of values. Set node to item node of nexts.

Show answer.

Expected stdout

18880

Open in Playground Standalone Page

#1291 · Sequential Digits

Medium Enumeration

The low is 100. The high is 300. The answers is an empty list of whole numbers.
For every first from 1 to 9.
The value is first. The digit is first.
Repeat while digit is less than 9 and value is at most high.
Add 1 to digit. Set value to value times 10 plus digit. If value is at least low and value is at most high, Place value into answers.

The pieces is an empty list of text.
For each answer in answers.
The piece is format whole number answer as text. Place piece into pieces.

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

Expected stdout

123,234

Open in Playground Standalone Page

#1292 · Maximum Side Length of a Square with Sum Less than or Equal to Threshold

Medium Array

The cells is a list of 1, 1, 3, 2, 4, 3, 2, 1, 1, 3, 2, 4, 3, 2, 1, 1, 3, 2, 4, 3, and 2. The height is 3. The width is 7. The threshold is 4. The answer is 0.
For every side from 1 to 3.
The possible is 0.
For every top from 0 to height minus side.
For every first from 0 to width minus side.
The total is 0.
For every down from 0 to side minus 1.
For every second from 0 to side minus 1.
The spot is (top plus down) times width plus first plus second. Increase total by item spot of cells. If down is side minus 1 and second is side minus 1 and total is at most threshold, set possible to 1. If possible is 1, set answer to side.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#1293 · Shortest Path in a Grid with Obstacles Elimination

Hard Array

Keep a shared cells holding 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, and 0. Keep a shared best as an empty list of whole numbers. For every spot from 0 to 14, Place -1 into best. Keep a shared queue as an empty list of whole numbers. Keep a shared remaining as an empty list of whole numbers. Keep a shared distances as an empty list of whole numbers. Keep a shared tally of 1. Keep a shared leftnow of 0. Keep a shared stepsnow of 0.

To probe from other (a whole number). If other is less than 0 or other is more than 14, return. The cost is 0. If item other of cells is 1, set cost to 1. The after is leftnow minus cost. If after is less than 0, return. If after is at most item other of best, return. Set item other of best to after. Place other into queue. Place after into remaining. Place stepsnow plus 1 into distances. Add 1 to tally.

Set item 0 of best to 1. Place 0 into queue. Place 1 into remaining. Place 0 into distances. The head is 0. The answer is -1.
Repeat while head is less than tally.
The here is item head of queue. The left is item head of remaining. The steps is item head of distances. Add 1 to head. If here is 14, set answer to steps, stop. The band is here divided by 3. The file is here modulo 3. The west is here minus 1. The east is here plus 1. The north is here minus 3. The south is here plus 3. Set leftnow to left. Set stepsnow to steps. If file is more than 0, Probe from west. If file is less than 2, Probe from east. If band is more than 0, Probe from north. If band is less than 4, Probe from south.

Show answer.

Expected stdout

6

Open in Playground Standalone Page

#1295 · Find Numbers with Even Number of Digits

Easy Array

The numbers is a list of 12, 345, 2, 6, and 7896. The answer is 0.
For each value in numbers.
The magnitude is value. The digits is 0.
Repeat while magnitude is more than 0.
Add 1 to digits. Set magnitude to magnitude divided by 10. If magnitude is 0 and digits modulo 2 is 0, add 1 to answer.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#1296 · Divide Array in Sets of K Consecutive Numbers

Medium Array

Keep a shared counts as an empty list of whole numbers. Keep a shared highest of 0. Keep a shared span of 4. Keep a shared possible of 1. The numbers is a list of 1, 2, 3, 3, 4, 4, 5, and 6. The last is the last position of numbers. Set highest to item 0 of numbers.
For every spot from 1 to last.
If item spot of numbers is more than highest, set highest to item spot of numbers.

For every spot from 0 to highest, Place 0 into counts.
For every spot from 0 to last.
Increase item (item spot of numbers) of counts by 1.

To build from base (a whole number).
For every offset from 0 to span minus 1.
If possible is 0, return. The wanted is base plus offset. If wanted is more than highest, set possible to 0, return. If item wanted of counts is 0, set possible to 0, return. Decrease item wanted of counts by 1.

For every value from 0 to highest.
Repeat while item value of counts is more than 0 and possible is 1.
Build from value.

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

Expected stdout

true

Open in Playground Standalone Page

#1297 · Maximum Number of Occurrences of a Substring

Medium String

The source is "aababcaab". The maxletters is 2. The span is 3. The size is the number of characters in source. The last is size minus span. The best is 0.
For every spot from 0 to last.
The piece is the slice of source from spot to spot plus span. The letters is a list of 128 zeros. The distinct is 0.
For every offset from 0 to span minus 1.
The code is character (spot plus offset) of source. If item code of letters is 0, add 1 to distinct. Set item code of letters to 1.
If offset is span minus 1 and distinct is at most maxletters.
The matches is 0.
For every start from 0 to last.
The other is the slice of source from start to start plus span. If other matches piece, add 1 to matches. If matches is more than best, set best to matches.

Show best.

Expected stdout

2

Open in Playground Standalone Page

#1298 · Maximum Candies You Can Get from Boxes

Hard Array

Keep a shared status holding 1, 0, 1, and 0. Keep a shared candies holding 7, 5, 4, and 100. Keep a shared keyfrom as an empty list of whole numbers. Place 2 into keyfrom. Keep a shared keybox as an empty list of whole numbers. Place 1 into keybox. Keep a shared boxstarts holding 0, 1, 2, and 2. Keep a shared boxcounts holding 2, 1, 0, and 0. Keep a shared boxitems holding 1, 2, and 3. Keep a shared held as a list of 4 zeros. Keep a shared opened as a list of 4 zeros. Keep a shared havekey as a list of 4 zeros. Keep a shared queue as an empty list of whole numbers. Keep a shared tally of 0.

To unlock from box (a whole number).
For every key from 0 to the last position of keyfrom.
If item key of keyfrom is box.
The door is item key of keybox. Set item door of havekey to 1. If item door of held is 1 and item door of opened is 0, Place door into queue, add 1 to tally.

To offer from box (a whole number). The start is item box of boxstarts. The many is item box of boxcounts.
If many is more than 0.
For every offset from 0 to many minus 1.
The inner is item (start plus offset) of boxitems. Set item inner of held to 1. If item inner of status is 1 or item inner of havekey is 1, Place inner into queue, add 1 to tally.

The total is 0. Set item 0 of held to 1. Place 0 into queue. Set tally to 1. The head is 0.
Repeat while head is less than tally.
The box is item head of queue. Add 1 to head.
If item box of opened is 0.
Set item box of opened to 1. Increase total by item box of candies. Unlock from box. Offer from box.

Show total.

Expected stdout

16

Open in Playground Standalone Page

#1299 · Replace Elements with Greatest Element on Right Side

Easy Array

The numbers is a list of 17, 18, 5, 4, 6, and 1. The last is the last position of numbers. The answer is a list of last plus 1 zeros. The greatest is -1.
For every offset from 0 to last.
The spot is last minus offset. Set item spot of answer to greatest. If item spot of numbers is more than greatest, set greatest to item spot of numbers.

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

18,6,6,6,1,-1

Open in Playground Standalone Page

#1300 · Sum of Mutated Array Closest to Target

Medium Array

The numbers is a list of 4, 9, and 3. The target is 10. The last is the last position of numbers. The highest is item 0 of numbers.
For every spot from 1 to last.
If item spot of numbers is more than highest, set highest to item spot of numbers.

The answer is 0. The best is 1000000.
For every guess from 0 to highest.
The total is 0.
For every spot from 0 to last.
The capped is item spot of numbers. If capped is more than guess, set capped to guess. Increase total by capped.
If spot is last.
The difference is total minus target. If difference is less than 0, set difference to 0 minus difference. If difference is less than best, set best to difference, set answer to guess.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#1301 · Number of Paths with Max Score

Hard Array

The board is a list of "E23", "2X2", and "12S". The cells is an empty list of whole numbers.
For each band in board.
For every column from 0 to 2.
The symbol is character column of band. If symbol is "X", Place -1 into cells. If symbol is "E", Place 0 into cells. If symbol is "S", Place 0 into cells. If symbol is between "0" and "9", The amount is the digit value of symbol, Place amount into cells.

The scores is a list of 9 zeros. The ways is a list of 9 zeros.
For every offset from 0 to 8.
The spot is 8 minus offset. If item spot of cells is -1, set item spot of scores to -1.
If item spot of cells is not -1.
If spot is 8, set item spot of ways to 1.
If spot is not 8.
The best is -1. The count is 0. If spot plus 3 is at most 8 and item (spot plus 3) of scores is more than best, set best to item (spot plus 3) of scores. If spot modulo 3 is less than 2 and item (spot plus 1) of scores is more than best, set best to item (spot plus 1) of scores. If spot plus 3 is at most 8 and item (spot plus 3) of scores is best and item (spot plus 3) of scores is at least 0, set count to count plus item (spot plus 3) of ways. If spot modulo 3 is less than 2 and item (spot plus 1) of scores is best and item (spot plus 1) of scores is at least 0, set count to count plus item (spot plus 1) of ways. If best is at least 0, set item spot of scores to best plus item spot of cells. If best is at least 0, set item spot of ways to count.

Show item 0 of scores. Show item 0 of ways.

Expected stdout

7
1

Open in Playground Standalone Page

#1302 · Deepest Leaves Sum

Medium Depth-First Search

Keep a shared values holding 1, 2, 3, 4, 5, -1, 6, 7, -1, -1, -1, -1, and 8. Keep a shared lefts holding 1, 3, 5, 7, 9, -1, 11, -1, -1, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, 6, 8, 10, -1, 12, -1, -1, -1, -1, -1, and -1. Keep a shared best of 0. Keep a shared total of 0.

To measure from node (a whole number) with level (a whole number). If node is less than 0, return. If item node of values is less than 0, return. If level is more than best, set best to level. Measure from item node of lefts with level plus 1. Measure from item node of rights with level plus 1.

To gather from node (a whole number) with level (a whole number). If node is less than 0, return. If item node of values is less than 0, return. If level is best, set total to total plus item node of values. Gather from item node of lefts with level plus 1. Gather from item node of rights with level plus 1.

Measure from 0 with 0. Gather from 0 with 0. Show total.

Expected stdout

15

Open in Playground Standalone Page

#1304 · Find N Unique Integers Sum up to Zero

Easy Array

The count is 5. The pairs is count divided by 2. The answers is an empty list of whole numbers.
For every offset from 0 to pairs minus 1.
The value is pairs minus offset. Place 0 minus value into answers.

If count modulo 2 is 1, Place 0 into answers.
For every offset from 0 to pairs minus 1.
The value is offset plus 1. Place value into answers.

The pieces is an empty list of text.
For each answer in answers.
The piece is format whole number answer as text. Place piece into pieces.

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

Expected stdout

-2,-1,0,1,2

Open in Playground Standalone Page

#1305 · All Elements in Two Binary Search Trees

Medium Sorting

Keep a shared firstvalues holding 2, 1, and 4. Keep a shared firstlefts holding 1, -1, and -1. Keep a shared firstrights holding 2, -1, and -1. Keep a shared secondvalues holding 1, 0, and 3. Keep a shared secondlefts holding 1, -1, and -1. Keep a shared secondrights holding 2, -1, and -1. Keep a shared combined as an empty list of whole numbers.

To gather first from node (a whole number). If node is less than 0, return. Gather first from item node of firstlefts. Place item node of firstvalues into combined. Gather first from item node of firstrights.

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

Gather first from 0. Gather second from 0. Sort combined. The pieces is an empty list of text.
For each value in combined.
The piece is format whole number value as text. Place piece into pieces.

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

Expected stdout

0,1,1,2,3,4

Open in Playground Standalone Page

#1306 · Jump Game III

Medium Array

Keep a shared numbers holding 4, 2, 3, 0, 3, 1, and 2. Keep a shared visited as a list of 7 zeros. Keep a shared found of 0.

To seek from spot (a whole number). If spot is less than 0 or spot is more than 6, return. If item spot of visited is 1, return. Set item spot of visited to 1. If item spot of numbers is 0, set found to 1, return. The jump is item spot of numbers. Seek from (spot minus jump). Seek from (spot plus jump).

Seek from 5. If found is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#1307 · Verbal Arithmetic Puzzle

Hard Array

Keep a shared assigned holding -1, -1, -1, -1, -1, -1, -1, and -1. Keep a shared taken as a list of 10 zeros. Keep a shared leading as a list of 8 zeros. Set item 0 of leading to 1. Set item 4 of leading to 1. Keep a shared addends holding 3, 1, 2, 6, 1, 5, 0, and 4. Keep a shared sumstarts holding 0, 2, 4, 6, and 8. Keep a shared sumcounts holding 2, 2, 2, 2, and 0. Keep a shared letterorder holding 3, 1, 7, 2, 6, 1, 1, 5, 2, 0, 4, 5, and 4. Keep a shared letterstarts holding 0, 3, 6, 9, and 12. Keep a shared lettercounts holding 3, 3, 3, 3, and 1. Keep a shared outputslots holding 7, 1, 2, 5, and 4. Keep a shared found of 0. Keep a shared passing of 0.

To verify from column (a whole number) with carried (a whole number). The total is carried. The start is item column of sumstarts. The count is item column of sumcounts. If count is more than 0, set total to total plus item (item start of addends) of assigned. If count is more than 1, set total to total plus item (item (start plus 1) of addends) of assigned. The target is 0. If item column of outputslots is at least 0, set target to item (item column of outputslots) of assigned. If total modulo 10 is not target, return. If column is 4 and total divided by 10 is 0, set found to 1, return. If column is 4, return. Fill from column plus 1 with 0 and total divided by 10.

To pick from column (a whole number) with offset (a whole number) and chosen (a whole number). The slot is item (item column of letterstarts plus offset) of letterorder. Set item slot of assigned to chosen. Set item chosen of taken to 1. Fill from column with offset plus 1 and passing. Set item slot of assigned to -1. Set item chosen of taken to 0.

To fill from column (a whole number) with offset (a whole number) and carried (a whole number). If found is 1, return. The count is item column of lettercounts. If offset is at least count, Verify from column with carried, return. The start is item column of letterstarts. The slot is item (start plus offset) of letterorder. If item slot of assigned is at least 0, Fill from column with offset plus 1 and carried, return.
For every candidate from 0 to 9.
Set passing to carried. The fresh is 1. If item candidate of taken is 1, set fresh to 0. If item slot of leading is 1 and candidate is 0, set fresh to 0. If fresh is 1, Pick from column with offset and candidate.

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

Expected stdout

true

Open in Playground Standalone Page

#1309 · Decrypt String from Alphabet to Integer Mapping

Easy String

The source is "10#11#12". The size is the number of characters in source. The alphabet is "abcdefghijklmnopqrstuvwxyz". The pieces is an empty list of text. The spot is 0.
Repeat while spot is less than size.
The here is character spot of source. The width is 1. If spot plus 2 is less than size and character (spot plus 2) of source is "#", set width to 3. The value is the digit value of here. The nextdigit is 0. If width is 3, set nextdigit to the digit value of character (spot plus 1) of source. If width is 3, set value to value times 10 plus nextdigit. The piece is the slice of alphabet from value minus 1 to value. Place piece into pieces. Add width to spot.

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

Expected stdout

jkab

Open in Playground Standalone Page

#1310 · XOR Queries of a Subarray

Medium Array

The numbers is a list of 1, 3, 4, and 8. The lefts is a list of 0, 1, 0, and 3. The rights is a list of 1, 2, 3, and 3. The prefixes is a list of 5 zeros.
For every spot from 0 to 3.
The mixed is exclusive or of item spot of prefixes and item spot of numbers. Set item (spot plus 1) of prefixes to mixed.

The pieces is an empty list of text.
For every query from 0 to 3.
The left is item query of lefts. The right is item query of rights. The value is exclusive or of item (right plus 1) of prefixes and item left of prefixes. The piece is format whole number value as text. Place piece into pieces.

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

Expected stdout

2,7,14,8

Open in Playground Standalone Page

#1311 · Get Watched Videos by Your Friends

Medium Array

Keep a shared names as an empty list of text. Keep a shared counts as an empty list of whole numbers. Keep a shared wantedtext as empty text. Keep a shared total of 0. Keep a shared winner of 0. Keep a shared starts as an empty list of whole numbers. Keep a shared edges as an empty list of whole numbers. Keep a shared degrees as an empty list of whole numbers. Keep a shared videos as an empty list of text. Keep a shared videostarts as an empty list of whole numbers. Keep a shared videosizes as an empty list of whole numbers. Keep a shared depths as an empty list of whole numbers. Keep a shared visited as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared tail of 0. Place 0 into starts. Place 2 into starts. Place 4 into starts. Place 6 into starts. Place 1 into edges. Place 2 into edges. Place 0 into edges. Place 3 into edges. Place 0 into edges. Place 3 into edges. Place 1 into edges. Place 2 into edges. Place 2 into degrees. Place 2 into degrees. Place 2 into degrees. Place 2 into degrees. Place "A" into videos. Place "B" into videos. Place "C" into videos. Place "B" into videos. Place "C" into videos. Place "D" into videos. Place 0 into videostarts. Place 2 into videostarts. Place 3 into videostarts. Place 5 into videostarts. Place 2 into videosizes. Place 1 into videosizes. Place 2 into videosizes. Place 1 into videosizes.
For every person from 0 to 3.
Place 0 into depths. Place 0 into visited. Place 0 into queue. Place 0 into queue.

To register. Set found to -1. For every spot from 0 to total minus 1, if item spot of names matches wantedtext, set found to spot. If found is less than 0, Place wantedtext into names, Place 1 into counts, add 1 to total, otherwise set item found of counts to item found of counts plus 1.

To offer from node (a whole number) with offset (a whole number). The base is item node of videostarts. The video is item (base plus offset) of videos. Set wantedtext to video. Register.

To gather from node (a whole number). For every offset from 0 to item node of videosizes minus 1, Offer from node with offset.

To reach from node (a whole number) with offset (a whole number). The base is item node of starts. The target is item (base plus offset) of edges. If item node of depths is less than 1 and item target of visited is 0, Set item target of visited to 1, Set item target of depths to item node of depths plus 1, Set item tail of queue to target, Add 1 to tail.

To expand from node (a whole number). For every offset from 0 to item node of degrees minus 1, Reach from node with offset.

To work from node (a whole number). If item node of depths is 1, Gather from node, otherwise Expand from node.

The head is 0. Set tail to 1. Set item 0 of queue to 0. Set item 0 of visited to 1.
Repeat while head is less than tail.
The node is item head of queue. Add 1 to head. Work from node.

To compare from other (a whole number) with best (a whole number). The othername is item other of names. The bestname is item best of names. If item other of counts is less than item best of counts or item other of counts is item best of counts and character 0 of othername is less than character 0 of bestname, set winner to other.

To settle from spot (a whole number). Set winner to spot. For every other from spot plus 1 to total minus 1, Compare from other with winner. The best is winner. The holdname is item spot of names. The holdcount is item spot of counts. Set item spot of names to item best of names. Set item spot of counts to item best of counts. Set item best of names to holdname. Set item best of counts to holdcount.

For every spot from 0 to total minus 2, Settle from spot. For each name in names, show name.

Expected stdout

B
C

Open in Playground Standalone Page

#1312 · Minimum Insertion Steps to Make a String Palindrome

Hard String

Keep a shared text as empty text. Keep a shared seeds as an empty list of text. Keep a shared table as an empty list of whole numbers. Place "mbadm" into seeds. Set text to item 0 of seeds. For every cell from 0 to 24, Place 0 into table.

To fill from start (a whole number) with finish (a whole number) and length (a whole number). If length is 1, Set item (start times 5 plus finish) of table to 1, otherwise Extend from start with finish.

To extend from start (a whole number) with finish (a whole number). The here is start times 5 plus finish. The inner is (start plus 1) times 5 plus finish minus 1. The down is (start plus 1) times 5 plus finish. The left is start times 5 plus finish minus 1. The first is character start of text. The second is character finish of text. The best is the larger of item down of table and item left of table. If first is second, Set item here of table to item inner of table plus 2, otherwise Set item here of table to best.

To row from start (a whole number).
For every length from 1 to 5 minus start.
The finish is start plus length minus 1. Fill from start with finish and length.

The start is 4.
Repeat while start is at least 0.
Row from start. Decrease start by 1.

Show 5 minus item 4 of table.

Expected stdout

2

Open in Playground Standalone Page

#1313 · Decompress Run-Length Encoded List

Easy Array

The numbers is a list of 1, 2, 3, and 4.
For every pair from 0 to 1.
The times is item (pair times 2) of numbers. The value is item (pair times 2 plus 1) of numbers. For every copy from 0 to times minus 1, Show value.

Expected stdout

2
4
4
4

Open in Playground Standalone Page

#1314 · Matrix Block Sum

Medium Array

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

To sweep from row (a whole number) with column (a whole number). Set total to 0.
For every other from 0 to 8.
The otherrow is other divided by 3. The othercolumn is other modulo 3. The rowgap is the absolute value of (otherrow minus row). The colgap is the absolute value of (othercolumn minus column). The piece is item other of matrix. If rowgap is at most 1 and colgap is at most 1, add piece to total.

For every cell from 0 to 8.
The row is cell divided by 3. The column is cell modulo 3. Sweep from row with column. Show total.

Expected stdout

12
21
16
27
45
33
24
39
28

Open in Playground Standalone Page

#1315 · Sum of Nodes with Even-Valued Grandparent

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 total of 0. Place 2 into values. Place 4 into values. Place 5 into values. Place 1 into values. Place 3 into values. Place 6 into values. Place 7 into values. Place 1 into lefts. Place 3 into lefts. Place 5 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place 2 into rights. Place 4 into rights. Place 6 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights.

To walk from node (a whole number) with parent (a whole number) and grand (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. The piece is item node of values. Walk from left with node and parent. Walk from right with node and parent. If grand is at least 0 and grand modulo 2 is 0, add piece to total.

Walk from 0 with -1 and -1. Show total.

Expected stdout

17

Open in Playground Standalone Page

#1316 · Distinct Echo Substrings

Hard String

Keep a shared text as empty text. Keep a shared seeds as an empty list of text. Keep a shared seen as an empty list of text. Keep a shared same of 0. Keep a shared count of 0. Place "abcabcabc" into seeds. Set text to item 0 of seeds.

To check from first (a whole number) with second (a whole number) and span (a whole number). Set same to 1.
For every offset from 0 to span minus 1.
The one is character (first plus offset) of text. The two is character (second plus offset) of text. If one is not two, set same to 0.

To collect from start (a whole number) with span (a whole number). The piece is the slice of text from start to start plus span. The known is 0. For every spot from 0 to the last position of seen, if item spot of seen matches piece, set known to 1. If known is 0, Place piece into seen, add 1 to count.

To probe from start (a whole number) with span (a whole number). Check from start with start plus span and span. If same is 1, Collect from start with span.

To survey from start (a whole number). The room is 9 minus start. The half is room divided by 2. For every span from 1 to half, Probe from start with span.

For every start from 0 to 8, Survey from start. Show count.

Expected stdout

3

Open in Playground Standalone Page