The TransCompilator

HTML chunk · 50 solved programs

Chunk 01 · #0001–#0050

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

#0001 · Two Sum

Easy Array

Start with the list of numbers 2, 7, 11, and 15. The target is 9.
For every first from 0 to the last position of numbers.
For every second from one more than first to the last position of numbers, if item first of numbers plus item second of numbers is target, display first, display second, then stop.

Expected stdout

0
1

Open in Playground Standalone Page

#0002 · Add Two Numbers

Medium Math

The firstlist holds 2, 4, and 3, while the secondlist holds 5, 6, and 4. Start with an empty list for the answer, a carry of 0, and a position of 0. The firstlast is the last position of firstlist. The secondlast is the last position of secondlist.
Repeat while position is at most firstlast or position is at most secondlast or carry is 1.
The sum is carry. If position is at most firstlast, set sum to sum plus item position of firstlist. If position is at most secondlast, set sum to sum plus item position of secondlist. Place sum modulo 10 into answer. Set carry to sum divided by 10. Add 1 to position.

For each value in answer, show value.

Expected stdout

7
0
8

Open in Playground Standalone Page

#0003 · Longest Substring Without Repeating Characters

Medium String

The word is "abcabcbb". The last is the last position of word. The best is 0.
For every start from 0 to last.
For every finish from start to last.
The repeated is 0. For every earlier from start to one less than finish, if character earlier of word is character finish of word, add 1 to repeated. The span is finish minus start plus 1. If repeated is 0 and span is greater than best, set best to span.

Show best.

Expected stdout

3

Open in Playground Standalone Page

#0004 · Median of Two Sorted Arrays

Hard Array

The first is a list of 1 and 3. The second is a list of 2. 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. The middle is the midpoint of 0 and the last position of combined. Display item middle of combined.

Expected stdout

2

Open in Playground Standalone Page

#0005 · Longest Palindromic Substring

Medium String

The word is "babad". The last is the last position of word. The best is empty text.
For every start from 0 to last.
For every finish from start to last.
The piece is the slice of word from start to one more than finish. The reversed is empty text. For every step from 0 to finish minus start, The position is finish minus step, Place character position of word into reversed. The piecelength is the length of piece. The bestlength is the length of best. If piece is reversed, if piecelength is greater than bestlength, set best to piece.

Report best.

Expected stdout

bab

Open in Playground Standalone Page

#0006 · Zigzag Conversion

Medium String

The source is "PAYPALISHIRING". The rows is 3. The lastrow is rows minus 1. The store is an empty list of text.
For every row from 0 to lastrow.
Place "" into store.

The row is 0. The step is 1. The sourcelast is the last position of source.
For every position from 0 to sourcelast.
Place character position of source into item row of store. If row is 0 and lastrow is more than 0, set step to 1. If row is lastrow and lastrow is more than 0, set step to 0 minus 1. Increase row by step.

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

Expected stdout

PAHNAPLSIIGYIR

Open in Playground Standalone Page

#0007 · Reverse Integer

Medium Math

Start with the numbers 123, -123, 120, and 1534236469.
For each value in numbers.
The sign is 1. The magnitude is value. If magnitude is less than 0, set sign to -1, set magnitude to 0 minus magnitude. The reversed is 0. Repeat while magnitude is more than 0, set digit to magnitude modulo 10, set reversed to reversed times 10 plus digit, set magnitude to magnitude divided by 10. If reversed is more than 2147483647, display 0, otherwise display reversed times sign.

Expected stdout

321
-321
21
0

Open in Playground Standalone Page

#0008 · String to Integer (atoi)

Medium String

The word is "   -42". The final is one less than the number of characters in word. The position is 0. The value is 0. The sign is 1.
Repeat while position is at most final and character position of word is 32.
Add 1 to position.

If position is at most final and character position of word is 45, set sign to -1, add 1 to position. If position is at most final and character position of word is 43, add 1 to position.
Repeat while position is at most final and character position of word is between "0" and "9".
The digit is the digit value of character position of word. Set value to value times 10 plus digit. Add 1 to position.

