The TransCompilator

HTML chunk · 50 solved programs

Chunk 15 · #0936–#0986

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

#0936 · Stamping The Sequence

Hard String

The stamp is "abc", while the target is "ababc". The state is an empty list of text. The marks is an empty list of text. The blanks is an empty list of text. Place "?" into blanks. The lasttarget is the last position of target. The laststamp is the last position of stamp.
For every spot from 0 to lasttarget.
The piece is the slice of target from spot to spot plus 1. Place piece into state.
For every spot from 0 to laststamp.
The piece is the slice of stamp from spot to spot plus 1. Place piece into marks.

The found is an empty list of whole numbers. The match is 1, while the unknown is 0, while the blank is 0, while the same is 0.
For every round from 0 to lasttarget.
For every start from 0 to lasttarget minus laststamp.
Set match to 1. Set unknown to 0.
For every offset from 0 to laststamp.
The spot is start plus offset. The here is item spot of state. The wanted is item offset of marks. Set blank to 0. Set same to 0. If here matches "?", set blank to 1. If here matches wanted, set same to 1. If blank is 0 and same is 0, set match to 0. If blank is 0, add 1 to unknown. If offset is laststamp and match is 1 and unknown is more than 0, set item start of state to item 0 of blanks, set item (start plus 1) of state to item 0 of blanks, set item (start plus 2) of state to item 0 of blanks, Place start into found.

The last is the last position of found.
For every spot from 0 to last.
The back is last minus spot. Show item back of found.

Expected stdout

0
2

Open in Playground Standalone Page

#0937 · Reorder Data in Log Files

Medium Array

Keep a shared logs holding "dig1 8 1 5 1", "let1 art can", "let2 own kit dig", and "let3 art zero". Keep a shared content as empty text. Keep a shared letterflag of 0. Keep a shared digitwords holding "0", "1", "2", "3", "4", "5", "6", "7", "8", and "9".

To extract from position (a whole number). The log is item position of logs. The parts is split log by " ". The last is the last position of parts. Set content to empty text.
For every index from 1 to last.
Place item index of parts into content. If index is less than last, Place " " into content.

To classify from position (a whole number). The log is item position of logs. The parts is split log by " ". The word is item 1 of parts. The lead is the slice of word from 0 to 1.
For every digit from 0 to 9.
The digittext is item digit of digitwords. If lead matches digittext, set letterflag to 0.

The ids is an empty list of text. The contents is an empty list of text. The kinds is an empty list of whole numbers.
For every position from 0 to the last position of logs.
The log is item position of logs. The parts is split log by " ". Place item 0 of parts into ids. Extract from position. Place content into contents. Set letterflag to 1. Classify from position. Place letterflag into kinds.

The last is the last position of logs.
For every left from 0 to last.
For every right from left to last.
The should is 0. The leftcontent is item left of contents. The rightcontent is item right of contents. The leftid is item left of ids. The rightid is item right of ids. The ahead is 0. If rightcontent comes before leftcontent, set ahead to 1. If rightcontent matches leftcontent, set ahead to 0. If rightcontent matches leftcontent, If rightid comes before leftid, set ahead to 1. If item left of kinds is 0 and item right of kinds is 1, set should to 1. If item left of kinds is 1 and item right of kinds is 1 and ahead is 1, set should to 1. If should is 1, swap item left of ids and item right of ids, swap item left of contents and item right of contents, swap item left of kinds and item right of kinds.

For every position from 0 to last.
The line is empty text. Place item position of ids into line. Place " " into line. Place item position of contents into line. Show line.

Expected stdout

let1 art can
let3 art zero
let2 own kit dig
dig1 8 1 5 1

Open in Playground Standalone Page

#0938 · Range Sum of BST

Easy Depth-First Search

Keep a shared values holding 10, 5, 15, 3, 7, -1, and 18. 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 low of 0. Keep a shared high of 0. Keep a shared total of 0. Set low to 7. Set high to 15.

To gather from node (a whole number). If node is less than 0, return. The value is item node of values. If value is at least low and value is at most high, add value to total. If value is more than low, Gather from item node of lefts. If value is less than high, Gather from item node of rights.

Gather from 0. Show total.

Expected stdout

32

Open in Playground Standalone Page

#0939 · Minimum Area Rectangle

Medium Array

The xs is a list of 1, 1, 3, and 3. The ys is a list of 1, 3, 1, and 3. The last is the last position of xs. The best is 1000000.
For every first from 0 to last.
For every second from first plus 1 to last.
The xone is item first of xs, while the yone is item first of ys. The xtwo is item second of xs, while the ytwo is item second of ys.
If xone is not xtwo and yone is not ytwo.
The haveone is 0, while the havetwo is 0.
For every third from 0 to last.
The xhere is item third of xs, while the yhere is item third of ys. If xhere is xone and yhere is ytwo, set haveone to 1. If xhere is xtwo and yhere is yone, set havetwo to 1. If third is last and haveone is 1 and havetwo is 1, The wide is the absolute value of (xone minus xtwo), The tall is the absolute value of (yone minus ytwo), The area is wide times tall, If area is less than best, set best to area.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#0940 · Distinct Subsequences II

Hard String

The word is "abc". The size is the number of characters in word. The ways is an empty list of whole numbers.
For every spot from 0 to size.
Place 0 into ways.

Set item 0 of ways to 1.
For every finish from 1 to size.
Set item finish of ways to (item (finish minus 1) of ways times 2) modulo 1000000007. The piece is the slice of word from (finish minus 1) to finish. The previous is 0 minus 1.
For every back from 0 to finish minus 2.
The otherpiece is the slice of word from back to back plus 1. If otherpiece matches piece, set previous to back. If back is finish minus 2 and previous is at least 0, set item finish of ways to (item finish of ways minus item previous of ways) modulo 1000000007.

Show item size of ways minus 1.

Expected stdout

7

Open in Playground Standalone Page

#0941 · Valid Mountain Array

Easy Array

The heights is a list of 0, 3, 2, and 1. The last is the last position of heights. The crest is 0.
Repeat while crest is less than last and item crest of heights is less than item (crest plus 1) of heights.
Add 1 to crest.

The base is crest.
Repeat while base is less than last and item base of heights is more than item (base plus 1) of heights.
Add 1 to base.

If crest is more than 0 and crest is less than last and base is last, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0942 · DI String Match

Easy Array

The pattern is "IDID". The size is the number of characters in pattern. The low is 0. The high is size. The answers is an empty list of whole numbers.
For every spot from 0 to size minus 1.
If character spot of pattern is "I", place low into answers, add 1 to low. If character spot of pattern is "D", place high into answers, set high to high minus 1.

Place low into answers. For each answer in answers, show answer.

Expected stdout

0
4
1
3
2

Open in Playground Standalone Page

#0943 · Find the Shortest Superstring

Hard Array

