Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hacking Spot Vol.1 Issue 3: Weights and Balance #1

Open
airekans opened this issue Oct 9, 2012 · 5 comments
Open

Hacking Spot Vol.1 Issue 3: Weights and Balance #1

airekans opened this issue Oct 9, 2012 · 5 comments

Comments

@airekans
Copy link
Owner

airekans commented Oct 9, 2012

You have a balance and a number of weights, and each is a certain power of three grams, e.g. 1g, 3g, 9g, 27g, etc. Note that you have only one for each of them. Given a thing with x grams, how can you place it and make the balance balanced. For example, given a book with 55 grams, how can you achieve it?

Sample:

Input: 55
Output: 55 + 3^3 = 3^4 + 3^0
@airekans
Copy link
Owner Author

Given a thing weighting x gram, we first convert it to 3-based representation, e.g. 55 in decimal to 2001 in 3. Check each digit of the representation, if the digit is 1, it means that the weight corresponding to this digit is put on the other side of the balance. If the digit is 2, we add 1 to this digit, and it means the weight corresponding to this digit it put on the same side of the thing. Continue till the last digit, and we get the answer.

The following Python program is the example code:

#! /usr/bin/env python

# num is a integer >= 0
# return a list containing the digits in base
# the digits are ordered from lower bits to higher bits
def convert_to_based_num(num, base):
    digits = []
    while num > 0:
        quotient, remainder = num / base, num % base
        digits.append(remainder)
        num = quotient

    return digits

def add_3_based_digits(digits, index, num):
    while digits[index] + num >= 3:
        digits[index] = (digits[index] + num) % 3
        num = 1
        index += 1
        if index >= len(digits):
            digits.append(0)

    digits[index] += num

def balance(digits):
    left = []
    for i, d in enumerate(digits):
        if d == 2:
            add_3_based_digits(digits, i, 1)
            left.append(1)
        else:
            left.append(0)

    return left, digits

def print_balance(num, left, right):
    print "Output:", num,
    for i, d in enumerate(left):
        if d != 0:
            print "+ 3^%d" % i,
    print "= 3^" + " + 3^".join([str(i) for i, d in enumerate(right) if d != 0])

if __name__ == '__main__':
    w = int(raw_input("Input: "))
    left, right = balance(convert_to_based_num(w, 3))
    print_balance(w, left, right)

@kmalloc
Copy link

kmalloc commented Oct 17, 2012

55 = 2_(3^3)+3^0
-->
55+3^3 = 3_(3^3)+3^0
-->
55+3^3 = 3^4+3^0

@airekans
Copy link
Owner Author

@kmalloc What do you mean?

@kmalloc
Copy link

kmalloc commented Oct 17, 2012

solution to the problem.

@airekans
Copy link
Owner Author

@kmalloc Yeah, the point is to transform a given number to 3 based representation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants