Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with lists
- 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.
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
<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
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.
- 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.
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
What is “n”? a variable? If so, could you explain how,when, and where to use that script?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
- drmcw
- Scratcher
1000+ posts
Help with lists
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.What is “n”? a variable? If so, could you explain how,when, and where to use that script?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
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)
- FlipnoteMrMan
- Scratcher
29 posts
Help with lists
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.What is “n”? a variable? If so, could you explain how,when, and where to use that script?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
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:
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: (Should be a purple block, not red as scratchblocks doesn't do those blocks)
[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. )
- drmcw
- Scratcher
1000+ posts
Help with lists
Thanks. I shall try and remember that. btw: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: (Should be a purple block, not red as scratchblocks doesn't do those blocks)[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. )
- Discussion Forums
- » Help with Scripts
- » Help with lists