Tips for cleaning up a challenges answer? Weighted Sum of Digits [closed]

2024/10/5 15:00:58

I've been going through some code challenges. And one struck me in particular. CodeAbbey has a Weighted Sum of Digits challenge. And this was my answer.

# open and read a file, hardcoded numbers, etc. Here is a sample
raw_data = (6, 19, 64, 6527226, 12345146)  for number in raw_data:wsd = 0  # Stores sumnumber_list = list(str(number))  # Converts the number into a list.for i, k in enumerate(number_list):  # Enumerates each numberwsd += (int(i+1) * int(k))  # Multiplies and adds product to wsdprint(wsd)output >>> 6, 19, 14, 114, 137

Anyone with more experience able to see a better way of getting the sum?

Answer

If using map:

>>> map(lambda d: sum([ (i+1) * int(v) for i,v in enumerate(list(str(d)))]), raw_data)
[6, 19, 14, 114, 137]
https://en.xdnf.cn/q/120268.html

Related Q&A

Get a variable as filename from python script and use it in a batch script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 3.x AttributeError: NoneType object has no attribute groupdict

Being a beginner in python I might be missing out on some kind of basics. But I was going through one of the codes from a project and happened to face this :AttributeError: NoneType object has no attri…

importing images from local folder instead of using Keras/Tensorflow dataset

Hi Can someone please help me to change this code so that it wont get data from keras mnist. instead it will be getting data from local folder. where do i need to make changes in it. and where in this …

why I have negative date by subtraction of two column?

Im trying to create a column his values is the subtraction of two column but I found strange values:Patient["Waiting"] = Patient["Appointment"] - Patient["Scheduled"]Sched…

Python 3: AttributeError: int object has no attribute choice [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Scraping web pages with Python vs PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

error switching to iframe selenium python

Currently Im attempting to switch to iframe/fancybox, but im getting the following error:line 237, in check_response raise exception_class (message, screen, stacktrace) selenium.common.exceptions.WebDr…

SQL Date Variables in Python

I am writing a query inside a Python script.The query is as follows:cur = conn.cursor() query1 = """select max(date_time) from tablename""" cur.execute(queryy1) conn.commi…

Truble to create a matrix for sudoku in python

I have to make a sudoku template, so I need to get random numbers into the matrix, but they can not be repeated in the rows or columns, but I can not do it. For this I can not use numpy. In this case I…