The TransCompilator

HTML chunk · 50 solved programs

Chunk 02 · #0051–#0100

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

#0051 · N-Queens

Hard Array

Keep a shared queens as an empty list of whole numbers. Keep a shared parts as an empty list of text. Keep a shared answers as an empty list of text. Keep a shared built as empty text. Keep a shared piece as empty text. Keep a shared safe of 0.
To check square at column (a whole number) for row (a whole number). Set safe to 1.
For every prior from 0 to row minus 1.
The other is item prior of queens. If other is column, set safe to 0. The difference is column minus other. If difference is less than 0, set difference to 0 minus difference. The distance is row minus prior. If difference is distance, set safe to 0.

To place from level (a whole number). If level is 4, The built is join parts with ",", Place built into answers, then return.
For every candidate from 0 to 3.
Check square at candidate for level. If safe is 1, Place candidate into queens, Set piece to format whole number candidate as text, Place piece into parts, place from level plus 1, remove the last item from queens, remove the last item from parts.

Place from 0. For each answer in answers, show answer.

Expected stdout

1,3,0,2
2,0,3,1

Open in Playground Standalone Page

#0052 · N-Queens II

Hard Backtracking

Keep a shared queens as an empty list of whole numbers. Keep a shared safe of 0. Keep a shared total of 0.
To check square at column (a whole number) for row (a whole number). Set safe to 1.
For every prior from 0 to row minus 1.
The other is item prior of queens. If other is column, set safe to 0. The difference is column minus other. If difference is less than 0, set difference to 0 minus difference. The distance is row minus prior. If difference is distance, set safe to 0.

To place from level (a whole number). If level is 4, add 1 to total, then return.
For every candidate from 0 to 3.
Check square at candidate for level. If safe is 1, Place candidate into queens, place from level plus 1, remove the last item from queens.

Place from 0. Display total.

Expected stdout

2

Open in Playground Standalone Page

#0053 · Maximum Subarray

Medium Array

The nums is a list of -2, 1, -3, 4, -1, 2, 1, -5, and 4. The best is item 0 of nums. The current is 0.
For every position from 0 to the last position of nums.
Set current to the larger of current plus item position of nums and item position of nums. Set best to the larger of best and current.

Report best.

Expected stdout

6

Open in Playground Standalone Page

#0054 · Spiral Matrix

Medium Array

The matrix is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The rows is 3. The columns is 3. The size is rows times columns. The visited is a list of size zeros. The spiral is an empty list of whole numbers. The row is 0. The column is 0. The down is 0. The right is 1.
For every step from 0 to one less than size.
Place item (row times columns plus column) of matrix into spiral. Set item (row times columns plus column) of visited to 1. The nextrow is row plus down. The nextcolumn is column plus right. The blocked is 0. The olddown is 0. If nextrow is less than 0 or nextrow is at least rows or nextcolumn is less than 0 or nextcolumn is at least columns, set blocked to 1. If blocked is 0 and item (nextrow times columns plus nextcolumn) of visited is 1, set blocked to 1. If blocked is 1, set olddown to down. If blocked is 1, set down to right. If blocked is 1, set right to 0 minus olddown. Increase row by down. Increase column by right.

For each value in spiral, show value.

Expected stdout

1
2
3
6
9
8
7
4
5

Open in Playground Standalone Page

#0055 · Jump Game

Medium Array

The jumps is a list of 2, 3, 1, 1, and 4. The farthest is 0. The possible is 1.
For every position from 0 to the last position of jumps.
If position is more than farthest, set possible to 0. The arrival is position plus item position of jumps. If position is at most farthest, set farthest to the larger of farthest and arrival.

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

Expected stdout

true

Open in Playground Standalone Page

#0056 · Merge Intervals

Medium Array

The begins is a list of 1, 2, 8, and 15. The finishes is a list of 3, 6, 10, and 18. The last is the last position of begins.
For every left from 0 to last.
For every right from left to last.
If item right of begins is less than item left of begins, swap item left of begins and item right of begins, swap item left of finishes and item right of finishes.

The outbegins is an empty list of whole numbers. The outfinishes is an empty list of whole numbers.
For every position from 0 to last.
The start is item position of begins. The finish is item position of finishes. The size is the number of items in outbegins. If size is 0, Place start into outbegins, Place finish into outfinishes. If size is more than 0 and start is at most item (size minus 1) of outfinishes and finish is more than item (size minus 1) of outfinishes, set item (size minus 1) of outfinishes to finish. If size is more than 0 and start is more than item (size minus 1) of outfinishes, Place start into outbegins, Place finish into outfinishes.