Keep a shared words as an empty list of text. Keep a shared order as an empty list of whole numbers. Keep a shared used as an empty list of whole numbers. Place "catg" into words. Place "ctaagt" into words. Place "gcta" into words. Place 0 into used. Place 0 into used. Place 0 into used. Keep a shared current as empty text. Keep a shared answer as empty text. Keep a shared shortest of 0. Keep a shared word as empty text. Keep a shared overlap of 0. Keep a shared agree of 0.

To check from span (a whole number). Set agree to 1. The last is the last position of current. The width is last plus 1. The begin is width minus span.
For every offset from 0 to span minus 1.
The actual is character (begin plus offset) of current. The wanted is character offset of word. If actual is not wanted, set agree to 0, stop.

To match. The last is the last position of current. The width is last plus 1. The final is the last position of word. The reach is final plus 1. The limit is the smaller of width and reach. Set overlap to 0.
For every span from 1 to limit.
Check from span. If agree is 1, set overlap to span.

To join from place (a whole number). The chosen is item place of order. The next is item chosen of words. Set word to next. Match. The last is the last position of word. The reach is last plus 1. The tail is the slice of word from overlap to reach. Place tail into current.

To assess. The first is item 0 of order. The seed is item first of words. Set current to seed. For every place from 1 to 2, Join from place. The close is the last position of current. The width is close plus 1. The copy is a copy of current. If shortest is 0 or width is less than shortest, set shortest to width, set answer to copy.

To take from spot (a whole number) with depth (a whole number). Place spot into order. Set item spot of used to 1. Arrange from depth plus 1. Remove the last item from order. Set item spot of used to 0.

To arrange from depth (a whole number). If depth is 3, Assess, return. For every spot from 0 to 2, if item spot of used is 0, Take from spot with depth.

Arrange from 0. Show answer.

Expected stdout

catgctaagt

Open in Playground Standalone Page

#0944 · Delete Columns to Make Sorted

Easy Array

Keep a shared board holding "cba", "daf", and "ghi". Keep a shared removed of 0. The opening is item 0 of board. The last column is the last position of opening.

To inspect with column (a whole number). The top is item 0 of board. The earlier is the slice of top from column to column plus 1.
For every row from 1 to the last position of board.
The line is item row of board. The letter is the slice of line from column to column plus 1. If letter comes before earlier, set removed to removed plus 1, stop. Set earlier to letter.

For every column from 0 to last column.
Inspect with column.

Show removed.

Expected stdout

1

Open in Playground Standalone Page

#0945 · Minimum Increment to Make Array Unique

Medium Array

The numbers is a list of 3, 2, 1, 2, 1, and 7. The last is the last position of numbers. Sort numbers. The raised is 0.
For every spot from 1 to last.
If item spot of numbers is at most item (spot minus 1) of numbers.
The gap is item (spot minus 1) of numbers minus item spot of numbers plus 1. Add gap to raised. Set item spot of numbers to item (spot minus 1) of numbers plus 1.

Show raised.

Expected stdout

6

Open in Playground Standalone Page

#0946 · Validate Stack Sequences

Medium Array

Keep a shared popped holding 4, 5, 3, 2, and 1. Keep a shared stack as an empty list of whole numbers. Keep a shared next of 0. The pushed is a list of 1, 2, 3, 4, and 5.

To drain. The final is the last position of popped.
Repeat while the number of items in stack is more than 0 and next is at most final.
The local is next. The size is the number of items in stack. The top is item (size minus 1) of stack. If top is not item local of popped, stop. remove the last item from stack. Add 1 to next.

For each value in pushed.
Place value into stack. Drain.

The final is the last position of popped. If next is more than final, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0947 · Most Stones Removed with Same Row or Column

Medium Depth-First Search

Keep a shared columns holding 0, 0, 1, 1, 2, and 2. Keep a shared rows holding 0, 1, 0, 2, 1, and 2. Keep a shared parents as an empty list of whole numbers. Keep a shared chief of 0.

To locate from stone (a whole number). If item stone of parents is stone, set chief to stone, return. The up is item stone of parents. Locate from up. Set item stone of parents to item up of parents.

To attach from first (a whole number) with second (a whole number). Locate from first. The one is chief. Locate from second. The two is chief. If one is not two, set item one of parents to two.

To pair from first (a whole number).
For every second from first plus 1 to the last position of parents.
If item first of columns is item second of columns or item first of rows is item second of rows, attach from first with second.

For every stone from 0 to the last position of columns.
Place stone into parents.

For every first from 0 to the last position of parents.
Pair from first.

The roots is 0.
For every stone from 0 to the last position of parents.
If item stone of parents is stone, add 1 to roots.

The final stone is the last position of columns. The total is final stone plus 1. The answer is total minus roots. Show answer.

Expected stdout

5

Open in Playground Standalone Page

#0948 · Bag of Tokens

Medium Array

The tokens is a list of 100, 200, 300, and 400. The power is 200. Sort tokens. The low is 0. The high is the last position of tokens. The coins is 0. The best is 0.
Repeat while low is at most high.
If power is at least item low of tokens, set power to power minus item low of tokens, add 1 to coins, add 1 to low, set best to coins. If low is at most high and power is less than item low of tokens and coins is more than 0, set power to power plus item high of tokens, set high to high minus 1, set coins to coins minus 1. If low is at most high and power is less than item low of tokens and coins is 0, stop.

Show best.

Expected stdout

2

Open in Playground Standalone Page

#0949 · Largest Time for Given Digits

Medium Array

Keep a shared digits holding 1, 2, 3, and 4. Keep a shared order as an empty list of whole numbers. Keep a shared used holding 0, 0, 0, and 0. Keep a shared latest of 0. Keep a shared pieces as an empty list of text.

To pad with value (a whole number).
If value is less than 10.
Place "0" into pieces.

To assess. The first is item 0 of order. The second is item 1 of order. The third is item 2 of order. The fourth is item 3 of order. The one is item first of digits. The two is item second of digits. The three is item third of digits. The four is item fourth of digits. The hours is one times 10 plus two. The minutes is three times 10 plus four.
If hours is less than 24 and minutes is less than 60.
The total is hours times 60 plus minutes. If total is more than latest, set latest to total.

To take from spot (a whole number) with depth (a whole number). Place spot into order. Set item spot of used to 1. Arrange from depth plus 1. remove the last item from order. Set item spot of used to 0.

To arrange from depth (a whole number). If depth is 4, Assess, return. For every spot from 0 to 3, if item spot of used is 0, Take from spot with depth.

Arrange from 0. The hours is latest divided by 60. The minutes is latest modulo 60. Pad with hours. The hour piece is format whole number hours as text. Place hour piece into pieces. Place ":" into pieces. Pad with minutes. The minute piece is format whole number minutes as text. Place minute piece into pieces. The answer is join pieces with "". Show answer.

Expected stdout

23:41

Open in Playground Standalone Page

#0950 · Reveal Cards In Increasing Order

Medium Array

The deck is a list of 17, 13, 11, 2, 3, 5, and 7. The last is the last position of deck. Sort deck. Keep a shared places as an empty list of whole numbers. Keep a shared head of 0. For every spot from 0 to last, place spot into places. The result is a list of 7 zeros.

To cycle. The local is head. Place item local of places into places. Add 1 to head.

For each card in deck.
The local is head. The spot is item local of places. Set item spot of result to card. Add 1 to head. The final is the last position of places. If head is at most final, Cycle.

For each card in result, show card.

Expected stdout

2
13
3
11
5
17
7

Open in Playground Standalone Page

#0951 · Flip Equivalent Binary Trees

Medium Depth-First Search

Keep a shared agreement of 0. Keep a shared first values as an empty list of whole numbers. Keep a shared first lefts as an empty list of whole numbers. Keep a shared first rights as an empty list of whole numbers. Keep a shared second values as an empty list of whole numbers. Keep a shared second lefts as an empty list of whole numbers. Keep a shared second rights as an empty list of whole numbers. Place 1 into first values. Place 2 into first values. Place 3 into first values. Place 4 into first values. Place 5 into first values. Place 6 into first values. Place -1 into first values. Place -1 into first values. Place -1 into first values. Place 7 into first values. Place 8 into first values. Place 1 into first lefts. Place 3 into first lefts. Place 5 into first lefts. Place -1 into first lefts. Place 9 into first lefts. Place -1 into first lefts. Place -1 into first lefts. Place -1 into first lefts. Place -1 into first lefts. Place -1 into first lefts. Place -1 into first lefts. Place 2 into first rights. Place 4 into first rights. Place -1 into first rights. Place -1 into first rights. Place 10 into first rights. Place -1 into first rights. Place -1 into first rights. Place -1 into first rights. Place -1 into first rights. Place -1 into first rights. Place -1 into first rights. Place 1 into second values. Place 3 into second values. Place 2 into second values. Place -1 into second values. Place 6 into second values. Place 4 into second values. Place 5 into second values. Place -1 into second values. Place -1 into second values. Place -1 into second values. Place -1 into second values. Place 8 into second values. Place 7 into second values. Place 1 into second lefts. Place -1 into second lefts. Place 5 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place 11 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place -1 into second lefts. Place 2 into second rights. Place 4 into second rights. Place 6 into second rights. Place -1 into second rights. Place -1 into second rights. Place -1 into second rights. Place 12 into second rights. Place -1 into second rights. Place -1 into second rights. Place -1 into second rights. Place -1 into second rights. Place -1 into second rights. Place -1 into second rights.

Set agreement to 1.

To compare from first (a whole number) with second (a whole number). If agreement is 0, return. If first is less than 0 and second is less than 0, return. If first is less than 0, set agreement to 0, return. If second is less than 0, set agreement to 0, return. If item first of first values is not item second of second values, set agreement to 0, return. The firstleft is item first of first lefts. The firstright is item first of first rights. The secondleft is item second of second lefts. The secondright is item second of second rights. Set agreement to 1. Compare from firstleft with secondleft. Compare from firstright with secondright. The straight is agreement. Set agreement to 1. Compare from firstleft with secondright. Compare from firstright with secondleft. The crossed is agreement. Set agreement to 0. If straight is 1, set agreement to 1. If crossed is 1, set agreement to 1.

Compare from 0 with 0. If agreement is 1, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0952 · Largest Component Size by Common Factor

Hard Array

Keep a shared numbers as an empty list of whole numbers. Keep a shared parent as an empty list of whole numbers. Keep a shared divisor of 0. Keep a shared rootanswer of 0. Place 2 into numbers. Place 3 into numbers. Place 6 into numbers. Place 7 into numbers. Place 4 into numbers. Place 12 into numbers. Place 21 into numbers. Place 39 into numbers. Place 0 into parent. Place 1 into parent. Place 2 into parent. Place 3 into parent. Place 4 into parent. Place 5 into parent. Place 6 into parent. Place 7 into parent.

To measure from first (a whole number) with second (a whole number). If second is 0, set divisor to first, return. Measure from second with first modulo second.

To root from node (a whole number). Set rootanswer to node. The walker is rootanswer.
Repeat while item walker of parent is not walker.
Set rootanswer to item walker of parent. Set walker to rootanswer.

The last is the last position of numbers.
For every left from 0 to last.
For every right from left plus 1 to last.
Measure from item left of numbers with item right of numbers.
If divisor is more than 1.
Root from left. The firstroot is rootanswer. Root from right. The secondroot is rootanswer. If firstroot is not secondroot, set item firstroot of parent to secondroot.

The roots is an empty list of whole numbers.
For every spot from 0 to last.
Root from spot. Place rootanswer into roots.

The largest is 0.
For every spot from 0 to last.
The base is item spot of roots. The count is 0.
For every check from 0 to last.
If item check of roots is base, add 1 to count. If count is more than largest, set largest to count.

Show largest.

Expected stdout

8

Open in Playground Standalone Page

#0953 · Verifying an Alien Dictionary

Easy Array

Keep a shared order as empty text. Keep a shared words as an empty list of text. Keep a shared wanted as empty text. Keep a shared rankanswer of 0. Keep a shared sorted of 0. Keep a shared lengths as an empty list of whole numbers. Keep a shared offsets as an empty list of whole numbers. Keep a shared ranks as an empty list of whole numbers. Place "hlabcdefgijkmnopqrstuvwxyz" into order. Place "hello" into words. Place "leetcode" into words. Set sorted to 1.

To rank from place (a whole number). If place is more than 25, return. The piece is the slice of order from place to place plus 1. If piece matches wanted, set rankanswer to place, return. Rank from place plus 1.

The at is 0.
For each entry in words.
Place at into offsets. The end is the last position of entry. The size is end plus 1. Place size into lengths.
For every spot from 0 to size minus 1.
Set wanted to the slice of entry from spot to spot plus 1. Rank from 0. Place rankanswer into ranks. Add 1 to at.

To verify with pair (a whole number). The firststart is item pair of offsets. The secondstart is item (pair plus 1) of offsets. The limit is item pair of lengths. If item (pair plus 1) of lengths is less than limit, set limit to item (pair plus 1) of lengths. The decided is 0.
For every spot from 0 to limit minus 1.
The leftrank is item (firststart plus spot) of ranks. The rightrank is item (secondstart plus spot) of ranks. If leftrank is less than rightrank, set decided to 1. If leftrank is more than rightrank, set decided to 2, set sorted to 0. If decided is not 0, stop. If decided is 0 and item pair of lengths is more than item (pair plus 1) of lengths, set sorted to 0.

The last is the last position of words. The finalpair is last minus 1.
For every pair from 0 to finalpair.
Verify with pair.

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

Expected stdout

true

Open in Playground Standalone Page

#0954 · Array of Doubled Pairs

Medium Array

