The TransCompilator

HTML chunk · 50 solved programs

Chunk 06 · #0335–#0409

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

#0335 · Self Crossing

Hard Array

The distances is a list of 2, 1, 1, and 2. The xs is an empty list of whole numbers. The ys is an empty list of whole numbers. Place 0 into xs. Place 0 into ys. The horiz is 0. The vert is 0. The crossed is 0.
For every step from 0 to 3.
The direction is step modulo 4. The length is item step of distances. If direction is 0, set vert to vert plus length. If direction is 1, set horiz to horiz minus length. If direction is 2, set vert to vert minus length. If direction is 3, set horiz to horiz plus length. Place horiz into xs. Place vert into ys. The here is step plus 1. The alox is the smaller of item step of xs and item here of xs. The ahix is the larger of item step of xs and item here of xs. The aloy is the smaller of item step of ys and item here of ys. The ahiy is the larger of item step of ys and item here of ys.
For every other from 0 to step.
If other is less than step minus 1 and crossed is 0.
The blox is the smaller of item other of xs and item (other plus 1) of xs. The bhix is the larger of item other of xs and item (other plus 1) of xs. The bloy is the smaller of item other of ys and item (other plus 1) of ys. The bhiy is the larger of item other of ys and item (other plus 1) of ys. If ahix is at least blox and bhix is at least alox and ahiy is at least bloy and bhiy is at least aloy, set crossed to 1.

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

Expected stdout

true

Open in Playground Standalone Page

#0336 · Palindrome Pairs

Hard Array

Keep a shared words holding "abcd", "dcba", "lls", "s", and "sssll". Keep a shared combined as empty text. Keep a shared okay of 0.

To check from left (a whole number) with right (a whole number). If left is at least right, set okay to 1, return. If character left of combined is not character right of combined, set okay to 0, return. Check from left plus 1 with right minus 1.

For every first from 0 to 4.
For every second from 0 to 4.
If first is not second.
Set combined to empty text. The one is item first of words. Place one into combined. The two is item second of words. Place two into combined. Set okay to 0. The last is the last position of combined. Check from 0 with last.
If okay is 1.
The lefttext is format whole number first as text. The righttext is format whole number second as text. The line is empty text. Place "[" into line. Place lefttext into line. Place ", " into line. Place righttext into line. Place "]" into line. Display line.

Expected stdout

[0, 1]
[1, 0]
[2, 4]
[3, 2]

Open in Playground Standalone Page

#0337 · House Robber III

Medium Dynamic Programming

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

Keep a shared aside as an empty list of whole numbers.
For every spot from 0 to 6.
Place 0 into aside.

To score from node (a whole number). If node is less than 0, return. The left is item node of lefts. The right is item node of rights. If left is at least 0, score from left. If right is at least 0, score from right. The here is item node of values. The leftaside is 0. The lefttake is 0. The rightaside is 0. The righttake is 0. If left is at least 0, set leftaside to item left of aside. If left is at least 0, set lefttake to item left of robbed. If right is at least 0, set rightaside to item right of aside. If right is at least 0, set righttake to item right of robbed. The bestleft is the larger of leftaside and lefttake. The bestright is the larger of rightaside and righttake. Set item node of robbed to here plus leftaside plus rightaside. Set item node of aside to bestleft plus bestright.

Score from 0. The answer is the larger of item 0 of robbed and item 0 of aside. Show answer.

Expected stdout

7

Open in Playground Standalone Page

#0338 · Counting Bits

Easy Dynamic Programming

The limit is 5. The bits is a list of 6 zeros. Set item 0 of bits to 0.
For every value from 1 to limit.
The half is value divided by 2. The rest is value modulo 2. Set item value of bits to item half of bits plus rest.

For every value from 0 to limit.
Show item value of bits.

Expected stdout

0
1
1
2
1
2

Open in Playground Standalone Page

#0342 · Power of Four

Easy Math

The number is 16. The value is number. The failed is 0.
Repeat while value is more than 1 and failed is 0.
If value modulo 4 is not 0, set failed to 1. If value modulo 4 is 0, set value to value divided by 4.

If failed is 0 and value is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0343 · Integer Break

Medium Math

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

Keep a shared best of 0.

To search from value (a whole number) with cut (a whole number). If cut is more than value minus 1, return. The other is value minus cut. The left is the larger of cut and item cut of depth. The right is the larger of other and item other of depth. The product is left times right. If product is more than best, set best to product. Search from value with cut plus 1.

Set item 1 of depth to 1.
For every value from 2 to 10.
Set best to 1. Search from value with 1. Set item value of depth to best.

Show item 10 of depth.

Expected stdout

36

Open in Playground Standalone Page

#0344 · Reverse String

Easy String

The letters is a list of "h", "e", "l", "l", and "o". The left is 0. The right is 4.
Repeat while left is less than right.
The saved is empty text. Set saved to item left of letters. Set item left of letters to item right of letters. Set item right of letters to saved. Add 1 to left. Subtract 1 from right.

For each letter in letters, show letter.

Expected stdout

o
l
l
e
h

Open in Playground Standalone Page

#0345 · Reverse Vowels of a String

Easy String

Keep a shared source as empty text. Place "hello" into source. Keep a shared vowel of 0. Keep a shared vowels as empty text.