The count is the number of items in outbegins. The final is count minus 1.
For every position from 0 to final.
Report item position of outbegins. Report item position of outfinishes. Report "---".

Expected stdout

1
6
---
8
10
---
15
18
---

Open in Playground Standalone Page

#0057 · Insert Interval

Medium Array

The begins is a list of 1 and 6. The finishes is a list of 3 and 9. Place 2 into begins. Place 5 into finishes. The last is the last position of begins.
For every left from 0 to last.
For every right from left to last.
If item right of begins is less than item left of begins, swap item left of begins and item right of begins, swap item left of finishes and item right of finishes.

The outbegins is an empty list of whole numbers. The outfinishes is an empty list of whole numbers.
For every position from 0 to last.
The start is item position of begins. The finish is item position of finishes. The size is the number of items in outbegins. If size is 0, Place start into outbegins, Place finish into outfinishes. If size is more than 0 and start is at most item (size minus 1) of outfinishes and finish is more than item (size minus 1) of outfinishes, set item (size minus 1) of outfinishes to finish. If size is more than 0 and start is more than item (size minus 1) of outfinishes, Place start into outbegins, Place finish into outfinishes.

The count is the number of items in outbegins. The final is count minus 1.
For every position from 0 to final.
Show item position of outbegins. Show item position of outfinishes. Show "---".

Expected stdout

1
5
---
6
9
---

Open in Playground Standalone Page

#0058 · Length of Last Word

Easy String

The word is "   fly me   to   the moon  ". The final is the last position of word. The position is final. The letters is 0.
Repeat while position is at least 0 and character position of word is 32.
Set position to one less than position.

Repeat while position is at least 0 and character position of word isn't equal to 32.
Add 1 to letters. Set position to one less than position.

Display letters.

Expected stdout

4

Open in Playground Standalone Page

#0059 · Spiral Matrix II

Medium Array

The size is 3. The matrix is a list of 9 zeros. The visited is a list of 9 zeros. The row is 0. The column is 0. The down is 0. The right is 1. The counter is 1.
For every step from 0 to 8.
Set item (row times size plus column) of matrix to counter. Set item (row times size plus column) of visited to 1. Add 1 to counter. The nextrow is row plus down. The nextcolumn is column plus right. The blocked is 0. The olddown is 0. If nextrow is less than 0 or nextrow is at least size or nextcolumn is less than 0 or nextcolumn is at least size, set blocked to 1. If blocked is 0 and item (nextrow times size plus nextcolumn) of visited is 1, set blocked to 1. If blocked is 1, set olddown to down. If blocked is 1, set down to right. If blocked is 1, set right to 0 minus olddown. Increase row by down. Increase column by right.

For each value in matrix, report value.

Expected stdout

1
2
3
8
9
4
7
6
5

Open in Playground Standalone Page

#0060 · Permutation Sequence

Hard Math

The size is 3. The rank is 3. Decrease rank by 1. The available is an empty list of whole numbers.
For every digit from 1 to size.
Place digit into available.

The block is 1.
For every step from 2 to size minus 1.
Set block to block times step.

The result is empty text. The last is size minus 1.
For every position from 0 to last.
The index is rank divided by block. Set rank to rank modulo block. The chosen is item index of available. Remove item index of available. Place the character for chosen into result. If position is less than last, set block to block divided by (last minus position).

Show result.

Expected stdout

213

Open in Playground Standalone Page

#0061 · Rotate List

Medium Two Pointers

The values is a list of 1, 2, 3, 4, and 5. The last is the last position of values. The size is last plus 1. The target is 2. The offset is target modulo size. The result is an empty list of whole numbers.
For every position from 0 to last.
The source is position plus size minus offset. If source is at least size, set source to source minus size. Place item source of values into result.

For each value in result, display value.

Expected stdout

4
5
1
2
3

Open in Playground Standalone Page

#0062 · Unique Paths

Medium Math

The rows is 3. The columns is 7. The size is the product of rows and columns. The table is a list of size zeros.
For every position from 0 to one less than size.
The row is position divided by columns. The column is position modulo columns. If row is 0 or column is 0, set item position of table to 1. If row is more than 0 and column is more than 0, set item position of table to item (position minus columns) of table plus item (position minus 1) of table.

The last is one less than size. Report item last of table.

Expected stdout

28

Open in Playground Standalone Page

#0063 · Unique Paths II

Medium Array

