Detect parity error in a row

Challenge Level: Growing experience

Learning outcomes

Students will be able to:

Requirement:

Write a program which asks the user to enter a row of black and white cards and checks if there is a parity error in that row (i.e. you should check if there is an odd number of black squares in that row).

Testing examples:

Your program should display the outputs shown in this table for the given inputs provided:

Input Output
WWWBBW There is no parity error in this row!
WBBWBW There is a parity error in this row!
WWWWWW There is no parity error in this row!

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

ask [Please enter a row of black and white cards (B for black and W for white):] and wait
if <(letter (index) of (cards)) = [B]> then
end

repeat (length of (cards))
end

if <((black cards total) mod (2)) = [0]> then
else
end
say [There is no parity error in this row!]

say [There is a parity error in this row!]
change [black cards total v] by (1)

change [index v] by (1)
set [black cards total v] to [0]

set [index v] to [1]

set [cards v] to (answer)
Hints
  • 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 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