To classify from spot (a whole number). Set vowel to 0. The symbol is character spot of source. If symbol is character 0 of "aeiou" or symbol is character 1 of "aeiou" or symbol is character 2 of "aeiou" or symbol is character 3 of "aeiou" or symbol is character 4 of "aeiou" or symbol is character 0 of "AEIOU" or symbol is character 1 of "AEIOU" or symbol is character 2 of "AEIOU" or symbol is character 3 of "AEIOU" or symbol is character 4 of "AEIOU", set vowel to 1.

To gather from spot (a whole number). If spot is more than 4, return. Classify from spot. If vowel is 1, The symbol is character spot of source, Place symbol into vowels. Gather from spot plus 1.

Gather from 0. The backwards is empty text. The last is the last position of vowels.
For every rank from 0 to last.
The spot is last minus rank. The symbol is character spot of vowels. Place symbol into backwards.

The result is empty text. The used is 0.
For every spot from 0 to 4.
Classify from spot. If vowel is 1, The symbol is character used of backwards, Place symbol into result, add 1 to used. If vowel is 0, The symbol is character spot of source, Place symbol into result.

Show result.

Expected stdout

holle

Open in Playground Standalone Page

#0347 · Top K Frequent Elements

Medium Array

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

The distinct is an empty list of whole numbers.
For each value in numbers.
If distinct contains value, skip to the next one. Place value into distinct.

The last is the last position of distinct.
For every left from 0 to last.
For every right from left to last.
The leftvalue is item left of distinct. The rightvalue is item right of distinct. The leftcount is entry leftvalue of counts. The rightcount is entry rightvalue of counts. If rightcount is more than leftcount, swap item left of distinct and item right of distinct.

For every spot from 0 to wanted minus 1.
Show item spot of distinct.

Expected stdout

1
2

Open in Playground Standalone Page

#0349 · Intersection of Two Arrays

Easy Array

The first is a list of 1, 2, 2, and 1. The second is a list of 2 and 2. The answers is an empty list of whole numbers.
For each value in first.
If answers contains value, skip to the next one. If second contains value, Place value into answers.

Sort answers. For each value in answers, show value.

Expected stdout

2

Open in Playground Standalone Page

#0350 · Intersection of Two Arrays II

Easy Array

The first is a list of 1, 2, 2, and 1. The second is a list of 2 and 2. The used is a list of 0 and 0. The answers is an empty list of whole numbers.
For each value in first.
The found is 0.
For every spot from 0 to 1.
If found is 0 and item spot of used is 0 and item spot of second is value, set item spot of used to 1, set found to 1, Place value into answers.

For each value in answers, show value.

Expected stdout

2
2

Open in Playground Standalone Page

#0354 · Russian Doll Envelopes

Hard Array

Keep a shared widths holding 5, 6, 6, and 7. Keep a shared heights holding 4, 4, 7, and 8. Keep a shared memo as an empty list of whole numbers.
For every spot from 0 to 3.
Place 0 into memo.

To chain from index (a whole number). If index is 0, set item 0 of memo to 1, return. If item index of memo is more than 0, return. The best is 1.
For every prior from 0 to index minus 1.
The narrow is item prior of widths. The short is item prior of heights.
If narrow is less than item index of widths and short is less than item index of heights.
Chain from prior. The candidate is item prior of memo plus 1. If candidate is more than best, set best to candidate. Set item index of memo to best.

The answer is 1.
For every spot from 0 to 3.
Chain from spot. If item spot of memo is more than answer, set answer to item spot of memo.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#0357 · Count Numbers with Unique Digits

Medium Math

The digits is 2. The total is 1. The product is 9.
For every length from 1 to digits.
If length is 1, add 9 to total. If length is more than 1, set product to product times (11 minus length), Add product to total.

Show total.

Expected stdout

91

Open in Playground Standalone Page

#0363 · Max Sum of Rectangle No Larger Than K

Hard Array

Keep a shared prefix as an empty list of whole numbers.
For every spot from 0 to 15.
Place 0 into prefix.

Keep a shared best of -1000000. The chart is a list of 1, 0, 1, 0, -1, 0, 1, 0, and 1.
For every spot from 0 to 8.
The row is spot divided by 3. The column is spot modulo 3. The target is (row plus 1) times 4 plus column plus 1. The above is target minus 4. The aside is target minus 1. The corner is target minus 5. Set item target of prefix to item spot of chart plus item above of prefix plus item aside of prefix minus item corner of prefix.

To sweep from left (a whole number) at top (a whole number) with bottom (a whole number). If left is at least 3, return. Sweep from left plus 1 at top with bottom.
For every right from left to 2.
The zone is (bottom plus 1) times 4 plus right plus 1. The upper is top times 4 plus right plus 1. The leftwan is top times 4 plus left. The leftsouth is (bottom plus 1) times 4 plus left. The total is item zone of prefix minus item upper of prefix minus item leftsouth of prefix plus item leftwan of prefix. If total is at most 2 and total is more than best, set best to total.

To bands from top (a whole number) with bottom (a whole number). If top is at least 3, return. If bottom is at least 3, Bands from top plus 1 with top plus 1, return. Sweep from 0 at top with bottom. Bands from top with bottom plus 1.

Bands from 0 with 0. Show best.

Expected stdout

2

Open in Playground Standalone Page

#0365 · Water and Jug Problem

Medium Math

The first is 3. The second is 5. The target is 4. Keep a shared common of 0.
To reduce from left (a whole number) with right (a whole number). If right is 0, set common to left, return. The rest is left modulo right. Reduce from right with rest.