The grid is a list of 0, 0, 0, 0, 1, 0, 0, 0, and 0. The columns is 3. The size is the number of items in grid. The table is a list of size zeros.
For every position from 0 to one less than size.
The row is position divided by columns. The column is position modulo columns. If item position of grid is 1, set item position of table to 0. If row is 0 and column is 0 and item position of grid is 0, set item position of table to 1. If row is 0 and column is more than 0 and item position of grid is 0, set item position of table to item (position minus 1) of table. If row is more than 0 and column is 0 and item position of grid is 0, set item position of table to item (position minus columns) of table. If row is more than 0 and column is more than 0 and item position of grid is 0, set item position of table to item (position minus 1) of table plus item (position minus columns) of table.

The last is one less than size. Show item last of table.

Expected stdout

2

Open in Playground Standalone Page

#0064 · Minimum Path Sum

Medium Array

The grid is a list of 1, 3, 1, 1, 5, 1, 4, 2, and 1. The columns is 3. The size is the number of items in grid. The table is a list of size zeros. Set item 0 of table to item 0 of grid.
For every position from 1 to one less than size.
The row is position divided by columns. The column is position modulo columns. If row is 0, set item position of table to item (position minus 1) of table plus item position of grid. If row is more than 0 and column is 0, set item position of table to item (position minus columns) of table plus item position of grid. If row is more than 0 and column is more than 0, The above is item (position minus columns) of table, The previous is item (position minus 1) of table, The best path is the smaller of above and previous, set item position of table to best path plus item position of grid.

The last is one less than size. Display item last of table.

Expected stdout

7

Open in Playground Standalone Page

#0065 · Valid Number

Hard String

The text is "2e10". The last is the last position of text. The position is 0. The digits is 0. The expcount is 0. The exponentseen is 0. The valid is 1.
Repeat while position is at most last.
If character position of text is not 32, break. Add 1 to position.

If position is at most last and (character position of text is 43 or character position of text is 45), add 1 to position.
Repeat while position is at most last and character position of text is between "0" and "9".
Add 1 to digits. Add 1 to position.

If position is at most last and character position of text is 46.
Add 1 to position.
Repeat while position is at most last and character position of text is between "0" and "9".
Add 1 to digits. Add 1 to position.

If position is at most last and (character position of text is 101 or character position of text is 69).
Set exponentseen to 1. Add 1 to position. If position is at most last and (character position of text is 43 or character position of text is 45), add 1 to position.
Repeat while position is at most last and character position of text is between "0" and "9".
Add 1 to expcount. Add 1 to position.

If exponentseen is 1 and expcount is 0, set valid to 0.
Repeat while position is at most last.
If character position of text is not 32, set valid to 0, break. Add 1 to position.

If digits is 0, set valid to 0. If valid is 1, report true. Otherwise, report false.

Expected stdout

true

Open in Playground Standalone Page

#0066 · Plus One

Easy Array

The digits is a list of 1, 2, and 3. The position is the last position of digits. The carry is 1.
Repeat while position is at least 0 and carry is 1.
The sum is item position of digits plus carry. Set item position of digits to sum modulo 10. Set carry to sum divided by 10. Set position to one less than position.

For each digit in digits, show digit.

Expected stdout

1
2
4

Open in Playground Standalone Page

#0067 · Add Binary

Easy String

The source is "11". The target is "1". The lastsource is the last position of source. The lasttarget is the last position of target. The position is lastsource. The mark is lasttarget. The carry is 0. The reversedresult is empty text.
Repeat while position is at least 0 or mark is at least 0 or carry is 1.
The sum is carry. The digit is 0. If position is at least 0, set digit to the digit value of character position of source. If position is at least 0, add digit to sum. If position is at least 0, set position to one less than position. If mark is at least 0, set digit to the digit value of character mark of target. If mark is at least 0, add digit to sum. If mark is at least 0, set mark to one less than mark. The bit is the character for sum modulo 2. Place bit into reversedresult. Set carry to sum divided by 2.

The result is empty text. The reversedlast is the last position of reversedresult. For every step from 0 to reversedlast, The number is reversedlast minus step, Place character number of reversedresult into result. Display result.

Expected stdout

100

Open in Playground Standalone Page

#0068 · Text Justification

Hard Array

The source is "This,is,an,example,of,text,justification.". The pieces is split source by ",". Keep a shared words as an empty list of text.
For each piece in pieces.
Place piece into words.

Keep a shared count of 7. Keep a shared width of 16. Keep a shared finish of 0. Keep a shared usedwidth of 0. Keep a shared padding as empty text. Keep a shared piece as empty text. Keep a shared padpiece as empty text. Set padding to 32 spaces.