If sign is -1, set value to 0 minus value. If value is more than 2147483647, set value to 2147483647. If value is less than -2147483648, set value to -2147483648. Report value.

Expected stdout

-42

Open in Playground Standalone Page

#0009 · Palindrome Number

Easy Math

The value is 121. The reversed is 0. The magnitude is value.
Repeat while magnitude is more than 0.
Set digit to magnitude modulo 10. Set reversed to reversed times 10 plus digit. Set magnitude to magnitude divided by 10.

If value is less than 0, show false. Otherwise, show reversed is value.

Expected stdout

true

Open in Playground Standalone Page

#0010 · Regular Expression Matching

Hard String

The text is "aab". The pattern is "c*a*b". The textsize is the number of characters in text. The patternsize is the number of characters in pattern. The width is patternsize plus 1. The height is textsize 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 column from 2 to patternsize.
If character (column minus 1) of pattern is 42 and item (column minus 2) of table is 1, set item column of table to 1.

For every row from 1 to textsize.
For every column from 1 to patternsize.
The index is row times width plus column. The symbol is character (column minus 1) of pattern. If symbol is not 42 and symbol is 46 and item (index minus width minus 1) of table is 1, set item index of table to 1. If symbol is not 42 and symbol is character (row minus 1) of text and item (index minus width minus 1) of table is 1, set item index of table to 1. If symbol is 42 and column is more than 1 and item (index minus 2) of table is 1, set item index of table to 1. If symbol is 42 and column is more than 1 and character (column minus 2) of pattern is 46 and item (index minus width) of table is 1, set item index of table to 1. If symbol is 42 and column is more than 1 and character (column minus 2) of pattern is character (row minus 1) of text and item (index minus width) 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

#0011 · Container With Most Water

Medium Array

The heights is a list of 1, 8, 6, 2, 5, 4, 8, 3, and 7. The bestarea is 0.
For every first from 0 to the last position of heights.
For every second from one more than first to the last position of heights.
The width is second minus first. The lowheight is the smaller of item first of heights and item second of heights. The area is the product of width and lowheight. If area is greater than bestarea, set bestarea to area.

Report bestarea.

Expected stdout

49

Open in Playground Standalone Page

#0012 · Integer to Roman

Medium String

The value is 1994. The result is empty text. Repeat while value is at least 1000, Place "M" into result, set value to value minus 1000. If value is at least 900, Place "CM" into result, set value to value minus 900. If value is at least 500, Place "D" into result, set value to value minus 500. If value is at least 400, Place "CD" into result, set value to value minus 400. Repeat while value is at least 100, Place "C" into result, set value to value minus 100. If value is at least 90, Place "XC" into result, set value to value minus 90. If value is at least 50, Place "L" into result, set value to value minus 50. If value is at least 40, Place "XL" into result, set value to value minus 40. Repeat while value is at least 10, Place "X" into result, set value to value minus 10. If value is at least 9, Place "IX" into result, set value to value minus 9. If value is at least 5, Place "V" into result, set value to value minus 5. If value is at least 4, Place "IV" into result, set value to value minus 4. Repeat while value is at least 1, Place "I" into result, set value to value minus 1. Show result.

Expected stdout

MCMXCIV

Open in Playground Standalone Page

#0013 · Roman to Integer

Easy String

The word is "MCMXCIV". The final is one less than the number of characters in word. The total is 0.
For every position from 0 to final.
The current is 0. The following is 0. The symbol is character position of word. If symbol is 77, set current to 1000. If symbol is 68, set current to 500. If symbol is 67, set current to 100. If symbol is 76, set current to 50. If symbol is 88, set current to 10. If symbol is 86, set current to 5. If symbol is 73, set current to 1. The nextposition is one more than position. If position is less than final, set following to character nextposition of word. If following is 77, set following to 1000. If following is 68, set following to 500. If following is 67, set following to 100. If following is 76, set following to 50. If following is 88, set following to 10. If following is 86, set following to 5. If following is 73, set following to 1. If current is less than following, set total to total minus current, otherwise set total to total plus current.

Display total.

