Discuss Scratch

Dtown10
New Scratcher
1 post

Even or Odd number sorter

I am working on a project where scratch generates ten random numbers and puts them in a list. Whenever you press s the numbers are supposed to be sorted into 2 different lists even and odd. How do i sort the numbers.
this is what i have so far
https://scratch.mit.edu/projects/178918750/
GMO_Ferret
Scratcher
7 posts

Even or Odd number sorter

I made a “game” that does what I think you want, I couldn't look at your game because of time constraints.
Here is the link! I will explain this when I get home.
Despicable_Dad
Scratcher
500+ posts

Even or Odd number sorter

One way is to take the number and divide it by 2, then see whether the answer is a whole number.

So 3 divided by 2 is 1.5 (not whole) but 10 divided by 2 is 5 (whole)

Get Scratch to find whether it's whole by comparing the result with the rounded-down result. If they're the same, it was whole to start with.

See remix here: https://scratch.mit.edu/projects/178925026/
deck26
Scratcher
1000+ posts

Even or Odd number sorter

Or use the mod function. N mod 2 is 0 if N is even and 1 if N is odd.
RholanT3344
Scratcher
62 posts

Even or Odd number sorter

when green flag clicked
set [odd v] to [1]
set [even v] to [2]
repeat ([half v] of (...)::operators)
add (even) to [even2 v]
change [even v] by (2)
end
repeat (...)
add (odd) to [odd2 v]
change [odd v] by (1)
end
deck26
Scratcher
1000+ posts

Even or Odd number sorter

RholanT3344 wrote:

when green flag clicked
set [odd v] to [1]
set [even v] to [2]
repeat ([half v] of (...)::operators)
add (even) to [even2 v]
change [even v] by (2)
end
repeat (...)
add (odd) to [odd2 v]
change [odd v] by (1)
end
Doesn't match the question which was about 10 random numbers - otherwise a good method to create odd and even lists.
asivi
Scratcher
1000+ posts

Even or Odd number sorter

Maybe change odd by 2.
deck26
Scratcher
1000+ posts

Even or Odd number sorter

asivi wrote:

Maybe change odd by 2.
Good spot, I didn't check it that closely.

No real need for two variables either. Just increase one variable by 1 each time you save it and save once to odd and once to even each time through the loop.
scratch14236749
Scratcher
1 post

Even or Odd number sorter

How do you make the numbers touch the wrong bowl and it explodes???

deck26
Scratcher
1000+ posts

Even or Odd number sorter

scratch14236749 wrote:

How do you make the numbers touch the wrong bowl and it explodes???

Please create your own topic rather than necroposting on a topic that has nothing to do with your question.
Killernotch101
Scratcher
100+ posts

Even or Odd number sorter

Dtown10 wrote:

I am working on a project where scratch generates ten random numbers and puts them in a list. Whenever you press s the numbers are supposed to be sorted into 2 different lists even and odd. How do i sort the numbers.
this is what i have so far
https://scratch.mit.edu/projects/178918750/
You want to sort an existing list (list), and sort it into two lists, (odd), and (even).

when green flag clicked
repeat until <(length of [list v] :: list) < [1]>
if <((item (1 v) of [list v] :: list) mod (2)) = [0]> then
add (item (1 v) of [list v] :: list) to [even v]
else
add (item (1 v) of [list v] :: list) to [odd v]
end
delete (1 v) of [list v]
end

This basically repeats until the “list” is empty. Until it is, it checks if it’s odd or even. (Using modulo!) It then adds it to the even, or odd list. Finally, it deletes the number it just sorted. (That was a horrible explanation, I know.)
ethmah01
Scratcher
14 posts

Even or Odd number sorter

when I receive [ sort numbers]
repeat (10)
if <[(((item (1) of [numbers] :: list)) / (2))] contains [.] ?> then


add (item (1) of [numbers] :: list) to [odd numbers]

else
add (item (1) of [numbers] :: list) to [even numbers]
end
delete (1) of [numbers]
end


My way of doing it. I would normally use the operator contains though, not the list one (I couldn't find the green one).

Last edited by ethmah01 (June 1, 2020 14:24:14)

ethmah01
Scratcher
14 posts

Even or Odd number sorter

Well I can't get the scratch blocks to work but what I normally do is divide the number by two, and if it contains a period/dot, then it must be odd. Then you just go from there
deck26
Scratcher
1000+ posts

Even or Odd number sorter

ethmah01 wrote:

Well I can't get the scratch blocks to work but what I normally do is divide the number by two, and if it contains a period/dot, then it must be odd. Then you just go from there
Please don't necropost. this topic was dealt with some time ago.
FatCatOn1Mat
Scratcher
1 post

Even or Odd number sorter

deck26 wrote:

RholanT3344 wrote:

when green flag clicked
set [odd v] to [1]
set [even v] to [2]
repeat ([half v] of (...)::operators)
add (even) to [even2 v]
change [even v] by (2)
end
repeat (...)
add (odd) to [odd2 v]
change [odd v] by (1)
end
Doesn't match the question which was about 10 random numbers - otherwise a good method to create odd and even lists.
deck26
Scratcher
1000+ posts

Even or Odd number sorter

FatCatOn1Mat wrote:

deck26 wrote:

RholanT3344 wrote:

when green flag clicked
set [odd v] to [1]
set [even v] to [2]
repeat ([half v] of (...)::operators)
add (even) to [even2 v]
change [even v] by (2)
end
repeat (...)
add (odd) to [odd2 v]
change [odd v] by (1)
end
Doesn't match the question which was about 10 random numbers - otherwise a good method to create odd and even lists.
Please don't necropost - you've just quoted an old post for no reason.
grandforks
Scratcher
96 posts

Even or Odd number sorter

An even and odd number sorter can work like this(WARNING! THIS ONLY WORKS IN SCRATCH 3.0):
define Sort(numberList :: custom-arg)
set [counter v] to [0]
delete (all v) of [odd v] :: list
delete (all v) of [even v] :: list
repeat (length of (numberList :: custom-arg))
change [counter v] by (1)
if <((letter (counter) of (numberList :: custom-arg))/(2)) contains [.]? :: operators> then
add (letter (counter) of (numberList :: custom-arg)) to [odd v]
else
add (letter (counter) of (numberList :: custom-arg)) to [even v]
end
end
Because if you divide 3 by 2, you get a result of 1.5. 1.5 has a decimal point which this code is built to detect. This only works for strings though.

Last edited by grandforks (Aug. 21, 2020 20:03:43)

deck26
Scratcher
1000+ posts

Even or Odd number sorter

grandforks wrote:

An even and odd number sorter can work like this(WARNING! THIS ONLY WORKS IN SCRATCH 3.0):
define Sort(numberList :: custom-arg)
set [counter v] to [0]
delete (all v) of [odd v] :: list
delete (all v) of [even v] :: list
repeat (length of (numberList :: custom-arg))
change [counter v] by (1)
if <((letter (counter) of (numberList :: custom-arg))/(2)) contains [.]? :: operators> then
add (letter (counter) of (numberList :: custom-arg)) to [odd v]
else
add (letter (counter) of (numberList :: custom-arg)) to [even v]
end
end
Because if you divide 3 by 2, you get a result of 1.5. 1.5 has a decimal point which this code is built to detect. This only works for strings though.
Very clumsy when the mod function exists. Not worth necroposting for!
scratch___user12345
Scratcher
1000+ posts

Even or Odd number sorter

try this if you want a simple boolen to find if it is even of odd. true=even false=odd. replace 3 with the number you want to find if it is even or odd:
file//media/fuse/drivefs-9c06423a8b863c8997cd065d6da1fd89/root/Screenshot%202021-04-28%2010.20.53%20AM.png
scratch___user12345
Scratcher
1000+ posts

Even or Odd number sorter

oops the image did not work

Powered by DjangoBB