To search from position (a whole number) with occupied (a whole number). If position is count, set finish to position, return. The word is item position of words. The wordlast is the last position of word. The nextusedwidth is occupied plus 2 plus wordlast. The previousused is occupied. Set usedwidth to nextusedwidth. If nextusedwidth is at most width, search from position plus 1 with nextusedwidth, return. Set usedwidth to previousused. Set finish to position.

To build line from start (a whole number). Set finish to 0. The firstword is item start of words. The firstlast is the last position of firstword. Set usedwidth to firstlast plus 1. Search from start plus 1 with usedwidth. The linewords is finish minus start. The gaps is linewords minus 1. The base is 0. The extra is 0. If finish is less than count and gaps is more than 0, set base to (width minus usedwidth plus gaps) divided by gaps, set extra to (width minus usedwidth plus gaps) modulo gaps. The line is empty text.
For every index from start to finish minus 1.
The word is item index of words. Place word into line. The gap is index minus start. The amount is base. If gap is less than extra, add 1 to amount. Set piece to the slice of padding from 0 to amount. If index is less than finish minus 1 and finish is count, Place " " into line. If index is less than finish minus 1 and finish is less than count, Place piece into line. If index is finish minus 1 and finish is count, set padpiece to the slice of padding from 0 to width minus usedwidth plus gaps, Place padpiece into line, report line. If index is finish minus 1 and finish is less than count, report line, build line from finish.

Build line from 0.

Expected stdout

This    is    an
example  of text
justification.

Open in Playground Standalone Page

#0069 · Sqrt(x)

Easy Math

The target is 8. The low is 0. The high is target. The answer is 0.
Repeat while low is at most high.
The middle is the midpoint of low and high. If middle times middle is at most target, set answer to middle, set low to one more than middle, otherwise set high to one less than middle.

Show answer.

Expected stdout

2

Open in Playground Standalone Page

#0070 · Climbing Stairs

Easy Math

The target is 5. The size is target plus 1. The ways is a list of size zeros. Set item 0 of ways to 1.
For every position from 1 to target.
If position is 1, set item position of ways to 1. If position is more than 1, set item position of ways to item (position minus 1) of ways plus item (position minus 2) of ways.

Display item target of ways.

Expected stdout

8

Open in Playground Standalone Page

#0071 · Simplify Path

Medium String

The path is empty text. Place "/home//foo/" into path. The parts is split path by "/". The stack is an empty list of text.
For each part in parts.
If part is "", skip to the next one. If part is ".", skip to the next one. The size is the number of items in stack. The parentstep is 0. If part is "..", set parentstep to 1. If parentstep is 1 and size is more than 0, remove the last item from stack. If parentstep is 1 and size is 0, skip to the next one. The keep is 1. If part is "", set keep to 0. If part is ".", set keep to 0. If parentstep is 1, set keep to 0. If keep is 1, Place part into stack.

The joined is join stack with "/". The result is empty text. Place "/" into result. Place joined into result. Report result.

Expected stdout

/home/foo

Open in Playground Standalone Page

#0072 · Edit Distance

Medium String

The first is "horse". The second is "ros". The lastfirst is the last position of first. The lastsecond is the last position of second. The rows is lastfirst plus 1. The columns is lastsecond plus 1. The width is columns plus 1. The height is rows plus 1. The size is width times height. The table is a list of size zeros.
For every column from 0 to lastsecond.
Set item column of table to column.

For every row from 1 to rows.
Set item (row times width) of table to row.

For every row from 1 to rows.
For every column from 1 to columns.
The index is row times width plus column. If character (row minus 1) of first is character (column minus 1) of second, set item index of table to item (index minus width minus 1) of table. Otherwise, The removal is item (index minus width) of table, The insertion is item (index minus 1) of table, The replacement is item (index minus width minus 1) of table, The best is the smaller of removal and insertion, The final best is the smaller of best and replacement, set item index of table to final best plus 1.

The last is one less than size. Show item last of table.

Expected stdout

3

Open in Playground Standalone Page

#0073 · Set Matrix Zeroes

Medium Array

The matrix is a list of 1, 1, 1, 1, 0, 1, 1, 1, and 1. The rows is 3. The columns is 3. The lastrow is rows minus 1. The lastcolumn is columns minus 1. The rowmarks is an empty list of whole numbers. The colmarks is an empty list of whole numbers.
For every row from 0 to lastrow.
For every column from 0 to lastcolumn.
If item (row times columns plus column) of matrix is 0, Place row into rowmarks. If item (row times columns plus column) of matrix is 0, Place column into colmarks.