Expected stdout

1994

Open in Playground Standalone Page

#0014 · Longest Common Prefix

Easy Array

The words is a list of "flower", "flow", and "flight". The reference is item 0 of words. The limit is the number of characters in reference.
For each word in words.
The size is the number of characters in word. If size is less than limit, set limit to size.

The count is the number of items in words. The prefix is empty text. The last is limit minus 1.
For every position from 0 to last.
The matches is 0. For each word in words, if character position of word is character position of reference, add 1 to matches. If matches is count, Place character position of reference into prefix, otherwise stop.

Report prefix.

Expected stdout

fl

Open in Playground Standalone Page

#0015 · 3Sum

Medium Array

The nums is a list of -1, 0, 1, and 2. Sort nums. The last is the last position of nums.
For every first from 0 to last.
The second is one more than first. The third is last.
Repeat while second is less than third.
The total is item first of nums plus item second of nums plus item third of nums. If total is less than 0, add 1 to second. If total is more than 0, subtract 1 from third. If total is 0, show item first of nums, show item second of nums, show item third of nums, show "---", add 1 to second, subtract 1 from third.

Expected stdout

-1
0
1
---

Open in Playground Standalone Page

#0016 · 3Sum Closest

Medium Array

The nums is a list of -1, 2, 1, and -4. The target is 1. Sort nums. The last is the last position of nums. The best is 0. The bestdistance is 2147483647.
For every first from 0 to last.
The second is one more than first. The third is last.
Repeat while second is less than third.
The total is item first of nums plus item second of nums plus item third of nums. The difference is total minus target. If difference is less than 0, set difference to 0 minus difference. If difference is less than bestdistance, set bestdistance to difference, set best to total. If total is less than target, add 1 to second. If total is more than target, subtract 1 from third. If total is target, add 1 to second, subtract 1 from third.

Display best.

Expected stdout

2

Open in Playground Standalone Page

#0017 · Letter Combinations of a Phone Number

Medium String

Keep a shared digits as empty text. Place "23" into digits. Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared current as empty text. Keep a shared answers as an empty list of text. Keep a shared copy as empty text.
To build from position (a whole number). If position is 2, The copy is a copy of current, Place copy into answers, then return. The digit is the digit value of character position of digits. The start is (digit minus 2) times 3.
For every offset from 0 to 2.
Place character (start plus offset) of alphabet into current. Build from position plus 1. Remove last character from current.

Build from 0. For each answer in answers, report answer.

Expected stdout

ad
ae
af
bd
be
bf
cd
ce
cf

Open in Playground Standalone Page

#0018 · 4Sum

Medium Array

The nums is a list of 1, 0, -1, and 2. The target is 2. Sort nums. The last is the last position of nums.
For every first from 0 to last.
For every second from one more than first to last.
For every third from one more than second to last.
For every fourth from one more than third to last.
The total is item first of nums plus item second of nums plus item third of nums plus item fourth of nums. If total is target, show item first of nums, show item second of nums, show item third of nums, show item fourth of nums, show "---".

Expected stdout

-1
0
1
2
---

Open in Playground Standalone Page

#0019 · Remove Nth Node From End of 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 position is last plus 1 minus 2. Remove item position of values. For each value in values, display value.

Expected stdout

1
2
3
5

Open in Playground Standalone Page

#0020 · Valid Parentheses

Easy String

The word is "()[]{}". The last is the last position of word. The stack is an empty list of whole numbers. The invalid is 0. The top is 0.
For every position from 0 to last.
The symbol is character position of word. The size is the number of items in stack. If symbol is "(" or symbol is "[" or symbol is "{", Place symbol into stack. If symbol is ")" and size is 0, add 1 to invalid. If symbol is ")" and size is more than 0, set top to item (size minus 1) of stack, remove the last item from stack, if top is not "(", add 1 to invalid. If symbol is "]" and size is 0, add 1 to invalid. If symbol is "]" and size is more than 0, set top to item (size minus 1) of stack, remove the last item from stack, if top is not "[", add 1 to invalid. If symbol is "}" and size is 0, add 1 to invalid. If symbol is "}" and size is more than 0, set top to item (size minus 1) of stack, remove the last item from stack, if top is not "{", add 1 to invalid.

