These are chat archives for andela/andela-21-lite

19th
Mar 2016
@andela-engmkwalusimbi
Mar 19 2016 07:55
Write a function sum_of_digits(A), that takes in a list of numbers A, and returns the sum of all the digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22.
"""
the solution isn't the best out there but it is good to guide beginners may be understand

Function sum_of_digits that returns sum of all digits in the list argument
@param list of numbers, the assumption is the list contains only numbers
@return the total of all numbers. e.g. sum_of_digits([10, 20, 3, 5, 6, 23])
should return 1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22
"""
def sum_of_digits(lst):
total = 0 # initialise total to 0
new_list = [] #create a new variable list
#iterate through the list
for a in lst:
#add the returned list to the new_list i.e. [] + ['1', '0'] for the 1st iteration
new_list += list(str(a))
#loop through the new_list
for number in new_list:
# add individual elements of new_list only after converting it to an integer int()
total += int(number)
#return the total

print(sum_of_digits([10, 20, 3, 5, 6, 23]))
janetwanguin
@janetwanguin
Mar 19 2016 08:01
Hello
@andela-engmkwalusimbi
Mar 19 2016 08:03
My intention is to hint about some of the concepts we introduced last week and introduce new methods like list("string") - which returns individual characters of a "string" i.e. ["s", "t", "r", "i", "n", "g"] and type-conversion methods like str(2.6) - returns "2.6" , int("2.6") - returns 2.
Nzisa
@fnzisa
Mar 19 2016 08:05
 
mylist = [10, 20, 3, 5, 6, 23]

def sum_digits(A):
total = 0
for a in A:
print a
b = str(a)
for c in b:
total += int(c)
print total

sum_digits(mylist)
A simple way of doing the sum_of_digits(A)
Tonida
@Tonida
Mar 19 2016 08:08
This message was deleted
def sum_of_digits(A):
"""Write a function sum_of_digits(A), that takes in a list of numbers A,
and returns the sum of all the digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return
1 + 0 + 2 + 0 + 3 + 5 + 6 + 2 + 3 = 22
"""

b = list("".join(str(e) for e in A))
return sum([int(i) for i in b])

print sum_of_digits([20, 50, 60])
print sum_of_digits([100, 400, 323])
another way of doing it
@andela-engmkwalusimbi
Mar 19 2016 08:10
@fnzisa one other thing please comment code. It's best practice to comment code.
Nzisa
@fnzisa
Mar 19 2016 08:32
@andela-engmkwalusimbi Thank you.
@andela-engmkwalusimbi
Mar 19 2016 08:40
Write a function sum_of_unique_digits(A), that takes in a list of numbers A, and returns the sum of all the unique digits in the numbers.

e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
Has anyone worked on the factorial problem? There are a lot of implementations to it. I would prefer you work on without going on google, this will help broaden your way of thinking about problems. That said, I'm glad you all made it today.
Tonida
@Tonida
Mar 19 2016 08:55
def sum_of_unique_digits(A):
"""Write a function sum_of_unique_digits(A), that takes in a list of numbers A,
and returns the sum of all the unique digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
"""

b = list("".join(str(e) for e in A))
for i in b:
if b.count(i) > 1:
b.remove(i)
return sum([int(i) for i in b])

print sum_of_unique_digits([1, 20, 1, 30, 2])
print sum_of_unique_digits([0, 20, 1, 30, 2, 30])
print sum_of_unique_digits([100, 50, 20])
Cmutisya
@Cmutisya
Mar 19 2016 08:58
hi
Eric G
@egichuri
Mar 19 2016 09:08
x = {"a": "apple", "b": "Boy"}
for key, value in x.iteritems():
print key, value
Tonida
@Tonida
Mar 19 2016 09:30

def sum_of_unique_digits(A):
"""Write a function sum_of_unique_digits(A), that takes in a list of numbers A,
and returns the sum of all the unique digits in the numbers.
e.g. [10, 20, 3, 5, 6, 23] should return 1 + 0 + 2 + 3 + 5 + 6 = 17.
"""

""" convert A into a string using .join() function  then use the list()
function to convert back to a list and store it in b
"""
b = list("".join(str(e) for e in A))
for i in b: # iterate through the elements in b
if b.count(i) > 1: # if an element appears more than once
b.remove(i)  # use remove function to delete from the list
return sum([int(i) for i in b]) # compute the sum of elements in b converted to integer type
the commented code on sum of unique digits
@andela-engmkwalusimbi
Mar 19 2016 09:33
@Tonida what does this line do?
b = list("".join(str(e) for e in A))
Tonida
@Tonida
Mar 19 2016 09:36
@andela-engmkwalusimbi I have written in the comment above that line
Nzisa
@fnzisa
Mar 19 2016 09:39
mylist = [10, 20, 3, 5, 6, 23]

def sum_of_digits(A):
#initialise total to 0
total = 0
#iterate through through the list
for a in A:

#Converts each element of A to a string at a time and assigns it to b. Example: 10 will be '10'
b = str(a)

#Loops through every element of b
for c in b:
#adds every element of b to the total after converting it to integer
total += int(c)
#print the total
print total

sum_digits(mylist)
@andela-engmkwalusimbi
Mar 19 2016 09:39
@Tonida seen. I'm a beginner. Would you please use a scenario to explain to me. Thank you :smile:. I mean give me an example for just that line :+1: .
@fnzisa, @Tonida appreciate the effort and hard work.... please explain to your colleagues. I don't mean it's just @fnzisa and @Tonida that are nailing it, everyone is. You all will progress when you discuss.
COLLABORATE
Tonida
@Tonida
Mar 19 2016 09:47
This message was deleted
clarch
@clarch
Mar 19 2016 11:12
@fnzisa @Tonida @MsCarol get the examples here https://repl.it/Bxlp/16
MsCarol
@MsCarol
Mar 19 2016 11:15
:thumbsup:
clarch
@clarch
Mar 19 2016 11:32