Before we leave CodeWars for a while here is another quick solution to one of the 7 Kyu questions. The question goes like this, given a list consists of numbers, find the total of all the factorials of those numbers. For example,
sum_factorial([6, 4, 2]) //will return 746
Because of the complexity of the question we will need to create two methods, the first one will accept the list input and then the second one will do the real factorial calculation, then we will sum the factorials up in the first method.
def sum_factorial(lst): total = 0 arr = list() for num in lst: total = factorial(num) arr.append(total) total = 0 for num in arr: total += num return total def factorial(num): sum = 0 if num > 1: sum = num * factorial(num-1) else: return 1 return sum
That will do the job! In the next coming article, I will start to write about my python journey because I believe you have seen enough coding and want some interesting python programming related story series. And next month we will start a new python project together. Follow me on Twitter or share this post if you want to. If you really like this post don’t forget to consider to donate 1 dollar to help this site out!
Follow @ChooWheiSee you in the next post!
from Planet Python
via read more
No comments:
Post a Comment