473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Converting a string into integers

I have to write this short program that converts digits/symbols in a string into integers. Basically it takes a string containing none or one operation sign "+" or "-" if __init__ allows it, followed by one or more digits, and can contain blank spaces anywhere. It then returns a triple in this format:

(number of characters in the string including blanks, but not including blanks after the digits, "-1" if there was a "-" right before the digits with no spaces as gap otherwise "1", the integer in the string)

["_" means a blank space]
So "__-" would return (0, 1, 0) since there are no digits
"__+1" would return (4, 1, 1)
"+1" would return (2, 1, 1)
"__-1" would return (4, -1, -1)
"-1" would return (2, -1, -1)
"__+_1" would return (5, 1, 1)
"__-_1" would return (5, 1, 1) since there was a gap between "-" and "1"
"_1-" would return (3, 1, 1) since "-" is in the wrong place
and "_+1___abcde" would return (3, 1, 1) since blank spaces after the digits are ignored and letters are ignored

For the __init__ method, how do create this scanner, and set up whether or not "+" and "-" signs are allowed?
So far I have:

Expand|Select|Wrap|Line Numbers
  1. class ConvertToTriple(object):
  2.  
  3.      def __init__(self, take_sign):
  4.           '''Create scanner that converts strings to integers, and accepts leading "+" or "-" signs if take_sign is True.'''
  5.  
  6.      def scan_string(self, s):
  7.           '''Scan a character from string s.'''
How would I set up the __init__ method?
Thanks for any tips.
Mar 12 '10 #1
1 1842
Glenton
391 Expert 256MB
Well, I'd start with an algorithm, and then try to code it.

Can you devise an algorithm that would do what you want?
Mar 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
1
by: Graeme Downes | last post by:
I have a string of integer, such as the following: "12 24 12 9 2" and I want to convert this into an array or arraylist which will seperate the string into an array of integers. How do I do...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
3
by: rgarf | last post by:
I am converting a C++ application to C#, What in C# replaces the following C++ functions: overloaded opertaor= _ecvt(Math.Abs(val),14,&dec,&sign) ; //this function converts a double to a string...
0
by: Sanjay Tibrewal | last post by:
Folks, I am still on 1.1 framework for various reasons. I have a DataColumn of doubles that I want to convert to an array of doubles. I can do that through a simple function but then I have to...
11
by: TomServo | last post by:
I am writing code that needs to run on a variety of Unix systems. I am calling the statvfs and statfs system calls and I need to to convert some of the integers returned to character strings....
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
12
by: Samir | last post by:
Hi Everyone, I am relatively new to Python so please forgive me for what seems like a basic question. Assume that I have a list, a, composed of nested lists with string representations of...
6
by: Amyly | last post by:
Hi.. I am trying to write a program itoa() that can convert negative integers to ASCII string. Any help on how to do it would be much appreciated. Thanks, Amyly
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.