Reduce from first with second. If target is at most first plus second and target modulo common is 0, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0367 · Valid Perfect Square

Easy Math

The number is 16. The root is 0. The spot is 1.
Repeat while spot times spot is at most number.
If spot times spot is number, set root to spot. Add 1 to spot.

If root is 0, show false, otherwise show true.

Expected stdout

true

Open in Playground Standalone Page

#0368 · Largest Divisible Subset

Medium Array

Keep a shared nums holding 1, 2, 4, and 8. Sort nums. Keep a shared memo as an empty list of whole numbers.
For every spot from 0 to 3.
Place 0 into memo.

To chain from index (a whole number). If index is 0, set item 0 of memo to 1, return. If item index of memo is more than 0, return. The best is 1.
For every prior from 0 to index minus 1.
The rest is item index of nums modulo item prior of nums.
If rest is 0.
Chain from prior. The candidate is item prior of memo plus 1. If candidate is more than best, set best to candidate. Set item index of memo to best.

The bestend is 0. The bestlen is 0.
For every spot from 0 to 3.
Chain from spot. If item spot of memo is more than bestlen, set bestlen to item spot of memo, set bestend to spot.

Keep a shared prior of 0.
To seek from spot (a whole number) with target (a whole number). If spot is less than 0, set prior to -1, return. The rest is item target of nums modulo item spot of nums. If rest is 0 and item spot of memo plus 1 is item target of memo, set prior to spot, return. Seek from spot minus 1 with target.

The current is bestend. The reversed is an empty list of whole numbers.
Repeat while current is at least 0.
Place item current of nums into reversed. Seek from current minus 1 with current. Set current to prior.

The parts is an empty list of text. The lastrev is the last position of reversed.
For every spot from 0 to lastrev.
The source is lastrev minus spot. The piece is format whole number item source of reversed as text. Place piece into parts.

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

Expected stdout

1,2,4,8

Open in Playground Standalone Page

#0371 · Sum of Two Integers

Medium Math

The first is 1. The second is 2. The left is first. The right is second.
Repeat while right is not 0.
The carry is bitwise and of left and right. Set left to exclusive or of left and right. Set right to shift carry left by 1.

Show left.

Expected stdout

3

Open in Playground Standalone Page

#0372 · Super Pow

Medium Math

The base is 2. The digits is a list of 1 and 0. The exponent is 0.
For each digit in digits.
Set exponent to exponent times 10 plus digit.

The result is 1. The reduced is base modulo 1337.
Repeat while exponent is more than 0.
Set result to result times reduced modulo 1337. Subtract 1 from exponent.

Show result.

Expected stdout

1024

Open in Playground Standalone Page

#0373 · Find K Pairs with Smallest Sums

Medium Array

Keep a shared firsts holding 1, 7, and 11. Keep a shared seconds holding 2, 4, and 6. Keep a shared used as an empty list of whole numbers.
For every spot from 0 to 8.
Place 0 into used.

Keep a shared pickrow of 0. Keep a shared pickcolumn of 0. Keep a shared smallest of 0.

To scan from spot (a whole number). If spot is at least 9, return. The row is spot divided by 3. The column is spot modulo 3. The total is item row of firsts plus item column of seconds. If item spot of used is 0 and total is less than smallest, set smallest to total, set pickrow to row, set pickcolumn to column. Scan from spot plus 1.

The answers is an empty list of text. The round is 0.
Repeat while round is less than 3.
Set smallest to 1000000. Scan from 0. The slot is pickrow times 3 plus pickcolumn. Set item slot of used to 1. The chosenrow is pickrow. The chosencolumn is pickcolumn. The left is format whole number item chosenrow of firsts as text. The right is format whole number item chosencolumn of seconds as text. The line is empty text. Place left into line. Place "," into line. Place right into line. Place line into answers. Add 1 to round.

For each answer in answers, show answer.

Expected stdout

1,2
1,4
1,6

Open in Playground Standalone Page

#0375 · Guess Number Higher or Lower II

Medium Math

Keep a shared memo as an empty list of whole numbers.
For every spot from 0 to 120.
Place 0 into memo.

To score from low (a whole number) with high (a whole number). If low is at least high, return. The slot is low times 11 plus high. If item slot of memo is more than 0, return. The best is 1000000.
For every pick from low to high.
The left is 0. The right is 0. If pick is more than low, score from low with pick minus 1, set left to item (low times 11 plus pick minus 1) of memo. If pick is less than high, score from pick plus 1 with high, set right to item ((pick plus 1) times 11 plus high) of memo. The worst is left. If right is more than worst, set worst to right. The cost is pick plus worst. If cost is less than best, set best to cost. Set item slot of memo to best.

Score from 1 with 10. Show item 21 of memo.

Expected stdout

16

Open in Playground Standalone Page

#0376 · Wiggle Subsequence

Medium Array

The nums is a list of 1, 7, 4, 9, 2, and 5. The last is 5. The count is 1. The direction is 0.
For every spot from 1 to last.
The change is item spot of nums minus item (spot minus 1) of nums. If change is more than 0 and direction is at most 0, add 1 to count. If change is less than 0 and direction is at least 0, add 1 to count. If change is not 0, set direction to change.

Show count.

Expected stdout

6

Open in Playground Standalone Page

#0377 · Combination Sum IV

Medium Array

