Detect parity error in any number of rows (after all the rows are entered)

Challenge Level: Ready to expand

Learning outcomes

Students will be able to:

Requirement:

Write a program that asks the user how many rows they would like to enter then asks for the rows one row at a time. It then displays the rows with parity error at the end as the output. You will need to use a list to store all the rows for this challenge (store each row as an item of the list).

Testing examples:

Your program should display the outputs shown in these panels for the given inputs provided:

Input Output
How many rows would you like to enter? 6
Enter 6 cards for row 1: WBBWWW
Enter 6 cards for row 2: BBWBWB
Enter 6 cards for row 3: BWWWBW
Enter 6 cards for row 4: BBBBBB
Enter 6 cards for row 5: WBBWWB
Enter 6 cards for row 6: BBBWBB
There is a parity error in row 5
There is a parity error in row 6
How many rows would you like to enter? 4
Enter 4 cards for row 1: BWWB
Enter 4 cards for row 2: BBBB
Enter 4 cards for row 3: WBBB
Enter 4 cards for row 4: WBWB
There is parity error in row 3

Languages

Scratch

What it should look like

Click on the green flag, enter the inputs provided in the “testing examples” to see the expected output of your program.

Recommended blocks
when green flag clicked

say (join [There is a parity error in row ] (index row)) for (3) secs

add (answer) to [rows v]

delete (all v) of [rows v]
set [black cards total v] to [0]

set [row number v] to [1]

set [number of rows v] to (answer)

change [row number v] by (1)

set [index row v] to [1]

set [index v] to [1]

change [black cards total v] by (1)

change [index v] by (1)

set [black cards total v] to [0]

change [index row v] by (1)

change [black cards total v] by (1)
ask [How many rows would you like to enter?] and wait

ask (join [Enter row ] (row number)) and wait
repeat (number of rows)
end

repeat (length of [rows v] :: list)
end

repeat (length of (item (index row) of [rows v] :: list))
end

if <((black cards total) mod (2)) = [1]> then
end

if <(letter (index) of (item (index row) of [rows v] :: list)) = [B]> then
end
Hints
  • For this challenge you need to use a list to store all the rows (each item in the list is a row). You then need to go through each row (item of your list) and check if they have a parity error (i.e. the total number of black cards in each row is odd). If one or more rows have parity errors, you then need to find their positions in the list and report these positions as the output.

  • To make a new list select “Make a list” under “Data” script.

  • Add items to your list by using the add () to [rows v] block.

  • You can access an item in a specific position in your list by using the item () of [rows v] :: list block.

  • The length of [rows v] :: list block reports how many items are in the list.

  • You need to delete all the previous items from your list at the start of your program. If you don’t do this, your new items get added to the list every time you run your program. To delete all the items from your list select “all” from the drop down list on the delete (all v) of [rows v] block.

  • You can access a letter at the specified position in a string by using the letter (1) of [world] block under “Operators”. For example: letter (1) of [world] //w

  • In this challenge you need to access all the letters in user’s input (each row of the parity trick) and check to see how many of them are equal to B (black). Store the total number of black squares in a variable called “black cards total”.

  • You can find how many letters a string has by using the length of [world] block unders “Operators”.

  • To find out if a number is even or odd, use the () mod () block (under "Operators") to find the remainder after dividing that number by two. If the remainder is zero the number is even. For example: (37) mod (10) //7

Show Scratch solution