The TransCompilator

HTML chunk · 50 solved programs

Chunk 14 · #0880–#0935

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

#0880 · Decoded String at Index

Medium String

The source is "leet2code3". The rank is 10. The last is the last position of source. The size is 0. The amount is 0.
For every spot from 0 to last.
The symbol is character spot of source. Set amount to the digit value of symbol. If symbol is between "0" and "9", set size to size times amount. If symbol is not between "0" and "9", add 1 to size. If size is more than rank, set size to rank.

The answer is empty text. The place is last.
Repeat while place is at least 0.
Set symbol to character place of source. Set amount to the digit value of symbol. If symbol is between "0" and "9", set size to size divided by amount. If symbol is between "0" and "9", set rank to rank minus 1. If symbol is between "0" and "9", set rank to rank modulo size. If symbol is between "0" and "9", add 1 to rank. If symbol is not between "0" and "9" and rank is size, set answer to the slice of source from place to place plus 1, stop. If symbol is not between "0" and "9", subtract 1 from size. Subtract 1 from place.

Show answer.

Expected stdout

o

Open in Playground Standalone Page

#0881 · Boats to Save People

Medium Array

The people is a list of 3, 2, 2, and 1. The limit is 3. Sort people. The low is 0. The high is the last position of people. The boats is 0.
Repeat while low is at most high.
If low is high, add 1 to boats, set low to low plus 1, set high to high minus 1.
If low is less than high.
The pair is item low of people plus item high of people. If pair is at most limit, add 1 to low. Decrease high by 1. Add 1 to boats.

Show boats.

Expected stdout

3

Open in Playground Standalone Page

#0882 · Reachable Nodes In Subdivided Graph

Hard Graph Theory

Keep a shared starts holding 0, 0, and 1. Keep a shared ends holding 1, 2, and 2. Keep a shared weights holding 10, 1, and 2. Keep a shared distances holding 0, 1000000, and 1000000. Keep a shared visited holding 0, 0, and 0. Keep a shared chosen of 0. Keep a shared closest of 0. Keep a shared partner of 0. Keep a shared candidate of 0.
To choose. Set chosen to -1. Set closest to 1000000.
For every node from 0 to 2.
If item node of visited is 0 and item node of distances is less than closest, set chosen to node, set closest to item node of distances.

To relax. The settled is chosen.
For every edge from 0 to 2.
Set partner to -1. If item edge of starts is settled, set partner to item edge of ends. If item edge of ends is settled, set partner to item edge of starts. Set candidate to item settled of distances plus item edge of weights plus 1. The target is partner. If target is at least 0 and candidate is less than item target of distances, set item target of distances to candidate.

For every round from 1 to 3.
Choose. The here is chosen. If here is at least 0, set item here of visited to 1. Relax.

The total is 3.
For every edge from 0 to 2.
The start is item edge of starts. The finish is item edge of ends. The startroom is 6 minus item start of distances. The finishroom is 6 minus item finish of distances. If startroom is less than 0, set startroom to 0. If finishroom is less than 0, set finishroom to 0. The share is startroom plus finishroom. If share is more than item edge of weights, set share to item edge of weights. Increase total by share.

Show total.

Expected stdout

13

Open in Playground Standalone Page

#0883 · Projection Area of 3D Shapes

Easy Array

The grid is a list of 1, 2, 3, and 4. The height is 2. The width is 2. The top is 0. The front is 0. The side is 0.
For every line from 0 to height minus 1.
The rowbest is 0.
For every column from 0 to width minus 1.
The amount is item (line times width plus column) of grid. If amount is more than 0, add 1 to top. If amount is more than rowbest, set rowbest to amount. If column is width minus 1, add rowbest to front.

For every column from 0 to width minus 1.
The columnbest is 0.
For every line from 0 to height minus 1.
The amount is item (line times width plus column) of grid. If amount is more than columnbest, set columnbest to amount. If line is height minus 1, add columnbest to side.

The area is top plus front plus side. Show area.

Expected stdout

17

Open in Playground Standalone Page

#0884 · Uncommon Words from Two Sentences

Easy String

The first is "this apple is sweet". The second is "this apple is sour". The firstwords is split first by " ". The secondwords is split second by " ". Keep a shared vocabulary as an empty list of text. Keep a shared counts as an empty list of whole numbers. Keep a shared sought as empty text. Keep a shared foundat of 0.
To find from spot (a whole number). If spot is more than the last position of vocabulary, set foundat to -1, return. If item spot of vocabulary matches sought, set foundat to spot, return. Find from spot plus 1.

To record. Set foundat to -1. Find from 0. The local is foundat. If local is at least 0, set item local of counts to item local of counts plus 1, return. The fresh is a copy of sought. Place fresh into vocabulary. Place 1 into counts.

For each piece in firstwords.
Set sought to piece. Record.

For each piece in secondwords.
Set sought to piece. Record.

For every spot from 0 to the last position of vocabulary.
If item spot of counts is 1, show item spot of vocabulary.

Expected stdout

sweet
sour

Open in Playground Standalone Page

#0885 · Spiral Matrix III

Medium Array

The rows is 5. The columns is 6. The total is rows times columns. Keep a shared answers as an empty list of text. Keep a shared found of 0. Keep a shared label as empty text. Keep a shared line of 0. Keep a shared column of 0. Set line to 1. Set column to 4.
To record. The linetext is format whole number line as text. Set label to linetext. Place "," into label. The columntext is format whole number column as text. Place columntext into label. The copy is a copy of label. Place copy into answers. Add 1 to found.

Record. The runleft is 0. The legs is 0. The heading is 0.
Repeat while found is less than total.
If runleft is 0, add 1 to legs, set runleft to (legs plus 1) divided by 2, set heading to (legs minus 1) modulo 4. If heading is 0, add 1 to column. If heading is 1, add 1 to line. If heading is 2, subtract 1 from column. If heading is 3, subtract 1 from line. If line is at least 0 and line is less than rows and column is at least 0 and column is less than columns, record. Subtract 1 from runleft.

For each entry in answers, show entry.

Expected stdout

1,4
1,5
2,5
2,4
2,3
1,3
0,3
0,4
0,5
3,5
3,4
3,3
3,2
2,2
1,2
0,2
4,5
4,4
4,3
4,2
4,1
3,1
2,1
1,1
0,1
4,0
3,0
2,0
1,0
0,0

Open in Playground Standalone Page

#0886 · Possible Bipartition

Medium Depth-First Search

Keep a shared people of 0. Keep a shared width of 0. Keep a shared edges as an empty list of whole numbers. Keep a shared colors as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared head of 0. Keep a shared sound of 1. Set people to 4. Set width to people plus 1.
For every cell from 0 to 24.
Place 0 into edges.

The pairs is a list of 1, 2, 1, 3, 2, and 4.
For every pair from 0 to 2.
The left is item (pair times 2) of pairs. The right is item (pair times 2 plus 1) of pairs. Set item (left times width plus right) of edges to 1. Set item (right times width plus left) of edges to 1.

For every tone from 0 to 4.
Place 0 into colors.

To paint from start (a whole number). Set head to 0. Place start into queue.
Repeat while head is at most the last position of queue.
The atspot is head. The person is item atspot of queue. Add 1 to head.
For every other from 1 to people.
If item (person times width plus other) of edges is 1 and item other of colors is 0, set item other of colors to 3 minus item person of colors, Place other into queue. If item (person times width plus other) of edges is 1 and item other of colors is item person of colors, set sound to 0.

Set item 1 of colors to 1. Paint from 1. If item 2 of colors is 0, set item 2 of colors to 1, paint from 2. If item 3 of colors is 0, set item 3 of colors to 1, paint from 3. If item 4 of colors is 0, set item 4 of colors to 1, paint from 4. If sound is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0887 · Super Egg Drop

Hard Math

The eggs is 2. The floors is 6. The width is floors plus 1. The ways is a list of 21 zeros.
For every count from 1 to eggs.
For every move from 1 to floors.
The above is item ((count minus 1) times width plus move minus 1) of ways. The below is item (count times width plus move minus 1) of ways. The total is above plus below plus 1. Set item (count times width plus move) of ways to total.

The moves is 0. The reached is 0.
Repeat while reached is less than floors.
Add 1 to moves. Set reached to item (eggs times width plus moves) of ways.

Show moves.

Expected stdout

3

Open in Playground Standalone Page

#0888 · Fair Candy Swap

Easy Array

The alice is a list of 1 and 1, while the bob is a list of 2 and 2. The alice total is 0, while the bob total is 0. For each treat in alice, add treat to alice total. For each sweet in bob, add sweet to bob total. The first is 0, while the second is 0. The found is 0.
For each treat in alice.
For each sweet in bob.
If found is 0 and alice total minus treat plus sweet is bob total plus treat minus sweet, set first to treat, set second to sweet, set found to 1. If found is 1, stop.

Show first. Show second.

Expected stdout

1
2

Open in Playground Standalone Page

#0889 · Construct Binary Tree from Preorder and Postorder Traversal

Medium Array

Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared built of 0. Keep a shared nextslot of 0. Keep a shared preindex of 0. Keep a shared postindex of 0. Keep a shared preorder as an empty list of whole numbers. Keep a shared postorder as an empty list of whole numbers. Place 1 into preorder. Place 2 into preorder. Place 4 into preorder. Place 5 into preorder. Place 3 into preorder. Place 6 into preorder. Place 7 into preorder. Place 4 into postorder. Place 5 into postorder. Place 2 into postorder. Place 6 into postorder. Place 7 into postorder. Place 3 into postorder. Place 1 into postorder.

To build. The slot is nextslot. Add 1 to nextslot. The prelocal is preindex. The here is item prelocal of preorder. Add 1 to preindex. Place here into values. Place -1 into lefts. Place -1 into rights. Set built to slot. The postlocal is postindex. If here is item postlocal of postorder, add 1 to postindex, then return. Build. Set item slot of lefts to built. The postlocal is postindex. If here is item postlocal of postorder, add 1 to postindex, then return. Build. Set item slot of rights to built. Add 1 to postindex. Set built to slot.

Build. The queue is an empty list of whole numbers. Place 0 into queue. The head is 0, while the count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Show item node of values. The left is item node of lefts. The right is item node of rights. If left is at least 0, Place left into queue, add 1 to count. If right is at least 0, Place right into queue, add 1 to count.

Expected stdout

1
2
3
4
5
6
7

Open in Playground Standalone Page

#0890 · Find and Replace Pattern

Medium Array

The pattern is "abb". The words is a list of "abc", "deq", "mee", "aqq", "dkd", and "ccc". The answers is an empty list of text.
For each word in words.
The last is the last position of word. The okay is 1.
For every left from 0 to last.
For every right from left plus 1 to last.
The patternleft is the slice of pattern from left to left plus 1. The patternright is the slice of pattern from right to right plus 1. The wordleft is the slice of word from left to left plus 1. The wordright is the slice of word from right to right plus 1. The patternsame is 0, while the wordsame is 0. If patternleft matches patternright, set patternsame to 1. If wordleft matches wordright, set wordsame to 1. If patternsame is not wordsame, set okay to 0. If left is last minus 1 and right is last and okay is 1, Place word into answers.

For each answer in answers, show answer.

Expected stdout

mee
aqq

Open in Playground Standalone Page

#0891 · Sum of Subsequence Widths

Hard Array

The numbers is a list of 5, 4, 3, 2, and 1. Sort numbers. The last is the last position of numbers. The powers is an empty list of whole numbers. Place 1 into powers.
For every step from 0 to last minus 1.
The tail is item step of powers. The next is (tail times 2) modulo 1000000007. Place next into powers.