The finalsize is the number of items in stack. If invalid is 0 and finalsize is 0, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0021 · Merge Two Sorted Lists

Easy Linked List

The first is a list of 1, 2, and 4. The second is a list of 1, 3, and 4. 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, show number.

Expected stdout

1
1
2
3
4
4

Open in Playground Standalone Page

#0022 · Generate Parentheses

Medium String

Keep a shared current as empty text. Keep a shared answers as an empty list of text. Keep a shared copy as empty text.
To build from opened (a whole number) with closed (a whole number). If opened plus closed is 6, The copy is a copy of current, Place copy into answers, then return. If opened is less than 3, Place "(" into current, build from opened plus 1 with closed, remove last character from current. If closed is less than opened, Place ")" into current, build from opened with closed plus 1, remove last character from current.

Build from 0 with 0. For each answer in answers, display answer.

Expected stdout

((()))
(()())
(())()
()(())
()()()

Open in Playground Standalone Page

#0023 · Merge k Sorted Lists

Hard Heap (Priority Queue)

The first is a list of 1, 4, and 5. The second is a list of 1, 3, and 4. The third is a list of 2 and 6. The merged is an empty list of whole numbers.
For each value in first.
Place value into merged.

For each value in second.
Place value into merged.

For each value in third.
Place value into merged.

Sort merged. For each value in merged, report value.

Expected stdout

1
1
2
3
4
4
5
6

Open in Playground Standalone Page

#0024 · Swap Nodes in Pairs

Medium Linked List

The values is a list of 1, 2, 3, and 4. The last is the last position of values.
For every position from 0 to last.
If position modulo 2 is 0 and position plus 1 is at most last, swap item position of values and item (position plus 1) of values.

For each value in values, show value.

Expected stdout

2
1
4
3

Open in Playground Standalone Page

#0025 · Reverse Nodes in k-Group

Hard Linked List

The values is a list of 1, 2, 3, 4, and 5. The groupsize is 2. The last is the last position of values. The start is 0.
Repeat while start plus groupsize minus 1 is at most last.
The left is start. The right is start plus groupsize 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. Increase start by groupsize.

For each value in values, display value.

Expected stdout

2
1
4
3
5

Open in Playground Standalone Page

#0026 · Remove Duplicates from Sorted Array

Easy Array

Start with the list of numbers 0, 0, 1, 1, 1, 2, 2, 3, 3, and 4. The unique is an empty list of whole numbers. For each number in the list, if unique does not contain the number, Place number into unique. Report the number of items in unique. For each number in unique, report number.

Expected stdout

5
0
1
2
3
4

Open in Playground Standalone Page

#0027 · Remove Element

Easy Array

Start with the list of numbers 3, 2, 2, and 3. Remove every 3 from the list. Show the number of items in the list. For each item in the list, show item.

Expected stdout

2
2
2

Open in Playground Standalone Page

#0028 · Find the Index of the First Occurrence in a String

Easy String

The word is "sadbutsad". The needle is "sad". The wordlength is the number of characters in word. The needlelength is the number of characters in needle. The final is wordlength minus needlelength. The answer is -1.
For every position from 0 to final.
The finish is position plus needlelength. The piece is the slice of word from position to finish. If piece is needle, set answer to position, display answer, then stop.

If answer is -1, display -1.

Expected stdout

0

Open in Playground Standalone Page

#0029 · Divide Two Integers

Medium Math

The top is 10. The bottom is 3. The quotient is 0.
Repeat while top is at least bottom.
Decrease top by bottom. Add 1 to quotient.

Report quotient.

Expected stdout

3

Open in Playground Standalone Page

#0030 · Substring with Concatenation of All Words

Hard String