The nums is a list of 1, 2, and 3. The target is 4. The ways is a list of 5 zeros. Set item 0 of ways to 1.
For every total from 1 to 4.
For each value in nums.
If total is at least value.
The prior is total minus value. The add is item prior of ways. Increase item total of ways by add.

Show item 4 of ways.

Expected stdout

7

Open in Playground Standalone Page

#0378 · Kth Smallest Element in a Sorted Matrix

Medium Array

Keep a shared numbers holding 1, 5, 9, 10, 11, 13, 12, 13, and 15. Keep a shared chosen of 0.
To seek from spot (a whole number) with best (a whole number). If spot is more than 8, set chosen to best, return. If item spot of numbers is less than item best of numbers, set best to spot. Seek from spot plus 1 with best.

For every position from 0 to 8.
Seek from position with position. swap item position of numbers and item chosen of numbers.

Show item 7 of numbers.

Expected stdout

13

Open in Playground Standalone Page

#0382 · Linked List Random Node

Medium Math

The values is a list of 1, 2, and 3. The nexts is a list of 1, 2, and -1. The node is 0. The rank is 0. The chosen is 0. The state is 12345.
Repeat while node is at least 0.
Add 1 to rank. Set state to state times 1103515245 plus 12345. Set state to state modulo 2147483648. The draw is state modulo rank. If draw is 0, set chosen to node. Set node to item node of nexts.

Show item chosen of values.

Expected stdout

1

Open in Playground Standalone Page

#0383 · Ransom Note

Easy String

Keep a shared magazine as empty text. Keep a shared note as empty text. Keep a shared seeds as an empty list of text. Keep a shared taken as an empty list of whole numbers. Keep a shared limit of 0. Keep a shared found of 0. Place "b" into seeds. Place "a" into seeds. Set magazine to item 0 of seeds. Set note to item 1 of seeds. The lastmag is the last position of magazine. Set limit to lastmag.
For every spot from 0 to lastmag.
Place 0 into taken.

To find from spot (a whole number) with target (a whole number). If spot is more than limit, set found to -1, return. If item spot of taken is 0 and character spot of magazine is character target of note, set found to spot, return. Find from spot plus 1 with target.

The valid is 1. The last is the last position of note.
For every spot from 0 to last.
Find from 0 with spot. If found is less than 0, set valid to 0. If found is at least 0, The slot is found, Set item slot of taken to 1.

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

Expected stdout

false

Open in Playground Standalone Page

#0385 · Mini Parser

Medium String

Keep a shared source as empty text. Place "[123,[456,[789]]]" into source. Keep a shared cursor of 0. Keep a shared rendered as empty text. Keep a shared value of 0.

To done. The piece is format whole number value as text. Place piece into rendered.

To read. The here is cursor. If here is more than the last position of source, done, return. The symbol is character here of source. If symbol is not between "0" and "9", done, return. The digit is the digit value of symbol. Set value to value times 10 plus digit. Add 1 to cursor. Read.

To parse group. Place "[" into rendered. Add 1 to cursor.
Repeat while cursor is at most the last position of source.
The here is cursor. The symbol is character here of source. If symbol is "]", Place "]" into rendered, add 1 to cursor, return. If symbol is ",", Place "," into rendered, add 1 to cursor. If symbol is "[", parse group. If symbol is "-", Place "-" into rendered, add 1 to cursor, set value to 0, read. If symbol is between "0" and "9", set value to 0, read.

Parse group. Show rendered.

Expected stdout

[123,[456,[789]]]

Open in Playground Standalone Page

#0386 · Lexicographical Numbers

Medium Depth-First Search

Keep a shared ceiling of 13.
To walk from value (a whole number). If value is more than ceiling, return. Show value.
For every digit from 0 to 9.
The next is value times 10 plus digit. If next is at most ceiling, walk from next.

For every start from 1 to 9.
Walk from start.

Expected stdout

1
10
11
12
13
2
3
4
5
6
7
8
9

Open in Playground Standalone Page

#0387 · First Unique Character in a String

Easy String

Keep a shared source as empty text. Place "leetcode" into source. Keep a shared wanted as empty text. Keep a shared count of 0. Keep a shared answer of -1.

To tally from other (a whole number) with finish (a whole number). If other is more than finish, return. The piece is the slice of source from other to other plus 1. If piece is wanted, add 1 to count. Tally from other plus 1 with finish.

To check from spot (a whole number). Set wanted to the slice of source from spot to spot plus 1. Set count to 0. Tally from 0 with the last position of source. If count is 1 and answer is -1, set answer to spot.

For every spot from 0 to the last position of source.
Check from spot.

Show answer.

Expected stdout

0

Open in Playground Standalone Page

#0388 · Longest Absolute File Path

Medium String

Keep a shared source as empty text. Place "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" into source. Keep a shared current as empty text. Keep a shared depth of 0. Keep a shared isfile of 0.

To measure.
For every spot from 0 to the last position of current.
If character spot of current is not "\t", break. Add 1 to depth.

To mark.
For every spot from 0 to the last position of current.
If character spot of current is ".", set isfile to 1, break.

