Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Problem 1288. Balanced Ternary Numbers: Easy as |, |-, |o

Created by Ned Gulley

This problem concerns the so-called balanced ternary system for representing numbers. It is a Base 3 system in which the digits can be 1, 0, or -1.

In balanced ternary, the number 8 would be represented as 9 (or 3^2) minus 1 (or 3^0). Typographically we will use "|" for one, "o" for zero (that's a lower-case O), and "-" for negative one. So if the decimal input d is the number 8, the balanced ternary output is the string "|o-". Thus

 dec 8  => bt "|o-"

Here are some more examples.

 dec 3  => bt "|o"
 dec 15 => bt "|--o"
 dec 52 => bt "|-o-|"

Given an integer d, return the string bt. Leading zeros should always be suppressed.

Problem Group

Solution Statistics

8 correct solutions 6 incorrect solutions
Last solution submitted on Nov 14, 2014