Answer:
The solution in Python is as follows:
class myClass:
def doubleUp(self,myList):
myList[:0] = myList[::2] = myList[1::2] = myList[:]
mylist = [1, 3, 2, 7]
list = myClass()
list.doubleUp(mylist)
print(mylist)
Explanation:
To create a method in Python, the first step is to create a Class.
This is done in the following line:
class myClass:
Then this line defines the method
def doubleUp(self,myList):
This line duplicates the elements of the list
myList[:0] = myList[::2] = myList[1::2] = myList[:]
This defines the list
mylist = [1, 3, 2, 7]
This creates an instance of the class
list = myClass()
This passes the list to the doubleUp method
list.doubleUp(mylist)
This prints the duplicated list
print(mylist)
Answer:
The function in Python is as follows:
def digitSum( n ):
if n == 0:
return 0
if n>0:
return (n % 10 + digitSum(int(n / 10)))
else:
return -1 * (abs(n) % 10 + digitSum(int(abs(n) / 10)))
Explanation:
This defines the method
def digitSum( n ):
This returns 0 if the number is 0
<em> if n == 0:
</em>
<em> return 0
</em>
If the number is greater than 0, this recursively sum up the digits
<em> if n>0:
</em>
<em> return (n % 10 + digitSum(int(n / 10)))
</em>
If the number is lesser than 0, this recursively sum up the absolute value of the digits (i.e. the positive equivalent). The result is then negated
<em> else:
</em>
<em> return -1 * (abs(n) % 10 + digitSum(int(abs(n) / 10)))</em>