-
-
Save Victoria-Hodder/5bdb1b2bde1218295dcd1ae9120ca028 to your computer and use it in GitHub Desktop.
""" | |
Write a function solution that, given integer N, returns the smallest non-negative | |
integer whose individual digits sum to N. | |
Examples: | |
1. Given N = 16, the function should return 79. There are many numbers whose | |
digits sum to 16 (for example: 79, 97, 808, 5551, 22822, etc.). | |
The smallest such number is 79. | |
2. Given N = 19, the function should return 199 (the sum of digits is 1 + 9 + 9 = 19). | |
3. Given N = 7, the function should return 7. | |
Assume that: N is an integer within the range [0..50]. | |
""" | |
def smallest_int(n): | |
list_to_string = list(str(n)) #converts n into a string, then splits the string into a list of strings | |
mapping = map(int,lst_converstion) # converts the list of strings back into integers | |
summing = sum(mapping) # adds the integers together | |
return summing | |
""" | |
This function is printing the sum of n by splitting the digits, then converting them to a list of ints, then summing them | |
I need to approach this the other way to find the minimum digit which, summed, would result in n | |
""" |
Hi @kpanic, thank you for the tips.
What I really need help with is the code I have written does not actually address the task. I did it the wrong way round.
The task requires that:
"Write a function solution that, given integer N, returns the smallest non-negative integer whose individual digits sum to N."
And I have written a function which sums the individual digits of N. Oops!
Could you please help me? I really have no idea where to start. Except that I know I must use min()
at some point.
sorry! I misread the exercise. Pretty busy.
You could do:
min((number for number in mapping if number >= 0))
This, as far as I remember, should generate a lazy iterable (with the ()
) that should be lazyly evaluated.
and then we pass to the min
function the iterable which will be evaluated only one time
mmm, I am thinking that the code is close enough. but not covering the requirement of the exercise.
Please have a thought about it given the snippet that I provided.
I might have some time to think about it this evening, pardon Victoria
Thank you, I'll have a run with it now :)
I will meet with Meili tomorrow for German-Python exchange, perhaps we will get a chance to talk it through.
Looks good
Please correct the typo
lst_converstion
tolist_of_strings
orlist_conversion
The rest is cool. I would add some unit tests and also check the boundaries (0 and 50)
Sorry for the late reply