Let me preface with my idea on how I planned to solve this before getting into my question.
-
split integer into list
-
create a for loop that removes index 0 from list, store it, reset, remove index 1, convert both to integers, compare the two, and save the lowest number
-
repeat step two through the list to find which number I must remove to make the smallest integer.
-
(haven't done it yet) create a while loop to iterate through the for look "K" number of times to hopefully get the right answer.
I I'm printing my list_num_2 and it's getting smaller as items are deleted so I'm assuming that is the issues I need to solve but can't figure it out. I tried list_num_2 = list_num.copy() however I get an attribute error. My code is as follows:
class Solution(object):
counter = 0
smallest_num = 0
def join_list(self, x):
return int(''.join(map(str, x)))
def removeKdigits(self, num, k):
k = 0
list_num = list(num)
list_num_2 = list_num[:]
i = 0
list_1 = []
list_2 = []
while i < len(list_num):
del list_num[i]
list_1 = list_num
list_num = list_num_2
i += 1
del list_num[i]
list_2 = list_num
list_num = list_num_2
i += 1
if list_1 > list_2:
self.smallest_num = self.join_list(list_2)
else:
self.smallest_num = self.join_list(list_1)
return self.smallest_num