Keep a shared numbers as an empty list of whole numbers. Keep a shared values as an empty list of whole numbers. Keep a shared counts as an empty list of whole numbers. Keep a shared foundat of 0. Keep a shared possible of 0. Keep a shared bestsize of 0. Keep a shared bestspot of 0. Keep a shared bestvalue of 0. Place 4 into numbers. Place -2 into numbers. Place 2 into numbers. Place -4 into numbers.

To locate from spot (a whole number) with target (a whole number). Set foundat to -1. If spot is more than the last position of values, return. If item spot of values is target, set foundat to spot, return. Locate from spot plus 1 with target.

For each number in numbers.
Locate from 0 with number. If foundat is less than 0, Place number into values, Place 1 into counts. The here is foundat. If foundat is at least 0, set item here of counts to item here of counts plus 1.

Set possible to 1. The last is the last position of numbers. The remaining is last plus 1. Locate from 0 with 0.
If foundat is at least 0.
The zerospot is foundat. If item zerospot of counts modulo 2 is not 0, set possible to 0. Decrease remaining by item zerospot of counts. Set item zerospot of counts to 0.

To choose. Set bestsize to -1. Set bestspot to -1. Set bestvalue to 0.
For every spot from 0 to the last position of values.
If item spot of counts is more than 0.
The size is the absolute value of item spot of values. If bestsize is less than 0 or size is less than bestsize, set bestsize to size, set bestspot to spot, set bestvalue to item spot of values.

Repeat while remaining is more than 0 and possible is 1.
Choose. If bestspot is less than 0, set possible to 0, stop. The wanted is bestvalue times 2. Locate from 0 with wanted. If foundat is less than 0, set possible to 0, stop. The chosen is bestspot. The here is foundat. The needed is item chosen of counts. If item here of counts is less than needed, set possible to 0, stop. Decrease item chosen of counts by needed. Decrease item here of counts by needed. Decrease remaining by needed times 2.

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

Expected stdout

true

Open in Playground Standalone Page

#0955 · Delete Columns to Make Sorted II

Medium Array

Keep a shared words as an empty list of text. Keep a shared resolved as an empty list of whole numbers. Keep a shared rows of 0. Keep a shared columns of 0. Keep a shared valid of 0. Keep a shared deleted of 0. Place "ca" into words. Place "bb" into words. Place "ac" into words. Place 0 into resolved. Place 0 into resolved. The last is the last position of words. Set rows to last plus 1. The leftword is item 0 of words. The width is the last position of leftword. Set columns to width plus 1.

To test with column (a whole number).
For every pair from 0 to rows minus 2.
If item pair of resolved is 0 and valid is 1.
The leftword is item pair of words. The rightword is item (pair plus 1) of words. The left is the slice of leftword from column to column plus 1. The right is the slice of rightword from column to column plus 1. If right comes before left, set valid to 0.

To settle with column (a whole number).
For every pair from 0 to rows minus 2.
If item pair of resolved is 0.
The leftword is item pair of words. The rightword is item (pair plus 1) of words. The left is the slice of leftword from column to column plus 1. The right is the slice of rightword from column to column plus 1. If left comes before right, set item pair of resolved to 1.

For every column from 0 to columns minus 1.
Set valid to 1. Test with column. If valid is 0, add 1 to deleted. If valid is 1, Settle with column.

Show deleted.

Expected stdout

1

Open in Playground Standalone Page

#0956 · Tallest Billboard

Hard Array

Keep a shared rods as an empty list of whole numbers. Keep a shared current as an empty list of whole numbers. Keep a shared following as an empty list of whole numbers. Keep a shared total of 0. Keep a shared size of 0. Keep a shared center of 0. Place 1 into rods. Place 2 into rods. Place 3 into rods. Place 6 into rods.

For each rod in rods.
Increase total by rod.

Set size to total times 2 plus 1. Set center to total.
For every slot from 0 to size minus 1.
Place -1 into current.

For every slot from 0 to size minus 1.
Place -1 into following.

Set item center of current to 0.

To extend with rod (a whole number).
For every slot from 0 to size minus 1.
If item slot of current is at least 0.
The height is item slot of current. If height is more than item slot of following, set item slot of following to height. The upper is slot plus rod.
If upper is less than size.
The raised is height plus rod. If raised is more than item upper of following, set item upper of following to raised. The diff is slot minus center. The raw is diff minus rod. The gap is the absolute value of raw. The lower is gap plus center. The shifted is height. If rod is more than diff, set shifted to height plus rod minus diff. If shifted is more than item lower of following, set item lower of following to shifted.

For each rod in rods.
Extend with rod.
For every slot from 0 to size minus 1.
Set item slot of current to item slot of following. Set item slot of following to -1.

The middle is center. Show item middle of current.

Expected stdout

6

Open in Playground Standalone Page

#0957 · Prison Cells After N Days

Medium Array

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

To advance. The oldcells is a list of item 0 of cells, item 1 of cells, item 2 of cells, item 3 of cells, item 4 of cells, item 5 of cells, item 6 of cells, and item 7 of cells. Set item 0 of cells to 0. Set item 7 of cells to 0.
For every place from 1 to 6.
The before is item (place minus 1) of oldcells. The after is item (place plus 1) of oldcells. If before is after, set item place of cells to 1. If before is not after, set item place of cells to 0.

The day is 0. The days is 7.
Repeat while day is less than days.
Advance. Add 1 to day.

The parts is an empty list of text.
For each value in cells.
The word is format whole number value as text. Place word into parts.

The joined is join parts with ",". The line is empty text. Place "[" into line. Place joined into line. Place "]" into line. Show line.

Expected stdout

[0,0,1,1,0,0,0,0]

Open in Playground Standalone Page

#0958 · Check Completeness of a Binary Tree

Medium Breadth-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 queue as an empty list of whole numbers. Keep a shared emptyseen of 0. Keep a shared complete of 0. Place 1 into values. Place 2 into values. Place 3 into values. Place 4 into values. Place 5 into values. Place 6 into values. Place 1 into lefts. Place 3 into lefts. Place 5 into lefts. Place -1 into lefts. Place -1 into lefts. Place -1 into lefts. Place 2 into rights. Place 4 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights. Place -1 into rights. Set complete to 1.

The last is the last position of values. The head is 0, while the count is 1. Place 0 into queue.
Repeat while head is less than count and complete is 1.
The node is item head of queue. Add 1 to head. If item node of values is less than 0, set emptyseen to 1.
If item node of values is at least 0.
If emptyseen is 1, set complete to 0.
If emptyseen is 0.
The leftindex is node times 2 plus 1. The rightindex is node times 2 plus 2. If leftindex is at most last, Place leftindex into queue, add 1 to count. If rightindex is at most last, Place rightindex into queue, add 1 to count.

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

Expected stdout

true

Open in Playground Standalone Page

#0959 · Regions Cut By Slashes

Medium Array

Keep a shared grid as an empty list of text. Keep a shared field as an empty list of whole numbers. Keep a shared visited as an empty list of whole numbers. Keep a shared regions of 0. Place "/\\" into grid. Place "\\/" into grid.
For every spot from 0 to 35.
Place 0 into field. Place 0 into visited.