Keep a shared text as empty text. Place "barfoothefoobarman" into text. Keep a shared words holding "foo" and "bar". Keep a shared word length as 0. Set word length to 3. Keep a shared word count as 0. Set word count to 2. Keep a shared used as an empty list of whole numbers. Keep a shared match of 0.
To verify from start (a whole number) at part (a whole number). If match is 1, return. If part is word count, set match to 1, return.
For every index from 0 to word count minus 1.
The offset is start plus part times word length. The piece is the slice of text from offset to offset plus word length. If used contains index, skip to the next one. If piece matches item index of words, Place index into used, verify from start at part plus 1, remove the last item from used, if match is 1, return.

For every start from 0 to 12.
Set match to 0. Verify from start at 0. If match is 1, show start.

Expected stdout

0
9

Open in Playground Standalone Page

#0031 · Next Permutation

Medium Array

The nums is a list of 1, 2, and 3. The last is the last position of nums. The pivot is -1. The position is last minus 1.
Repeat while position is at least 0.
If item position of nums is less than item (position plus 1) of nums, set pivot to position, break. Set position to one less than position.

The mark is last.
If pivot is at least 0.
Repeat while item mark of nums is at most item pivot of nums.
Set mark to one less than mark.

If pivot is at least 0, swap item pivot of nums and item mark of nums. The left is pivot plus 1. The right is last.
Repeat while left is less than right.
Swap item left of nums and item right of nums. Add 1 to left. Subtract 1 from right.

For each value in nums, display value.

Expected stdout

1
3
2

Open in Playground Standalone Page

#0032 · Longest Valid Parentheses

Hard String

The text is "(()". The last is the last position of text. The best is 0.
For every start from 0 to last.
The balance is 0.
For every end from start to last.
If character end of text is "(", add 1 to balance. If character end of text is ")", subtract 1 from balance. If balance is less than 0, break. If balance is 0, The width is end minus start plus 1, if width is more than best, set best to width.

Report best.

Expected stdout

2

Open in Playground Standalone Page

#0033 · Search in Rotated Sorted Array

Medium Array

Start with the list of numbers 4, 5, 6, 7, 0, 1, and 2. The target is 0. The answer is -1.
For every position from 0 to the last position of numbers.
If item position of numbers is target, set answer to position, then stop.

Show answer.

Expected stdout

4

Open in Playground Standalone Page

#0034 · Find First and Last Position of Element in Sorted Array

Medium Array

Start with the list of numbers 5, 7, 7, 8, 8, and 10. The target is 8. The first is -1. The last is -1.
For every position from 0 to the last position of numbers.
If item position of numbers is target and first is -1, set first to position. If item position of numbers is target, set last to position.

Display first. Display last.

Expected stdout

3
4

Open in Playground Standalone Page

#0035 · Search Insert Position

Easy Array

Start with the list of numbers 1, 3, 5, and 6. The target is 5. The count is the number of items in numbers. The foundposition is -1.
For every position from 0 to the last position of numbers.
If item position of numbers is target or item position of numbers is more than target, set foundposition to position, report foundposition, then stop.

If foundposition is -1, report count.

Expected stdout

2

Open in Playground Standalone Page

#0036 · Valid Sudoku

Medium Array

The board is a list of 5, 3, 0, 0, 7, 0, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 6, 0, 8, 0, 0, 0, 6, 0, 0, 0, 3, 4, 0, 0, 8, 0, 3, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, 9. The invalid is 0.
For every row from 0 to 8.
The seen is an empty map.
For every column from 0 to 8.
The value is item (row times 9 plus column) of board. If value is more than 0 and seen has entry value, set invalid to 1. If value is more than 0, add 1 to entry value of seen.

For every column from 0 to 8.
The seen is an empty map.
For every row from 0 to 8.
The value is item (row times 9 plus column) of board. If value is more than 0 and seen has entry value, set invalid to 1. If value is more than 0, add 1 to entry value of seen.

For every box from 0 to 8.
The startrow is (box divided by 3) times 3. The startcolumn is (box modulo 3) times 3. The seen is an empty map.
For every inner row from 0 to 2.
For every inner column from 0 to 2.
The row is startrow plus inner row. The column is startcolumn plus inner column. The value is item (row times 9 plus column) of board. If value is more than 0 and seen has entry value, set invalid to 1. If value is more than 0, add 1 to entry value of seen.

