The TransCompilator

HTML chunk · 50 solved programs

Chunk 16 · #0987–#1037

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

#0987 · Vertical Order Traversal of a Binary Tree

Hard Sorting

Keep a shared labels holding 3, 9, 20, 15, and 7. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, 4, -1, and -1. Keep a shared order as an empty list of whole numbers. Keep a shared keys as an empty list of whole numbers. Keep a shared shown of 0. Keep a shared seen of 0.

To gather from node (a whole number) with code (a whole number). If node is less than 0, return. The column is code divided by 10000 minus 1000. The depth is code modulo 10000. Place item node of labels into order. The key is (column plus 1000) times 100000000 plus depth times 10000 plus item node of labels. Place key into keys. Gather from item node of lefts with (code minus 10000). Gather from item node of rights with (code plus 10000).

Gather from 0 with 10000000.
For every spot from 1 to the last position of keys.
The hold is item spot of keys. The keep is item spot of order. The prior is spot minus 1.
Repeat while prior is at least 0 and item prior of keys is more than hold.
Set item (prior plus 1) of keys to item prior of keys. Set item (prior plus 1) of order to item prior of order. Subtract 1 from prior. Set item (prior plus 1) of keys to hold. Set item (prior plus 1) of order to keep.

For every spot from 0 to the last position of keys.
The packed is item spot of keys. The column is packed divided by 100000000 minus 1000. If seen is more than 0 and column is not shown, show "---". Show item spot of order. Set shown to column. Add 1 to seen.

Expected stdout

9
---
3
15
---
20
---
7

Open in Playground Standalone Page

#0988 · Smallest String Starting From Leaf

Medium String

Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared labels holding 0, 1, 2, 3, 4, 3, and 4. 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 trail as an empty list of text. Keep a shared bestpath as an empty list of text. Keep a shared final as empty text. Keep a shared have of 0. Keep a shared smaller of 0.

To drain. The count is the number of items in bestpath.
Repeat while count is more than 0.
remove the last item from bestpath. Subtract 1 from count.

To record. For each piece in trail, The saved is a copy of piece, Place saved into bestpath.

To store. Drain. Record.

To compare. Set smaller to 0. The spot is the last position of trail. The other is the last position of bestpath.
Repeat while spot is at least 0 and other is at least 0.
The left is item spot of trail. The right is item other of bestpath. If left comes before right, set smaller to 1, return. If right comes before left, return. Subtract 1 from spot. Subtract 1 from other.

To visit. If have is 0, store, set have to 1, return. Compare. If smaller is 1, store.

To report. The spot is the last position of bestpath.
Repeat while spot is at least 0.
Place item spot of bestpath into final. Subtract 1 from spot.

To look from node (a whole number). If node is less than 0, return. The piece is the slice of alphabet from item node of labels to item node of labels plus 1. The held is a copy of piece. Place held into trail. If item node of lefts is less than 0 and item node of rights is less than 0, visit. Look from item node of lefts. Look from item node of rights. Remove the last item from trail.

Look from 0. Report. Show final.

Expected stdout

dba

Open in Playground Standalone Page

#0989 · Add to Array-Form of Integer

Easy Array

The digits is a list of 1, 2, 0, and 0. The extra is 34. The answers is an empty list of whole numbers. The spot is the last position of digits. The carry is extra.
Repeat while spot is at least 0 or carry is more than 0.
If spot is at least 0, The sum is item spot of digits plus carry, Place (sum modulo 10) into answers, set carry to sum divided by 10. If spot is less than 0, Place (carry modulo 10) into answers, set carry to carry divided by 10. Subtract 1 from spot.

The spot is the last position of answers.
Repeat while spot is at least 0.
Show item spot of answers. Subtract 1 from spot.

Expected stdout

1
2
3
4

Open in Playground Standalone Page

#0990 · Satisfiability of Equality Equations

Medium Array

Keep a shared rules holding "a==b", "b!=c", and "c==a". Keep a shared parent as an empty list of whole numbers. For every slot from 0 to 25, Place slot into parent. Keep a shared alphabet as empty text. Place "abcdefghijklmnopqrstuvwxyz" into alphabet. Keep a shared letter as empty text. Keep a shared leftpairs as an empty list of whole numbers. Keep a shared rightpairs as an empty list of whole numbers. Keep a shared slot of 0. Keep a shared ancestor of 0. Keep a shared valid of 1.

To locate. Set slot to 0. The current is slot.
Repeat while current is less than 26.
The symbol is the slice of alphabet from current to current plus 1. If symbol matches letter, set slot to current, stop. Add 1 to current.

To find from place (a whole number). Set ancestor to place. The current is ancestor.
Repeat while item current of parent is not current.
The next is item current of parent. Set current to next. Set ancestor to next.

To connect from first (a whole number) with second (a whole number). Find from first. The leftroot is ancestor. Find from second. The rightroot is ancestor. If leftroot is not rightroot, set item leftroot of parent to rightroot.

To check from first (a whole number) with second (a whole number). Find from first. The leftroot is ancestor. Find from second. The rightroot is ancestor. If leftroot is rightroot, set valid to 0.

For each rule in rules.
The leftletter is the slice of rule from 0 to 1. The mark is the slice of rule from 1 to 2. The rightletter is the slice of rule from 3 to 4. Set letter to leftletter. Locate. The leftslot is slot. Set letter to rightletter. Locate. The rightslot is slot. If mark matches "=", connect from leftslot with rightslot, otherwise Place leftslot into leftpairs, Place rightslot into rightpairs.

For every which from 0 to the last position of leftpairs.
The one is item which of leftpairs. The two is item which of rightpairs. Check from one with two.

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

Expected stdout

false

Open in Playground Standalone Page

#0991 · Broken Calculator

Medium Math

The start is 2, while the target is 3. The steps is 0, while the current is target.
Repeat while current is more than start.
If current modulo 2 is 0 and current divided by 2 is at least start, set current to current divided by 2. Otherwise, set current to current plus 1. Add 1 to steps.

Increase steps by current minus start. Show steps.

Expected stdout

2

Open in Playground Standalone Page

#0992 · Subarrays with K Different Integers

Hard Array

Keep a shared numbers holding 1, 2, 1, 2, and 3. Keep a shared counts as a list of 6 zeros. Keep a shared limit of 0. Keep a shared beginning of 0. Keep a shared distinct of 0. Keep a shared total of 0. Keep a shared window of 0.

To wipe. For every spot from 0 to 5, set item spot of counts to 0.

To tighten from right (a whole number).
Repeat while distinct is more than limit.
The position is beginning. The old is item position of numbers. Decrease item old of counts by 1. If item old of counts is 0, set distinct to distinct minus 1. Increase beginning by 1. Set window to right minus beginning plus 1.

To tally with bound (a whole number). Set limit to bound. Wipe. Set total to 0. Set beginning to 0. Set distinct to 0.
For every right from 0 to 4.
The value is item right of numbers. If item value of counts is 0, add 1 to distinct. Increase item value of counts by 1. Set window to right minus beginning plus 1. Tighten from right. Increase total by window.

Tally with 2. The high is total. Tally with 1. The low is total. Show high minus low.

Expected stdout

7

Open in Playground Standalone Page

#0993 · Cousins in Binary Tree

Easy Depth-First Search

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

To walk from node (a whole number) with parent (a whole number) and level (a whole number). If node is less than 0, return. Set item node of depths to level. Set item node of parents to parent. Walk from item node of lefts with node and level plus 1. Walk from item node of rights with node and level plus 1.

Walk from 0 with -1 and 0. The first is -1, while the second is -1.
For every spot from 0 to 4.
If item spot of values is 4, set second to spot. If item spot of values is 5, set first to spot.

The verdict is 0. If item first of depths is item second of depths and item first of parents is not item second of parents, set verdict to 1. If verdict is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0994 · Rotting Oranges

Medium Array

Keep a shared grid holding 2, 1, 1, 1, 1, 0, 0, 1, and 1. Keep a shared distances as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. For every cell from 0 to 8, Place 0 into distances. For every cell from 0 to 8, Place 0 into queue. Keep a shared fresh of 0. Keep a shared count of 0.
For every cell from 0 to 8.
If item cell of grid is 1, add 1 to fresh. If item cell of grid is 2, set item count of queue to cell, add 1 to count.

To spread from node (a whole number). The offsets is a list of -3, 3, -1, and 1. The row is node divided by 3, while the column is node modulo 3.
For every which from 0 to 3.
The offset is item which of offsets. The target is node plus offset. The level is item node of distances plus 1. The open is 1. If which is 0 and row is 0, set open to 0. If which is 1 and row is 2, set open to 0. If which is 2 and column is 0, set open to 0. If which is 3 and column is 2, set open to 0.
If open is 1 and item target of grid is 1.
Set item target of grid to 2. Set item target of distances to level. Set item count of queue to target. Add 1 to count. Decrease fresh by 1.

The head is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Spread from node.

The best is 0.
For every cell from 0 to 8.
If item cell of distances is more than best, set best to item cell of distances.

If fresh is more than 0, show -1, otherwise show best.

Expected stdout

4

Open in Playground Standalone Page

#0995 · Minimum Number of K Consecutive Bit Flips

Hard Array

The bits is a list of 0, 0, 0, 1, 0, 1, 1, and 0. The ends is a list of 9 zeros. The size is 8, while the span is 3. The flips is 0, while the active is 0, while the impossible is 0.
For every spot from 0 to size minus 1.
If item spot of ends is 1, set active to 1 minus active. The value is item spot of bits. If active is 1, set value to 1 minus value.
If value is 0 and spot plus span is at most size.
Set item (spot plus span) of ends to 1. Add 1 to flips. Set active to 1 minus active.
If value is 0 and spot plus span is more than size.
Set impossible to 1.

If impossible is 1, show -1, otherwise show flips.

Expected stdout

3

Open in Playground Standalone Page

#0996 · Number of Squareful Arrays

Hard Array

Keep a shared numbers holding 1, 17, and 8. Sort numbers. Keep a shared used as an empty list of whole numbers. Keep a shared order as an empty list of whole numbers. For every spot from 0 to 2, Place 0 into used. For every spot from 0 to 2, Place 0 into order. Keep a shared squares holding 1, 4, 9, 16, 25, and 36. Keep a shared total of 0.

To seed.
For every spot from 0 to 2.
If item spot of used is 1, skip to the next one. If spot is more than 0 and item spot of numbers is item (spot minus 1) of numbers and item (spot minus 1) of used is 0, skip to the next one. Set item 0 of order to item spot of numbers. Set item spot of used to 1. Extend from 1. Set item spot of used to 0.

To extend from depth (a whole number). If depth is 3, add 1 to total, return.
For every spot from 0 to 2.
If item spot of used is 1, skip to the next one. If spot is more than 0 and item spot of numbers is item (spot minus 1) of numbers and item (spot minus 1) of used is 0, skip to the next one. The previous is item (depth minus 1) of order. The candidate is previous plus item spot of numbers. If squares does not contain candidate, skip to the next one. Set item depth of order to item spot of numbers. Set item spot of used to 1. Extend from depth plus 1. Set item spot of used to 0.

