Question

Write a Python script named assignment2.py that implements the following steps given an integer value: 1.)...

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 = 18.

3. Calculate the remainder when the sum is divided by 10. For the previous example, the remainder is 8. If the result equals zero then return True otherwise return false.

You must define the following functions for this assignment:

toDigits takes an integer an returns a list of its digits

doubleEveryOther takes a list of digits and returns a new list of digits where every other number is doubled beginning from the right.

sumDigits takes a list of integers and sums up the digits of the numbers.

validate that indicates whether an integer is valid according to the algorithm described above. This function must use the functions defined previously in this list.

* Note it must be done using python 3 and not import any libraries, functions names must be in there so toDigits is one list doubleEveryOther another list

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#give it a thumbs up

Code

def toDigits(x):
   l = []             #empty list
   while x!=0:            #extract digits using while loop
       temp = x%10
       l.append(temp)
       x = int(x/10)
   i = len(l)-1
   rev = []
   while i>=0:           #reverse digits to get them back in correct order
       rev.append(l[i])
       i-=1
   return rev

def doubleEveryOther(x):
   alt = []      
   i = len(x) - 1
   j = 0
   while i>=0:       #from end reverse every second number on list and make new list
       if j%2!=0:
           alt.append(2*x[i])
       else:
           alt.append(x[i])
       i-=1
       j+=1
   i = len(alt)-1
   rev = []
   while i>=0:        #reverse digits to get them back in correct order
       rev.append(alt[i])
       i-=1
   return rev

def sumDigits(x):
   i = 0
   tot = 0
   while i<len(x):    #add digits
       if x[i]/10 == 0:   #if single digit number add directly
           tot += x[i]
       else:           #if 2 or more digit number, split digits and add
           temp = x[i]
           while temp != 0:
               tot += (temp%10)
               temp = int(temp/10)
       i += 1
   if tot%10 == 0:    #if number divisible by 10, return true otherwise false
       return True
   else:
       return False

inp = int(input("Enter number : "))    #input number

li = toDigits(inp)           #first function call

alt = doubleEveryOther(li)       #second function call

tot = sumDigits(alt)           #third function call

print(li)
print(alt)
print(tot)

Output 1

Output 2

Check indentation of the code here if it doesn't work

Add a comment
Know the answer?
Add Answer to:
Write a Python script named assignment2.py that implements the following steps given an integer value: 1.)...
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
  • In Python 3 (2.5 pts] Write the recursive function thirtyTwos(n) that takes an integer greater or...

    In Python 3 (2.5 pts] Write the recursive function thirtyTwos(n) that takes an integer greater or equal to 0 and returns an integer that represents the number of times that a 2 directly follows a 3 in the digits of n. Hint: The % and // operations from sumDigits could be helpful here >>> thirtyTwos (132432601)

  • 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...

  • IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns...

    IN PYTHON: Write a function that takes, as an argument, a positive integer n, and returns a LIST consisting of all of the digits of n (as integers) in the same order. Name this function intToList(n). For example, intToList(123) should return the list [1,2,3].

  • 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...

  • Python Script format please! 1. Write a script that takes in three integer numbers from the...

    Python Script format please! 1. Write a script that takes in three integer numbers from the user, calculates, and displays the sum, average, product, smallest, and largest of the numbers input. Important things to note: a. You cannot use the min() or max() functions, you must provide the logic yourself b. The calculated average must be displayed as an integer value (ex. If the sum of the three values is 7, the average displayed should be 2, not 2.3333). Example:...

  • Write a Python file containing the following functions. Also turn in the output from testing the...

    Write a Python file containing the following functions. Also turn in the output from testing the functions. All arguments to the functions may be hard-coded. Function 1 takes a list of strings as a parameter and returns a list of strings consisting of all the strings in the original list that have identical consecutive letters. For example: fun1 ([llama, good, cat, abba, abab, 112, dog]) returns [llama, good, abba, 112] Function 2 takes an integer (n), representing the number of...

  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • Please write code in Python! You are asked to implement the following checksum formula to validate...

    Please write code in Python! You are asked to implement the following checksum formula to validate an identifi- cation number given to you. The formula works as follows. Using the original number, double the value of every other digit. Then add the values of the individual digits together (if a doubled value now has two digits, add the digits individually). The identification number is valid if the resulting sum is divisible by 10. Write a function called validateID that takes...

  • Python code that: Accepts a list of integers and an integer, and returns the index of...

    Python code that: Accepts a list of integers and an integer, and returns the index of the SECOND  occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index([2,34,3,45,34,45,3,3], 3) returns 6 Example 2: second_index([2,34,3,45,34,45,3,3], 45) returns 5 Example 3: second_index([2,34,3,45,134,45,3,3], 134) returns None Example 4: second_index([2,34,3,45,134,45,3,3], 100) returns None

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

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