If invalid is 0, show true. Otherwise, show false.

Expected stdout

true

Open in Playground Standalone Page

#0037 · Sudoku Solver

Hard Array

Keep a shared board holding 5, 3, 0, 0, 7, 0, 0, 0, 0, 6, 0, 0, 1, 9, 5, 0, 0, 0, 0, 9, 8, 0, 0, 0, 0, 6, 0, 8, 0, 0, 0, 6, 0, 0, 0, 3, 4, 0, 0, 8, 0, 3, 0, 0, 1, 7, 0, 0, 0, 2, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 2, 8, 0, 0, 0, 0, 4, 1, 9, 0, 0, 5, 0, 0, 0, 0, 8, 0, 0, 7, and 9.

Keep a shared solved of 0. Keep a shared row safe as 0. Keep a shared column safe as 0. Keep a shared box safe as 0.
To check row at row (a whole number) with value (a whole number). Set row safe to 1.
For every column from 0 to 8.
If item (row times 9 plus column) of board is value, set row safe to 0, break.

To check column at column (a whole number) with value (a whole number). Set column safe to 1.
For every row from 0 to 8.
If item (row times 9 plus column) of board is value, set column safe to 0, break.

To check box at box (a whole number) with value (a whole number). Set box safe to 1. The startrow is (box divided by 3) times 3. The startcolumn is (box modulo 3) times 3.
For every inner row from 0 to 2.
For every inner column from 0 to 2.
The row is startrow plus inner row. The column is startcolumn plus inner column. If item (row times 9 plus column) of board is value, set box safe to 0, break.

To solve from position (a whole number). If solved is 1, return. If position is 81, set solved to 1, return. If item position of board is not 0, solve from position plus 1, return. The row is position divided by 9. The column is position modulo 9. The box is (row divided by 3) times 3 plus column divided by 3.
For every step from 0 to 8.
The digit is 9 minus step. Check row at row with digit. Check column at column with digit. Check box at box with digit.
If row safe is 1 and column safe is 1 and box safe is 1.
Set item position of board to digit. Solve from position plus 1. If solved is 1, return. Set item position of board to 0.

Solve from 0. For each value in board, display value.

Expected stdout

5
3
4
6
7
8
9
1
2
6
7
2
1
9
5
3
4
8
1
9
8
3
4
2
5
6
7
8
5
9
7
6
1
4
2
3
4
2
6
8
5
3
7
9
1
7
1
3
9
2
4
8
5
6
9
6
1
5
3
7
2
8
4
2
8
7
4
1
9
6
3
5
3
4
5
2
8
6
1
7
9

Open in Playground Standalone Page

#0038 · Count and Say

Medium String

The current is empty text. Place "1" into current.

The built is empty text. The position is 0. The limit is the last position of current. The previous is character 0 of current. The run is 0.
Repeat while position is at most limit.
The symbol is character position of current. If symbol is previous, add 1 to run. If symbol is not previous, Place the character for run into built, Place previous into built, set run to 1, set previous to symbol. Add 1 to position.

Place the character for run into built. Place previous into built. Set current to built.

Set built to empty text. Set position to 0. Set limit to the last position of current. Set previous to character 0 of current. Set run to 0.
Repeat while position is at most limit.
The symbol is character position of current. If symbol is previous, add 1 to run. If symbol is not previous, Place the character for run into built, Place previous into built, set run to 1, set previous to symbol. Add 1 to position.

Place the character for run into built. Place previous into built. Set current to built.

Set built to empty text. Set position to 0. Set limit to the last position of current. Set previous to character 0 of current. Set run to 0.
Repeat while position is at most limit.
The symbol is character position of current. If symbol is previous, add 1 to run. If symbol is not previous, Place the character for run into built, Place previous into built, set run to 1, set previous to symbol. Add 1 to position.

Place the character for run into built. Place previous into built. Set current to built.

Report current.

Expected stdout

1211

Open in Playground Standalone Page

#0039 · Combination Sum

Medium Array