For every row from 0 to 1.
The line is item row of grid.
For every column from 0 to 1.
The piece is the slice of line from column to column plus 1. The baserow is row times 3. The basecol is column times 3. The top is baserow times 6 plus basecol. If piece matches "/", Set item (top plus 2) of field to 1, Set item (top plus 7) of field to 1, Set item (top plus 12) of field to 1. If piece matches "\\", Set item top of field to 1, Set item (top plus 7) of field to 1, Set item (top plus 14) of field to 1.

To flood from start (a whole number). The queue is an empty list of whole numbers. Place start into queue. Set item start of visited to 1. The head is 0, while the count is 1.
Repeat while head is less than count.
The cell is item head of queue. Add 1 to head. The row is cell divided by 6. The column is cell modulo 6. If row is more than 0 and item (cell minus 6) of field is 0 and item (cell minus 6) of visited is 0, set item (cell minus 6) of visited to 1, Place (cell minus 6) into queue, add 1 to count. If row is less than 5 and item (cell plus 6) of field is 0 and item (cell plus 6) of visited is 0, set item (cell plus 6) of visited to 1, Place (cell plus 6) into queue, add 1 to count. If column is more than 0 and item (cell minus 1) of field is 0 and item (cell minus 1) of visited is 0, set item (cell minus 1) of visited to 1, Place (cell minus 1) into queue, add 1 to count. If column is less than 5 and item (cell plus 1) of field is 0 and item (cell plus 1) of visited is 0, set item (cell plus 1) of visited to 1, Place (cell plus 1) into queue, add 1 to count.

For every start from 0 to 35.
If item start of field is 0 and item start of visited is 0.
Add 1 to regions. Flood from start.

Show regions.

Expected stdout

5

Open in Playground Standalone Page

#0960 · Delete Columns to Make Sorted III

Hard Array

Keep a shared words as an empty list of text. Keep a shared keep as an empty list of whole numbers. Keep a shared rows of 0. Keep a shared cols of 0. Keep a shared fits of 0. Place "babca" into words. Place "bbazb" into words. Place 1 into keep. Place 1 into keep. Place 1 into keep. Place 1 into keep. Place 1 into keep. The last is the last position of words. Set rows to last plus 1. The sample is item 0 of words. The width is the last position of sample. Set cols to width plus 1.

To check with left (a whole number) with right (a whole number). Set fits to 1.
For every row from 0 to rows minus 1.
The word is item row of words. The before is the slice of word from left to left plus 1. The after is the slice of word from right to right plus 1. If after comes before before, set fits to 0.

For every right from 0 to cols minus 1.
For every left from 0 to right minus 1.
Check with left with right. If fits is 1 and item left of keep plus 1 is more than item right of keep, set item right of keep to item left of keep plus 1.

The most is 0.
For each value in keep.
If value is more than most, set most to value.

Show cols minus most.

Expected stdout

3

Open in Playground Standalone Page

#0961 · N-Repeated Element in Size 2N Array

Easy Array

Keep a shared numbers as an empty list of whole numbers. Keep a shared seen as an empty list of whole numbers. Keep a shared repeated of 0. Place 5 into numbers. Place 1 into numbers. Place 5 into numbers. Place 2 into numbers. Place 5 into numbers. Place 3 into numbers. Place 5 into numbers. Place 4 into numbers.
For each value in numbers.
If seen contains value, set repeated to value, stop. Place value into seen.

Show repeated.

Expected stdout

5

Open in Playground Standalone Page

#0962 · Maximum Width Ramp

Medium Array

Keep a shared numbers as an empty list of whole numbers. Keep a shared best of 0. Place 6 into numbers. Place 0 into numbers. Place 8 into numbers. Place 2 into numbers. Place 1 into numbers. Place 5 into numbers. The last is the last position of numbers.
For every left from 0 to last minus 1.
For every right from left plus 1 to last.
If item right of numbers is at least item left of numbers.
The width is right minus left. If width is more than best, set best to width.

Show best.

Expected stdout

4

Open in Playground Standalone Page

#0963 · Minimum Area Rectangle II

Medium Array

Keep a shared xs as an empty list of whole numbers. Keep a shared ys as an empty list of whole numbers. Keep a shared otherone of 0. Keep a shared othertwo of 0. Keep a shared doubled of 0. Place 1 into xs. Place 2 into xs. Place 1 into xs. Place 0 into xs. Place 2 into ys. Place 1 into ys. Place 0 into ys. Place 1 into ys. Set doubled to 1000000.

To examine with cornerone (a whole number) with cornertwo (a whole number). The thirdspot is otherone. The fourthspot is othertwo. The firstx is item cornerone of xs. The firsty is item cornerone of ys. The secondx is item cornertwo of xs. The secondy is item cornertwo of ys. The thirdx is item thirdspot of xs. The thirdy is item thirdspot of ys. The fourthx is item fourthspot of xs. The fourthy is item fourthspot of ys.
If firstx plus thirdx is secondx plus fourthx.
If firsty plus thirdy is secondy plus fourthy.
The firstdx is thirdx minus firstx. The firstdy is thirdy minus firsty. The seconddx is fourthx minus secondx. The seconddy is fourthy minus secondy. The firstspan is firstdx times firstdx plus firstdy times firstdy. The secondspan is seconddx times seconddx plus seconddy times seconddy.
If firstspan is secondspan.
The cross is firstdx times seconddy minus firstdy times seconddx. The area is the absolute value of cross. If area is less than doubled, set doubled to area.

The last is the last position of xs. The finalfirst is last minus 3. The finalsecond is last minus 2. The finalthird is last minus 1.
For every cornerone from 0 to finalfirst.
For every cornertwo from cornerone plus 1 to finalsecond.
For every cornerthree from cornertwo plus 1 to finalthird.
For every cornerfour from cornerthree plus 1 to last.
Set otherone to cornertwo, set othertwo to cornerfour. Examine with cornerone with cornerthree. Set otherone to cornerthree, set othertwo to cornerfour. Examine with cornerone with cornertwo. Set otherone to cornertwo, set othertwo to cornerthree. Examine with cornerone with cornerfour.

The whole is doubled divided by 2. The line is format whole number whole as text. Place "." into line. Place "0" into line. Show line.

Expected stdout

2.0

Open in Playground Standalone Page

#0964 · Least Operators to Express Number

Hard Math

Keep a shared costs as an empty list of whole numbers.
For every slot from 0 to 200.
Place -1 into costs.

Keep a shared base of 0. Keep a shared degree of 0. Keep a shared reachpower of 0. Keep a shared downcost of 0. Set base to 3.
To grow with value (a whole number). Set degree to 2. Set reachpower to base times base.
Repeat while reachpower is less than value.
Set reachpower to reachpower times base. Add 1 to degree.

