Question

how to add the digits of the doubled values and the digits that were not doubled...

how to add the digits of the doubled values and the digits that were not doubled from the original number in python?

The first part prints it in a list:

def toDigits(g):
return [int (y) for y in str(g)]
print (str(toDigits(759283)))

The second part: prints every number and multiplies all by 2

def doubleEveryOther(g):
return [int(y) * 2 if i % 2 == 0 else int(y) for i, y in enumerate(toDigits(g))]
print(str(doubleEveryOther(759283)))

The third part: I'm trying to add the digits of the doubled values and the digits that were not doubled from the original number. Example: [14,5,18,2,16,3] becomes 14+5+18+2+16+3 = 58

def sumDigits(g):
return [int (y) sum(doubleEveryOther)]
print(str(sumDigits(759283)))

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def toDigits(g):
    return [int(y) for y in str(g)]


print(str(toDigits(759283)))


def doubleEveryOther(g):
    return [int(y) * 2 if i % 2 == 0 else int(y) for i, y in enumerate(toDigits(g))]


print(str(doubleEveryOther(759283)))


def sumDigits(g):
    return sum(doubleEveryOther(g))


print(str(sumDigits(759283)))
Add a comment
Know the answer?
Add Answer to:
how to add the digits of the doubled values and the digits that were not doubled...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • I will post what I currently have and also the tests for it. Unfortunately it randomly...

    I will post what I currently have and also the tests for it. Unfortunately it randomly fails some tests so I was wondering if anyone could help. def sumDigits(s): '''Input s is a string. The returned value should be the sum of all numerical digits in s. In addition, if the first non-empty char of s is a minus sign, then the returned value should add a minus sign to the sum. E.g., sumDigits('A33$%4C*+D') should return 10, sumDigits('-+z33$%4-D') and sumDigits('...

  • It's known that if you add the digits of any number, and that it eventually equals...

    It's known that if you add the digits of any number, and that it eventually equals nine, that the original number is divisible by nine. Write a program that determines if a number is by divisible by nine by using this method. 1 def div_9(n): 2 #TO DO: IMPLEMENT THIS FUNCTION 4 x = [99,0,18273645,22] 6 7 for i in x: print(div_9(i)) Output function.py True True True False Deliverables Problem 4

  • Create a python add the following functions below to the module. Each section below is a...

    Create a python add the following functions below to the module. Each section below is a function that you must implement, make sure the function's names and parameters match the documentation (Copy-Paste). DO NOT put the functions in an if-name-main block. 1. def productSum(x: int, y: int, z: int) -> int This function should return: The product of x and y, if the product of x and y is less than z. Else it should return the sum of x...

  • Inspect the code below. The function subtract_five should take one integer as input and return that...

    Inspect the code below. The function subtract_five should take one integer as input and return that integer minus 5. Running the function will cause Python to throw an error. Change the function so the program correctly prints the value in y. This is the answer. but, see #. def subtract_five(inp):     print(int(inp-5))     return(int(inp-5)) y = subtract_five(18) - 5 print("This is the value in y = ", y)          # I don't understand this part? Why can I just use, print(y)??

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • IN PYTHON ''' Helper func 3: checksum_ISBN Input: a list with 9 single digit number in...

    IN PYTHON ''' Helper func 3: checksum_ISBN Input: a list with 9 single digit number in it (first 9 digit in ISBN) Output: print out: "The correct checksum digit is:__. Now we have a legit ISBN: _____" Hint: just loop through 0-9, test every one with helper func1 to find out the one checksum that forms a legit ISBN with the correct ISBN in lis (10 numbers), call helper func2 to format it correctly. Then print the final result. '''...

  • I am writing a class that can multiply and add very large values with hundreds and...

    I am writing a class that can multiply and add very large values with hundreds and thousands of digits in C++. Cannot add any more #includes Please implement with array of size 1000 2 3 4 // do not add more #includes. #include <string> #include <Cassert > #include <algorithm» 6 #include <vector» 7 #include <iostream> 8 9 using namespace std; 18 IIThe class LargeNumber is used to add and multiply large values up to hundreds of digits and/or more. 11...

  • Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MA...

    Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...

  • Look at the following function definition: def my_function(x, y): return x[y] a. Write a statement that...

    Look at the following function definition: def my_function(x, y): return x[y] a. Write a statement that calls this function and uses keyword arguments to pass ‘testing’ into x and 2 into y. b. What will be printed when the function call executes? 6. Write a statement that generates a random number in the range I'm using python to solve this this is what i did def main():      result= my_function(x='testing', y=2)      print(result) def my_function(x,y):      return x[y] main()

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT