[SOLVED]Bits and trits (python)


#1
# Incoming Ogre Brawlers!
# Make use of a Robot Walker to dispatch these enemies.
# The Robot Walker requires commands as strings.
# The first part will the enemy's health in ternary.
# The second part will be the enemy's type as binary.

def toTernary(number):
    # Start with an empty string.
    string = ""
    # Then, while the number isn't zero:
    while number != 0:
        # We grab the remainder of our number.
        remainder = number % 3
        # This is our iterator method. 'number' decrements here.
        number = (number - remainder) / 3
        # Append the string to the remainder.
        string = remainder + string
    # Finally we want to return our constructed string.
    return string
    
def toBinary(number):
    string = ""
    # Go through the steps again:
        # Get remainder, decrement, and append string.
    # Remember that binary is another way of saying '2'!
    hero.say("1001")
    return string

while True:
    enemies = hero.findEnemies()
    dangerous = findMostDangerous(enemies)
    if dangerous:
        # The way the robot takes commands is in the form of:
        # ternary(enemyHealth) + " " + binary(enemyType)
        hero.say(toTernary(dangerous.health) + " " + toBinary(dangerous.type))

# In this level the Ogre Brawlers are more powerful if they have more health.
def findMostDangerous(enemies):
    mostDangerous = None
    mostHealth = 0
    for i in range(len(enemies)):
        enemy = enemies[i]
        if enemy.health > mostHealth:
            mostDangerous = enemy
            mostHealth = enemy.health
    return mostDangerous


#2

This is my code :DDDDDDDDDD


#3

rip no one is helping me XD


#4

Should I change my profile picture

  • yes
  • no

0 voters


#5

You haven’t followed the directions. You have an example from the previous function, right above this function, telling you what to do. This is one of the easiest levels in the Mountain campaign. Just do what it says.

Bits%26Trits


#6

Actually no, forget what I said.


#7

PS I use ^ to precise a power or exponent

using remainder you make an inversed string
11 in binary
11 % 2 = 1
5 % 2 = 1
2 % 2 = 0
1 % 2 = 1

1101 inversed = 1 0 1 1

1 0 1 1 = 11 (8+0+2+1)
or (2^3 + 0 + 2^1 + 2^0) where the exponent is the number of char after 1.

11 / 2 = 5
5 / 2 = 2
2 / 2 = 1

same thing in ternary

32 in ternary
32 % 3 = 2
10 % 3 = 1
3 % 3 = 0
1 % 3 = 1

2101 inversed = 1012
(3^3 + 0 + 3^1 + (3^0 * 2)
27 + 0 + 3 + 2 = 32

Difference in ternary is that the 2 mean the double of base 3.

let’s make 58 in ternary

58 % 3 = 1
19 % 3 = 1
6 % 3 = 0
2 % 3 = 2

1102 inversed is 2011
2011 = (3^3 *2) + 0 + 3^1 + 3^0

3^3 * 2 = 27*2 = 54
3^1 = 3
3^0 = 1
54+3+1 = 58.

Now quiz what is -22.55 in binary? :stuck_out_tongue: