Weighted sum of digits

Back to Problem Solutions forum

sleepijs     2016-01-16 09:44:43
User avatar

What Am I missing here? I thought it would work..

include <stdio.h>

include <math.h>

int main(){

int value, i, nTestC, digit; int wsd = 0; int n = 1;

freopen("wSoD.txt","r",stdin);
scanf("%d", &nTestC);

for( i = 0; i < nTestC; i++){
scanf(" %d", &value);
    for (; value != 0; n++) {

        digit = value % 10;
        wsd += (digit * n);
        value /= 10;

}

printf("%d ", wsd); wsd = 0; n = 1;

}

return 0; }

Quandray     2016-01-16 10:58:57
User avatar

Welcome back,

For the input 1776, you know that the answer is the sum of

1*1
7*2
7*3
6*4

Try you code, with the addition of this line after you've calculated digit

printf("%d*%d\n",digit,n);

and with this input file

1
1776

and you'll, see that you are summing

6*1
7*2
7*3
1*4
sleepijs     2016-01-16 13:12:58
User avatar

I see. Is there a way to turn 6771 back into 1776?

sleepijs     2016-01-16 18:24:48
User avatar

I may have found a way to do this. :D What if I count the number of digits in a integer. And then exract the digits:

1776 / 1000 = 1 776 / 100 = 7 76 / 10 = 7 all that is left is 6..

I already have the counter part of the program ready, now all I need is the second part. Not sure how to do it yet though. :D

DaveRoox     2016-01-16 19:17:13
User avatar

what if you build your result integer iteration by iteration multiplying it by 10 and adding the result modulo 10 of the other number? (6771) and before your next iteration you update your 6771 with the integer part of 6771 / 10 repeat until your "6771" is greater than zero.

sleepijs     2016-01-18 20:57:56
User avatar

With a bit more help I got it to work.. Thanks for help!

Freddie_Lavoie     2020-07-16 05:24:51

for j in range(len(num)): new = index * int(num[j])

need some help with this.. When i do this code in Jupyter notebook i dont get any error but when i try to solve it here, it gives me this error

Traceback (most recent call last): File "solution.py", line 7, in <module> new = index * int(num[j])ValueError: invalid literal for int() with base 10: ' '

Can someone please help as to what am i doing wrong ?

Rodion (admin)     2020-07-16 14:11:06
User avatar

It's hard to tell without knowing what you loaded into your num[j] because it complains you have empty string at some point.

Freddie_Lavoie     2020-07-18 05:08:03

I am not sure if i should be posting my code here or not. but here is the code.. when i run this in jupyter notebook , i get the desired result but it doesn't work on code abbey. please if you can help

n = int(input())

for i in range(n):
    num = input().strip()
    index = 1
    Sum = 0
    for item in num:
        new = index * int(item)    
        Sum += new
        index += 1
    print(Sum,'',end = '')

*** this is the error i am getting

Traceback (most recent call last): File "solution.py", line 8, in <module> new = index * int(item)
ValueError: invalid literal for int() with base 10: ' '

Rodion (admin)     2020-07-18 07:43:26
User avatar

I'm not sure why your jupyther passes this, but your code has obvious error. Let me show how you should debug this.

You see the message that error is in line 8. It says item is something inconvertible to number (really, space).

Let's comment or remove the offending code temporarily and add print out of the num from which item is taken:

n = int(input())

for i in range(n):
    num = input().strip()
    index = 1
    Sum = 0
    print('***' + num + '***')

We still get error saying that end of file is reached while trying to read input. That's strange. That means we read too much. Let's change for i in range(1) so only one number is read.

Now the output will show

***9 15 1776***

So the whole string instead of single number is read!

That won't do! You should split the input into separate numbers!

Not sure why you claim this works in jupyther, this may depend on how you test it, perhaps. If you have troubles matching jupyther with normal python environment, it would be beneficial for you to work with normal python instead!

E.g. in console, with console editor probably etc. Though perhaps you may prefer some IDE with built-in python :)

Freddie_Lavoie     2020-07-18 21:00:05

Awesome thank you so much. I got it .. Basically the way i was doing it was taking each number at a time and thats how i was testing it.. But seems like the question was one big string of numbers. I got it figured.. That was helpful thank you so much

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