6 kyu
Expressing Integers as Sum of Powers of Three
125 of 161CheeSenTan
Description:
Problem
All integers can be uniquely expressed as a sum of powers of 3 using each power of 3 at most once.
For example:
17 = (-1) + 0 + (-9) + 27 = (-1 * 3^0) + ( 0 * 3^1) + (-1 * 3^2) + (1 * 3^3)
-8 = 1 + 0 + (-9) = ( 1 * 3^0) + ( 0 * 3^1) + (-1 * 3^2)
25 = 1 + (-3) + 0 + 27 = ( 1 * 3^0) + (-1 * 3^1) + ( 0 * 3^2) + (1 * 3^3)
We can use the string +-0+
to represent 25 as the sum of powers of 3:
Symbols : "+" "-" "0" "+"
Powers of 3 : 1 3 9 27
Values : 1 -3 0 27
Given an integer n
(not necessarily strictly positive), we want to write a function that expresses n
as a sum of powers of 3 using the symbols -0+
:
n = 17 -> "-0-+"
n = -8 -> "+0-"
Note: The last symbol in the solution string represents the largest power of 3 used (added +
or subtracted -
) and will never be 0
, except if the integer is 0
itself.
Fundamentals
Similar Kata:
Stats:
Created | Jan 28, 2021 |
Published | Jan 28, 2021 |
Warriors Trained | 1062 |
Total Skips | 35 |
Total Code Submissions | 973 |
Total Times Completed | 161 |
Python Completions | 125 |
Haskell Completions | 10 |
JavaScript Completions | 35 |
Lua Completions | 7 |
Total Stars | 28 |
% of votes with a positive feedback rating | 88% of 51 |
Total "Very Satisfied" Votes | 42 |
Total "Somewhat Satisfied" Votes | 6 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 8 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 8 kyu |