-3

In Python, I want to convert all strings in the list to integers.

My task: I have to simply sum some integers. However, I cannot use in code the following: for,while,sum,map,reduce,filter,import,eval,exec,compile,single.

Sample Input 0:

1 2 3 4

Sample Output 0:

10

So if I have:

ls = ['1', '2', '3']

How do I make it:

ls = [1, 2, 3]

def listSum(ls):

    def recursion(index, result):
        if index == len(ls):
            return result
        return recursion(index + 1, result + ls[index])

    return recursion(0, 0)

ls = (input()).split()

ls = list(map(int, ls))
# print((ls))
print(listSum(ls))
pogiloyded
  • 37
  • 3
  • I cannot use in code the following: for,while,sum,map,reduce,filter,import,eval,exec,compile,single. – pogiloyded Nov 18 '19 at 21:23
  • 2
    How about modifying your `recursion` function's return statement to: `return recursion(index + 1, result + int(ls[index]))` – Paul M. Nov 18 '19 at 21:24
  • 1
    But your code already works without any of those things, as it is using recursion. Surely all you need to do is to add int to the sum in the return. – Daniel Roseman Nov 18 '19 at 21:24
  • When i run my code i type 1 2 3 4 with spaces then creating a list ['1','2','3','4'] I cant simply strings – pogiloyded Nov 18 '19 at 21:25
  • All answers in the suggested duplicate make use of functions and structures that the OP is not allowed to use. – Thierry Lathuille Nov 18 '19 at 21:54
  • @Prune I've reopened the question because it isn't really a duplicate to the linked question as there are self-imposed restrictions in this question that makes it fundamentally different from the linked question. – blhsing Nov 18 '19 at 21:55

4 Answers4

3

Just as you tried, you can do it recursively:

def recursive_sum(lst):
    if not lst:
        return 0
    else:
        return int(lst[0]) + recursive_sum(lst[1:])

print(recursive_sum([1, 2, 3]))
# 6

If you want to input the numbers, just do:

print(recursive_sum(input().split()))
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

To convert a list of number strings to a list of integers, you can use a recursive function like this:

def convert(ls):
    return ls and [int(ls[0]), *listSum(ls[1:])]
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You can use recursion to get the first and second part of your question:

b=['1', '2', '3', '4']
newarray=[] 
thesum=0
def recursion(hm): 
  global thesum, newarray
  if len(hm) == 0: 
     return thesum, newarray 
  thesum += int(hm[0])
  newarray.append(int(hm[0])) 
  return recursion(hm[1:]) 

recursion(b)  
# (10, [1, 2, 3, 4])
oppressionslayer
  • 6,942
  • 2
  • 7
  • 24
0

To convert a list of number strings to a list of integers without using any of the keywords/names blacklisted in your question, you can also create an iterator from the list, and use the iter function with a callback function that returns the next item from the iterator, converted to an integer, until it exhausts the iterator:

ls = ['1', '2', '3']
i = iter(ls)
print(list(iter(lambda: int(next(i)), None)))

This outputs:

[1, 2, 3]
blhsing
  • 91,368
  • 6
  • 71
  • 106