Seed. Show total.

Expected stdout

2

Open in Playground Standalone Page

#0997 · Find the Town Judge

Easy Array

The trust is a list of 1, 3, 2, and 3. The scores is a list of 4 zeros.
For every pair from 0 to 1.
The giver is item (pair times 2) of trust. The taker is item (pair times 2 plus 1) of trust. Decrease item giver of scores by 1. Increase item taker of scores by 1.

The judge is -1.
For every person from 1 to 3.
If item person of scores is 2, set judge to person.

Show judge.

Expected stdout

3

Open in Playground Standalone Page

#0998 · Maximum Binary Tree II

Medium Tree

Keep a shared values holding 4, 1, 3, 2, and 0. Keep a shared lefts holding 1, -1, 3, -1, and -1. Keep a shared rights holding 2, -1, -1, -1, and -1. Keep a shared desired of 5. Keep a shared spot of 0.

To descend from current (a whole number). Set spot to current. The right is item current of rights. If right is at least 0 and item right of values is more than desired, Descend from right.

The root is 0.
If desired is more than item 0 of values.
Set item 4 of values to desired. Set item 4 of lefts to root. Set item 4 of rights to -1. Set root to 4.

If desired is at most item 0 of values.
Descend from 0. The branch is spot. The hole is item branch of rights. Set item 4 of values to desired. Set item 4 of lefts to hole. Set item 4 of rights to -1. Set item branch of rights to 4.

The queue is a list of 16 zeros. Set item 0 of queue to root. The head is 0, while the count is 1. The order is an empty list of whole numbers.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. If node is less than 0, Place -1 into order.
If node is at least 0.
Place item node of values into order. Set item count of queue to item node of lefts. Add 1 to count. Set item count of queue to item node of rights. Add 1 to count.

The cut is the last position of order.
Repeat while cut is at least 0 and item cut of order is -1.
Decrease cut by 1.

For every spot from 0 to cut.
Show item spot of order.

Expected stdout

5
4
-1
1
3
-1
-1
2

Open in Playground Standalone Page

#0999 · Available Captures for Rook

Easy Array

Keep a shared board holding "...p....", "...p....", "........", ".p.R..p.", "........", "...p....", "........", and "........". Keep a shared captures of 0.

To sweep from spot (a whole number) with step (a whole number) and steps (a whole number). The cursor is spot plus step, while remaining is steps, while open is 0.
Repeat while remaining is more than 0 and open is 0.
The row is cursor divided by 8, while the column is cursor modulo 8. The line is item row of board. The symbol is the slice of line from column to column plus 1. If symbol matches "p", add 1 to captures, set open to 1. If symbol matches "R", set open to 1. Increase cursor by step. Decrease remaining by 1.

The rook is -1.
For every spot from 0 to 63.
The row is spot divided by 8, while the column is spot modulo 8. The line is item row of board. The symbol is the slice of line from column to column plus 1. If symbol matches "R", set rook to spot.

The rank is rook divided by 8, while the file is rook modulo 8. Sweep from rook with -8 and rank. Sweep from rook with 8 and 7 minus rank. Sweep from rook with -1 and file. Sweep from rook with 1 and 7 minus file. Show captures.

Expected stdout

4

Open in Playground Standalone Page

#1000 · Minimum Cost to Merge Stones

Hard Array

The stones is a list of 3, 2, 4, and 1. The prefix is a list of 5 zeros.
For every spot from 0 to 3.
Set item (spot plus 1) of prefix to item spot of prefix plus item spot of stones.

The table is a list of 16 zeros.
For every length from 2 to 4.
For every start from 0 to 4 minus length.
The finish is start plus length minus 1. The cell is start times 4 plus finish. The before is item start of prefix, while the after is item (finish plus 1) of prefix. The range is after minus before. Set item cell of table to 1000000.
For every split from start to finish minus 1.
The beyond is split plus 1. The leftcell is start times 4 plus split. The rightcell is beyond times 4 plus finish. The candidate is item leftcell of table plus item rightcell of table plus range. If candidate is less than item cell of table, set item cell of table to candidate.

Show item 3 of table.

Expected stdout

20

Open in Playground Standalone Page

#1001 · Grid Illumination

Hard Array

The lamp rows is a list of 0 and 4. The lamp columns is a list of 0 and 4. The lit is a list of 1 and 1. The query rows is a list of 1 and 1. The query columns is a list of 1 and 0. The answers is an empty list of whole numbers.
For every query from 0 to 1.
The target row is item query of query rows. The target column is item query of query columns. The glowing is 0.
For every lamp from 0 to 1.
If item lamp of lit is 1 and item lamp of lamp rows is target row, set glowing to 1. If item lamp of lit is 1 and item lamp of lamp columns is target column, set glowing to 1. The row gap is the absolute value of (item lamp of lamp rows minus target row). The column gap is the absolute value of (item lamp of lamp columns minus target column). If item lamp of lit is 1 and row gap is column gap, set glowing to 1. If item lamp of lit is 1 and row gap is at most 1 and column gap is at most 1, set item lamp of lit to 0. If lamp is 1, Place glowing into answers.

The line is empty text.
For each answer in answers.
If the length of line is more than 0, Place "," into line. Place format whole number answer as text into line.

Show line.

Expected stdout

1,0

Open in Playground Standalone Page

#1002 · Find Common Characters

Easy Array

