I came across a problem and I was unable to find a solution for it over the net. Maybe I wasn't entering in the right words for the accurate search. However, the problem goes like this:
Find how many of Nines, Threes and Ones it would take when added together to reach the given integer. When also multiplied by 1, 3 and 9.
In other words, say n = 25, we will then have 2 (Nines), 2 (Threes) and 1 (Ones). When we multiply them
2 * 9 = 18
2 * 3 = 6
1 * 1 = 1
total = 18 + 6 + 1 = 25 = n.
I was able to find a solution; however I was wondering if there's a smaller version of the same.
Here's my code.
def one_3_9(num):
temp = num
threes = []
ones = []
nines = []
val_ = 0
while temp != 0:
if 0 < num <=8:
if num >= 3:
val_ = num // 3
threes.append(val_)
temp = temp - (3 * val_)
ones.append(temp)
temp = temp - temp
elif num < 3:
ones.append(num)
temp = temp - temp
if 26 >= num >= 9:
val_ = num // 9
nines.append(val_)
temp = temp - (9 * val_)
ones.append(temp)
temp = temp - temp
if temp >= 3:
num = temp
val_ = num // 3
threes.append(val_)
temp = temp - (3 * val_)
ones.append(temp)
temp = temp - temp
else:
print("Number Should be more than 0 and less than 26.")
break
print("threes: ", threes)
print("Ones : ", ones)
print("nines: ", nines)
I completely AGREE the answer is long-winded and perhaps so is my explanation. However, I really hope I make sense with this example.
Thank you.
Aucun commentaire:
Enregistrer un commentaire