To settle with value (a whole number). The plusway is value times 2 minus 1. The minusway is 2 times (base minus value). Set item value of costs to plusway. If minusway is less than plusway, set item value of costs to minusway.

To reach with value (a whole number). If item value of costs is at least 0, return. If value is 0, set item 0 of costs to 0, return. If value is 1, set item 1 of costs to 2, return. If base is at least value, Settle with value, return. Grow with value. The savedpower is reachpower. The saveddegree is degree. The spread is savedpower minus value. The lower is savedpower divided by base. Set downcost to 1000000. If spread is less than value, Reach with spread. If spread is less than value, set downcost to saveddegree plus item spread of costs. The firstcost is downcost. Reach with value minus lower. The upcost is saveddegree minus 1 plus item (value minus lower) of costs. Set item value of costs to upcost. If firstcost is less than upcost, set item value of costs to firstcost.

Reach with 19. The answer is item 19 of costs minus 1. Show answer.

Expected stdout

5

Open in Playground Standalone Page

#0965 · Univalued Binary Tree

Easy Depth-First Search

Keep a shared values holding 1, 1, 1, 1, 1, 1, and 1. Keep a shared lefts holding 1, 3, 6, -1, -1, -1, and -1. Keep a shared rights holding 2, 4, -1, -1, -1, -1, and -1. Keep a shared answer of 1.
To walk from node (a whole number). If node is less than 0, return. If item node of values is not item 0 of values, set answer to 0, return. Walk from item node of lefts. Walk from item node of rights.

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

Expected stdout

true

Open in Playground Standalone Page

#0966 · Vowel Spellchecker

Medium Array

Keep a shared words holding "Kiwi", "Kiwi", "apple", "apple", and "banana". Keep a shared lowers holding "kiwi", "kiwi", "apple", "apple", and "banana". Keep a shared vowels holding "k*w*", "k*w*", "*ppl*", "*ppl*", and "b*n*n*". Keep a shared queries holding "Kiwi", "kiwi", "Apple", "apple", "Banana", and "banana". Keep a shared querylowers holding "kiwi", "kiwi", "apple", "apple", "banana", and "banana". Keep a shared queryvowels holding "k*w*", "k*w*", "*ppl*", "*ppl*", "b*n*n*", and "b*n*n*". Keep a shared found of 0. Keep a shared wordlast of 0.
To exact from query (a whole number) with word (a whole number). If word is more than wordlast, return. If item query of queries matches item word of words, set found to word, return. Exact from query with word plus 1.

To lower from query (a whole number) with word (a whole number). If word is more than wordlast, return. If item query of querylowers matches item word of lowers, set found to word, return. Lower from query with word plus 1.

To vowel from query (a whole number) with word (a whole number). If word is more than wordlast, return. If item query of queryvowels matches item word of vowels, set found to word, return. Vowel from query with word plus 1.

Set wordlast to the last position of words. The querylast is the last position of queries.
For every query from 0 to querylast.
Set found to -1. Exact from query with 0. If found is less than 0, Lower from query with 0. If found is less than 0, Vowel from query with 0. The place is found. If found is at least 0, show item place of words, otherwise show "none".

Expected stdout

Kiwi
Kiwi
apple
apple
banana
banana

Open in Playground Standalone Page

#0967 · Numbers With Same Consecutive Differences

Medium Breadth-First Search

Keep a shared answers as an empty list of whole numbers. Keep a shared gap of 7.
To extend from value (a whole number) with depth (a whole number). If depth is 0, Place value into answers, return. The lastdigit is value modulo 10. The down is lastdigit minus gap. The up is lastdigit plus gap. If down is at least 0, Extend from value times 10 plus down with depth minus 1. If up is at most 9 and up is not down, Extend from value times 10 plus up with depth minus 1.

For every first from 1 to 9.
Extend from first with 2.

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

Expected stdout

181
292
707
818
929

Open in Playground Standalone Page

#0968 · Binary Tree Cameras

Hard Dynamic Programming

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

The cameras is 0. The node is 4.
Repeat while node is at least 0.
The left is item node of lefts. The right is item node of rights. The state is 0. If left is at least 0 and item left of marks is 0, set state to 2. If right is at least 0 and item right of marks is 0, set state to 2. If state is 0 and left is at least 0 and item left of marks is 2, set state to 1. If state is 0 and right is at least 0 and item right of marks is 2, set state to 1. If state is 2, add 1 to cameras. Set item node of marks to state. Subtract 1 from node.

If item 0 of marks is 0, add 1 to cameras. Show cameras.

Expected stdout

1

Open in Playground Standalone Page

#0969 · Pancake Sorting

Medium Array

Keep a shared numbers holding 3, 2, 4, and 1. Keep a shared flips as an empty list of whole numbers. Keep a shared picked of 0. Keep a shared best of 0.
To flip at depth (a whole number). Place depth plus 1 into flips. The low is 0. The high is depth.
Repeat while low is less than high.
swap item low of numbers and item high of numbers. Add 1 to low. Subtract 1 from high.

To locate with limit (a whole number). Set best to item 0 of numbers. Set picked to 0.
For every spot from 1 to limit.
If item spot of numbers is more than best, set best to item spot of numbers, set picked to spot.

To arrange with limit (a whole number). Locate with limit. If picked is more than 0, flip at picked. If picked is less than limit and picked is more than 0, flip at limit. If picked is 0, flip at limit.

The size is the last position of numbers.
Repeat while size is more than 0.
Arrange with size. Subtract 1 from size.

The parts is an empty list of text.
For each value in flips.
The piece is format whole number value as text. Place piece into parts.

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

Expected stdout

3,4,2,3,2

Open in Playground Standalone Page

#0970 · Powerful Integers

Medium Math

Keep a shared xpowers as an empty list of whole numbers. Keep a shared ypowers as an empty list of whole numbers. Keep a shared totals as an empty list of whole numbers. Keep a shared power of 0. Keep a shared base of 0. Keep a shared bound of 0.
To growx. If power is more than bound, return. Place power into xpowers. If base is 1, return. Set power to power times base. Growx.

To growy. If power is more than bound, return. Place power into ypowers. If base is 1, return. Set power to power times base. Growy.

Set power to 1. Set base to 2. Set bound to 10. Growx. Set power to 1. Set base to 3. Growy.
For each left in xpowers.
For each right in ypowers.
The sum is left plus right. If sum is at most 10, Place sum into totals.

Sort totals. The previous is 0.
For every spot from 0 to the last position of totals.
If spot is 0, show item spot of totals. If spot is more than 0 and item spot of totals is not previous, show item spot of totals. Set previous to item spot of totals.

Expected stdout

2
3
4
5
7
9
10

Open in Playground Standalone Page

#0971 · Flip Binary Tree To Match Preorder Traversal

Medium Depth-First Search

Keep a shared values holding 1, 2, and 3. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared voyage holding 1, 3, and 2. Keep a shared flips as an empty list of whole numbers. Keep a shared pending as an empty list of whole numbers.
For every slot from 0 to 99.
Place -1 into pending.