For each row in rowmarks.
For every column from 0 to lastcolumn.
Set item (row times columns plus column) of matrix to 0.

For each column in colmarks.
For every row from 0 to lastrow.
Set item (row times columns plus column) of matrix to 0.

For each value in matrix, display value.

Expected stdout

1
0
1
0
0
0
1
0
1

Open in Playground Standalone Page

#0074 · Search a 2D Matrix

Medium Array

The matrix is a list of 1, 3, 5, 7, 10, 11, 16, 20, 23, 30, 34, 50. The target is 3. The found is -1.
For every position from 0 to the last position of matrix.
If item position of matrix is target, set found to position, then stop.

If found is not equal to -1, report true, otherwise report false.

Expected stdout

true

Open in Playground Standalone Page

#0075 · Sort Colors

Medium Array

The colors is a list of 2, 0, 2, 1, 1, and 0. Sort colors. For each color in colors, show color.

Expected stdout

0
0
1
1
2
2

Open in Playground Standalone Page

#0076 · Minimum Window Substring

Hard String

The source is "ADOBECODEBANC". The target is "ABC". The sourcelast is the last position of source. The targetlast is the last position of target. The need is an empty map.
For every position from 0 to targetlast.
The symbol is character position of target. Add 1 to entry symbol of need.

The window is an empty map. The required is the number of entries in need. The formed is 0. The left is 0. The beststart is 0. The bestlength is 1000000.
For every right from 0 to sourcelast.
The symbol is character right of source. Add 1 to entry symbol of window. If need has entry symbol and entry symbol of window is entry symbol of need, add 1 to formed.
Repeat while formed is required.
The width is right minus left plus 1. If width is less than bestlength, set beststart to left, set bestlength to width. The mark is character left of source. Subtract 1 from entry mark of window. If need has entry mark and entry mark of window is less than entry mark of need, subtract 1 from formed. Add 1 to left.

The finish is beststart plus bestlength. The answer is the slice of source from beststart to finish. Display answer.

Expected stdout

BANC

Open in Playground Standalone Page

#0077 · Combinations

Medium Backtracking

Keep a shared size of 4. Keep a shared choose of 2. Keep a shared parts as an empty list of text. Keep a shared answers as an empty list of text.
To combine from index (a whole number) with depth (a whole number). If depth is choose, The built is join parts with ",", Place built into answers, then return. If index is more than size, return.
For every value from index to size.
The piece is format whole number value as text. Place piece into parts. Combine from value plus 1 with depth plus 1. Remove the last item from parts.

Combine from 1 with 0. For each answer in answers, report answer.

Expected stdout

1,2
1,3
1,4
2,3
2,4
3,4

Open in Playground Standalone Page

#0078 · Subsets

Medium Array

Keep a shared nums holding 1, 2, and 3. Keep a shared parts as an empty list of text. Keep a shared answers as an empty list of text. Keep a shared marker as empty text. Keep a shared built as empty text. Keep a shared piece as empty text. Keep a shared partcount of 0. Place "()" into marker.
To collect from index (a whole number). Set built to join parts with ",". If partcount is 0, set built to marker. Place built into answers.
For every position from index to 2.
Set piece to format whole number item position of nums as text. Place piece into parts. Add 1 to partcount. Collect from position plus 1. Remove the last item from parts. Subtract 1 from partcount.

Collect from 0. For each answer in answers, show answer.

Expected stdout

()
1
1,2
1,2,3
1,3
2
2,3
3

Open in Playground Standalone Page

#0079 · Word Search

Medium Array

Keep a shared board holding 65, 66, 67, 69, 83, 70, 67, 83, 65, 68, 69, and 69. Keep a shared word as empty text. Place "ABCCED" into word. Keep a shared visited as an empty list of whole numbers.
For every cell from 0 to 11.
Place 0 into visited.

Keep a shared found of 0.
To search from row (a whole number) at column (a whole number) with index (a whole number). If found is 1, return. If row is less than 0 or row is more than 2 or column is less than 0 or column is more than 3, return. If item (row times 4 plus column) of visited is 1, return. If item (row times 4 plus column) of board is not character index of word, return. If index is 3, set found to 1, return. Set item (row times 4 plus column) of visited to 1. If found is 0, search from row plus 1 at column with index plus 1. If found is 0, search from row minus 1 at column with index plus 1. If found is 0, search from row at column plus 1 with index plus 1. If found is 0, search from row at column minus 1 with index plus 1. Set item (row times 4 plus column) of visited to 0.

