Coding Challenge #04: Write a program to find the sum of all elements in a list

Coding Challenge #04: Write a program to find the sum of all elements in a list

·

3 min read

Earlier this year, Women Who Code launched its WWCode Days of Code Challenge 2024. Participants could choose challenges ranging from machine learning and mobile to Python, cloud, data, and other emerging technologies.

I enthusiastically joined the Women Who Code Python community for their daily coding challenges. This blog series of mine named, "Coding Challenge" will serve as my documentation and accountability to consistency.

To enhance the experience for all WWCode Days of Code Challenge 2024 participants, I also crafted a nifty "checklist" or tracker. Excited to share that it's already making waves within the community! Check it out here: lnkd.in/dPuX7_qc

I've also published an article about Step-by-step guide on how to solve daily coding challenges.

Python Days of Code Challenge - Day 4

How It Works:

  • 60 Days, 60 Questions: Dive into daily Python challenges, each marking a step closer to coding mastery. You can start anytime in between as well!

Learning Together:

  • Share your solutions, ask questions, and connect with fellow coders in this thread or on social media (Twitter, LinkedIn) - or Github!

  • Don't forget to tag us @WWCodePython or use the hashtags #WomenWhoCodePython #PythonDaysofCode so we can celebrate your achievements!

Bonus:

  • React to today’s question if you’d like us to solve this LIVE in our upcoming Python Code Jam Sessions

Today's Challenge:

  • Write a program to find the sum of all elements in a list.

Solution

# 1. Understand the Problem
# Inputs: list of integers
# Outputs: an integer, the sum of all elements in the list
# Restrictions:


# 2. Examples

# Example 1:
# Input: list1 = [3, 5, 7, 9, 11, 45]
# Output: 80

# Example 2:
# Input: list1 = [12, 15, 3, 10]
# Output: 40

# 3. Pseudocode

# Create a list and assign random numbers
# Find a way to iterate to each element in the list (for loop)
# Use add function to add each element in the list
# Define a bucket to put the total sum
# Print the sum

# 4. Solve/Simplify

sumTotal = 0

list1 = [3, 5, 7, 9, 11, 45]

for everyItem in range(0, len(list1)):
    sumTotal = sumTotal + list1[everyItem]

# 5. Refactor or Look Back


# Define the list
list1 = [3, 5, 7, 9, 11, 45]

# Use the sum function to find the sum of elements in the list
# Python provides a built-in sum function, which simplifies the process of finding the sum of elements in a list.
sum_total = sum(list1)


# 6. Test the code

print(f"The sum of all elements in the list is {sumTotal}")

print(f"The sum of all elements in the list is {sum_total}")

Goals I achieved

Daily goals

  • [x] Code Day 4

  • [x] Write an awesome commit message and push your code!

  • [x] Continue writing on your documentation.

  • [x] Share your wins!

  • [x] Take care of yourself: pause, stretch, hydrate. More on WWCode Code of Balance.


Find the GitHub repo here.

I trust we all learned something from this blog. If you found it helpful too, give back and show your support by clicking heart or like, share this article as a conversation starter and join my newsletter so that we can continue learning together and you won’t miss any future posts.

Thanks for reading until the end! If you have any questions or feedback, feel free to leave a comment.

Did you find this article valuable?

Support anj by becoming a sponsor. Any amount is appreciated!