The total is 0.
For every spot from 0 to last.
The leftpower is item spot of powers. The mirror is last minus spot. The rightpower is item mirror of powers. The gap is leftpower minus rightpower. If gap is less than 0, add 1000000007 to gap. The value is item spot of numbers. The term is (value times gap) modulo 1000000007. Add term to total. Set total to total modulo 1000000007.

Show total.

Expected stdout

72

Open in Playground Standalone Page

#0892 · Surface Area of 3D Shapes

Easy Array

The shape is a list of 1, 2, 3, and 4. The lines is 2, while the columns is 2. The lastline is lines minus 1, while the lastcolumn is columns minus 1. The area is 0.
For every line from 0 to lastline.
For every column from 0 to lastcolumn.
The cell is line times columns plus column. The height is item cell of shape. If height is more than 0, add 2 to area, add height times 4 to area. If column is less than lastcolumn, The rightheight is item (cell plus 1) of shape, The shared is the smaller of height and rightheight, subtract shared times 2 from area. If line is less than lastline, The downheight is item (cell plus columns) of shape, The shared is the smaller of height and downheight, subtract shared times 2 from area.

Show area.

Expected stdout

34

Open in Playground Standalone Page

#0893 · Groups of Special-Equivalent Strings

Medium Array

The words is a list of "abcd", "cdab", "cbad", "xyzz", "zzxy", and "zzyx". The signatures is an empty list of text. The groups is 0.
For each word in words.
The last is the last position of word. The evenletters is empty text, while the oddletters is empty text.
For every spot from 0 to last.
The piece is the slice of word from spot to spot plus 1. If spot modulo 2 is 0, Place piece into evenletters. If spot modulo 2 is 1, Place piece into oddletters. The sortedeven is sort characters of evenletters. The sortedodd is sort characters of oddletters. The signature is empty text. Place sortedeven into signature. Place "|" into signature. Place sortedodd into signature. The known is 0. For each saved in signatures, if saved matches signature, set known to 1. If spot is last and known is 0, Place signature into signatures, add 1 to groups.

Show groups.

Expected stdout

3

Open in Playground Standalone Page

#0894 · All Possible Full Binary Trees

Medium Dynamic Programming

The wanted is 7. The shapes is an empty list of text. The starts is a list of 8 zeros. The counts is a list of 8 zeros. Place "()" into shapes. Set item 1 of counts to 1. The slot is 1.
For every size from 3 to wanted by 2.
Set item size of starts to slot. The leftsize is 1.
Repeat while leftsize is less than size.
The rightsize is size minus 1 minus leftsize. The leftbegin is item leftsize of starts. The leftcount is item leftsize of counts. The rightbegin is item rightsize of starts. The rightcount is item rightsize of counts. The leftend is leftbegin plus leftcount minus 1. The rightend is rightbegin plus rightcount minus 1.
For every leftspot from leftbegin to leftend.
For every rightspot from rightbegin to rightend.
The leftshape is item leftspot of shapes. The rightshape is item rightspot of shapes. The shape is empty text. Place "(" into shape. Place leftshape into shape. Place rightshape into shape. Place ")" into shape. Place shape into shapes. Increase item size of counts by 1. Add 1 to slot. If leftspot is leftend and rightspot is rightend, add 2 to leftsize.

Show item wanted of counts. The first is item wanted of starts. The last is the last position of shapes.
For every spot from first to last.
Show item spot of shapes.

Expected stdout

5
(()(()(()())))
(()((()())()))
((()())(()()))
((()(()()))())
(((()())())())

Open in Playground Standalone Page

#0896 · Monotonic Array

Easy Array

The numbers is a list of 1, 2, 2, and 3. The last is the last position of numbers. The rising is 1, while the falling is 1.
For every spot from 1 to last.
The before is item (spot minus 1) of numbers. The here is item spot of numbers. If before is more than here, set rising to 0. If before is less than here, set falling to 0.

If rising is 1 or falling is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0897 · Increasing Order Search Tree

Easy Depth-First Search

Keep a shared values as an empty list of whole numbers. Keep a shared lefts as an empty list of whole numbers. Keep a shared rights as an empty list of whole numbers. Keep a shared order as an empty list of whole numbers. Place 5 into values. Place 3 into values. Place 6 into values. Place 2 into values. Place 4 into values. Place 8 into values. Place 1 into values. Place 7 into values. Place 9 into values. Place 1 into lefts. Place 3 into lefts. Place -1 into lefts. Place 6 into lefts. Place -1 into lefts. Place 7 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place 2 into rights. Place 4 into rights. Place 5 into rights. Place -1 into rights. Place -1 into rights. Place 8 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights.

To gather from node (a whole number). If node is less than 0, return. Gather from item node of lefts. Place item node of values into order. Gather from item node of rights.

Gather from 0. The last is the last position of order. The final is item last of order. The pieces is an empty list of text.
For each value in order.
The piece is format whole number value as text. Place piece into pieces. If value is not final, Place "null" into pieces.

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

Expected stdout

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

Open in Playground Standalone Page

#0898 · Bitwise ORs of Subarrays

Medium Array

The numbers is a list of 1, 1, and 2. The last is the last position of numbers. The seen is an empty list of whole numbers.
For every start from 0 to last.
The running is 0.
For every finish from start to last.
The value is item finish of numbers. Set running to bitwise or of running and value. If seen does not contain running, Place running into seen.

Show the number of items in seen.

Expected stdout

3

Open in Playground Standalone Page

#0899 · Orderly Queue

Hard String

The word is "cba". The turns is 1. The last is the last position of word. The best is a copy of word. The current is a copy of word.
If turns is 1.
For every step from 0 to last.
If current comes before best, set best to current. The first is the slice of current from 0 to 1. The rest is the slice of current from 1 to last plus 1. The moved is empty text. Place rest into moved. Place first into moved. Set current to moved.