The parts is split source by "\n". The lastpart is the last position of parts. The prefix is a list of 20 zeros. The best is 0.
For every index from 0 to lastpart.
Set current to item index of parts. Set depth to 0. Measure. The deep is depth. The last is the last position of current. The size is last plus 1. The name is the slice of current from deep to size. The final is the last position of name. The namelen is final plus 1. Set current to name. Set isfile to 0. Mark. The candidate is 0. The total is 0. If isfile is 1, set candidate to namelen. If isfile is 1 and deep is more than 0, set candidate to candidate plus item (deep minus 1) of prefix. If isfile is 1 and deep is more than 0, set candidate to candidate plus 1. If isfile is 1 and candidate is more than best, set best to candidate. If isfile is 0, set total to namelen. If isfile is 0 and deep is more than 0, set total to total plus item (deep minus 1) of prefix. If isfile is 0 and deep is more than 0, set total to total plus 1. If isfile is 0, set item deep of prefix to total.

Show best.

Expected stdout

20

Open in Playground Standalone Page

#0389 · Find the Difference

Easy String

Keep a shared first as empty text. Place "abcd" into first. Keep a shared second as empty text. Place "abcde" into second. Keep a shared leftcount of 0. Keep a shared rightcount of 0.

To countleft from spot (a whole number). Set leftcount to 0.
For every mark from 0 to the last position of first.
If character mark of first is character spot of second, add 1 to leftcount.

To countright from spot (a whole number). Set rightcount to 0.
For every mark from 0 to the last position of second.
If character mark of second is character spot of second, add 1 to rightcount.

The answer is empty text.
For every spot from 0 to the last position of second.
Countleft from spot. Countright from spot. If leftcount is less than rightcount, set answer to the slice of second from spot to one more than spot, break.

Show answer.

Expected stdout

e

Open in Playground Standalone Page

#0390 · Elimination Game

Medium Math

The numbers is a list of 1, 2, 3, 4, 5, 6, 7, 8, and 9. The alive is a list of 9 zeros.
For every mark from 0 to 8.
Set item mark of alive to 1.

The last is 8. The remaining is 9. The forward is 1.
Repeat while remaining is more than 1.
The rank is 0. If forward is 1, for every spot from 0 to last, if item spot of alive is 1, add 1 to rank, if item spot of alive is 1, if rank modulo 2 is 1, set item spot of alive to 0, subtract 1 from remaining. If forward is 0, for every offset from 0 to last, the spot is last minus offset, if item spot of alive is 1, add 1 to rank, if item spot of alive is 1, if rank modulo 2 is 1, set item spot of alive to 0, subtract 1 from remaining. Set forward to 1 minus forward.

For every spot from 0 to last.
If item spot of alive is 1, show item spot of numbers.

Expected stdout

6

Open in Playground Standalone Page

#0391 · Perfect Rectangle

Hard Array

The lefts is a list of 1, 3, 3, 1, and 2. The bottoms is a list of 1, 1, 2, 3, and 3. The rights is a list of 3, 4, 4, 2, and 3. The tops is a list of 3, 2, 4, 4, and 4. The last is 4. The minleft is item 0 of lefts. The maxright is item 0 of rights. The minbottom is item 0 of bottoms. The maxtop is item 0 of tops. The area is 0.
For every spot from 0 to last.
The left is item spot of lefts. The right is item spot of rights. The bottom is item spot of bottoms. The top is item spot of tops. The width is right minus left. The height is top minus bottom. Add (width times height) to area. If left is less than minleft, set minleft to left. If right is more than maxright, set maxright to right. If bottom is less than minbottom, set minbottom to bottom. If top is more than maxtop, set maxtop to top.

The boxwidth is maxright minus minleft. The boxheight is maxtop minus minbottom. The boxarea is boxwidth times boxheight. The good is 1. If area is not boxarea, set good to 0. The leftmax is 0. The rightmin is 0. The bottommax is 0. The topmin is 0.
For every first from 0 to last.
For every second from 0 to last.
If second is at most first, skip to the next one. Set leftmax to item first of lefts. If item second of lefts is more than leftmax, set leftmax to item second of lefts. Set rightmin to item first of rights. If item second of rights is less than rightmin, set rightmin to item second of rights. Set bottommax to item first of bottoms. If item second of bottoms is more than bottommax, set bottommax to item second of bottoms. Set topmin to item first of tops. If item second of tops is less than topmin, set topmin to item second of tops. The width is rightmin minus leftmax. The height is topmin minus bottommax. If width is more than 0 and height is more than 0, set good to 0.

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

Expected stdout

true

Open in Playground Standalone Page

#0392 · Is Subsequence

Easy String

The source is "abc". The target is "ahbgdc". The sourcelast is the last position of source. The targetlast is the last position of target. The spot is 0. The mark is 0.
Repeat while spot is at most sourcelast and mark is at most targetlast.
The symbol is character spot of source. The other is character mark of target. If symbol is other, add 1 to spot. Add 1 to mark.

If spot is more than sourcelast, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0393 · UTF-8 Validation

Medium Array

The items is a list of 197, 130, and 1. The size is 3. The spot is 0. The valid is 1.
Repeat while spot is less than size and valid is 1.
The lead is item spot of items. The need is 0. The startpos is spot. If lead is less than 128, add 1 to spot. If lead is at least 192 and lead is less than 224, set need to 1, add 2 to spot. If lead is at least 224 and lead is less than 240, set need to 2, add 3 to spot. If lead is at least 240 and lead is less than 248, set need to 3, add 4 to spot. If lead is at least 128 and lead is less than 192, set valid to 0, add 1 to spot. If lead is at least 248, set valid to 0, add 1 to spot.
For every step from 1 to need.
The index is startpos plus step. If index is at least size, set valid to 0. If index is less than size and item index of items is less than 128, set valid to 0. If index is less than size and item index of items is more than 191, set valid to 0.

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

