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

  • Home
  • SEARCH
  • 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 7194619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:24:31+00:00 2026-05-28T20:24:31+00:00

Given an arbitrary input string I’m meant to find the sum of all numbers

  • 0

Given an arbitrary input string I’m meant to find the sum of all numbers in that string.
This obviously requires that i know the NEXT element in string while iterating through it…and make the decision whether its an integer. if the previous element was an integer also, the two elements form a new integer, all other characters are ignored and so on.

For instance an input string

ab123r.t5689yhu8 

should result in the sum of 123 + 5689 + 8 = 5820.

All this is to be done without using regular expressions.

I have implemented an iterator in python, whose (next()) method i think returns the next element, but passing the input string

acdre2345ty 

I’m getting the following output

a
c
d
r
e
2
4
t
y

Some numbers 3 and 5 are missing…why is this? I need that the next() to work for me to be able to sift through an input string and do the calculations correctly

Better still, how should i implement the next method so that it yields the element to the immediate right during a given iteration?

Here is my code

class Inputiterator(object):
    '''
    a simple iterator to yield all elements from a given
    string successively from  a given input string
    '''
    def __init__(self, data):
        self.data = data
        self.index = 0

    def __iter__(self):
        return self

    def next(self):
        """
        check whether we've reached the end of the input
        string, if not continue returning the current value
        """
        if self.index == len(self.data)-1:
            raise StopIteration
        self.index = self.index + 1
        return self.data[self.index]

# Create a method to get the input from the user
# simply return a string

def get_input_as_string():
    input=raw_input("Please enter an arbitrary string of numbers")
    return input

def sort_by_type():
    maininput= Inputiterator(get_input_as_string())
    list=[]
    s=""
    for char in maininput:
        if str(char).isalpha():
            print ""+ str(char)
        elif str(char).isdigit() and str(maininput.next()).isdigit():
            print ""+ str(char)

sort_by_type()
  • 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-05-28T20:24:32+00:00Added an answer on May 28, 2026 at 8:24 pm

    Edit: I first deleted this answer as there are much better solutions for your problem in this thread, but as you are directly asking how to use the next method to get your code working I have recovered it, in case you find it useful.

    Try this (I mocked the iterator for convenience):

    def sort_by_type():
        maininput = iter("acdre2345ty")
    
        for char in maininput:
            if char.isalpha():
                print char
            elif char.isdigit():
                number = char
                while True:
                    # try/except could take care of the StopIteration exception 
                    # when a digit is last in the string
                    #
                    # try:
                    #    char = maininput.next()
                    # except StopIteration:
                    #    char = ""
                    #
                    # however using next(iterator, default) is much better:
                    #
                    char = next(maininput, "")
    
                    if char.isdigit():
                        number += char
                    else:
                        break
                print number
                print char
    

    if produces:

    a
    c
    d
    r
    e
    2345
    t
    y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given N arbitrary integers, how to find average of top half of these numbers?
Given an arbitrary string, what is an efficient method of finding duplicate phrases? We
Given any arbitrary, one-line string, my goal is to render it into a bitmap
Given four (x,y) pairs that represent the four corners of an arbitrary polygon (quadrilateral)
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Given an arbitrary string, for example ( I'm going to play croquet next Friday
I am writing a c++ app to implement this: Given an arbitrary text document
I know that the total number of permutations for a given base is the
I know that this topic has been beaten to death, but it seems that
Given some list numbers = {2,3,5,7,11,13}; How do I translate this to translatedNumbers =

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.