The teacher wishes to see if her pupils can make sense of words in which certain letters have been removed.
She requires a when[space]key_pressed script that will take a word from the user and display a new word that is the same as the original except that each letter from the first ten letters of the alphabet has been removed. For clarification, the first ten letters of the alphabet should be considered as: a, b, c, d, e, f, g, h, i and j.
The user should input the word as a single string and the new word that is displayed should be a single string.
For example, if the user enters
‘party’
then
‘prty’
is displayed.
One way to create the new word to be displayed is to start with the new word as an empty string and then work through the letters in the original word from first to last, creating the new word incrementally. If the current letter is not in the first ten letters of the alphabet, then we add it to the new word, otherwise we do not add it to the new word. This might be visualised as in Figure 2.
Described image
Figure 2 Example of how to solve the problem described
A box labelled ‘Original word’ is shown, containing the following characters printed in a row: p a r t y. Below the box is a row of clouds containing text. The first cloud is below and to the left of the box and contains the text: New word so far: ‘’. Subsequent clouds are each aligned with a character in the box above. The cloud below ‘p’ contains the text: New word so far: ‘p’. The cloud below ‘a’ contains the text: New word so far: ‘p’. The cloud below ‘r’ contains the text: New word so far: ‘pr’. The cloud below ‘t’ contains the text: New word so far: ‘prt’. The cloud below ‘y’ contains the text: New word so far: ‘prty’.
Figure 2 Example of how to solve the problem described
Recall that, as in Part 4 Activity 4.8, a simple way to check whether a given character is one of a set of specific characters is to check whether it is in a list that has been populated with those specific characters.
a.Create and write down an algorithm to solve this problem. You might like to use the idea above, or an alternative of your own.
(10 marks)
b.Create a when[space]key_pressed script to implement your algorithm. Depending on your algorithm and the way in which you choose to implement it, you may also decide to have a when_green_flag_clicked script.
Take a screenshot of your script(s) and paste it into your TMA document.
(11 marks)
I'm using OUBuild that saves as .sb2 (I'm not sure what version this is)
Assistant
Let's walk through the problem step by step.
Part a: Algorithm to remove the first ten letters of the alphabet (a-j) from a word
Algorithm:
Start
Define a list removeLetters containing the letters ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'].
Ask the user to input a word and store it in originalWord.
Initialize an empty string newWord.
For each letter l in originalWord from first to last:
If l is not in removeLetters:
Add l to newWord.
Otherwise, do not add l to newWord.
After processing all letters, display newWord.
End
Explanation:
The algorithm iterates over each letter in the input word.
If the letter is NOT within the first 10 letters of the alphabet, it is appended to the new word.
Otherwise, it is ignored.
At the end, the new word containing the filtered letters is shown.
Part b: Creating the when [space] key pressed script in Scratch (OUBuild / version .sb2)
Since you are working with OUBuild (.sb2 format), here is a textual description of the blocks you need, plus a screenshot suggestion.
Scratch/OUBuild Script Description:
Variables needed:
originalWord (to store the user input)
newWord (to build the filtered word)
Lists needed:
removeLetters = [a, b, c, d, e, f, g, h, i, j]
Script triggered by:
when [space] key pressed
Script wants to:
Ask the user: "Enter a word:"
Set originalWord to the answer
Set newWord to empty string ""
Repeat for each character l in originalWord:
Use letter (position) of (originalWord) to get each letter
User
So far I have:
when flag clicked
ask "enter a word" the wait
set [original_word] to (answer)
set [new_word] to ""
delete [all] of [letters_to_remove]
add (a) to [letters_to_remove]
add (b) to [letters_to_remove]
add (c) to [letters_to_remove]
add (d) to [letters_to_remove]
add (e) to [letters_to_remove]
add (f) to [letters_to_remove]
add (g) to [letters_to_remove]
add (h) to [letters_to_remove]
add (i) to [letters_to_remove]
add (j) to [letters_to_remove]
repeat (length of (original_word))
Assistant
Your script is off to a great start! The only thing missing is the inner logic inside the repeat loop, where you check each letter of the original_word and decide whether to add it to new_word depending on whether it is in letters_to_remove.
Here’s how you can complete your loop:
Completing your scratch script:
scratch
when green flag clicked
ask "enter a word" and wait
set [original_word] to (answer)
set [new_word] to ""
delete all of [letters_to_remove]
add (a) to [letters_to_remove]
add (b) to [letters_to_remove]
add (c) to [letters_to_remove]
add (d) to [letters_to_remove]
add (e) to [letters_to_remove]
add (f) to [letters_to_remove]
add (g) to [letters_to_remove]
add (h) to [letters_to_remove]
add (i) to [letters_to_remove]
add (j) to [letters_to_remove]
repeat (length of (original_word))
set [current_letter] to (letter (repeat index) of (original_word))
if <not <(current_letter) is in [letters_to_remove]>> then
set [new_word] to (join (new_word) (current_letter))
end
end
say (new_word) for 2 seconds
Explanation of the new blocks inside the repeat:
set [current_letter] to (letter (repeat index) of (original_word))
— takes the current letter you are checking.
if <not <(current_letter) is in [letters_to_remove]>> then
— tests if the letter is NOT in the list letters_to_remove.
set [new_word] to (join (new_word) (current_letter))
— appends the letter to the new_word if it is allowed.
Notes:
The repeat index refers to the current iteration number (from 1 to length of original_word).
The letter (repeat index) of (original_word) block fetches the letter at position repeat index.
Finally, after the loop finishes, your variable will contain the filtered word.
User
where is repeat index
Assistant
Great question!
In Scratch (and OUBuild, which is very similar), repeat index is not a block itself; it means the current iteration number inside a repeat (N) loop.
How to get the current iteration number inside a repeat loop?
You need to create a variable to act as a counter because the built-in repeat block in Scratch does not automatically provide the iteration number.
For example, inside your repeat loop:
scratch
set [counter] to 1
repeat (length of (original_word))
set [current_letter] to (letter (counter) of (original_word))
if <not <(current_letter) is in [letters_to_remove]>> then
set [new_word] to (join (new_word) (current_letter))
end
change [counter] by 1
end
Explanation:
Before the repeat loop starts, set variable counter to 1.
Inside the loop, counter keeps track of the current position in the word.
After processing each letter, increase counter by 1.
Use (letter (counter) of (original_word)) to get the letter at the current position.
Summary:
There is no built-in repeat index block; you simulate it by:
Creating a variable, say counter.
Initialize it before the loop.
Increment it inside the loop.
Let me know if you'd like me to clarify this with step-by-step block instructions!
User
I can't add current_letter to the block
Assistant
You're right — in Scratch (and OUBuild), you cannot put a variable inside the "is in list" block directly inside the <not> block with a mouse drag because the <not> block takes a boolean input, but the "is (item) in list" block wants an input that must be fixed or carefully assembled.
How to properly check if a variable (like current_letter) is NOT in a list?
You do this:
code
if <not <(letters_to_remove) contains (current_letter)>> then
// do something
end
Explanation:
Instead of "current_letter is in letters_to_remove", Scratch block says "list contains item" but the inputs are: you pick the list first, then the item.
So, use the <list> contains <item> block with your list selected and the variable as the item.
Then wrap that whole block inside the <not> block.
In Scratch blocks, this looks like:
From the Operators palette, drag the <not ()> block.
Inside that <not>, put the list contains (item) block from the List palette.
Select your letters_to_remove list in the dropdown of the list contains block.
Drag the variable current_letter into the item input of list contains.
Summary:
The correct check is:
code
if <not <(letters_to_remove) contains (current_letter)>> then
...
end
NOT
code
if <not <(current_letter) is in [letters_to_remove]>> then
...
end
because the second syntax doesn’t exist as a block.
If you’d like, I can write the exact block names in the menus to find these blocks!
User
I can't add letters_to_remove to block
Assistant
Thanks for clarifying!
In Scratch (and OUBuild), the <not> block takes a boolean (true/false) input, and the "list contains item" block itself is a boolean reporter block — so you should be able to put the (list) contains (item) block inside the <not> block.
If you’re having trouble putting the letters_to_remove list inside the contains block (or the contains block inside the <not> block), here’s a detailed step-by-step:
Step-by-step to check if current_letter is NOT in letters_to_remove
Go to the Operators palette and drag out the <not ()> block.
Into the empty slot inside <not>, drag a <list> contains () block from the Variables or Lists palette (in Scratch it’s usually under Lists).
On the list contains block:
On the list dropdown, select letters_to_remove.
In the item slot, drag your variable current_letter.
Visual look in Scratch:
scratch
if <not <(letters_to_remove) contains (current_letter)>> then
// add letter to new_word
end
Important hints:
Make sure letters_to_remove is a list variable, not a normal variable.
current_letter should be a normal variable holding a single character.
You cannot put a list.variable directly inside <not>.
You must use the list contains item reporter inside<not>.
Troubleshooting:
If you can’t drag letters_to_remove into the “list contains” block, check that letters_to_remove is definitely a list, not a normal variable.
In Scratch, lists have a special icon (like a little bulleted list).
If you can’t put the “list contains” block inside the <not> block, try dragging the <not> block first, then carefully drop the “list contains” block inside, or drag the “list contains” out and then wrap it with <not> afterwards.
If you want, I can also write out exactly which palettes to open and which blocks to drag step-by-step! Would that help?
User
It is a list item and still can not be added to the block
Assistant
Thanks for the extra info!
Scratch (and OUBuild) definitely allow this pattern:
code
<not <(list) contains (item)>>
If you are unable to drag the entire "list contains item" block inside the <not> block, here are some troubleshooting tips and alternative approaches:
1. Drag the <not> block out first, then drag the "list contains item" block inside it
First, from Operators, drag out the <not ()> block and place it where you want the condition.
Then, from Lists, drag the letters_to_remove contains (item) block into the empty slot inside the <not> block.
If you try dragging the <not> block onto the list contains block (or vice versa) it might sometimes glitch — so do this step by step.
2. Make sure you are dragging the full "list contains" block
Sometimes when dragging blocks, if you only drag the variable or the title, it won't work.
You must drag the full block that looks like:
letters_to_remove contains ( )
(with a dropdown for the list name).
3. Check for block versions in your environment / OUBuild
Some older Scratch-like editors don’t have the list contains item block or may implement it differently.
In that case, you can simulate "list contains" by looping through the list manually:
Alternative: Manual loop to check if letter is in the list
If you cannot use the list contains block, use a manual approach:
scratch
set [found] to [false]
set [index] to [1]
repeat (length of [letters_to_remove])
if <(current_letter) = (item (index) of [letters_to_remove])> then
set [found] to [true]
end
change [index] by 1
end
if <not (found)> then
set [new_word] to (join (new_word) (current_letter))
end
This code manually searches through the list to find if current_letter is in it, sets found to true if yes, then adds the letter only if not found.
4. Summary
Try dragging <not> block first, then put list contains inside it.
If not possible, use the manual loop method above (which works everywhere).
If you want, I can write a full together script using the manual loop — just ask!
User
what is alternative to 'is in'
Assistant
In Scratch (and OUBuild), there is no single "is in" block to directly check if an item is in a list. Instead, you typically use the list contains block:
(letters_to_remove) contains (item)
If your environment doesn't have this block or you can't use it for some reason, the alternative is to manually check the list by:
Alternative: Manually searching the list with a loop
You:
Create a variable, e.g., found, and set it to false initially.
Loop through the list items from 1 to length of the list.
In each iteration, compare the list item against the letter.
If equal, set found to true.
After loop, if found is still false, the letter is NOT in the list.
Example Scratch pseudocode:
scratch
set [found] to [false]
set [index] to [1]
repeat (length of [letters_to_remove])
if <(current_letter) = (item (index) of [letters_to_remove])> then
set [found] to [true]
end
change [index] by 1
end
if <not (found)> then
// Add current_letter to new_word
set [new_word] to (join (new_word) (current_letter))
end
Why do this?
This replaces the "is in" or "list contains" block with straightforward numeric-indexed lookup and comparison.
It works in all Scratch versions and Scratch-like editors.
Summary
If you can’t find or use (list contains item) block, use this manual search loop to achieve the same check.
If you want, I can help you write the full script with this in your Scratch blocks! Just ask.
Not yet included in a published release.
If l is NOT in removeLetters, then join l to newWord
Display newWord (e.g., using say newWord for 2 seconds)
Scratch Block Breakdown:
when [space] key pressed
ask "Enter a word:" and wait
set originalWord to (answer)
set newWord to ""
repeat (length of originalWord) times
set l to (letter (repeat index) of (originalWord))
if <not <(l) contains in list (removeLetters)>> then
set newWord to (join (newWord) (l))
say (newWord) for 2 secs
Optional: when green flag clicked
Initialize the removeLetters list with the letters ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
How to set up removeLetters list:
Inside the when green flag clicked:
Delete all from removeLetters
Add each letter to the list:
add a to removeLetters
add b to removeLetters
...
add j to removeLetters
Example solution in Scratch/OUBuild:
when green flag clicked
delete all from removeLetters
add a,b,c,d,e,f,g,h,i,j individually to the list
when [space] key pressed
ask "Enter a word:" and wait
set originalWord to (answer)
set newWord to ""
repeat (length of (originalWord)) times
set l to (letter (repeat index) of (originalWord))
if <NOT (l is in removeLetters)> then
set newWord to (join (newWord) (l))
say (newWord) for 2 seconds
Posting the Screenshot
Run your project in OUBuild
Take a screenshot showing:
-- The when green flag clicked script that initializes removeLetters list
-- The when space key pressed script that performs the filtering
If you want, I can also write the Scratch Python equivalent pseudocode or export the blocks as text.
Let me know if you want me to create the Scratch .sb2 project or a full step-by-step guide on how to create this in OUBuild Scratch editor!
new_word
The script uses say (new_word) for 2 seconds to display the result.
Alternative:
If you want the prompt on space key press instead of the green flag, move the ask part to a when [space] key pressed script, and only initialize the list in when green flag clicked.
If you want, I can also help you organize this into two scripts:
Script 1: Initialize list
scratch
when green flag clicked
delete all of [letters_to_remove]
add (a) to [letters_to_remove]
add (b) to [letters_to_remove]
add (c) to [letters_to_remove]
add (d) to [letters_to_remove]
add (e) to [letters_to_remove]
add (f) to [letters_to_remove]
add (g) to [letters_to_remove]
add (h) to [letters_to_remove]
add (i) to [letters_to_remove]
add (j) to [letters_to_remove]
Script 2: Process word on space
scratch
when space key pressed
ask "enter a word" and wait
set [original_word] to (answer)
set [new_word] to ""
repeat (length of (original_word))
set [current_letter] to (letter (repeat index) of (original_word))
if <not <(current_letter) is in [letters_to_remove]>> then
set [new_word] to (join (new_word) (current_letter))
end
end
say (new_word) for 2 seconds
If you want to paste your final screenshot of these blocks into your TMA document, this separation is ideal and clean.
If you want me to provide the exact block images or more help, just ask!