Expected stdout

true

Open in Playground Standalone Page

#0394 · Decode String

Medium String

Keep a shared source as empty text. Place "3[a]2[bc]" into source. Keep a shared cursor of 0. Keep a shared frames as an empty list of text. Keep a shared counts as an empty list of whole numbers. Keep a shared multiplier of 0. The base is empty text. Place base into frames.

To gather. Set multiplier to 0.
For every step from 0 to the last position of source.
The here is cursor. If here is more than the last position of source, break. If character here of source is not between "0" and "9", break. The digit is the digit value of character here of source. Set multiplier to multiplier times 10 plus digit. Add 1 to cursor.

To enter. The fresh is empty text. Place fresh into frames. Place multiplier into counts. Set multiplier to 0. Add 1 to cursor.

To unwind. The depth is the number of items in frames. The part is item (depth minus 1) of frames. The times is item (depth minus 2) of counts. Remove the last item from frames. Remove the last item from counts. The duplicate is a copy of part. The parent is item (depth minus 2) of frames. Add 1 to cursor.
For every step from 1 to times.
Place duplicate into parent. Set item (depth minus 2) of frames to parent.

To emit. The depth is the number of items in frames. The here is cursor. The piece is the slice of source from here to here plus 1. The current is item (depth minus 1) of frames. Place piece into current. Set item (depth minus 1) of frames to current. Add 1 to cursor.

Repeat while cursor is at most the last position of source.
The here is cursor. The symbol is character here of source. If symbol is between "0" and "9", gather. If symbol is "[", enter. If symbol is "]", unwind. If symbol is not between "0" and "9" and symbol is not "[" and symbol is not "]", emit.

Show item 0 of frames.

Expected stdout

aaabcbc

Open in Playground Standalone Page

#0395 · Longest Substring with At Least K Repeating Characters

Medium String

Keep a shared source as empty text. Place "aaabb" into source. Keep a shared needed of 3. Keep a shared wanted as empty text. Keep a shared count of 0. Keep a shared okay of 0.

To tally from other (a whole number) with finish (a whole number). If other is more than finish, return. The piece is the slice of source from other to other plus 1. If piece is wanted, add 1 to count. Tally from other plus 1 with finish.

To check from start (a whole number) with finish (a whole number). Set okay to 1.
For every spot from start to finish.
Set wanted to the slice of source from spot to spot plus 1. Set count to 0. Tally from start with finish. If count is less than needed, set okay to 0.

The last is 4. The best is 0.
For every start from 0 to last.
For every finish from start to last.
Check from start with finish. The width is finish minus start plus 1. If okay is 1 and width is more than best, set best to width.

Show best.

Expected stdout

3

Open in Playground Standalone Page

#0396 · Rotate Function

Medium Array

The numbers is a list of 4, 3, 2, and 6. The size is 4. The last is 3. The best is -1000000.
For every turn from 0 to last.
The total is 0.
For every spot from 0 to last.
The source is spot plus turn. The source is source modulo size. The value is item source of numbers. Increase total by (spot times value). If total is more than best, set best to total.

Show best.

Expected stdout

26

Open in Playground Standalone Page

#0397 · Integer Replacement

Medium Dynamic Programming

The start is 8. The queue is an empty list of whole numbers. The distances is an empty list of whole numbers. The seen is an empty list of whole numbers. Place start into queue. Place 0 into distances. Place start into seen. The head is 0. The depth is 1. The answer is 0. The found is 0.
Repeat while head is less than depth and found is 0.
The current is item head of queue. The steps is item head of distances. Add 1 to head. The parity is current modulo 2. The chosen is current minus 1. The upper is current plus 1. If parity is 0, set chosen to current divided by 2. If chosen is 1, set answer to steps plus 1, set found to 1. If chosen is more than 1, if seen does not contain chosen, Place chosen into queue, Place (steps plus 1) into distances, Place chosen into seen, add 1 to depth. If parity is 1, if upper is 1, set answer to steps plus 1, set found to 1. If parity is 1, if upper is more than 1, if seen does not contain upper, Place upper into queue, Place (steps plus 1) into distances, Place upper into seen, add 1 to depth.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#0398 · Random Pick Index

Medium Math

The numbers is a list of 1, 2, 3, 3, and 3. The target is 3. The last is the last position of numbers. The total is 0.
For every spot from 0 to last.
If item spot of numbers is target, add 1 to total.

The wanted is total divided by 2. Add 1 to wanted. The seen is 0. The answer is 0.
For every spot from 0 to last.
If item spot of numbers is target, add 1 to seen. If item spot of numbers is target and seen is wanted, set answer to spot, break.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#0399 · Evaluate Division

Medium Array

Keep a shared lefts holding 0, 1, 1, and 2. Keep a shared rights holding 1, 0, 2, and 1. Keep a shared numerators holding 2, 1, 3, and 1. Keep a shared denominators holding 1, 2, 1, and 3. Keep a shared visited as an empty list of whole numbers.
For every spot from 0 to 4.
Place 0 into visited.

Keep a shared known holding 1, 1, 1, 0, and 0.

Keep a shared wantednode of 0. Keep a shared foundnum of 0. Keep a shared foundden of 0. Keep a shared done of 0. Keep a shared rendered as empty text.

To seek from node (a whole number) at num (a whole number) with den (a whole number). If done is 1, return. If node is wantednode, set foundnum to num, set foundden to den, set done to 1, return.
For every edge from 0 to 3.
The source is item edge of lefts. The dest is item edge of rights. The factor is 0. The divisor is 0.
If source is node and item dest of visited is 0.
Set factor to item edge of numerators. Set divisor to item edge of denominators. Set item dest of visited to 1. The newnum is num times factor. The newden is den times divisor. Seek from dest at newnum with newden.

To expand from divisor (a whole number) with left (a whole number). If divisor is 0, return. The part is left divided by divisor. The piece is format whole number part as text. Place piece into rendered. The rest is left modulo divisor. Expand from divisor divided by 10 with rest.

The starts is a list of 0, 1, 0, 0, and 4. The ends is a list of 2, 0, 3, 0, and 4. The scaled is 0. The wholepart is 0. The fraction is 0. The marked is empty text. The allowed is 1.
For every query from 0 to 4.
The start is item query of starts. The end is item query of ends. Set wantednode to end. Set done to 0. Set foundnum to 0. Set foundden to 1. Set item 0 of visited to 0. Set item 1 of visited to 0. Set item 2 of visited to 0. Set item 3 of visited to 0. Set item 4 of visited to 0. Set item start of visited to 1. Set allowed to 1. If item start of known is 0, set allowed to 0. If allowed is 1, Seek from start at 1 with 1. Set rendered to empty text. If done is 1, set scaled to foundnum times 100000 divided by foundden. If done is 1, set wholepart to scaled divided by 100000. If done is 1, set fraction to scaled modulo 100000. If done is 1, set marked to format whole number wholepart as text. If done is 1, place marked into rendered. If done is 1, place "." into rendered. If done is 1, expand from 10000 with fraction. If done is 1, show rendered. If done is 0, show "-1.00000".

Expected stdout

6.00000
0.50000
-1.00000
1.00000
-1.00000

Open in Playground Standalone Page

#0400 · Nth Digit

Medium Math

The position is 11. The width is 1. The start is 1. The count is 9.
Repeat while position is more than count.
Subtract count from position. Increase width by 1. Set start to start times 10. Set count to 9 times start.

The offset is position minus 1. The number is start plus (offset divided by width). The place is offset modulo width. The places is width minus 1. The value is number.
For every step from 0 to places.
The digit is value modulo 10. If step is places minus place, show digit, break. Set value to value divided by 10.

Expected stdout

0

Open in Playground Standalone Page

#0401 · Binary Watch

Easy Backtracking

Keep a shared bits of 0.
To score from value (a whole number). If value is 0, return. The low is value modulo 2. Add low to bits. Score from value divided by 2.

The turned is 1.
For every hour from 0 to 11.
For every minute from 0 to 59.
Set bits to 0. Score from hour. Score from minute.
If bits is turned.
The tens is minute divided by 10. The ones is minute modulo 10. The mins is format whole number tens as text. Place format whole number ones as text into mins. The line is format whole number hour as text. Place ":" into line. Place mins into line. Show line.

Expected stdout

0:01
0:02
0:04
0:08
0:16
0:32
1:00
2:00
4:00
8:00

Open in Playground Standalone Page

#0402 · Remove K Digits

Medium String

Keep a shared number as empty text. Place "1432219" into number. Keep a shared removes of 3. Keep a shared chosen of 0.
To choose. Set chosen to -1. The last is the last position of number. The limit is last minus 1.
For every spot from 0 to limit.
The leftdigit is the digit value of character spot of number. The rightdigit is the digit value of character (spot plus 1) of number. If chosen is -1 and leftdigit is more than rightdigit, set chosen to spot.

To carve. Choose. The last is the last position of number. If chosen is -1, set chosen to last. The head is the slice of number from 0 to chosen. The tail is the slice of number from chosen plus 1 to last plus 1. The joined is empty text. Place head into joined. Place tail into joined. Set number to joined.

For every step from 1 to removes.
Carve.

The size is the number of characters in number. The zero is character 0 of "0".
Repeat while size is more than 1.
The first is character 0 of number. If first is not zero, break. The last is the last position of number. The joined is the slice of number from 1 to last plus 1. Set number to joined. Set size to the number of characters in number.

Show number.

Expected stdout

1219

Open in Playground Standalone Page

#0403 · Frog Jump

Hard Array

Keep a shared stones holding 0, 1, 3, 5, 6, 8, 12, and 17. Keep a shared seen as an empty list of whole numbers.
For every spot from 0 to 63.
Place 0 into seen.

Keep a shared found of 0. Keep a shared good of 0.
To locate from target (a whole number) with spot (a whole number). If spot is more than 7, return. If item spot of stones is target, set found to spot, return. Locate from target with spot plus 1.

To walk from spot (a whole number) with jump (a whole number). If good is 1, return. If spot is 7, set good to 1, return. The mark is spot times 8 plus jump. If item mark of seen is 1, return. Set item mark of seen to 1. Expand from spot with jump.

To expand from spot (a whole number) with jump (a whole number). If good is 1, return. The low is jump minus 1. If low is more than 0, set found to -1. If low is more than 0, locate from item spot of stones plus low with 0. If low is more than 0 and found is at least 0, walk from found with low. The same is jump. Set found to -1. Locate from item spot of stones plus same with 0. If found is at least 0, walk from found with same. The high is jump plus 1. Set found to -1. Locate from item spot of stones plus high with 0. If found is at least 0, walk from found with high.

Walk from 0 with 1. If good is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0404 · Sum of Left Leaves

Easy Depth-First Search

Keep a shared values holding 3, 9, 20, 0, 0, 15, and 7. Keep a shared lefts holding 1, -1, 5, -1, -1, -1, and -1. Keep a shared rights holding 2, -1, 6, -1, -1, -1, and -1.

Keep a shared total of 0.
To visit from node (a whole number) with side (a whole number). If node is less than 0, return. The leftnode is item node of lefts. The rightnode is item node of rights. The amount is item node of values. If leftnode is -1 and rightnode is -1 and side is 1, add amount to total. Visit from leftnode with 1. Visit from rightnode with 0.

Visit from 0 with 0. Show total.

Expected stdout

24

Open in Playground Standalone Page

#0405 · Convert a Number to Hexadecimal

Easy String

The value is 26. The alphabet is "0123456789abcdef". The reversed is empty text.
Repeat while value is more than 0.
The digit is value modulo 16. The piece is the slice of alphabet from digit to digit plus 1. Place piece into reversed. Set value to value divided by 16.

The size is the number of characters in reversed. If size is 0, Place "0" into reversed. The last is the last position of reversed. The result is empty text.
For every step from 0 to last.
The source is last minus step. The piece is the slice of reversed from source to source plus 1. Place piece into result.

Show result.

Expected stdout

1a

Open in Playground Standalone Page

#0406 · Queue Reconstruction by Height

Medium Array

The heights is a list of 7, 4, 7, 5, 6, and 5. The ranks is a list of 0, 4, 1, 0, 1, and 2. The last is the last position of heights.
For every left from 0 to last.
For every right from left plus 1 to last.
The taller is 0. If item right of heights is more than item left of heights, set taller to 1. If item right of heights is item left of heights and item right of ranks is less than item left of ranks, set taller to 1. If taller is 1, swap item left of heights and item right of heights, swap item left of ranks and item right of ranks.

Keep a shared outheights as an empty list of whole numbers. Keep a shared outranks as an empty list of whole numbers.
For every spot from 0 to 5.
Place 0 into outheights. Place 0 into outranks.

To shift from spot (a whole number) with target (a whole number). If spot is at most target, return. Set item spot of outheights to item (spot minus 1) of outheights. Set item spot of outranks to item (spot minus 1) of outranks. Shift from spot minus 1 with target.

The placed is 0.
For every spot from 0 to last.
The seat is item spot of ranks. Shift from placed with seat. Set item seat of outheights to item spot of heights. Set item seat of outranks to item spot of ranks. Add 1 to placed.

For every spot from 0 to last.
The height is item spot of outheights. The rank is item spot of outranks. The line is format whole number height as text. Place "," into line. Place format whole number rank as text into line. Show line.

Expected stdout

5,0
7,0
5,2
6,1
4,4
7,1

Open in Playground Standalone Page

#0407 · Trapping Rain Water II

Hard Array

Keep a shared heights holding 1, 4, 3, 1, 3, 2, 3, 2, 1, 3, 2, 4, 2, 3, 3, 2, 3, and 1. Keep a shared levels as an empty list of whole numbers. Keep a shared changed of 0.
For every spot from 0 to 17.
Place 0 into levels.

To relax from spot (a whole number). The low is 100. The row is spot divided by 6. The column is spot modulo 6. If row is more than 0 and item (spot minus 6) of levels is less than low, set low to item (spot minus 6) of levels. If row is less than 2 and item (spot plus 6) of levels is less than low, set low to item (spot plus 6) of levels. If column is more than 0 and item (spot minus 1) of levels is less than low, set low to item (spot minus 1) of levels. If column is less than 5 and item (spot plus 1) of levels is less than low, set low to item (spot plus 1) of levels. If item spot of heights is more than low, set low to item spot of heights. If low is less than item spot of levels, set item spot of levels to low, set changed to 1.

For every spot from 0 to 17.
Set item spot of levels to 100. The row is spot divided by 6. The column is spot modulo 6. If row is 0 or row is 2 or column is 0 or column is 5, set item spot of levels to item spot of heights.

Set changed to 1.
Repeat while changed is 1.
Set changed to 0.
For every spot from 0 to 17.
The row is spot divided by 6. The column is spot modulo 6. The edge is 0. If row is 0 or row is 2 or column is 0 or column is 5, set edge to 1. If edge is 0, relax from spot.

The total is 0.
For every spot from 0 to 17.
The extra is item spot of levels minus item spot of heights. Add extra to total.

Show total.

Expected stdout

4

Open in Playground Standalone Page

#0409 · Longest Palindrome

Easy String

Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared target as empty text. Keep a shared found of 0.
To search from spot (a whole number). If spot is more than 25, return. The piece is the slice of alphabet from spot to spot plus 1. If piece is target, set found to spot, return. Search from spot plus 1.

The word is "abccccdd". The last is the last position of word. The counts is a list of 26 zeros.
For every spot from 0 to last.
The symbol is the slice of word from spot to spot plus 1. Set target to symbol. Set found to -1. Search from 0. The letter is found. Increase item letter of counts by 1.

The answer is 0.
For every spot from 0 to 25.
The amount is item spot of counts. The pairs is amount divided by 2. Add pairs times 2 to answer.

The extra is 0.
For every spot from 0 to 25.
The amount is item spot of counts. If amount modulo 2 is 1, set extra to 1.

Add extra to answer.

Show answer.

Expected stdout

7

Open in Playground Standalone Page