Keep a shared nums holding 2, 3, 6, and 7. Keep a shared target of 7. 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 total (a whole number). If total is target, The built is join parts with ",", Place built into answers, then return. If total is more than target, return. If index is at least 4, return.
For every position from index to 3.
The value is item position of nums.
If total plus value is at most target.
The piece is format whole number value as text. Place piece into parts. Combine from position with total plus value. Remove the last item from parts.

Combine from 0 with 0. For each answer in answers, show answer.

Expected stdout

2,2,3
7

Open in Playground Standalone Page

#0040 · Combination Sum II

Medium Array

Keep a shared nums holding 10, 1, 2, 7, 6, 1, and 5. Sort nums. Keep a shared target of 8. 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 total (a whole number). If total is target, The built is join parts with ",", Place built into answers, then return. If total is more than target, return. If index is at least 7, return.
For every position from index to 6.
If position is more than index and item position of nums is item (position minus 1) of nums, skip to the next one. The value is item position of nums.
If total plus value is at most target.
The piece is format whole number value as text. Place piece into parts. Combine from position plus 1 with total plus value. Remove the last item from parts.

Combine from 0 with 0. For each answer in answers, display answer.

Expected stdout

1,1,6
1,2,5
1,7
2,6

Open in Playground Standalone Page

#0041 · First Missing Positive

Hard Array

Start with the list of numbers 3, 4, -1, and 1.
For every candidate from 1 to one more than the number of items in the list.
If the list does not contain candidate, report candidate, then stop.

Expected stdout

2

Open in Playground Standalone Page

#0042 · Trapping Rain Water

Hard Array

The heights is a list of 0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, and 1. The water is 0.
For every position from 0 to the last position of heights.
The current is item position of heights. The lefthighest is 0. For every earlier from 0 to position, if item earlier of heights is greater than lefthighest, set lefthighest to item earlier of heights. The righthighest is 0. For every later from position to the last position of heights, if item later of heights is greater than righthighest, set righthighest to item later of heights. The boundary is the smaller of lefthighest and righthighest. The depth is boundary minus current. If depth is more than 0, add depth to water.

Show water.

Expected stdout

6

Open in Playground Standalone Page

#0043 · Multiply Strings

Medium String

The first is "123". The second is "456". The lastfirst is the last position of first. The lastsecond is the last position of second. The firstdigits is an empty list of whole numbers. The seconddigits is an empty list of whole numbers.
For every position from 0 to lastfirst.
The at is lastfirst minus position. The digit is the digit value of character at of first. Place digit into firstdigits.

For every position from 0 to lastsecond.
The at is lastsecond minus position. The digit is the digit value of character at of second. Place digit into seconddigits.

The total is lastfirst plus lastsecond plus 2. The result is a list of total zeros.
For every left from 0 to lastfirst.
For every right from 0 to lastsecond.
The product is item left of firstdigits times item right of seconddigits. The index is left plus right. The sum is item index of result plus product. Set item index of result to sum modulo 10. Increase item (index plus 1) of result by sum divided by 10.

The resultlast is total minus 1. The output is empty text. The started is 0.
For every step from 0 to resultlast.
The at is resultlast minus step. The digit is item at of result.
If digit is more than 0 or started is 1 or step is resultlast.
Place the character for digit into output. Set started to 1.

Display output.

Expected stdout

56088

Open in Playground Standalone Page

#0044 · Wildcard Matching

Hard String

The text is "adceb". The pattern is "*a*b". The textsize is the number of characters in text. The patternsize is the number of characters in pattern. The width is patternsize plus 1. The height is textsize 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 column from 1 to patternsize.
If character (column minus 1) of pattern is 42 and item (column minus 1) of table is 1, set item column of table to 1.