Set item 0 of pending to 0. The top is 1. The index is 0. The failed is 0.
Repeat while top is more than 0 and failed is 0.
Subtract 1 from top. The node is item top of pending. The start is index. The reach is the last position of voyage. If start is more than reach, set start to 0. If item node of values is not item start of voyage, set failed to 1. If failed is 0, add 1 to index. The position is index. If position is more than reach, set position to 0. The left is item node of lefts. The right is item node of rights. The probeleft is left. If probeleft is less than 0, set probeleft to node. The flip is 0. If left is at least 0 and item probeleft of values is not item position of voyage, set flip to 1. If flip is 1, Place item node of values into flips. If flip is 1 and left is at least 0, Set item top of pending to left, add 1 to top. If flip is 1 and right is at least 0, Set item top of pending to right, add 1 to top. If flip is 0 and right is at least 0, Set item top of pending to right, add 1 to top. If flip is 0 and left is at least 0, Set item top of pending to left, add 1 to top.

If failed is 1, show -1. If failed is 0 and the last position of flips is at least 0, For each value in flips, show value. If failed is 0 and the last position of flips is less than 0, show 0.

Expected stdout

1

Open in Playground Standalone Page

#0972 · Equal Rational Numbers

Hard String

Keep a shared strings holding "0.1(6)" and "0.1666(6)". Keep a shared text as empty text. Keep a shared digits of 0. Keep a shared power of 0. Keep a shared foundpos of 0. Keep a shared upper of 0. Keep a shared lower of 0. Keep a shared firsttop of 0. Keep a shared firstbottom of 0. Keep a shared secondtop of 0. Keep a shared secondbottom of 0.
To finddot. Set foundpos to -1.
For every spot from 0 to the last position of text.
If character spot of text is ".", set foundpos to spot, return.

To findopen. Set foundpos to -1.
For every spot from 0 to the last position of text.
If character spot of text is "(", set foundpos to spot, return.

To findclose. Set foundpos to -1.
For every spot from 0 to the last position of text.
If character spot of text is ")", set foundpos to spot, return.

To valueof from start (a whole number) with finish (a whole number). Set digits to 0.
For every spot from start to finish minus 1.
The digit is the digit value of character spot of text. Set digits to digits times 10 plus digit.

To raiseten with count (a whole number). Set power to 1.
Repeat while count is more than 0.
Set power to power times 10. Subtract 1 from count.

To parse. Finddot. The dot is foundpos. Findopen. The open is foundpos. The last is the last position of text. The length is last plus 1. The whole is 0. Valueof from 0 with dot. Set whole to digits. The fractionend is open. If open is less than 0, set fractionend to length. The fractionstart is dot plus 1. Valueof from fractionstart with fractionend. The fractionpart is digits. The fractionlength is fractionend minus fractionstart. The repeating is 0. The repeatinglength is 0.
If open is at least 0.
Findclose. The close is foundpos. Valueof from open plus 1 with close. Set repeating to digits. Set repeatinglength to close minus open minus 1. Raiseten with repeatinglength. The repeatingscale is power. The unit is repeatingscale minus 1. Raiseten with fractionlength. The fractionscale is power. The denominator is fractionscale times unit. The numerator is fractionpart times unit plus repeating. Set upper to whole times denominator plus numerator. Set lower to denominator.

For every spot from 0 to 1.
Set text to item spot of strings. Parse. If spot is 0, set firsttop to upper, set firstbottom to lower. If spot is 1, set secondtop to upper, set secondbottom to lower.

The left is firsttop times secondbottom. The right is secondtop times firstbottom. If left is right, show true, otherwise show false.

Expected stdout

true

Open in Playground Standalone Page

#0973 · K Closest Points to Origin

Medium Array

Keep a shared across holding 1 and -2. Keep a shared down holding 3 and 2. Keep a shared distances holding 10 and 8. Keep a shared used holding 0 and 0. Keep a shared chosen of 0. Keep a shared count of 0. Keep a shared wanted of 1.
To find. Set chosen to -1. The best is 1000000.
For every spot from 0 to the last position of across.
If item spot of used is 0 and item spot of distances is less than best, set best to item spot of distances, set chosen to spot.

Repeat while count is less than wanted.
Find. The target is chosen. Set item target of used to 1. The east is item target of across. The north is item target of down. The point is format whole number east as text. Place "," into point. The northtext is format whole number north as text. Place northtext into point. Show point. Add 1 to count.

Expected stdout

-2,2

Open in Playground Standalone Page

#0974 · Subarray Sums Divisible by K

Medium Array

The numbers is a list of 4, 5, 0, -2, -3, 1. The divisor is 5. The counts is a list of 5 zeros. Set item 0 of counts to 1. The total is 0. The ways is 0.
For each value in numbers.
Add value to total. The rem is total modulo divisor. If rem is less than 0, add divisor to rem. Increase ways by item rem of counts. Increase item rem of counts by 1.

Show ways.

Expected stdout

7

Open in Playground Standalone Page

#0975 · Odd Even Jump

Hard Array

Keep a shared values holding 10, 13, 12, 14, and 15. Keep a shared end of the last position of values. Keep a shared oddlandings as an empty list of whole numbers.
For every spot from 0 to end.
Place 0 into oddlandings.

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

Set item end of oddlandings to 1. Set item end of evenlandings to 1. Keep a shared higher of 0. Keep a shared highervalue of 0. Keep a shared lower of 0. Keep a shared lowervalue of 0.
To pickodd with origin (a whole number). Set higher to -1.
For every spot from origin plus 1 to end.
If item spot of values is at least item origin of values.
If higher is less than 0 or item spot of values is less than highervalue or item spot of values is highervalue and spot is less than higher, set higher to spot, set highervalue to item spot of values.

To pickeven with origin (a whole number). Set lower to -1.
For every spot from origin plus 1 to end.
If item spot of values is at most item origin of values.
If lower is less than 0 or item spot of values is more than lowervalue or item spot of values is lowervalue and spot is less than lower, set lower to spot, set lowervalue to item spot of values.

The place is end minus 1.
Repeat while place is at least 0.
Pickodd with place. Pickeven with place. If higher is end, set item place of oddlandings to 1. The highstep is higher. The lowstep is lower. If higher is less than end and higher is at least 0, set item place of oddlandings to item highstep of evenlandings. If lower is end, set item place of evenlandings to 1. If lower is less than end and lower is at least 0, set item place of evenlandings to item lowstep of oddlandings. Subtract 1 from place.

The good is 0.
For each flag in oddlandings.
If flag is 1, add 1 to good.

Show good.

Expected stdout

2

Open in Playground Standalone Page

#0976 · Largest Perimeter Triangle

Easy Array

The numbers is a list of 2, 1, 2. Sort numbers. The spot is the last position of numbers. The best is 0.
Repeat while spot is at least 2.
The first is item (spot minus 2) of numbers. The second is item (spot minus 1) of numbers. The third is item spot of numbers. If first plus second is more than third, set best to first plus second plus third, stop. Subtract 1 from spot.

Show best.

Expected stdout

5

Open in Playground Standalone Page

#0977 · Squares of a Sorted Array

Easy Array

The numbers is a list of -4, -1, 0, 3, and 10. The answers is a list of 5 zeros. The left is 0. The right is the last position of numbers. The spot is the last position of answers.
Repeat while left is at most right.
The low is item left of numbers. The high is item right of numbers. If low times low is more than high times high, set item spot of answers to low times low, add 1 to left, otherwise set item spot of answers to high times high, subtract 1 from right. Subtract 1 from spot.

For each value in answers, show value.

Expected stdout

0
1
9
16
100

Open in Playground Standalone Page

#0978 · Longest Turbulent Subarray

Medium Array

The numbers is a list of 9, 4, 2, 10, 7, 8, 8, 1, and 9. The rising is 0. The falling is 0. The best is 1.
For every spot from 1 to the last position of numbers.
The before is item (spot minus 1) of numbers. The here is item spot of numbers. If here is more than before, set rising to falling plus 1, set falling to 0. If here is less than before, set falling to rising plus 1, set rising to 0. If here is before, set rising to 0, set falling to 0. If rising plus 1 is more than best, set best to rising plus 1. If falling plus 1 is more than best, set best to falling plus 1.

Show best.

Expected stdout

5

Open in Playground Standalone Page

#0979 · Distribute Coins in Binary Tree

Medium Depth-First Search

Keep a shared values holding 0, 3, and 0. Keep a shared lefts holding 1, -1, and -1. Keep a shared rights holding 2, -1, and -1. Keep a shared moves of 0. Keep a shared flow of 0.

To settle from node (a whole number). If node is less than 0, set flow to 0, return. Settle from item node of lefts. The leftshare is flow. Settle from item node of rights. The rightshare is flow. The surplus is item node of values plus leftshare plus rightshare minus 1. Set flow to surplus. The gap is the absolute value of surplus. Increase moves by gap.

Settle from 0. Show moves.

Expected stdout

3

Open in Playground Standalone Page

#0980 · Unique Paths III

Hard Array

Keep a shared board holding 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, and -1. Keep a shared visited as a list of 12 zeros. Keep a shared height of 3. Keep a shared width of 4. Keep a shared target of 10. Keep a shared walkable of 11. Keep a shared paths of 0. Set item 0 of visited to 1.

To seek from other (a whole number) with seen (a whole number). If other is less than 0, return. If other is at least 12, return. If item other of board is -1, return. If item other of visited is 1, return. Set item other of visited to 1. Explore from other with seen plus 1. Set item other of visited to 0.

To explore from place (a whole number) with seen (a whole number). If place is target and seen is walkable, add 1 to paths. If place is target, return. The band is place divided by width. The file is place modulo width. If file is more than 0, seek from (place minus 1) with seen. If file is less than width minus 1, seek from (place plus 1) with seen. If band is more than 0, seek from (place minus width) with seen. If band is less than height minus 1, seek from (place plus width) with seen.

Explore from 0 with 1. Show paths.

Expected stdout

2

Open in Playground Standalone Page

#0982 · Triples with Bitwise AND Equal To Zero

Hard Array

The numbers is a list of 2, 1, and 3. The total is 0.
For every first from 0 to the last position of numbers.
For every second from 0 to the last position of numbers.
For every third from 0 to the last position of numbers.
The pair is bitwise and of item first of numbers and item second of numbers. The whole is bitwise and of pair and item third of numbers. If whole is 0, add 1 to total.

Show total.

Expected stdout

12

Open in Playground Standalone Page

#0983 · Minimum Cost For Tickets

Medium Array

Keep a shared days holding 1, 4, 6, 7, 8, and 20. Keep a shared prices holding 2, 7, and 15. Keep a shared travel as an empty list of whole numbers. Keep a shared table as an empty list of whole numbers. For every slot from 0 to 30, Place 0 into travel. For every slot from 0 to 30, Place 0 into table. For each day in days, set item day of travel to 1.

To fill from date (a whole number). If item date of travel is 0, set item date of table to item (date minus 1) of table, return. The week is date minus 7. If week is less than 0, set week to 0. The month is date minus 30. If month is less than 0, set month to 0. The single is item (date minus 1) of table plus item 0 of prices. The span is item week of table plus item 1 of prices. The longspan is item month of table plus item 2 of prices. The best is the smaller of single and span. The finest is the smaller of best and longspan. Set item date of table to finest.

For every date from 1 to 20, fill from date. Show item 20 of table.

Expected stdout

11

Open in Playground Standalone Page

#0984 · String Without AAA or BBB

Medium String

The acount is 4. The bcount is 1. The text is empty text. The prior is 0. The streak is 0.
Repeat while acount plus bcount is more than 0.
The side is 1. If acount is less than bcount, set side to 2. If side is prior and streak is 2, if side is 1, set side to 2, otherwise set side to 1. If side is 1, subtract 1 from acount. If side is 2, subtract 1 from bcount. If side is prior, add 1 to streak, otherwise set prior to side, set streak to 1. If side is 1, Place "a" into text, otherwise Place "b" into text.

Show text.

Expected stdout

aabaa

Open in Playground Standalone Page

#0985 · Sum of Even Numbers After Queries

Medium Array

The numbers is a list of 1, 2, 3, and 4. The values is a list of 1, -3, -4, and 2. The spots is a list of 0, 1, 0, and 3. The total is 0. For each number in numbers, if number modulo 2 is 0, add number to total.
For every which from 0 to the last position of values.
The spot is item which of spots. The oldvalue is item spot of numbers. If oldvalue modulo 2 is 0, subtract oldvalue from total. Set item spot of numbers to oldvalue plus item which of values. The fresh is item spot of numbers. If fresh modulo 2 is 0, add fresh to total. Show total.

Expected stdout

8
6
2
4

Open in Playground Standalone Page

#0986 · Interval List Intersections

Medium Array

The firststarts is a list of 0, 5, 13, and 24. The firstends is a list of 2, 10, 23, and 25. The secondstarts is a list of 1, 8, 15, and 25. The secondends is a list of 5, 12, 24, and 26. The first is 0. The second is 0. The found is 0.
Repeat while first is at most the last position of firststarts and second is at most the last position of secondstarts.
The low is item first of firststarts. If item second of secondstarts is more than low, set low to item second of secondstarts. The high is item first of firstends. If item second of secondends is less than high, set high to item second of secondends. The firstend is item first of firstends. The secondend is item second of secondends. If firstend is less than secondend, add 1 to first, otherwise add 1 to second.
If low is at most high.
If found is more than 0, show "---". Show low. Show high. Add 1 to found.

Expected stdout

1
2
---
5
5
---
8
10
---
15
23
---
24
24
---
25
25

Open in Playground Standalone Page