Search from 0 at 0 with 0. If found is 1, display true. Otherwise, display false.

Expected stdout

true

Open in Playground Standalone Page

#0080 · Remove Duplicates from Sorted Array II

Medium Array

The nums is a list of 1, 1, 1, 2, 2, and 3. The result is an empty list of whole numbers.
For each number in nums.
The occurrences is 0. For every existing in result, if existing is number, add 1 to occurrences. If occurrences is less than 2, Place number into result.

Report the number of items in result. For each number in result, report number.

Expected stdout

5
1
1
2
2
3

Open in Playground Standalone Page

#0081 · Search in Rotated Sorted Array II

Medium Array

The nums is a list of 2, 5, 6, 0, 0, 1, and 2. The target is 0. The found is -1.
For every position from 0 to the last position of nums.
If item position of nums is target, set found to position, then stop.

If found is not equal to -1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0082 · Remove Duplicates from Sorted List II

Medium Two Pointers

The values is a list of 1, 2, 3, 3, 4, 4, and 5. The counts is an empty map.
For each value in values.
Add 1 to entry value of counts.

The result is an empty list of whole numbers.
For each value in values.
If entry value of counts is 1, Place value into result.

For each value in result, display value.

Expected stdout

1
2
5

Open in Playground Standalone Page

#0083 · Remove Duplicates from Sorted List

Easy Linked List

The values is a list of 1, 1, 2. The result is an empty list of whole numbers. The resultcount is 0.
For each value in values.
If resultcount is 0 or item (resultcount minus 1) of result is not value, Place value into result, add 1 to resultcount.

For each value in result, report value.

Expected stdout

1
2

Open in Playground Standalone Page

#0084 · Largest Rectangle in Histogram

Hard Array

The heights is a list of 2, 1, 5, 6, 2, and 3. The last is the last position of heights. The best is 0.
For every left from 0 to last.
The minimum is item left of heights.
For every right from left to last.
If item right of heights is less than minimum, set minimum to item right of heights. The area is minimum times (right minus left plus 1). If area is more than best, set best to area.

Show best.

Expected stdout

10

Open in Playground Standalone Page

#0085 · Maximal Rectangle

Hard Array

The matrix is a list of 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, and 0. The rows is 4. The columns is 5. The width is columns plus 1. The height is rows plus 1. The size is width times height. The prefix is a list of size zeros.
For every row from 1 to rows.
For every column from 1 to columns.
The index is row times width plus column. The cellrow is row minus 1. The cellindex is cellrow times columns plus column minus 1. Set item index of prefix to item (index minus width) of prefix plus item (index minus 1) of prefix minus item (index minus width minus 1) of prefix plus item cellindex of matrix.

The best is 0.
For every top from 0 to one less than rows.
For every bottom from top to one less than rows.
For every left from 0 to one less than columns.
For every right from left to one less than columns.
The area is (bottom minus top plus 1) times (right minus left plus 1). The ones is item ((bottom plus 1) times width plus right plus 1) of prefix minus item (top times width plus right plus 1) of prefix minus item ((bottom plus 1) times width plus left) of prefix plus item (top times width plus left) of prefix. If ones is area and area is more than best, set best to area.

Display best.

Expected stdout

6

Open in Playground Standalone Page

#0086 · Partition List

Medium Two Pointers

The values is a list of 1, 4, 3, 2, 5, and 2. The pivot is 3. The lower is an empty list of whole numbers. The upper is an empty list of whole numbers.
For each value in values.
If value is less than pivot, Place value into lower. If value is at least pivot, Place value into upper.

For each value in upper.
Place value into lower.

For each value in lower, report value.

Expected stdout

1
2
2
4
3
5

Open in Playground Standalone Page

#0087 · Scramble String

Hard String

The firsttext is "great". The secondtext is "rgeat". The size is 5. The width is 6. The table is a list of 150 zeros.
For every first from 0 to 4.
For every second from 0 to 4.
If character first of firsttext is character second of secondtext, set item ((first times 5 plus second) times 6 plus 1) of table to 1.

For every length from 2 to 5.
For every first from 0 to (5 minus length).
For every second from 0 to (5 minus length).
For every split from 1 to length minus 1.
The base is (first times 5 plus second) times 6. The splitindex is base plus split. The noother is (first plus split) times 5 plus second plus split. The nootherindex is noother times 6 plus length minus split. The swapother is first times 5 plus second plus length minus split. The swapotherindex is swapother times 6 plus split. The swapbase is (first plus split) times 5 plus second. The swapbaseindex is swapbase times 6 plus length minus split. The noswap is 0. If item splitindex of table is 1 and item nootherindex of table is 1, set noswap to 1. The withswap is 0. If item swapotherindex of table is 1 and item swapbaseindex of table is 1, set withswap to 1. The fullindex is base plus length. If noswap is 1 or withswap is 1, set item fullindex of table to 1.

If item 5 of table is 1, show true. Otherwise, show false.

Expected stdout

true

Open in Playground Standalone Page

#0088 · Merge Sorted Array

Easy Array

The first is a list of 1, 2, and 3. The second is a list of 2, 5, and 6. The combined is an empty list of whole numbers. For each number in first, Place number into combined. For each number in second, Place number into combined. Sort combined. For each number in combined, display number.

Expected stdout

1
2
2
3
5
6

Open in Playground Standalone Page

#0089 · Gray Code

Medium Math

The count is 4. The codes is an empty list of whole numbers.
For every value from 0 to one less than count.
The shifted is shift value right by 1. The code is exclusive or of value and shifted. Place code into codes.

For each code in codes, report code.

Expected stdout

0
1
3
2

Open in Playground Standalone Page

#0090 · Subsets II

Medium Array

Keep a shared nums holding 1, 2, and 2. Keep a shared parts as an empty list of text. Keep a shared answers as an empty list of text. Keep a shared marker as empty text. Keep a shared built as empty text. Keep a shared piece as empty text. Keep a shared partcount of 0. Place "()" into marker.
To collect from index (a whole number). Set built to join parts with ",". If partcount is 0, set built to marker. Place built into answers.
For every position from index to 2.
If position is more than index and item position of nums is item (position minus 1) of nums, skip to the next one. Set piece to format whole number item position of nums as text. Place piece into parts. Add 1 to partcount. Collect from position plus 1. Remove the last item from parts. Subtract 1 from partcount.

Collect from 0. For each answer in answers, show answer.

Expected stdout

()
1
1,2
1,2,2
2
2,2

Open in Playground Standalone Page

#0091 · Decode Ways

Medium String

The word is "226". The size is the number of characters in word. The ways is a list of size plus 1 zeros. Set item 0 of ways to 1.
For every position from 1 to size.
The previous is one less than position. The firstdigit is the digit value of character previous of word. If firstdigit is more than 0, set item position of ways to item previous of ways. The seconddigit is 0. If position is more than 1, set seconddigit to the digit value of character (position minus 2) of word. The pair is seconddigit times 10 plus firstdigit. If position is more than 1 and pair is at least 10 and pair is at most 26, set item position of ways to item position of ways plus item (position minus 2) of ways.

Display item size of ways.

Expected stdout

3

Open in Playground Standalone Page

#0092 · Reverse Linked List II

Medium Linked List

The values is a list of 1, 2, 3, 4, and 5. The leftedge is 2. The rightedge is 4. The left is leftedge minus 1. The right is rightedge minus 1. Repeat while left is less than right, swap item left of values and item right of values, add 1 to left, subtract 1 from right.

For each value in values, report value.

Expected stdout

1
4
3
2
5

Open in Playground Standalone Page

#0093 · Restore IP Addresses

Medium String

Keep a shared text as empty text. Place "25525511135" into text. Keep a shared parts as an empty list of text. Keep a shared answers as an empty list of text. Keep a shared piece as empty text. Keep a shared partcount of 0. Keep a shared text length as 0. Set text length to 11.
To save piece from start (a whole number) to finish (a whole number). Set piece to the slice of text from start to finish. Place piece into parts. Add 1 to partcount. Restore from finish. Remove the last item from parts. Subtract 1 from partcount.

To restore from position (a whole number). If partcount is 4 and position is text length, The built is join parts with ".", Place built into answers, then return. If partcount is 4, return. If position is text length, return. The first is the digit value of character position of text. The second is 0. If position plus 1 is at most text length, set second to the digit value of character (position plus 1) of text. The third is 0. If position plus 2 is at most text length, set third to the digit value of character (position plus 2) of text. If position is less than text length, save piece from position to position plus 1. If position plus 1 is at most text length and first is not 0, save piece from position to position plus 2. If position plus 2 is at most text length and first is not 0 and first times 100 plus second times 10 plus third is at most 255, save piece from position to position plus 3.

Restore from 0. For each answer in answers, show answer.

Expected stdout

255.255.11.135
255.255.111.35

Open in Playground Standalone Page

#0094 · Binary Tree Inorder Traversal

Easy Depth-First Search

Keep a shared values holding 1, 2, and 3. Keep a shared lefts holding -1, 2, and -1. Keep a shared rights holding 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 node into order. Gather from item node of rights.

Gather from 0. For each node in order, display item node of values.

Expected stdout

1
3
2

Open in Playground Standalone Page

#0095 · Unique Binary Search Trees II

Medium Dynamic Programming

Keep a shared used as an empty list of whole numbers. Keep a shared prefix as empty text. Keep a shared answers as an empty list of text. Keep a shared copy as empty text.
To check permutation. The root is the digit value of character 0 of prefix. The second is the digit value of character 1 of prefix. The third is the digit value of character 2 of prefix. The okay is 1. If second is more than root and third is less than root, set okay to 0. If okay is 1, Set copy to a copy of prefix, Place copy into answers.

To permute from depth (a whole number). If depth is 3, check permutation, then return.
For every value from 1 to 3.
If used contains value, skip to the next one. Place value into used. Place the character for value into prefix. Permute from depth plus 1. Remove the last item from used. Remove last character from prefix.

Permute from 0. Report the length of answers. For each answer in answers, report answer.

Expected stdout

5
123
132
213
312
321

Open in Playground Standalone Page

#0096 · Unique Binary Search Trees

Medium Math

The target is 5. The size is target plus 1. The ways is a list of size zeros. Set item 0 of ways to 1.
For every count from 1 to target.
For every split from 0 to one less than count.
The before is item split of ways. The afterposition is count minus 1 minus split. The after is item afterposition of ways. Increase item count of ways by before times after.

Show item target of ways.

Expected stdout

42

Open in Playground Standalone Page

#0097 · Interleaving String

Medium String

The first is "aabcc". The second is "dbbca". The third is "aadbbcbcac". The firstsize is the number of characters in first. The secondsize is the number of characters in second. The width is secondsize plus 1. The height is firstsize plus 1. The size is width times height. The table is a list of size zeros. Set item 0 of table to 1.
For every row from 1 to firstsize.
If character (row minus 1) of first is character (row minus 1) of third and item ((row minus 1) times width) of table is 1, set item (row times width) of table to 1.

For every column from 1 to secondsize.
If character (column minus 1) of second is character (column minus 1) of third and item (column minus 1) of table is 1, set item column of table to 1.

For every row from 1 to firstsize.
For every column from 1 to secondsize.
The index is row times width plus column. The spot is row plus column minus 1. If character (row minus 1) of first is character spot of third and item (index minus width) of table is 1, set item index of table to 1. If character (column minus 1) of second is character spot of third and item (index minus 1) of table is 1, set item index of table to 1.

The last is one less than size. If item last of table is 1, display true. Otherwise, display false.

Expected stdout

true

Open in Playground Standalone Page

#0098 · Validate Binary Search Tree

Medium Depth-First Search

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

Gather from 0. The valid is 1. The last is order count minus 1.
For every position from 1 to last.
The before is item (position minus 1) of order. The current is item position of order. If item before of values is at least item current of values, set valid to 0.

If valid is 1, report true. Otherwise, report false.

Expected stdout

true

Open in Playground Standalone Page

#0099 · Recover Binary Search Tree

Medium Depth-First Search

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

Gather from 0. The firstbad is -1. The secondbad is -1. The last is order count minus 1.
For every position from 1 to last.
The before is item (position minus 1) of order. The current is item position of order. If item before of values is more than item current of values and firstbad is -1, set firstbad to before. If item before of values is more than item current of values, set secondbad to current.

Swap item firstbad of values and item secondbad of values. For each node in order, show item node of values.

Expected stdout

1
2
3
4

Open in Playground Standalone Page

#0100 · Same Tree

Easy Depth-First Search

Keep a shared valuesone holding 1, 2, and 3. Keep a shared leftone holding 1, -1, and -1. Keep a shared rightone holding 2, -1, and -1. Keep a shared valuestwo holding 1, 2, and 3. Keep a shared lefttwo holding 1, -1, and -1. Keep a shared righttwo holding 2, -1, and -1. Keep a shared same of 1.
To compare at first (a whole number) with second (a whole number). If same is 0, return. If first is -1 and second is -1, return. If first is -1, set same to 0, return. If second is -1, set same to 0, return. If item first of valuesone is not item second of valuestwo, set same to 0, return. Compare at item first of leftone with item second of lefttwo. Compare at item first of rightone with item second of righttwo.

Compare at 0 with 0. If same is 1, display true. Otherwise, display false.

Expected stdout

true

Open in Playground Standalone Page