Task 13 - Weighted sum of digits

Back to Problem Solutions forum

christianlauw123     2015-02-17 09:02:17

Can i get some suggestions , how to times the digits of the number by its position , thanks

Rodion (admin)     2015-02-17 12:18:57
User avatar

I'm not sure I understood well what trouble you encountered. You should multiply first digit by 1, second by 2, third by 3 etc... Or are you asking about something different?

Technically you need to create variable sum and then in a loop add each digit multiplied by the counter of this loop or something like this...

Ashish Padalkar     2015-02-18 16:32:28
User avatar

what is the most important skill required for solving this problem is that you should know how to extract digit at one's place , ten's place and hundred's place and so on after this the question is very simple just multiiply one's digit by 1 , ten's digit by 2 ,hundred's digit by 3 and so on

Mandeep Bhutani     2015-02-19 03:25:22

I'm also having trouble with this problem. I can find the weighted number for each digit. I can also multiply each single digit by the weighted digit. The problem I'm having is that once I multiply each digit by its weighted number, I don't know how to combine the weighted sum of the digits into their original integer length. I've posted my code below in Python if it helps paint a better picture:

// part of the code erased to prevent spoiling the solution - admin
combined = [a * b for a,b in zip(weighted_digits, single_digits)]
Rodion (admin)     2015-02-19 05:48:35
User avatar

> I don't know how to combine the weighted sum of the digits

Strange question. If you are asked to get the sum - then just sum them :)

Mandeep Bhutani     2015-02-19 13:27:52

If it were that simple I wouldn't be posting! My outcome is a list where each member of the list is each digit in the input data multiplied by it's weighted sum. I can easily sum the digits to get the answer by writing something like: combined[0] + combined[1]

However, I'd like to believe that there is a more elegant solution. Imagine having to use indexes to find the sum for hundreds of numbers.

Quandray     2015-02-19 14:20:04
User avatar

Python isn't my first language, but to sum numbers in a list, try the using the sum function, e.g. sum(list)

Mandeep Bhutani     2015-02-19 17:36:24

Unfortunately, the problem isn't asking to return the sum of the entire list. So return sum(combined) is not an option.

Let's say we have a list:

something = [1, 2, 3, 4, 5, 6, 7]

What I'm trying to do is add 1 + 2, 3+4, 5+6+7. This is easy to do using an index slice. With an index slice I can do something[0] + something[1], something[2] + something[3], etc.

However, when the list contains a large amount of numbers, this is not an option.

Hopefully, someone can point me in the direction of a better method.

Rodion (admin)     2015-02-19 18:37:43
User avatar

> Unfortunately, the problem isn't asking to return the sum of the entire list

Perhaps I'm missing something, but the problem asks to return the sum of entire list!

e.g. for value 1776 you get two lists and multiply their member:

    [1, 7, 7, 6]
*
    [1, 2, 3, 4]
=
    [1, 14, 21, 24]

and now you need to sum them i.e.

sum([1, 14, 21, 24]) = 60

> What I'm trying to do is add 1 + 2, 3+4, 5+6+7.

To be honest I do not understand well what are you trying to calculate in this manner... :-o

Probably, problem statement is vague or misleading? please feel free to point at sentences which look queer to you - I'll try to improve them!

Mandeep Bhutani     2015-02-19 23:57:31

I think it would be easier to understand if my code hadn't been removed. Thanks for the help everyone, I'll continue searching for a way to solve this problem.

Mandeep Bhutani     2015-02-20 03:31:43

So I finally solved this problem. I was making it a lot harder than it should have been. For those who are using Python, the list() function goes a long way. Hope this helps.

Vadim Tukaev     2015-03-04 10:35:57

I looked at your code. You used the correct principle, but too congested. My code.

Please login and solve 5 problems to be able to post at forum