Discuss Scratch

theodd1sout_imcool
Scratcher
12 posts

Three same in a list

I'm doing a project in which I have a list that will have ten numbers in it. It then check if three numbers in the list are the same. The problem is, I don't know how to do this and have tried lots of different scripts but none of them have worked. Please help!
sCrAtCh_139508
Scratcher
100+ posts

Three same in a list

sense if list has (3) or more of the same thing
(?) // the variable that represents if the list has 3 or more of a number or not
define sense if list has (in1) or more of the same thing
set [? v] to [false]
set [i v] to [0]
delete all of [a v] :: list
repeat (length of [list v] :: list)
change [i v] by (1)
repeat ((item (i) of [list v] :: list) - (length of [a v] :: list))
add [0] to [a v]
end
replace item (item (i) of [list v] :: list) of [a v] with ((item (item (i) of [list v] :: list) of [a v] :: list) + (1))
end
set [i v] to [0]
repeat (length of [a v] :: list)
change [i v] by (1)
if <((item (i) of [a v] :: list) + (1)) > (in1)> then
set [? v] to [true]
stop [this script v]
end
end
Scratch-Minion
Scratcher
1000+ posts

Three same in a list

In case this is homework, here is some logic that will solve the problem. You need to translate it into Scratch blocks.
The logic sets Start Index to 1 and checks if there are two more entries the same as at Start Index = 1,
If not, it checks if there are two more entries the same as at Start Index = 2,
etc etc


Set Start Index to 1
Set Same Number Count to 1

Repeat the following until Same Number Count >= 3 or Start Index > 8 (ie. 3 numbers the same or no room for 3 equal numbers)

Set Same Number Count to 1

Loop through the list with Index going from (Start Index + 1) to 10
If the list entry at that Index equals the list entry at Start Index, then Add 1 to Same Number Count

Add 1 to Start Index

Now Same Number Count will be >= 3 if three list numbers are the same.

Edit: ninja'd as I am a slow typist but this solution is quite different to that above

Last edited by Scratch-Minion (Dec. 13, 2022 22:54:41)

theodd1sout_imcool
Scratcher
12 posts

Three same in a list

sCrAtCh_139508 wrote:

sense if list has (3) or more of the same thing
(?) // the variable that represents if the list has 3 or more of a number or not
define sense if list has (in1) or more of the same thing
set [? v] to [false]
set [i v] to [0]
delete all of [a v] :: list
repeat (length of [list v] :: list)
change [i v] by (1)
repeat ((item (i) of [list v] :: list) - (length of [a v] :: list))
add [0] to [a v]
end
replace item (item (i) of [list v] :: list) of [a v] with ((item (item (i) of [list v] :: list) of [a v] :: list) + (1))
end
set [i v] to [0]
repeat (length of [a v] :: list)
change [i v] by (1)
if <((item (i) of [a v] :: list) + (1)) > (in1)> then
set [? v] to [true]
stop [this script v]
end
end
This works, only there's one problem. My list is mostly numbers that have decimals in it, and this script won't work for it (it only picks up on the first number). Do you know how I could fix this?
deck26
Scratcher
1000+ posts

Three same in a list

I suspect you need two extra lists, both empty to start with.

Copy the first item of your list (List1) the first new list (List2) and add 1 to List3.

Now loop through the rest of the values in List1. In each case you have two possible situations.

- Item is not in List2 so add it and add another 1 (ie as a new item) to List3.

- Item is in List2. Use the item # of x in List2 to identify which position it is in and increase the corresponding item in List3 by 1.

You can potentially stop as soon as List3 contains 3.

Powered by DjangoBB