Code:
n = int(input("Type n: "))
def some_function(n):
numbers, numbers_sum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], 45
while True:
digit = n % 10
n //= 10
if digit in numbers:
numbers.remove(digit)
numbers_sum -= digit
else:
break
return numbers_sum
print(some_function(n))
Program needs to print the sum of numbers that are not found in the input.
I covered the scenario when numbers are not repeating and for this scenario if the input is 234567890 output is 1 . But don't have an idea for scenario when numbers are repeating.Etc. if the input is 22334567890 or 223344556677889900 output still needs to be 1 but it's not.
P.S. I can't do it with strings. I know 0 doesn't make difference because we are doing with - and .
CodePudding user response:
the easier way to do it is to transform it to a string and do set operations
>>> n=22334567890
>>> numbers=set("123456789")
>>> numbers-set(str(n))
{'1'}
>>>
and then transform back to integers
>>> sum(map(int,{'1'}))
1
>>>
if you don't want the string conversion, just make a function that give you the digits of the numbers and do the same
>>> def digits(n): #this is a generator function
if n==0:
yield 0
while n:
n,d = divmod(n,10)
yield d
>>> n=22334567890
>>> numbers=set(range(10))
>>> numbers
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> list(digits(n))
[0, 9, 8, 7, 6, 5, 4, 3, 3, 2, 2]
>>>
>>> numbers - set(digits(n))
{1}
>>> sum({1})
1
>>>
CodePudding user response:
I solved it.Code:
n, finish = int(input("Type n: ")), 0
dig_list, num_list = [], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def sum(n):
while n > 0:
digit = n % 10
n //= 10
dig_list.append(digit)
for i in range (0, 11):
if i in dig_list:
num_list.remove(i)
return num_list
for e in (sum(n)):
finish = e
print(finish)
Purpose of code.
Explanation: First, we create 2 lists. The first one(dig_list) has no elements and there we are gonna put all digits of input(n). The second list is a list of numbers from 0 to 9(num_list). Then we put all digits of n in the first list.Then using for loop we go through the first list.If numbers from 0 to 9 are found in the first list we remove them from the second list. In the end, we save the sum( ) of the remaining elements of the second list to a variable sum.