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)))
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)))
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...
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...
Write a Python script named assignment2.py that implements the following steps given an integer value: 1.) Double the value of every second digit beginning from the right. For example, the number 1386 has digits [1, 3, 8, 6] which become [2, 3, 16, 6]. 2.) Add the digits of the doubled values and the digits that were not doubled from the original number. For example, [2, 3, 16, 6] becomes 2 + 3 + 1 + 6 + 6 =...
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 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 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 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)??
Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...
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 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. '''...