Please explain to me how to write an array and what an array does.
Thanks, Lydia
well, arrays define a “lot” of different “things” of the same species . Saying for example:
enemies = hero.findEnemies()
means that it finds all the enemies in your sight and puts them like this:
I am so sorry that this is so messy and that there is an error in the way. As you can see n the pic, you can also array items. To write an array in python (I have no idea in java, since I am learning it), you have to do this:
enemies = hero.findEnemies
if enemy:
hero.attack(enemy)
The “enemies” you can also replace with items.
Hope this helps,
Muumbo_6
Yes!
An array is a term for a list of properties in a programming language. There are a few types, but we’ll stick with lists. This is what you use in CodeCombat and what you probably think of when you think “array”, but remember they’re not identical.
# this is a basic array:
fruit = ["banana", "apple", "pear", "orange"]
# properties are separated by , always!
# properties can be strings: "banana",
# integers: 1 (whole numbers)
# floats: 1.45 (numbers with decimal places)
# and pretty much any other type of "thing".
# These can be mixed:
mixed = ["banana", 14, 1.55, "apple"]
# to access a specific place in the array use [] with a number:
hero.say(mixed[0]) # (= "banana")
# you can change arrays like this:
mixed[0] = "pear"
# Now mixed is:
mixed = ["pear", 14, 1.55, "apple"]
# To loop through an array:
# with a for loop:
# Easy:
for unit in mixed:
hero.say(unit)
# this will say all of the properties from "pear" to "apple".
# Hard:
for i in range(0, len(mixed)):
unit = mixed[i] # i will go from 0 (the start of the range() to the length of mixed 4 -1 (this is weird but you need to remember it).
# so that's 0-3, and because arrays start at 0 (they're "zero-indexed"), that makes 4 numbers, so it will loop through the whole array.
hero.say(unit)
# this will do the same thing.
# while:
count = 0
while count < 4:
hero.say(mixed[count])
count += 1
# again, this will do the same thing.
# in coco you often make arrays like this:
arr = hero.findEnemies()
# but remember they still have multiple properties and you need to treat them like an array. It's a common error to mistake an array for a single object.
I hope this isn’t too much… Just take a while to read it and try it out (for programming like this I would recommend getting python idle, because it’s good to familiarise yourself with the real thing) and if you have any specific questions, ask away!
Danny
Wow @Deadpool198 you are really good at writing descriptions…
I wish I could do that too.
Mumbo_6
Your description was good too . I just went a bit more in depth… There are many more things about arrays that I know if you want me to tell you. They’re very cool and you can use them in CoCo. Like array.index(“banana”) and it will return the index where “banana” is in the “array” array, so in the mixed array it would return 0.
I’ve been programming + explaining for quite a while, and I’ve used actual Python a lot, not just CoCo (which is obviously great), but doesn’t have all the features of Python taught inside it.
Danny
Thanks, ive been doing CoCo for about half a year now.
Mumbo_6
@Lydia_Song, an array is basically just a list of items. There is a lot of ways to use arrays, which become very helpful in later levels. If you want to find an item in an array, you just need to know the placement of it. Something important to remember is that when calling an area through an index, the first item is number 0.
#this is your array:
array = ["item0, item1, item2, item3"]
An array can be named anything, and the items inside the array can be named anything, you just need to follow this format. Now, if you want to call an item from the array, let’s say the 2nd item, you would do something like this:
hero.say(array[2])
Which pretty much does this:
hero.say("item1")
Arrays can be very helpful for dealing with multiple enemies. For example, let’s say there is 20 enemies you need to defeat. Instead of writing:
for i in range(20):
enemy = hero.findNearestEnemy()
hero.attack(enemy)
You can write:
enemies = hero.findEnemies()
for enemy in enemies:
hero.attack(enemy)
There doesn’t seem to be much difference, and you’re right, if there is 20 enemies they will both do the same thing. It is more helpful for if levels where you do not know the amount of enemies you need to defeat. It also comes in handy when having to defeat enemies and then do something afterwards, because once you iterate through the enemies array, it will move on if you have some other code beneath it and run that. There is many more examples of how arrays can be helpful, but I hope you get the idea now and you can go find some ways of your own to experiment with arrays. I hope I didn’t over complicate anything (even though it looks like a couple paragraphs), and I hope this helps. Have fun with arrays!
Grzmot
You’d probably use arrays for longer complex areas, such as matrices/2D arrays. Usually they’re used for more tricky machine learning and data science. Lists are much more simpler and used for storing fewer items, such as 3 ogre names.
corpses = hero.findCorpses()
for corpse in corpses:
hero.attack(corpse)
Would this be an example of an array?
Lydia
That would be an array. One cool tip to classify a list from an array is by trying hero.say(list)
. If it prints the inside element, then it’s a list.
Sorry,it is Danny,not Daniel.
That’s fine .
A array can store items like integers [2,3,7]
, strings ["hi"]
or Boolean [True]
You can put a tuple in a array [2, "Hi", (3, 3), False]
. If you put a array in a array its called a 2D array
Normal array: [7, "Hi", True, False]
2D arrays: [[1,0,1,0],[1,0]]
Hi Danny,
You write code in Python too?
Kevin
I mainly write in Python. The code in my post is python, if that’s what you meant.
I can also do javascript if you need help with that.
These are the basic methods for arrays.
- .extend() you input a array and it will take and put the value it the array before dot.
- .sort() it will sort the array in a special order
- .remove() removes the item in the bracket
- .append() appends the item in the bracket
- .reverse() reverses the array
names = []
names.extend(["daniel", "alex"]
# names are now ["daniel", "alex"], not [["daniel", "alex"]]
names.sort()
# names are now ["alex", "daniel"]
names.remove("alex")
# names are now ["daniel"]
names.append("richard")
# names are now ["daniel", "richard"]
names.reverse()
# names are now ["richard", "daniel"]
Remember that indexes start at 0, not 1.
Thank you,Danny,and i have a question on how to make the swing move about this CQ competiton?It is a little bit difficult for me,i think it is better to write in Python,and i also know the JS,but i prefer the python.
Hi @kevinChan The CodeQuest competition ended a few weeks ago.
Lydia
Sorry, I don’t really know what mean. Please could you explain a bit more.
Thanks
Danny