Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7814767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:17:58+00:00 2026-06-02T05:17:58+00:00

Return the sum of the numbers in the array, except ignore sections of numbers

  • 0

Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 7 (every 6 will be followed by at least one 7). Return 0 for no numbers.

sum67([1, 2, 2]) ? 5
sum67([1, 2, 2, 6, 99, 99, 7]) ? 5
sum67([1, 1, 6, 7, 2]) ? 4
def sum67(nums):
    dontadd = 0
    sum = 0
    for i in range(0, len(nums)):
        if dontadd == 0:
            if nums[i] == 6:
                dontadd = 1
            else:
                sum += nums[i]
        else:
            if nums[i] == 7:
                dontadd = 0
            else:
                pass# nothing happens. It is useful as a placeholder when a statement is required syntactically
    return sum

Looking for a more elegant solution to this problem from codingbat. This answer doesn’t seem as intuitive as it could be

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:18:00+00:00Added an answer on June 2, 2026 at 5:18 am

    If we can just remove the elements that we don’t want, then we can use a simple sum. Here is an example:

    def sum67(nums):
        nums=nums[:]
        while 6 in nums:
            i=nums.index(6)
            j=nums.index(7,i)
            del nums[i:j+1]
        return sum(nums)
    

    First, we use nums=nums[:] to make a copy. The caller probably isn’t expecting nums to change.

    nums.index(6) finds the index of the first element that has a value of 6. nums.index(7,i) finds the index of the first element that has a value of 7 after the index i. del nums[i:j+1] then deletes the elements in the range from i to j, including the element at j.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a method that will sum two timeO objects and return
I'm working on the following codingbat problem: Return the sum of the numbers in
i am trying to get the sum of even numbers from a recursive array
Am trying to return the sum of each day of a week in mysql
How can I sum the values in two maps and return the map with
Is it possible to order return rows with a criteria on a sum group
Why can't we use return keyword inside ternary operators in C, like this: sum
I'm trying to write program calculating average of given numbers stored in an array.
// Write a program to sum all the odd elements of an array a
I write simple C++ code that compute array reduction sum, but with OpenMP reduction

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.