For every row from 1 to textsize.
For every column from 1 to patternsize.
The index is row times width plus column. The symbol is character (column minus 1) of pattern. If symbol is 42 and item (index minus width) of table is 1, set item index of table to 1. If symbol is 42 and item (index minus 1) of table is 1, set item index of table to 1. If symbol is 63 and item (index minus width minus 1) of table is 1, set item index of table to 1. If symbol is character (row minus 1) of text and item (index minus width 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, report true. Otherwise, report false.

Expected stdout

true

Open in Playground Standalone Page

#0045 · Jump Game II

Medium Array

The jumps is a list of 2, 3, 1, 1, and 4. The last is the last position of jumps. The steps is 0. The start is 0. The currentend is 0. The farthest is 0.
Repeat while currentend is less than last.
For every scan from start to currentend, The reach is scan plus item scan of jumps, if reach is greater than farthest, set farthest to reach. Set start to one more than currentend. Set currentend to farthest. Add 1 to steps.

Show steps.

Expected stdout

2

Open in Playground Standalone Page

#0046 · Permutations

Medium Array

Keep a shared nums holding 1, 2, and 3. Keep a shared used 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.
To permute from depth (a whole number). If depth is 3, The built is join parts with "", Place built into answers, then return.
For every position from 0 to 2.
If used contains position, skip to the next one. Place position into used. The piece is format whole number item position of nums as text. Place piece into parts. Permute from depth plus 1. Remove the last item from used. Remove the last item from parts.

Permute from 0. For each answer in answers, display answer.

Expected stdout

123
132
213
231
312
321

Open in Playground Standalone Page

#0047 · Permutations II

Medium Array

Keep a shared nums holding 1, 1, and 2. Sort nums. Keep a shared used 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.
To permute from depth (a whole number). If depth is 3, The built is join parts with "", Place built into answers, then return.
For every position from 0 to 2.
If used contains position, skip to the next one. The duplicate is 0. If position is more than 0 and item position of nums is item (position minus 1) of nums, set duplicate to 1. If duplicate is 1 and used contains (position minus 1), set duplicate to 0. If duplicate is 1, skip to the next one. Place position into used. The piece is format whole number item position of nums as text. Place piece into parts. Permute from depth plus 1. Remove the last item from used. Remove the last item from parts.

Permute from 0. For each answer in answers, report answer.

Expected stdout

112
121
211

Open in Playground Standalone Page

#0048 · Rotate Image

Medium Array

The matrix is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The size is 3. The last is size minus 1.
For every row from 0 to last.
For every column from row to last.
Swap item (row times size plus column) of matrix and item (column times size plus row) of matrix.

For every row from 0 to last.
For every column from 0 to 1.
The mirror is last minus column. Swap item (row times size plus column) of matrix and item (row times size plus mirror) of matrix.

For each value in matrix, show value.

Expected stdout

7
4
1
8
5
2
9
6
3

Open in Playground Standalone Page

#0049 · Group Anagrams

Medium Array

The text is "eat,tea,tan,ate,nat,bat". The wordslist is split text by ",". The last is the last position of wordslist. The signatures is an empty list of text.
For each word in wordslist.
The signature is sort characters of word. Place signature into signatures.

For every left from 0 to last.
For every right from left to last.
If item right of signatures comes before item left of signatures, swap item left of signatures and item right of signatures, swap item left of wordslist and item right of wordslist.

The group is empty text. The groupcount is 0. The current is empty text. The answers is an empty list of text.
For every position from 0 to last.
The signature is item position of signatures. The word is item position of wordslist. If position is 0, set current to signature, Place word into group, add 1 to groupcount. The same is 1. If signature comes before current, set same to 0. If current comes before signature, set same to 0. If position is more than 0 and same is 1 and groupcount is more than 0, Place "," into group, Place word into group, add 1 to groupcount. If position is more than 0 and same is 0, Place group into answers, set group to empty text, set groupcount to 0, set current to signature, Place word into group, add 1 to groupcount.

Place group into answers. For each answer in answers, display answer.

Expected stdout

bat
tea,ate,eat
nat,tan

Open in Playground Standalone Page

#0050 · Pow(x, n)

Medium Math

The result is 1.0. The factor is 2.0. The power is 10.
Repeat while power is more than 0.
If power modulo 2 is 1, set result to result times factor. Set factor to factor times factor. Set power to power divided by 2.

Report result.

Expected stdout

1024.0000

Open in Playground Standalone Page