Keep a shared words as an empty list of text. Keep a shared alphabet as empty text. Keep a shared tally of 0. Place "bella" into words. Place "label" into words. Place "roller" into words. Place "abcdefghijklmnopqrstuvwxyz" into alphabet.

To measure from index (a whole number) with letter (a whole number). The word is item index of words. The symbol is the slice of alphabet from letter to letter plus 1. Set tally to 0.
For every spot from 0 to the last position of word.
The piece is the slice of word from spot to spot plus 1. If piece matches symbol, add 1 to tally.

The least is a list of 26 zeros.
For every letter from 0 to 25.
The smallest is 100.
For every index from 0 to 2.
Measure from index with letter. If tally is less than smallest, set smallest to tally. If index is 2, set item letter of least to smallest.

The found is an empty list of text.
For every letter from 0 to 25.
For every copy from 0 to item letter of least minus 1.
The piece is the slice of alphabet from letter to letter plus 1. Place piece into found.

The line is empty text.
For each piece in found.
If the length of line is more than 0, Place "," into line. Place piece into line.

Show line.

Expected stdout

e,l,l

Open in Playground Standalone Page

#1003 · Check If Word Is Valid After Substitutions

Medium String

The source is "aabcbc". The stack is an empty list of text.
For every spot from 0 to the last position of source.
The symbol is the slice of source from spot to spot plus 1. Place symbol into stack. The size is the number of items in stack.
If size is at least 3.
The bottom is item (size minus 3) of stack. The middle is item (size minus 2) of stack. The top is item (size minus 1) of stack. The formed is 0. If bottom matches "a", add 1 to formed. If middle matches "b", add 1 to formed. If top matches "c", add 1 to formed. If formed is 3, remove the last item from stack, remove the last item from stack, remove the last item from stack.

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

Expected stdout

true

Open in Playground Standalone Page

#1004 · Max Consecutive Ones III

Medium Array

Keep a shared bits as an empty list of whole numbers. Keep a shared limit of 0. Keep a shared opening of 0. Keep a shared zeros of 0. Keep a shared best of 0.

To shrink from spot (a whole number).
Repeat while zeros is more than limit.
If item spot of bits is 0, subtract 1 from zeros. Add 1 to spot. Set opening to spot.

Set limit to 2. Place 1 into bits. Place 1 into bits. Place 1 into bits. Place 0 into bits. Place 0 into bits. Place 0 into bits. Place 1 into bits. Place 1 into bits. Place 1 into bits. Place 1 into bits. Place 0 into bits.
For every right from 0 to the last position of bits.
If item right of bits is 0, add 1 to zeros. Shrink from opening. The width is right minus opening plus 1. If width is more than best, set best to width.

Show best.

Expected stdout

6

Open in Playground Standalone Page

#1005 · Maximize Sum Of Array After K Negations

Easy Array

The scores is a list of 3, -1, 0, and 2. The budget is 3. Sort scores.
For every spot from 0 to the last position of scores.
If budget is more than 0 and item spot of scores is less than 0, set item spot of scores to 0 minus item spot of scores, subtract 1 from budget.

The smallest is item 0 of scores.
For every spot from 1 to the last position of scores.
If item spot of scores is less than smallest, set smallest to item spot of scores.

The total is 0.
For each value in scores.
Add value to total.

The penalty is smallest plus smallest. If budget modulo 2 is 1, subtract penalty from total.

Show total.

Expected stdout

6

Open in Playground Standalone Page

#1006 · Clumsy Factorial

Medium Math

The count is 10. The stack is an empty list of whole numbers. Place count into stack. The spot is count minus 1. The phase is 0.
Repeat while spot is at least 1.
The size is the number of items in stack. The top is item (size minus 1) of stack. If phase is 0, set item (size minus 1) of stack to top times spot. If phase is 1, set item (size minus 1) of stack to top divided by spot. If phase is 2, Place spot into stack. If phase is 3, Place 0 minus spot into stack. Set phase to (phase plus 1) modulo 4. Decrease spot by 1.

The total is 0.
For each entry in stack.
Add entry to total.

Show total.

Expected stdout

12

Open in Playground Standalone Page

#1007 · Minimum Domino Rotations For Equal Row

Medium Array

Keep a shared tops holding 2, 1, 2, 4, 2, and 2. Keep a shared bottoms holding 5, 2, 6, 2, 3, and 2. Keep a shared best of 0.

To check with candidate (a whole number). The possible is 1. The topcost is 0. The bottomcost is 0.
For every spot from 0 to 5.
If item spot of tops is not candidate and item spot of bottoms is not candidate, set possible to 0. If item spot of tops is not candidate, add 1 to topcost. If item spot of bottoms is not candidate, add 1 to bottomcost. If spot is 5, The take is the smaller of topcost and bottomcost, If possible is 1 and take is less than best, set best to take.

Set best to 100. The firsttop is item 0 of tops. The firstbottom is item 0 of bottoms. Check with firsttop. Check with firstbottom. If best is 100, show -1, otherwise show best.

Expected stdout

2

Open in Playground Standalone Page

#1008 · Construct Binary Search Tree from Preorder Traversal

Medium Array

Keep a shared preorder holding 8, 5, 1, 7, 10, and 12. Keep a shared lefts holding -1, -1, -1, -1, -1, and -1. Keep a shared rights holding -1, -1, -1, -1, -1, and -1. Keep a shared next slot as 0. Keep a shared child of 0.

To build from bound (a whole number). Set child to -1. If next slot is more than the last position of preorder, return. The slot is next slot. The current is item slot of preorder. If current is more than bound, return. Add 1 to next slot. Build from current. Set item slot of lefts to child. Build from bound. Set item slot of rights to child. Set child to slot.

Build from 1000000. The queue is an empty list of whole numbers. The order is an empty list of whole numbers. Place 0 into queue. The head is 0. The count is 1.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. Place node into order. If node is at least 0, Place item node of lefts into queue, Place item node of rights into queue, add 2 to count.

The pieces is an empty list of text.
For each node in order.
If node is less than 0, Place "null" into pieces, otherwise The piece is format whole number item node of preorder as text, Place piece into pieces.

The tail is the last position of pieces.
Repeat while tail is at least 0.
If item tail of pieces matches "null", remove the last item from pieces, set tail to tail minus 1, otherwise stop.

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

Expected stdout

8,5,10,1,7,null,12

Open in Playground Standalone Page

#1009 · Complement of Base 10 Integer

Easy Bit Manipulation

The number is 5. The rest is number. The power is 1.
Repeat while rest is more than 0.
Set power to power times 2. Set rest to rest divided by 2.

The mask is power minus 1. The answer is mask minus number. Show answer.

Expected stdout

2

Open in Playground Standalone Page

#1010 · Pairs of Songs With Total Durations Divisible by 60

Medium Array

The lengths is a list of 30, 20, 150, 100, and 40. The remainders is a list of 60 zeros. The total is 0.
For every spot from 0 to the last position of lengths.
The rest is item spot of lengths modulo 60. The partner is 0. If rest is not 0, set partner to 60 minus rest. Increase total by item partner of remainders. Increase item rest of remainders by 1.

Show total.

Expected stdout

3

Open in Playground Standalone Page

#1011 · Capacity To Ship Packages Within D Days

Medium Array

Keep a shared weights holding 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. Keep a shared days of 0. Keep a shared fits of 0.

To test with capacity (a whole number). Set fits to 1. The used is 0. The needed is 1.
For every spot from 0 to the last position of weights.
The parcel is item spot of weights. If used plus parcel is more than capacity, add 1 to needed, set used to 0. Add parcel to used. If needed is more than days, set fits to 0, stop.

Set days to 5. The low is 1.
For each parcel in weights.
If parcel is more than low, set low to parcel.

The high is 0.
For each parcel in weights.
Add parcel to high.

Repeat while low is less than high.
The middle is the midpoint of low and high. Test with middle. If fits is 1, set high to middle, otherwise set low to middle plus 1.

Show low.

Expected stdout

15

Open in Playground Standalone Page

#1012 · Numbers With Repeated Digits

Hard Math

Keep a shared duplicated of 0.

To screen from number (a whole number). Set duplicated to 0. If number is 0, return. The seen is a list of 10 zeros. The rest is number.
Repeat while rest is more than 0.
The digit is rest modulo 10. If item digit of seen is 1, set duplicated to 1. Set item digit of seen to 1. Set rest to rest divided by 10.

The limit is 100. The total is 0.
For every number from 0 to limit.
Screen from number. If duplicated is 1, add 1 to total.

Show total.

Expected stdout

10

Open in Playground Standalone Page

#1013 · Partition Array Into Three Parts With Equal Sum

Easy Array

The numbers is a list of 0, 2, 1, -6, 6, -7, 9, 1, 2, 0, and 1. The total is 0.
For each value in numbers.
Add value to total.

The target is 0. If total modulo 3 is 0, set target to total divided by 3. The valid is 0.
If total modulo 3 is 0.
The running is 0. The finish is the last position of numbers. The cuts is 0.
For every spot from 0 to the last position of numbers.
Increase running by item spot of numbers. If running is target and cuts is less than 2 and spot is less than finish, add 1 to cuts, set running to 0. If spot is finish and cuts is 2 and running is target, set valid to 1.

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

Expected stdout

true

Open in Playground Standalone Page

#1014 · Best Sightseeing Pair

Medium Array

The values is a list of 8, 1, 5, 2, and 6. The best is 0. The carry is item 0 of values.
For every spot from 1 to the last position of values.
The candidate is carry plus item spot of values minus spot. If candidate is more than best, set best to candidate. The offer is item spot of values plus spot. If offer is more than carry, set carry to offer.

Show best.

Expected stdout

11

Open in Playground Standalone Page

#1015 · Smallest Integer Divisible by K

Medium Math

The divisor is 3. The remainder is 0. The length is 0. The answer is -1.
Repeat while length is less than divisor and answer is less than 0.
Add 1 to length. Set remainder to (remainder times 10 plus 1) modulo divisor. If remainder is 0, set answer to length.

Show answer.

Expected stdout

3

Open in Playground Standalone Page

#1016 · Binary String With Substrings Representing 1 To N

Medium String

Keep a shared source as empty text. Keep a shared parts as an empty list of text. Keep a shared encoded as empty text. Keep a shared found of 0. Keep a shared powers as an empty list of whole numbers. Keep a shared limit of 0.

To erase.
Repeat while the last position of parts is at least 0.
Remove the last item from parts.

To encode from number (a whole number) with power (a whole number). The rest is number.
Repeat while power is more than 0.
The bit is 0. If rest is at least power, set bit to 1, set rest to rest minus power. The piece is format whole number bit as text. Place piece into parts. Set power to power divided by 2.

To assemble. Set found to 0. Set encoded to join parts with "".

To seek from marker (a whole number). The lastpiece is the last position of encoded. The size is lastpiece plus 1. The lastsource is the last position of source. The endmark is lastsource plus 1. The final is endmark minus size plus 1.
For every start from 0 to final.
The piece is the slice of source from start to start plus size. If piece is encoded, set found to 1.

Set limit to 3. Place "0110" into source. The powers is a list of 4 zeros. Set item 1 of powers to 1.
For every number from 1 to limit.
The power is 1.
Repeat while power times 2 is at most number.
Set power to power times 2. If power times 2 is more than number, set item number of powers to power.

The valid is 1.
For every number from 1 to limit.
The power is item number of powers. Erase. Encode from number with power. Assemble. Seek from 0. If found is 0, set valid to 0.

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

Expected stdout

true

Open in Playground Standalone Page

#1017 · Convert to Base -2

Medium Math

The number is 2. The rest is number. The reversed is an empty list of text.
Repeat while rest is not 0.
The bit is rest modulo 2. If bit is less than 0, set bit to 0 minus bit. The piece is format whole number bit as text. Place piece into reversed. The adjusted is rest minus bit. Set rest to adjusted divided by -2.

The result is empty text. The last is the last position of reversed.
For every step from 0 to last.
The place is last minus step. Place item place of reversed into result.

Show result.

Expected stdout

110

Open in Playground Standalone Page

#1018 · Binary Prefix Divisible By 5

Easy Array

The bits is a list of 0, 1, and 1. The answers is an empty list of text. The remainder is 0.
For every spot from 0 to the last position of bits.
Set remainder to (remainder times 2 plus item spot of bits) modulo 5. If remainder is 0, Place "true" into answers, otherwise Place "false" into answers.

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

Expected stdout

true,false,false

Open in Playground Standalone Page

#1019 · Next Greater Node In Linked List

Medium Array

Keep a shared scores holding 2, 7, 4, 3, and 5. The answers is an empty list of whole numbers. Keep a shared following of 0.

To seek from start (a whole number) with value (a whole number). Set following to 0. The begin is start plus 1. The finish is the last position of scores.
For every probe from begin to finish.
If item probe of scores is more than value, set following to item probe of scores, stop.

For every spot from 0 to the last position of scores.
The value is item spot of scores. Seek from spot with value. Place following into answers.

The line is empty text.
For each answer in answers.
If the length of line is more than 0, Place "," into line. Place format whole number answer as text into line.

Show line.

Expected stdout

7,0,5,5,0

Open in Playground Standalone Page

#1020 · Number of Enclaves

Medium Array

The grid is a list of 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, and 0. The seen is a list of 16 zeros. The queue is an empty list of whole numbers. The count is 0.
For every cell from 0 to 15.
The rank is cell divided by 4. The column is cell modulo 4. The edge is 0. If rank is 0 or rank is 3 or column is 0 or column is 3, set edge to 1. If edge is 1 and item cell of grid is 1, set item cell of seen to 1, Place cell into queue, add 1 to count.

The head is 0.
Repeat while head is less than count.
The node is item head of queue. Add 1 to head. The rank is node divided by 4. The column is node modulo 4. If rank is more than 0 and item (node minus 4) of grid is 1 and item (node minus 4) of seen is 0, set item (node minus 4) of seen to 1, Place (node minus 4) into queue, add 1 to count. If rank is less than 3 and item (node plus 4) of grid is 1 and item (node plus 4) of seen is 0, set item (node plus 4) of seen to 1, Place (node plus 4) into queue, add 1 to count. If column is more than 0 and item (node minus 1) of grid is 1 and item (node minus 1) of seen is 0, set item (node minus 1) of seen to 1, Place (node minus 1) into queue, add 1 to count. If column is less than 3 and item (node plus 1) of grid is 1 and item (node plus 1) of seen is 0, set item (node plus 1) of seen to 1, Place (node plus 1) into queue, add 1 to count.

The total is 0.
For every cell from 0 to 15.
If item cell of grid is 1 and item cell of seen is 0, add 1 to total.

Show total.

Expected stdout

3

Open in Playground Standalone Page

#1021 · Remove Outermost Parentheses

Easy String

Keep a shared source as empty text. Place "(()())(())" into source. The built is empty text. The depth 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. The opening is 0. If symbol is "(", set opening to 1. If opening is 1 and depth is more than 0, Place symbol into built. If opening is 1, add 1 to depth. If opening is 0, subtract 1 from depth. If opening is 0 and depth is more than 0, Place symbol into built.

Show built.

Expected stdout

()()()

Open in Playground Standalone Page

#1022 · Sum of Root To Leaf Binary Numbers

Easy Depth-First Search

Keep a shared values holding 1, 0, 1, 0, 1, 0, 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 total of 0.

To gather from node (a whole number) with path (a whole number). If node is less than 0, return. The carried is path times 2 plus item node of values. The left is item node of lefts. If left is less than 0 and item node of rights is less than 0, add carried to total, return. Gather from left with carried. Gather from item node of rights with carried.

Gather from 0 with 0. Show total.

Expected stdout

22

Open in Playground Standalone Page

#1023 · Camelcase Matching

Medium Array

Keep a shared current as empty text. Keep a shared raised as empty text. Keep a shared capitals as empty text. Place "ABCDEFGHIJKLMNOPQRSTUVWXYZ" into capitals. The pattern is "FB". The queries is a list of "FooBar", "FooBarTest", "FootBall", "FrameBuffer", and "ForceFeedBack". The flags is an empty list of whole numbers.

To raise. Set raised to empty text.
For every spot from 0 to the last position of current.
The symbol is the slice of current from spot to spot plus 1. If capitals contains symbol, Place symbol into raised.

For each query in queries.
Set current to query. Raise. If raised is pattern, Place 1 into flags, otherwise Place 0 into flags.

For each flag in flags.
If flag is 1, show true, otherwise show false.

Expected stdout

true
false
true
true
false

Open in Playground Standalone Page

#1024 · Video Stitching

Medium Array

The starts is a list of 0, 4, 8, 1, 1, and 5. The ends is a list of 2, 6, 10, 9, 5, and 9. The time is 10. The table is a list of 11 zeros. Set item 0 of table to 0.
For every moment from 1 to 10.
Set item moment of table to 1000000.

For every moment from 1 to 10.
For every clip from 0 to 5.
The start is item clip of starts. The finish is item clip of ends.
If start is less than moment and moment is at most finish.
The prior is item start of table. If prior plus 1 is less than item moment of table, set item moment of table to prior plus 1.

Show item 10 of table.

Expected stdout

3

Open in Playground Standalone Page

#1025 · Divisor Game

Easy Math

The number is 3. The table is a list of 4 zeros. Set item 0 of table to 0.
For every amount from 1 to 3.
For every pick from 1 to amount minus 1.
If amount modulo pick is 0 and item (amount minus pick) of table is 0.
Set item amount of table to 1.

If item 3 of table is 1, show true, otherwise show false.

Expected stdout

false

Open in Playground Standalone Page

#1026 · Maximum Difference Between Node and Ancestor

Medium Depth-First Search

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

To search from node (a whole number) with low (a whole number) and high (a whole number). If node is less than 0, return. If item node of values is less than 0, return. The value is item node of values. The floor is low. The ceiling is high. If value is less than floor, set floor to value. If value is more than ceiling, set ceiling to value. The gap is ceiling minus floor. If gap is more than best, set best to gap. Search from item node of lefts with floor and ceiling. Search from item node of rights with floor and ceiling.

Search from 0 with 8 and 8. Show best.

Expected stdout

7

Open in Playground Standalone Page

#1027 · Longest Arithmetic Subsequence

Medium Array

The numbers is a list of 3, 6, 9, and 12. The size is 4. The table is a list of 16 zeros. The best is 2.
For every later from 1 to 3.
For every earlier from 0 to later minus 1.
Set item (later times size plus earlier) of table to 2. The step is item later of numbers minus item earlier of numbers.
For every prior from 0 to earlier minus 1.
The oldstep is item earlier of numbers minus item prior of numbers.
If oldstep is step.
The candidate is item (earlier times size plus prior) of table plus 1. If candidate is more than item (later times size plus earlier) of table, set item (later times size plus earlier) of table to candidate. If candidate is more than best, set best to candidate.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#1028 · Recover a Tree From Preorder Traversal

Hard String

Keep a shared traversal as empty text. Place "1-2--3--4-5--6--7" into traversal. Keep a shared values as an empty list of whole numbers. Keep a shared depths 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 stack as an empty list of whole numbers. The number is 0. The depth is 0. The inside is 0. The gaps is 0.

For every spot from 0 to the last position of traversal.
The symbol is the slice of traversal from spot to spot plus 1. The dash is 0. If symbol is "-", set dash to 1. If dash is 1 and inside is 1, Place number into values, Place depth into depths, set number to 0, set inside to 0, set depth to 0. If dash is 1, add 1 to depth. If dash is 0, The digit is the digit value of character spot of traversal, set number to number times 10 plus digit, set inside to 1.

If inside is 1, Place number into values, Place depth into depths.
For every spot from 0 to the last position of values.
Place -1 into lefts. Place -1 into rights.

To trim with level (a whole number). The settled is 0.
Repeat while settled is 0.
The end is the last position of stack. If end is less than 0, set settled to 1, stop. If item (item end of stack) of depths is less than level, set settled to 1, stop. Remove the last item from stack.

Place 0 into stack.
For every node from 1 to the last position of values.
Trim with item node of depths. The end is the last position of stack. The parent is item end of stack. If item parent of lefts is less than 0, set item parent of lefts to node, otherwise set item parent of rights to node. Place node into stack.

The queue is an empty list of whole numbers. Place 0 into queue. The head is 0. 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
5
3
4
6
7

Open in Playground Standalone Page

#1029 · Two City Scheduling

Medium Array

The firsts is a list of 10, 30, 400, and 30. The seconds is a list of 20, 200, 50, and 20. The size is 4. The wanted is 2. The order is an empty list of whole numbers. The gaps is an empty list of whole numbers.
For every city from 0 to 3.
Place city into order. Place item city of firsts minus item city of seconds into gaps.

For every spot from 0 to 3.
For every look from spot plus 1 to 3.
The here is item spot of order. The there is item look of order. If item there of gaps is less than item here of gaps, swap item spot of order and item look of order.

The total is 0.
For every city from 0 to 3.
The picked is item city of order. If city is less than wanted, set total to total plus item picked of firsts. If city is at least wanted, set total to total plus item picked of seconds.

Show total.

Expected stdout

110

Open in Playground Standalone Page

#1030 · Matrix Cells in Distance Order

Easy Array

The rows is 2, while the columns is 2. The centerrow is 0, while the centercolumn is 1. The keys is an empty list of whole numbers.
For every cell from 0 to 3.
The across is cell divided by columns. The down is cell modulo columns. The steps is the absolute value of (across minus centerrow). The side is the absolute value of (down minus centercolumn). The key is (steps plus side) times 100 plus cell. Place key into keys.

For every spot from 0 to 3.
For every look from spot plus 1 to 3.
If item look of keys is less than item spot of keys, swap item spot of keys and item look of keys.

