I have the following reduce function in python which is supposed to return the sum of the ASCII values of all the characters in the string. For example for the string “BOY” the reduce function should return 234.
reduce(lambda x,y:ord(x)+ord(y),list("BOY"))
But im getting the following error:
TypeError: ord() expected string of length 1, but int found
What is the problem with my code?
Think about how this reduces:
What is
ord(145) + ord('Y')? Its an error.Others have suggested:
Which reduces like:
I’d recommend