# LeetCode: Arrays 101: Introduction – Solutions

Hello,

LeetCode is a good place to practice programming by solving problems of all levels!

After you solve a problem, you’ll get access to all submitted solutions, sorted by time and memory usage. That’s a nice way to compare your code to others and see what you did well and where you can improve.

Here’s my solutions for the Arrays 101: Introduction card:

## Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones/

```class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
max_counter = 0
counter = 0
for i in nums:
if i == 1:
counter += 1
else:
if counter > max_counter:
max_counter = counter
counter = 0
if counter > max_counter:
max_counter = counter
return max_counter
```

## Find Numbers with Even Number of Digits

https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

```class Solution:

@staticmethod
def is_even(value):
return len(str(value)) % 2 == 0

def findNumbers(self, nums: List[int]) -> int:
return_value = 0
for number in nums:
if self.is_even(number):
return_value += 1
return return_value
```

## Squares of a Sorted Array

https://leetcode.com/problems/squares-of-a-sorted-array/

When writing code like this be careful because you’re altering the A list in place.

```class Solution:
def sortedSquares(self, A: List[int]) -> List[int]:
for index, num in enumerate(A):
A[index] = num ** 2
return sorted(A)
```

If you like what I’m writing, don’t forget to subscribe! 🙂

# LeetCode: Add Two Numbers Python 3 iterative solution

Hello,

Here’s my solution for the add two numbers problem on LeetCode.

Example:

```Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
```

When doing problems on websites such as LeetCode, I like to code in my own IDE, that’s why you’ll see some additional helper functions besides the solution code.

```class Solution(object):
def _get_append_func(self, root):
_root = root
nonlocal _root
_root.next = ListNode(value)
_root = _root.next

def _compute(self, result, base=10):
carry = result // base
rest = result % base
return carry, rest

"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
root_node = None
root_append_func = None

term_1 = l1
term_2 = l2
saved_carry = 0
while True:
# end of expression ; add cary if any
if term_1 is None and term_2 is None:
if saved_carry > 0:
root_append_func(saved_carry)
break

# We haven't reached the end, we assume that whatever term is left is 0
if term_1 is None:
term_1 = ListNode(0)
if term_2 is None:
term_2 = ListNode(0)

term_sum_carry, term_sum_no_carry = self._compute(term_1.val + term_2.val)

# add sum to current carry
next_carry, total_with_carry = self._compute(term_sum_no_carry + saved_carry)

# save carry
saved_carry = term_sum_carry + next_carry

# save total
if not root_node:
root_node = ListNode(total_with_carry)
root_append_func = self._get_append_func(root_node)
else:
root_append_func(total_with_carry)

# move on
term_1 = term_1.next
term_2 = term_2.next

return root_node
```

My solution isn’t that great because of the _add_to_list function which iterates the root_node to append the next value. I have no idea how to optimize that right now.

UPDATE:

I’ve learnt about nonlocal and found a way to slightly optimize my code:

```    def _get_append_func(self, root):
_root = root