Prime pathing, help!


#1

Hey everyone,

I am having troubles with my code on prime pathing :

traps = self.findHazards()
trapsIndex = 0
while trapsIndex < len(traps):
    trap = traps[trapIndex]
    for i in range (2,Math.ceil(Math.sqrt(trap.value))):
        if (trap.value % i) == 0:
            self.say("it's not a prime number")
            trapsIndex += 1
            break
        else:
            self.move(trap.pos)
            trapsIndex += 1

I am erroring on line 6, the message is “put each command on a separate line”, and I can’t find why. Any idea ?
Is that because of the 2 loops ? I have no idea how to code this without it, if you have hints, they are welcome.


#2

Just tried the level the first time and I found your problem. It is with the compiler.

if (trap.value % i) == 0:

will work if written as:

if trap.value % i == 0:

#3

I finally managed to beat the level with the bonus! Hardest part was getting the line to corner…


#4

what world and level is this


#5

All level urls follow a pattern. If you want to see a level just type in the name. It will allow you to skip ahead (but not access blocked levels). This level is called prime pathing

This is not always a good idea though, as the later levels will probably require you to use code you may not have learned yet.


#6

Thanks for your help Hinkle, I can now determine if a number is prime or not.
I am still stuck though, I don’t know how to narrow the prime number search down on the closest fire-traps, so my hero move toward a prime number which is far away and explode on the closest.
Is it possible to narrow the search ?
Or should I try to build a new array with all the prime number and when I am done move toward the path ?


#7

Google primality test or prime number generator.
You only need to check if a given number (the hazard.value number) is prime or not.

You do not need to store the prime numbers. Even for the bear traps, you will have at least 15% primes numbers in the search interval so even if you use every number instead of only prime numbers you’ll take maximum 6 times longer. As the help on the level said, this downside is simply fixed by performing the primality test once and storing the result.

Then every troop and the hero will read your list of points telling them where they need to go.

Ask for help again if you are still stuck, but take at least a day to search for the solution


#8

I used a filtering algorithm to find all the find and save all the traps with a trap.vaule that was prime (using the provided test) to an array and then used a best choice algorithm using that array to find the next closest excluding the one I was standing on and only looking for ones in the proper direction.

There other ways to solve the level though…

(p.s. If you can’t remember what a flitering or best choice algorithm look like, check out my posters!)


#9

@AdrianCgw thanks, I always try before asking for help (several hours) but as I am reaching the end it gets more difficult, and I need you guys’s help.

@Hinkle, your hints and posters are really helpful, thanks a lot !!!

I am having some trouble with my prime algorithm. It stores some numbers that aren’t primes, as you can see in the screen print below :

here is the code :

traps = self.findByType("fire-trap")
trapsIndex = 0
primesIndex = 0
primes = []
for trap in traps:
    for i in range (2,Math.ceil(Math.sqrt(trap.value))):
        if trap.value % i == 0:
            self.say("not a prime")
            break
        else:
            primes.append(trap)
            self.say(trap.value)
            break

I can’t find what’s wrong, I tried to edit the result of 625 % 5 and it sorted =0 as it should. But it seems like the algorithm doesn’t see it that way. Any Idea ?


#10

A number is prime if is not divisible by ANY of the numbers between 2 and Math.floor(Math.sqrt(trap.value))
Your algorithm just test for the division with 2

if divisible with 2
     not a prime
else
     prime number

#11

By the way, this is how you implement a logical AND:

target_dead=false
for i in execution_squad:
    if good_shot(i):
        target_dead=true
        break
if not target_dead:
   self.say("one lucky guy")

#12

As AdrianCgw said you are only checking once.

I used a function to check, as when you return from a function, the function ends.
Here is an example of how you would use a function to check there is at least 1 apple in a basket

def areThereApples(basket):
    for fruit in basket:
          if fruit is apple:
             return True
    return False
    # no else is needed because the function will have ended if apple
    # this will only work with functions

You can then use a filtering algorithm to check the function:

basketsWithApples = []
for basket in store:
    if areThereApples(basket):
        basketsWithApples.append(basket)

#13

Thanks for both your help, I rewrited my code and the primes are know stored in a specific array. I thought it would be easy from here, but it’s not, the comments in the feedback are here to prove it. :frowning:

I am having troubles with my best choice algortithm. It doesn’t get the nearest dud. It chooses the last one and return a weird distance. I have tried a lot of different things to move through the path but as none was working I came back to the simplest code I could do, to understand where I was erroring.

Here is my code:

def areTherePrimes(trap):
    for div in range (2,Math.ceil(Math.sqrt(trap.value))):
        if trap.value % div == 0:
            return False
    self.say(trap.value)
    return True
   
traps = self.findByType("fire-trap")
duds = []
for trap in traps:
    if areTherePrimes(trap):
        duds.append(trap)

nearestDistance = 30
nearestDud = None
for dud in duds:
    if self.distanceTo(dud) < nearestDistance:
        nearestDistance = self.distanceTo(dud)
        nearestDud = dud
self.say(nearestDistance)
self.say(dud.value)

Anyone knows what’s happening ?


#14

Have you tried just a simple findNearest(duds) to see if that works?
if not try a self.say(duds)

I had a problem earlier today with .append method not functioning properly and problems in the level with hazards forgetting they were objects.


#15

Can’t beat it. Did something change?


#16

What are trying to do?

Think about how you would beat the level, and if you can’t figure out the code for part of it, ask for help.


#17

Like, the code and error fixing doesn’t work anymore.

traps = self.findHazards()
trapsIndex = 0
while trapsIndex < len(traps):
    trap = traps[trapsIndex]
    for i in range (2,Math.ceil(Math.sqrt(trap.value))):
        if trap.value % i == 0:
            self.say("message")
            trapsIndex += 1
            break
        else:
            self.move(trap.pos)
            trapsIndex += 1
            break

Shoule I use floor?


#18

Right… That code never worked. If you read the rest of the forum you can see the problems with it. The winning code for this level is not posted on this forum.

If it bringing up an error message, let me know the message. If it is causing you to blow up and die… read the rest of the posts where croks was working what was wrong with it. The information on this set of posts should get you safely to the first mine.


#19

What’s wrong w/ this then? I mean, can you help me fix the “back and forth” problem?

def areTherePrimes(trap):
    for div in range (2 , Math.ceil(Math.sqrt(trap.value))):
        if trap.value % div == 0:
            return False
    return True
traps = self.findByType("fire-trap")
duds = []
loop:
    for trap in traps:
        if areTherePrimes(trap):
            duds.append(trap)
    nearestDud = None
    leastDistance = 8
    greatestDistance = 29
    for dud in duds:
        if self.distanceTo(dud) > leastDistance and self.distanceTo(dud) < greatestDistance:
            nearestDud = dud
            self.moveXY(self.pos.x, nearestDud.pos.y)
            self.moveXY(nearestDud.pos.x, nearestDud.pos.y)

#20

You not only have to pass your hero across the traps but your troops also… Group the traps by their x or y positions. Keep a counter for what group of traps you are passing.

You might what to: move in front of a dud. Move past the dud. Move in front of the next group dud.
Like this you can avoid cutting corners and exploding because you clip a live mine.