If turns is more than 1, The sorted is sort characters of word, show sorted. If turns is 1, show best.

Expected stdout

acb

Open in Playground Standalone Page

#0902 · Numbers At Most N Given Digit Set

Hard Array

Keep a shared digits holding 1, 3, 5, and 7. Keep a shared counter of 0. The limit is 100.
To screen from value (a whole number). Set counter to 1. The rest is value.
Repeat while rest is more than 0.
The digit is rest modulo 10. If digits contains digit, set rest to rest divided by 10, otherwise set counter to 0, set rest to 0.

The total is 0.
For every value from 1 to limit.
Screen from value. If counter is 1, add 1 to total.

Show total.

Expected stdout

20

Open in Playground Standalone Page

#0903 · Valid Permutations for DI Sequence

Hard String

Keep a shared pattern as empty text. Place "DID" into pattern. Keep a shared size of 4. Keep a shared used holding 0, 0, 0, 0, and 0. Keep a shared picks as an empty list of whole numbers. Keep a shared good of 0. Keep a shared total of 0.
To validate. Set good to 1. The last is the last position of pattern.
For every spot from 0 to last.
The belowpick is item spot of picks. The abovepick is item (spot plus 1) of picks. The symbol is character spot of pattern. If symbol is "I" and belowpick is more than abovepick, set good to 0. If symbol is "D" and belowpick is less than abovepick, set good to 0.

To finish. Validate. If good is 1, add 1 to total.

To arrange from spot (a whole number). If spot is at least size, finish.
For every choice from 1 to size.
If item choice of used is 0, set item choice of used to 1, Place choice into picks, Arrange from spot plus 1, remove the last item from picks, set item choice of used to 0.

Arrange from 0. Show total.

Expected stdout

5

Open in Playground Standalone Page

#0904 · Fruit Into Baskets

Medium Array

The fruits is a list of 1, 2, 3, 2, and 2. The lastfruit is -1. The priorfruit is -1. The lastcount is 0. The priorcount is 0. The held is 0. The best is 0.
For each fruit in fruits.
If fruit is lastfruit, add 1 to lastcount. If fruit is priorfruit, set held to lastcount, set lastcount to priorcount plus 1, set priorcount to held, set priorfruit to lastfruit, set lastfruit to fruit. If fruit is not lastfruit and fruit is not priorfruit, set priorfruit to lastfruit, set priorcount to lastcount, set lastfruit to fruit, set lastcount to 1. The span is lastcount plus priorcount. If span is more than best, set best to span.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#0905 · Sort Array By Parity

Easy Array

The numbers is a list of 3, 1, 2, and 4. The ordered is an empty list of whole numbers.
For each value in numbers.
If value modulo 2 is 0, Place value into ordered.

For each value in numbers.
If value modulo 2 is not 0, Place value into ordered.

For each value in ordered, show value.

Expected stdout

2
4
3
1

Open in Playground Standalone Page

#0906 · Super Palindromes

Hard String

Keep a shared reflection of 0. Keep a shared root of 0. Keep a shared total of 0. Keep a shared mirrorvalue of 0. The low is 4. The high is 1000.
To reverse from remaining (a whole number) with built (a whole number). If remaining is less than 1 and built is not mirrorvalue, set reflection to 0, return. If remaining is less than 1, return. The digit is remaining modulo 10. Reverse from remaining divided by 10 with built times 10 plus digit.

To mirror from value (a whole number). Set mirrorvalue to value. Set reflection to 1. Reverse from value with 0.

To seek from value (a whole number). Set root to 0.
Repeat while root times root is less than value.
Add 1 to root.

To inspect from value (a whole number). Mirror from value. If reflection is 0, return. Seek from value. If root times root is value and root is more than 0, Mirror from root, if reflection is 1, add 1 to total.

For every value from low to high.
Inspect from value.

Show total.

Expected stdout

4

Open in Playground Standalone Page

#0907 · Sum of Subarray Minimums

Medium Array

The numbers is a list of 3, 1, 2, and 4. The last is the last position of numbers. The total is 0.
For every start from 0 to last.
The lowest is item start of numbers.
For every finish from start to last.
If item finish of numbers is less than lowest, set lowest to item finish of numbers. Increase total by lowest.

Show total.

Expected stdout

17

Open in Playground Standalone Page

#0908 · Smallest Range I

Easy Array

The numbers is a list of 0 and 10. The slack is 2. The last is the last position of numbers. The lowest is item 0 of numbers. The highest is item 0 of numbers.
For every spot from 1 to last.
If item spot of numbers is less than lowest, set lowest to item spot of numbers. If item spot of numbers is more than highest, set highest to item spot of numbers.

The spread is highest minus lowest. Decrease spread by 2 times slack. If spread is less than 0, set spread to 0. Show spread.

Expected stdout

6

Open in Playground Standalone Page

#0909 · Snakes and Ladders

Medium Array

The jumps is a list of -1, -1, -1, 9, -1, -1, -1, -1, -1, and -1. The visited is a list of 10 zeros. The distance is a list of 10 zeros. The queue is an empty list of whole numbers. Set item 1 of visited to 1. Place 1 into queue. The head is 0. The count is 1. The target is 9.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head.
For every roll from 1 to 6.
The landing is node plus roll.
If landing is at most target.
The jump is item landing of jumps. If jump is not -1, set landing to jump. If item landing of visited is 0, set item landing of visited to 1, set item landing of distance to item node of distance plus 1, Place landing into queue, add 1 to count.

Show item target of distance.

Expected stdout

1

Open in Playground Standalone Page

#0910 · Smallest Range II

Medium Array

The numbers is a list of 1, 3, and 6. The slack is 3. Sort numbers. The last is the last position of numbers. The best is item last of numbers minus item 0 of numbers.
For every spot from 0 to last minus 1.
The low is item 0 of numbers plus slack. The high is item last of numbers minus slack. If item (spot plus 1) of numbers minus slack is less than low, set low to item (spot plus 1) of numbers minus slack. If item spot of numbers plus slack is more than high, set high to item spot of numbers plus slack. The spread is high minus low. If spread is less than best, set best to spread.

Show best.

Expected stdout

3

Open in Playground Standalone Page

#0912 · Sort an Array

Medium Array

The numbers is a list of 5, 2, 3, and 1. The last is the last position of numbers.
For every spot from 0 to last.
For every look from spot plus 1 to last.
If item look of numbers is less than item spot of numbers, swap item spot of numbers and item look of numbers.

For each value in numbers, show value.

Expected stdout

1
2
3
5

Open in Playground Standalone Page

#0913 · Cat and Mouse

Hard Math

Keep a shared edges holding 1, 3, 0, 3, 0, and 2. Keep a shared starts holding 0, 2, 3, and 4. Keep a shared counts holding 2, 1, 1, and 2. Keep a shared catdegrees holding 0, 0, 0, and 0. Keep a shared colors as an empty list of whole numbers.
For every spot from 0 to 31.
Place 0 into colors.

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

Keep a shared queue as an empty list of whole numbers. Keep a shared count of 0. Keep a shared champion of 0. Keep a shared nowmouse of 0. Keep a shared nowcat of 0. Keep a shared nowturn of 0.
To relax from pred (a whole number) with winner (a whole number). If item pred of colors is not 0, return. The actor is pred modulo 2. If actor is 0 and winner is 1, set item pred of colors to winner, Place pred into queue, add 1 to count, return. If actor is 1 and winner is 2, set item pred of colors to winner, Place pred into queue, add 1 to count, return. Decrease item pred of remaining by 1. If item pred of remaining is 0, set item pred of colors to winner, Place pred into queue, add 1 to count.

To catparents from mouse (a whole number) with cat (a whole number) and winner (a whole number). The begin is item cat of starts. The total is item cat of counts.
For every slot from begin to begin plus total minus 1.
The step is item slot of edges.
If step is not 0.
The pred is (mouse times 4 plus step) times 2 plus 1. Relax from pred with winner.

To mouseparents from mouse (a whole number) with cat (a whole number) and winner (a whole number). The begin is item mouse of starts. The total is item mouse of counts.
For every slot from begin to begin plus total minus 1.
The step is item slot of edges.
If step is not 0 and step is not cat.
The pred is (step times 4 plus cat) times 2. Relax from pred with winner.

For every cat from 1 to 3.
The begin is item cat of starts. The total is item cat of counts.
For every slot from begin to begin plus total minus 1.
If item slot of edges is not 0, set item cat of catdegrees to item cat of catdegrees plus 1.

For every mouse from 0 to 3.
For every cat from 0 to 3.
For every turn from 0 to 1.
If mouse is 0, The index is (mouse times 4 plus cat) times 2 plus turn, set item index of colors to 1, Place index into queue, add 1 to count.

For every mouse from 0 to 3.
For every cat from 0 to 3.
For every turn from 0 to 1.
If mouse is more than 0 and mouse is cat, The index is (mouse times 4 plus cat) times 2 plus turn, set item index of colors to 2, Place index into queue, add 1 to count.

For every mouse from 1 to 3.
For every cat from 0 to 3.
For every turn from 0 to 1.
If mouse is not cat and cat is not 0.
The index is (mouse times 4 plus cat) times 2 plus turn. If turn is 0, set item index of remaining to item mouse of counts. If turn is 1, set item index of remaining to item cat of catdegrees.

The head is 0.
Repeat while head is less than count.
The state is item head of queue. Add 1 to head. Set champion to item state of colors. Set nowmouse to state divided by 8. The rest is state modulo 8. Set nowcat to rest divided by 2. Set nowturn to rest modulo 2. If nowturn is 0, Catparents from nowmouse with nowcat and champion. If nowturn is 1, Mouseparents from nowmouse with nowcat and champion.

The start is (1 times 4 plus 2) times 2. Show item start of colors.

Expected stdout

1

Open in Playground Standalone Page

#0914 · X of a Kind in a Deck of Cards

Easy Array

The deck is a list of 1, 2, 3, 4, 4, 3, 2, and 1. The counts is a list of 5 zeros.
For each card in deck.
Increase item card of counts by 1.

Keep a shared answer of 0.
To gather from left (a whole number) with right (a whole number). If right is 0, set answer to left, return. Gather from right with left modulo right.

For every rank from 0 to 4.
If item rank of counts is more than 0.
The amount is item rank of counts. If answer is 0, set answer to amount. If answer is more than 0, Gather from answer with amount.

If answer is at least 2, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0915 · Partition Array into Disjoint Intervals

Medium Array

The values is a list of 5, 0, 3, 8, and 6. The size is the number of items in values. The last is the last position of values. The ceilings is a list of size zeros. Set item 0 of ceilings to item 0 of values.
For every spot from 1 to last.
The prior is item (spot minus 1) of ceilings. If prior is more than item spot of values, set item spot of ceilings to prior, otherwise set item spot of ceilings to item spot of values.

The floors is a list of size zeros. Set item last of floors to item last of values.
For every step from 1 to last.
The spot is last minus step. The after is item (spot plus 1) of floors. If after is less than item spot of values, set item spot of floors to after, otherwise set item spot of floors to item spot of values.

The boundary is 0.
For every spot from 0 to last minus 1.
The upper is item spot of ceilings. The lower is item (spot plus 1) of floors. If upper is at most lower, set boundary to spot plus 1, then stop.

Report boundary.

Expected stdout

3

Open in Playground Standalone Page

#0916 · Word Subsets

Medium Array

Keep a shared current as empty text. Keep a shared alphabet as empty text. Keep a shared required as an empty list of whole numbers. Keep a shared seen of 0. Keep a shared enough of 0.
For every spot from 0 to 25.
Place 0 into required.

To require. Set seen to 0.
For every spot from 0 to 25.
Set seen to 0. The symbol is character spot of alphabet. For every place from 0 to the last position of current, if character place of current is symbol, add 1 to seen. If seen is more than item spot of required, set item spot of required to seen.

To check. Set enough to 1.
For every spot from 0 to 25.
Set seen to 0. The symbol is character spot of alphabet. For every place from 0 to the last position of current, if character place of current is symbol, add 1 to seen. If seen is less than item spot of required, set enough to 0.

The words is a list of "amazon", "apple", "facebook", "google", and "leetcode". The demands is a list of "e" and "o". Place "abcdefghijklmnopqrstuvwxyz" into alphabet.
For each demand in demands.
Set current to demand. Require.

For each word in words.
Set current to word. Check. If enough is 1, report word.

Expected stdout

facebook
google
leetcode

Open in Playground Standalone Page

#0917 · Reverse Only Letters

Easy String

Keep a shared code of 0. Keep a shared verdict of 0. Keep a shared lower as empty text. Keep a shared upper as empty text. Place "abcdefghijklmnopqrstuvwxyz" into lower. Place "ABCDEFGHIJKLMNOPQRSTUVWXYZ" into upper.

To classify. Set verdict to 0. For every spot from 0 to 25, if code is character spot of lower, set verdict to 1. For every spot from 0 to 25, if code is character spot of upper, set verdict to 1.

The source is "ab-cd". The pieces is an empty list of text.
For every spot from 0 to the last position of source.
The piece is the slice of source from spot to spot plus 1. Place piece into pieces.

The left is 0. The right is the last position of pieces.
Repeat while left is less than right.
The here is item left of pieces. The there is item right of pieces. Set code to character 0 of here. Classify. The leftok is verdict. Set code to character 0 of there. Classify. The rightok is verdict. If leftok is 1 and rightok is 1, swap item left of pieces and item right of pieces, add 1 to left, subtract 1 from right. If leftok is 0, add 1 to left. If rightok is 0, subtract 1 from right.

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

Expected stdout

dc-ba

Open in Playground Standalone Page

#0918 · Maximum Sum Circular Subarray

Medium Array

The numbers is a list of 1, -2, 3, and -2. The best is item 0 of numbers. The worst is item 0 of numbers. The ceiling is 0, while the floor is 0. The total is 0.
For every spot from 0 to the last position of numbers.
The value is item spot of numbers. Set ceiling to the larger of ceiling plus value and value. Set floor to the smaller of floor plus value and value. Set best to the larger of best and ceiling. Set worst to the smaller of worst and floor. Add value to total.

If best is less than 0, show best.
If best is at least 0.
The wrapped is total minus worst. The answer is the larger of best and wrapped. Show answer.

Expected stdout

3

Open in Playground Standalone Page

#0920 · Number of Music Playlists

Hard Math

The songs is 3, while the goal is 3. The gap is 1. The modulo is 1000000007. The width is songs plus 1. The size is (goal plus 1) times width. The ways is a list of size zeros. Set item 0 of ways to 1.
For every length from 1 to goal.
For every unique from 1 to songs.
If length is at least unique.
The at is length times width plus unique. The before is (length minus 1) times width plus (unique minus 1). The fresh is item before of ways times (songs minus unique plus 1). The prior is (length minus 1) times width plus unique. The old is item prior of ways. The factor is the larger of unique minus gap and 0. The reuse is old times factor. The total is fresh plus reuse. The reduced is total modulo modulo. Set item at of ways to reduced.

The target is goal times width plus songs. Report item target of ways.

Expected stdout

6

Open in Playground Standalone Page

#0921 · Minimum Add to Make Parentheses Valid

Medium String

The source is "())". The opened is 0. The added is 0.
For every spot from 0 to the last position of source.
The symbol is the slice of source from spot to spot plus 1. If symbol is "(", add 1 to opened. If symbol is ")", subtract 1 from opened. If opened is less than 0, add 1 to added, set opened to 0.

Report added.

Expected stdout

1

Open in Playground Standalone Page

#0922 · Sort Array By Parity II

Easy Array

The numbers is a list of 4, 2, 5, and 7. The size is the number of items in numbers. The answer is a list of size zeros. The even is 0. The odd is 1.
For each value in numbers.
If value modulo 2 is 0, set item even of answer to value, add 2 to even. If value modulo 2 is 1, set item odd of answer to value, add 2 to odd.

For each value in answer, show value.

Expected stdout

4
5
2
7

Open in Playground Standalone Page

#0923 · 3Sum With Multiplicity

Medium Array

The numbers is a list of 1, 1, 2, 2, 2, and 2. The target is 5. The last is the last position of numbers. The count is 0.
For every first from 0 to last.
For every second from first plus 1 to last.
For every third from second plus 1 to last.
The total is item first of numbers plus item second of numbers plus item third of numbers. If total is target, add 1 to count.

Report count.

Expected stdout

12

Open in Playground Standalone Page

#0924 · Minimize Malware Spread

Hard Array

Keep a shared edges holding 1 and 0. Keep a shared starts holding 0, 1, and 2. Keep a shared counts holding 1, 1, and 0. Keep a shared initial holding 0 and 1. Keep a shared infected holding 0, 0, and 0. Keep a shared banned of 0. Keep a shared total of 0.

To spread. For every node from 0 to 2, set item node of infected to 0. Set total to 0. The queue is an empty list of whole numbers. For each seed in initial, if seed is not banned and item seed of infected is 0, set item seed of infected to 1, add 1 to total, Place seed into queue. The head is 0.
Repeat while head is at most the last position of queue.
The node is item head of queue. Add 1 to head.
For every offset from 0 to item node of counts minus 1.
The next is item (item node of starts plus offset) of edges. If item next of infected is 0, set item next of infected to 1, add 1 to total, Place next into queue.