For each key in keys.
The cell is key modulo 100. The across is cell divided by columns. The down is cell modulo columns. The line is empty text. Place format whole number across as text into line. Place "," into line. The side is format whole number down as text. Place side into line. Show line.

Expected stdout

0,1
0,0
1,1
1,0

Open in Playground Standalone Page

#1031 · Maximum Sum of Two Non-Overlapping Subarrays

Medium Array

The numbers is a list of 0, 6, 5, 2, 2, 5, 1, 9, and 4. The size is 9. The firstlen is 1, while the secondlen is 2. The prefix is a list of 10 zeros.
For every spot from 0 to 8.
Set item (spot plus 1) of prefix to item spot of prefix plus item spot of numbers.

The best is 0.
For every first from 0 to size minus firstlen.
For every second from 0 to size minus secondlen.
The firstsum is item (first plus firstlen) of prefix minus item first of prefix. The secondsum is item (second plus secondlen) of prefix minus item second of prefix. The apart is 0. If first plus firstlen is at most second, set apart to 1. If second plus secondlen is at most first, set apart to 1. If apart is 1 and firstsum plus secondsum is more than best, set best to firstsum plus secondsum.

Show best.

Expected stdout

20

Open in Playground Standalone Page

#1033 · Moving Stones Until Consecutive

Medium Math

The stones is a list of 1, 2, and 5. Sort stones. The smallest is item 0 of stones. The middle is item 1 of stones. The largest is item 2 of stones. The firstgap is middle minus smallest. The secondgap is largest minus middle. The fewest is 2. If firstgap is 1 and secondgap is 1, set fewest to 0. If firstgap is 2 or secondgap is 2, set fewest to 1. If firstgap is 1 and secondgap is more than 1, set fewest to 1. The most is largest minus smallest minus 2. Show fewest. Show most.

Expected stdout

1
2

Open in Playground Standalone Page

#1034 · Coloring A Border

Medium Array

Keep a shared grid holding 1, 1, 1, and 2. Keep a shared visited as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared border as an empty list of whole numbers. Keep a shared rows of 0. Keep a shared columns of 0. Keep a shared old of 0. Keep a shared color of 0. Keep a shared count of 0. Set rows to 2. Set columns to 2. Set old to 1. Set color to 3.
For every cell from 0 to 3.
Place 0 into visited.

To include with node (a whole number).
If item node of visited is 0 and item node of grid is old.
Set item node of visited to 1. Place node into queue. Add 1 to count.

To mark with node (a whole number). The across is node divided by columns. The down is node modulo columns. The edge is 0. If across is 0 or across is rows minus 1 or down is 0 or down is columns minus 1, set edge to 1. If across is more than 0 and item (node minus columns) of grid is not old, set edge to 1. If across is less than rows minus 1 and item (node plus columns) of grid is not old, set edge to 1. If down is more than 0 and item (node minus 1) of grid is not old, set edge to 1. If down is less than columns minus 1 and item (node plus 1) of grid is not old, set edge to 1. If edge is 1, Place node into border.

The head is 0. Include with 0.
Repeat while head is less than count.
The spot is head. The node is item spot of queue. Add 1 to head. Mark with node. The across is node divided by columns. The down is node modulo columns. If across is more than 0, Include with node minus columns. If across is less than rows minus 1, Include with node plus columns. If down is more than 0, Include with node minus 1. If down is less than columns minus 1, Include with node plus 1.

For each node in border.
Set item node of grid to color.

For each value in grid.
Show value.

Expected stdout

3
3
3
2

Open in Playground Standalone Page

#1035 · Uncrossed Lines

Medium Array

The first is a list of 1, 4, and 2. The second is a list of 1, 2, and 4. The width is 3. The table is a list of 16 zeros.
For every left from 1 to 3.
For every right from 1 to 3.
The cell is left times 4 plus right. The above is left minus 1. The side is right minus 1. The here is item above of first. The there is item side of second. If here is there, The old is item (above times 4 plus side) of table, set item cell of table to old plus 1. The north is item (above times 4 plus right) of table. The west is item (left times 4 plus side) of table. The best is the larger of north and west. If here is not there, set item cell of table to best.

Show item 15 of table.

Expected stdout

2

Open in Playground Standalone Page

#1036 · Escape a Large Maze

Hard Array

Keep a shared blocked holding 1 and 1000000. Keep a shared visited as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. The source is 0. The target is 2. The count is 0. The head is 0.

To open with code (a whole number). The across is code divided by 1000000. The down is code modulo 1000000. If across is less than 0 or across is more than 999999, return. If down is less than 0 or down is more than 999999, return. If blocked contains code, return. If visited contains code, return. Place code into visited. Place code into queue. Add 1 to count.

Open with source.
Repeat while head is less than count and count is at most 20000.
The node is item head of queue. Add 1 to head. The across is node divided by 1000000. The down is node modulo 1000000. If across is more than 0, Open with node minus 1000000. If across is less than 999999, Open with node plus 1000000. If down is more than 0, Open with node minus 1. If down is less than 999999, Open with node plus 1.

The escaped is 0. If visited contains target, set escaped to 1. If count is more than 20000, set escaped to 1. If escaped is 1, show true, otherwise show false.

Expected stdout

false

Open in Playground Standalone Page

#1037 · Valid Boomerang

Easy Array

The firstx is 1, while the firsty is 1. The secondx is 2, while the secondy is 3. The thirdx is 3, while the thirdy is 2. The left is (secondx minus firstx) times (thirdy minus firsty). The right is (secondy minus firsty) times (thirdx minus firstx). If left is right, show false, otherwise show true.

Expected stdout

true

Open in Playground Standalone Page