Discuss Scratch

FlipnoteMrMan
Scratcher
29 posts

Help with lists

I'm trying to make a sodoku game and I need to know how to detect how many of one number are in a list.

Example:

List
1–|2
2–|3
3–|3
4–|7
5–|4

How would I detect how many threes there are in that, with a script? I only need this part and I should be working through with a new project


Thanks in advance.

Last edited by FlipnoteMrMan (Dec. 7, 2013 05:08:01)

Xyyzzz
Scratcher
79 posts

Help with lists

<length of (list)

Last edited by Xyyzzz (Dec. 7, 2013 05:22:22)

FlipnoteMrMan
Scratcher
29 posts

Help with lists

Xyyzzz wrote:

<length of (list)>


I mean like, if there were multiple of one number, how would I make a script so where it could identify that there were two of one numbers.
Xyyzzz
Scratcher
79 posts

Help with lists

Oh, so if there are more than 1 5s, you'd like to know how many 5s there are?
FlipnoteMrMan
Scratcher
29 posts

Help with lists

Xyyzzz wrote:

Oh, so if there are more than 1 5s, you'd like to know how many 5s there are?

Yeah. But it has to be automatic.
ProdigyZeta7
Scratcher
1000+ posts

Help with lists

define Number of items = (n)
set [counter v] to [1]
set [return v] to [0]
repeat (length of [list v])
if <(item (counter) of [list v]) = (n)>
change [return v] by (1)
end
change [counter v] by (1)
end



Xyyzzz
Scratcher
79 posts

Help with lists

This is harder than I thought. o.o

Anyway, here is what I have so far: http://scratch.mit.edu/projects/15151435/

It will generate 5 numbers from 2 to 7, add them to the list, filter out all extras (add those to a list), and put the filtered numbers into a third list. When I get more time tomorrow I'll make it so that it shows how many multiples of each number there were.

Last edited by Xyyzzz (Dec. 7, 2013 06:06:06)

FlipnoteMrMan
Scratcher
29 posts

Help with lists

ProdigyZeta7 wrote:

define Number of items = (n)
set [counter v] to [1]
set [return v] to [0]
repeat (length of [list v])
if <(item (counter) of [list v]) = (n)>
change [return v] by (1)
end
change [counter v] by (1)
end
What is “n”? a variable? If so, could you explain how,when, and where to use that script?
drmcw
Scratcher
1000+ posts

Help with lists

FlipnoteMrMan wrote:

ProdigyZeta7 wrote:

define Number of items = (n)
set [counter v] to [1]
set [return v] to [0]
repeat (length of [list v])
if <(item (counter) of [list v]) = (n)>
change [return v] by (1)
end
change [counter v] by (1)
end
What is “n”? a variable? If so, could you explain how,when, and where to use that script?
n is called a parameter and in this case it is a number passed into the procedure so it would be 3 if you wanted the count for all the 3's in the list.
Called like
Number of Items = [3]
(Should be a purple block, not red as scratchblocks doesn't do those blocks)
You'd make the define block by using the “More blocks” button. If you didn't want to use “More blocks” then just use ProdigyZeta7 script without the hat and substitute n with a variable containing the number you want a count of.

The variable “return” contains the count of the number in the list.

Last edited by drmcw (Dec. 7, 2013 07:28:20)


10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
FlipnoteMrMan
Scratcher
29 posts

Help with lists

drmcw wrote:

FlipnoteMrMan wrote:

ProdigyZeta7 wrote:

define Number of items = (n)
set [counter v] to [1]
set [return v] to [0]
repeat (length of [list v])
if <(item (counter) of [list v]) = (n)>
change [return v] by (1)
end
change [counter v] by (1)
end
What is “n”? a variable? If so, could you explain how,when, and where to use that script?
n is called a parameter and in this case it is a number passed into the procedure so it would be 3 if you wanted the count for all the 3's in the list.

You'd make the define block by using the “More blocks” button. If you didn't want to use “More blocks” then just use ProdigyZeta7 script without the hat and substitute n with a variable containing the number you want a count of.

The variable “return” contains the count of the number in the list.


Okay thank you
blob8108
Scratcher
1000+ posts

Help with lists

btw:

drmcw wrote:

(Should be a purple block, not red as scratchblocks doesn't do those blocks)
If you write the define hat inside the same script, it'll detect it as a custom block. Alternatively, you can put `// category=custom` at the end of the line:
[scratchblocks]
Number of Items = [3] // category=custom
[/scratchblocks]
Number of Items = [3] // category=custom
(This is going to be changed to `:: custom` pretty soon, which should be easier to remember. )

tosh · slowly becoming a grown-up adult and very confused about it
drmcw
Scratcher
1000+ posts

Help with lists

blob8108 wrote:

btw:

drmcw wrote:

(Should be a purple block, not red as scratchblocks doesn't do those blocks)
If you write the define hat inside the same script, it'll detect it as a custom block. Alternatively, you can put `// category=custom` at the end of the line:
[scratchblocks]
Number of Items = [3] // category=custom
[/scratchblocks]
Number of Items = [3] // category=custom
(This is going to be changed to `:: custom` pretty soon, which should be easier to remember. )
Thanks. I shall try and remember that.

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?

Powered by DjangoBB