The best is 4. The answer is -1.
For each candidate in initial.
Set banned to candidate. Spread. If total is less than best, set best to total, set answer to candidate. If total is best and candidate is less than answer, set answer to candidate.

Report answer.

Expected stdout

0

Open in Playground Standalone Page

#0925 · Long Pressed Name

Easy String

The name is "alex", while the typed is "aaleex". The namechars is an empty list of text. The namecounts is an empty list of whole numbers.
For every spot from 0 to the last position of name.
The symbol is the slice of name from spot to spot plus 1. If spot is 0, Place symbol into namechars, Place 1 into namecounts.
If spot is more than 0.
The prior is the slice of name from spot minus 1 to spot. If symbol matches prior, The local is the last position of namecounts, set item local of namecounts to item local of namecounts plus 1. If symbol does not match prior, Place symbol into namechars, Place 1 into namecounts.

The typedchars is an empty list of text. The typedcounts is an empty list of whole numbers.
For every spot from 0 to the last position of typed.
The symbol is the slice of typed from spot to spot plus 1. If spot is 0, Place symbol into typedchars, Place 1 into typedcounts.
If spot is more than 0.
The prior is the slice of typed from spot minus 1 to spot. If symbol matches prior, The local is the last position of typedcounts, set item local of typedcounts to item local of typedcounts plus 1. If symbol does not match prior, Place symbol into typedchars, Place 1 into typedcounts.

The good is 1. The namegroups is the number of items in namechars. The typedgroups is the number of items in typedchars. If namegroups is not typedgroups, set good to 0.
For every spot from 0 to the last position of namechars.
If item spot of namechars does not match item spot of typedchars, set good to 0. If item spot of typedcounts is less than item spot of namecounts, set good to 0.

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

Expected stdout

true

Open in Playground Standalone Page

#0926 · Flip String to Monotone Increasing

Medium String

The source is "00110". The ones is 0. The flips is 0.
For every spot from 0 to the last position of source.
The symbol is the slice of source from spot to spot plus 1. If symbol is "1", add 1 to ones. If symbol is "0", add 1 to flips. If flips is more than ones, set flips to ones.

Report flips.

Expected stdout

1

Open in Playground Standalone Page

#0927 · Three Equal Parts

Hard Array

The bits is a list of 1, 0, 1, 0, and 1. The last is the last position of bits. The size is last plus 1. The ones is an empty list of whole numbers.
For every spot from 0 to last.
If item spot of bits is 1, Place spot into ones.

The count is the number of items in ones. The valid is 1.
If count is 0.
Show 0. Show last. Set valid to 0.

If count is more than 0 and count modulo 3 is not 0.
Show -1. Show -1. Set valid to 0.

The good is 1. The left is -1. The right is -1.
If valid is 1.
The group is count divided by 3. The firststart is item 0 of ones. The secondstart is item group of ones. The thirdstart is item (group plus group) of ones. The span is size minus thirdstart. Set left to firststart plus span minus 1. Set right to secondstart plus span.
For every offset from 0 to span minus 1.
The first is item (firststart plus offset) of bits. The second is item (secondstart plus offset) of bits. The third is item (thirdstart plus offset) of bits. If first is not second, set good to 0. If first is not third, set good to 0.

If valid is 1 and good is 0, show -1, show -1. If valid is 1 and good is 1, Show left, Show right.

Expected stdout

0
3

Open in Playground Standalone Page

#0928 · Minimize Malware Spread II

Hard Array

Keep a shared graph holding 1, 1, 0, 1, 1, 0, 0, 0, and 1. Keep a shared initial holding 0 and 1. Keep a shared infected holding 0, 0, and 0. Keep a shared candidate of 0. Keep a shared cases of 0.

To erase from spot (a whole number).
For every node from 0 to 2.
Set item node of infected to 0.

To spread from node (a whole number). If node is candidate, return. If item node of infected is 1, return. Set item node of infected to 1. The base is node times 3.
For every neighbor from 0 to 2.
If item (base plus neighbor) of graph is 1, Spread from neighbor.

To infect from spot (a whole number).
For each other in initial.
If other is not candidate, Spread from other.

To tally from spot (a whole number).
For every node from 0 to 2.
Increase cases by item node of infected.

The best is 4, while the bestnode is 0.
For every position from 0 to the last position of initial.
The cand is item position of initial. Set candidate to cand. Set cases to 0. Erase from 0. Infect from 0. Tally from 0. If cases is less than best, set best to cases, set bestnode to cand.

Show bestnode.

Expected stdout

0

Open in Playground Standalone Page

#0929 · Unique Email Addresses

Easy Array

Keep a shared emails holding "test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", and "testemail+david@lee.tcode.com". Keep a shared ordered as an empty list of text. Keep a shared built as empty text.

To normalize from position (a whole number). The email is item position of emails. The size is the number of characters in email. Set built to empty text. The at is 0, while the skipping is 0, while the keep is 0, while the dot is 0, while the plus is 0, while the sign is 0.
For every spot from 0 to size minus 1.
Set keep to 0. Set dot to 0. Set plus to 0. Set sign to 0. The here is the slice of email from spot to spot plus 1. If here matches ".", set dot to 1. If here matches "+", set plus to 1. If here matches "@", set sign to 1. If at is 0 and skipping is 0, set keep to 1. If at is 1, set keep to 1. If at is 0 and dot is 1, set keep to 0. If sign is 1, set keep to 0. If at is 0 and plus is 1, set keep to 0. If plus is 1, set skipping to 1. If keep is 1, Place here into built. If sign is 1 and at is 0, Place "@" into built. If sign is 1, set at to 1.

For every position from 0 to the last position of emails.
Normalize from position. Place built into ordered.

The last is the last position of ordered.
For every left from 0 to last.
For every right from left to last.
The rightword is item right of ordered. The leftword is item left of ordered. If rightword comes before leftword, swap item left of ordered and item right of ordered.

The count is 1.
For every position from 1 to last.
The here is item position of ordered. The prior is item (position minus 1) of ordered. If here does not match prior, add 1 to count.

Show count.

Expected stdout

2

Open in Playground Standalone Page

#0930 · Binary Subarrays With Sum

Medium Array

The numbers is a list of 1, 0, 1, 0, and 1. The goal is 2. The last is the last position of numbers. The total is 0.
For every start from 0 to last.
The window is 0.
For every finish from start to last.
Increase window by item finish of numbers. If window is goal, add 1 to total.

Show total.

Expected stdout

4

Open in Playground Standalone Page

#0931 · Minimum Falling Path Sum

Medium Array

The cells is a list of 2, 1, 3, 6, 5, 4, 7, 8, and 9. The width is 3, while the height is 3. The table is a list of 9 zeros.
For every column from 0 to 2.
Set item column of table to item column of cells.

For every level from 1 to height minus 1.
For every column from 0 to width minus 1.
The index is (level times width) plus column. The above is index minus width. The best is item above of table. If column is more than 0, set best to the smaller of best and item (above minus 1) of table. If column is less than width minus 1, set best to the smaller of best and item (above plus 1) of table. Set item index of table to best plus item index of cells.

The last is the last position of table. The answer is item (last minus 2) of table.
For every column from 1 to 2.
The candidate is item (last minus 2 plus column) of table. If candidate is less than answer, set answer to candidate.

Show answer.

Expected stdout

13

Open in Playground Standalone Page

#0932 · Beautiful Array

Medium Array

The target is 5. The working is a list of 1. The size is 1, while the nextsize is 0. Keep a shared next as an empty list of whole numbers.
To prune from position (a whole number).
Repeat while the number of items in next is more than 0.
Remove the last item from next.

Repeat while size is less than target.
Prune from 0. Set nextsize to 0.
For every step from 0 to (size times 2) minus 1.
The source is step modulo size. The value is item source of working. The doubled is value times 2. The candidate is doubled. If step is less than size, set candidate to doubled minus 1. If candidate is at most target, Place candidate into next, add 1 to nextsize. If step is (size times 2) minus 1, set working to next, set size to nextsize.

For each value in working, show value.

Expected stdout

1
5
3
2
4

Open in Playground Standalone Page

#0934 · Shortest Bridge

Medium Array

Keep a shared land holding 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, and 0. Keep a shared seen as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers.
For every spot from 0 to 15.
Place 0 into seen.

To mark from node (a whole number). If node is less than 0, return. If node is more than 15, return. If item node of land is 0, return. If item node of seen is 1, return. Set item node of seen to 1. Place node into queue. Mark from node minus 4. Mark from node plus 4. Mark from node minus 1. Mark from node plus 1.

Mark from 6. The distance is an empty list of whole numbers.
For every spot from 0 to 15.
Place 100 into distance.

For every spot from 0 to 15.
If item spot of seen is 1, set item spot of distance to 0.

Repeat 20 times.
For every node from 0 to 15.
The here is item node of distance. If node is at least 4 and here is less than 100 and item (node minus 4) of distance is more than here plus 1, set item (node minus 4) of distance to here plus 1. If node is at most 11 and here is less than 100 and item (node plus 4) of distance is more than here plus 1, set item (node plus 4) of distance to here plus 1. If node modulo 4 is more than 0 and here is less than 100 and item (node minus 1) of distance is more than here plus 1, set item (node minus 1) of distance to here plus 1. If node modulo 4 is less than 3 and here is less than 100 and item (node plus 1) of distance is more than here plus 1, set item (node plus 1) of distance to here plus 1.

The answer is 100.
For every node from 0 to 15.
If item node of land is 1 and item node of seen is 0 and item node of distance is less than answer, set answer to item node of distance.

Show answer minus 1.

Expected stdout

1

Open in Playground Standalone Page

#0935 · Knight Dialer

Medium Dynamic Programming

The steps is 2. The origins is an empty list of whole numbers. The targets is an empty list of whole numbers. Place 0 into origins. Place 4 into targets. Place 0 into origins. Place 6 into targets. Place 1 into origins. Place 6 into targets. Place 1 into origins. Place 8 into targets. Place 2 into origins. Place 7 into targets. Place 2 into origins. Place 9 into targets. Place 3 into origins. Place 4 into targets. Place 3 into origins. Place 8 into targets. Place 4 into origins. Place 0 into targets. Place 4 into origins. Place 3 into targets. Place 4 into origins. Place 9 into targets. Place 6 into origins. Place 0 into targets. Place 6 into origins. Place 1 into targets. Place 6 into origins. Place 7 into targets. Place 7 into origins. Place 2 into targets. Place 7 into origins. Place 6 into targets. Place 8 into origins. Place 1 into targets. Place 8 into origins. Place 3 into targets. Place 9 into origins. Place 2 into targets. Place 9 into origins. Place 4 into targets. The current is an empty list of whole numbers.
For every digit from 0 to 9.
Place 1 into current.

The next is an empty list of whole numbers.
For every digit from 0 to 9.
Place 0 into next.

Repeat while steps is more than 1.
For every step from 0 to 29.
If step is less than 10, set item step of next to 0. If step is at least 10, The edge is step minus 10, The source is item edge of origins, The target is item edge of targets, The contribution is item source of current, set item target of next to (item target of next plus contribution) modulo 1000000007. If step is 29, set current to next. Subtract 1 from steps.

The total is 0.
For every digit from 0 to 9.
Increase total by item digit of current.

Show total.

Expected stdout

20

Open in Playground Standalone Page