1

Problem:

I have a string of numbers, lets call it strNumbers. I need to go through this string, character by character doing calculations on each number up until a certain point. At that point, I need to start pulling two of the numbers at a time and doing calculations on those two numbers. Here's what I have come up with thus far, as you can see I have figured out how to iterate through the loop pulling single characters no problem. I also understand that I need a counter to determine exactly when I need to start pulling two characters at a time, but now what? Any help is appreciated, thank you.

for i in strNumbers:    
    intNumber = int (i)
    **do math on intNumber*
    **print result**
    count = count +1
    if count == 5:
        ??

Edit: I've decided to use two separate loops to accomplish this task, I've encountered another issue however. The following code throws a, TypeError: Can't convert 'int' object to str implicitly at line, number = int(strTail[i:i+2])

for i in strTail:
    number = int(strTail[i:i+2])
    intRooted = int( math.sqrt(number))
    strDecoded += str(intRooted)

Logically this seems like exactly what I want to do, I put to pull the number at position 'i' and the number one position ahead of i. What am I missing here?

Talen Kylon
  • 1,908
  • 7
  • 32
  • 60

4 Answers4

5

You can treat the string as an array accessing each character individually (or two at a time). So once you reach your designated amount you can set a flag that will tell your loop to start taking two characters at a time.

i=0
offset = 1
while i <len(str_numbers):
    if offset>1:
        int_number = int(str_numbers[i:i+offset])
    else:
        int_number = int(str_numbers[i])
    if i==5:
        offset=2
    i+=offset

EDIT: I just thought of a less confusing way of doing this:

offset=1
i=0
while i<len(str_numbers):
    if i==5:
        offset=2
    int_number = int(str_numbers[i:i+offset])
    i+=offset
ajon
  • 7,868
  • 11
  • 48
  • 86
  • Please consider adding some comments - bare code as a homework answer is hardly helpful to anyone. – georg Oct 01 '12 at 16:31
  • @BasicWolf Is there a particular style guide point that you are referring to? – ajon Oct 01 '12 at 16:35
  • 2
    I don't think `i++` is valid python. In any case, would modifying `i` change what comes next in the iteration? – Kevin Oct 01 '12 at 16:35
  • Yes, it is the **official** styling guide for Python code described in Python Enhancement Proposal no. 8 aka **PEP8**. What you've written looks like Java code. – Zaur Nasibov Oct 01 '12 at 16:37
  • @Kevin You are right, momentary confusion with i++ I made the edit. – ajon Oct 01 '12 at 16:41
  • @Kevin you were also right about i not changing while iterating over the virtual list created by xrange, so I needed to change it to a while loop. Thanks for the catch! – ajon Oct 01 '12 at 16:44
  • @BasicWolf I'm actually decently familiar with python the python style guide. I was asking what in particular looks off to you? – ajon Oct 01 '12 at 16:45
  • According to the PEP, variable names ought to be all lowercase, with words separated by underscores. So prefer `int_number` over `intNumber`. On the other hand, "mixedCase is allowed only in contexts where that's already the prevailing style, to retain backwards compatibility". the OP's code was in mixed case, so IMO it's fine if the answers are in mixed case too. – Kevin Oct 01 '12 at 16:49
  • @Kevin thanks, I didn't realize Python didn't use camel case. I changed the code to reflect that. – ajon Oct 01 '12 at 16:59
  • @glglgl Nice catch! Corrected – ajon Oct 02 '12 at 07:03
0
>>> my_nums = [1,2,3,4,5,6,7,8,9]
>>> dbl_nums = [[my_nums[i],my_nums[i+1]] for i in range(5,len(my_nums),2)] #double up numbers with index >= 5
>>> sngl_nums = map(lambda x:x*2,my_nums[:5]) #perform some math on single numbers for index < 5
>>> dbl_nums = map(lambda x:x[0]+x[1],dbl_nums) # do some math on 2 nums
>>> print dbl_nums # (6+7 , 8+9 )
[13, 17]
>>> print sngl_nums #(2*1,2*2,2*3,2*4,2*5)
[2, 4, 6, 8, 10]
>>> print sngl_nums+dbl_nums  #combined back into one list
[2, 4, 6, 8, 10, 13, 17]

to convert a string of numbers to a list do

my_nums = map(int,num_str)
>>> map(int,"123")
[1, 2, 3]
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
0

If you already know the number of single characters you want, then you could make two loops:

for s in strNumbers[:count]:
    do_something

indices = range(len(strNumbers))
for i in indices[count::2]:
    s = strNumbers[i:i+2]
    do_something_else

or

for (s1, s2) in zip(strNumbers[count::2], srtNumbers[count+1::2]):
    s = s1+s2
    do_something_else
Pierre GM
  • 19,809
  • 3
  • 56
  • 67
0

There's some code we can use in a related question, "How do you split a list into evenly sized chunks in Python?". First split your string into a single-digit section and double-digit section, and chunk as necessary.

def chunks(l, n):
    return [l[i:i+n] for i in range(0, len(l), n)]

strNumbers = "123456789012345"
single_part = chunks(strNumbers[:5], 1)
double_part = chunks(strNumbers[5:], 2)
string_numbers = single_part + double_part
numbers = [int(x) for x in string_numbers]
print numbers
#result: [1, 2, 3, 4, 5, 67, 89, 1, 23, 45]
Community
  • 1
  • 1
Kevin
  • 74,910
  • 12
  • 133
  • 166