If statement from a number to infinity

Viewed 724

I'm writing a code which takes a string, then splits it up in its characters and then do something depending on the list length. The problem is I don't know how to write it:

If the length (in characters) = 1, 4, 7, 10... (up to 'infinity') execute x code

If the length (in characters) = 2, 5, 8, 11... (up to 'infinity') execute y code

If the length (in characters) = 3, 6, 9, 12... (up to 'infinity') execute x code

What I mean with infinity is either infinity or a big enough number that no human person would be able to write

I've been working with python for a while I and understand how loops/if statements work, but I've never needed to provided an condition like this one where the condition itself comprehends an range of specific numbers up to infinity. One solution would be write three lists with a lots of numbers in them, but I wanna know if there is an easier and less pain taking way of doing it.

decoded is the variable assigned to the input (which is further up on the code) The mathematical expression for it would be, for the first statement, 1 + 3n, for the second, 2 +3n, and for the third, 3 +3n, being n a real number between 0 and infinity

decodedl = list(decoded)
if len(decodel) == 1 #rest of the stament here
    #execute x chunk of code
if len(decodel) == 2 #rest of the stament here
    #execute y chunk of code
if len(decodel) == 3 #rest of the stament here
    #execute z chunk of code

The expected result is the following:

If I input, for example: 'Hello how are you', then the code should execute code chunk y, as the length of the generated list would be 17: But as it is now, it would do nothing as the length of the list isn't 1, 2 nor 3.

4 Answers

This is a logical puzzle to solve. Instead of checking membership in all numbers, just observe the pattern as you stated (1 + 3n, 2 + 3n, and 3+ 3n) and build your logic from that. Numbers divisible by 3 have 0 as remainder.

if decodel: #make sure you eliminate empty string case
    if len(decodel)%3 == 1: #1 + 3n
        pass
    elif len(decodel)%3 == 2: #2 + 3n
        pass
    else: #3n
        pass

To get the requested behaviour, you can use the modulo operator(%) with argument 3. This will remove the number 3 from it as many times as possible and stops just before it would get to a negative number. What remains will always be 1 for the first set, 2 for the second and 0 for the last. So in code:

if len(decodel)%3 == 1:
    #The length of decodel is either 1, 4, 7, 10.....
elif len(decodel)%3 == 2:
    #The length of decodel is either  2, 5, 8, 11... 
elif len(decodel)%3 == 0:
    #The length of decodel is either 3, 6, 9, 12...

As you said, what you need is a mathematical expression that matches 1 + 3n in the first case, 2 + 3n in the second and 3 + 3n in the last case.

In order to do so, you can think the problem in the following way:

  1. The first set of numbers will be formed by all those numbers whose remainder after dividing it by 3 is 1 (Ex. 1, 4, 7, 10...)
  2. The second set of numbers will be formed by all those numbers whose remainder after dividing it by 3 is 2 (Ex. 2, 5, 8, 11...)
  3. The third set of numbers will be formed by all those numbers whose remainder after dividing it by 3 is 0 (Ex. 3, 6, 9, 12...)

So now that you figured out how to "classify" those numbers, what we are missing is the mathematical operator that gives you the remainder after dividing A by B.

That mathematical operator is called modulo (more info here). In python the symbol we use is %.

For example, 13 % 3 = 1 and 81 % 3 = 0.

So, to sum up, what you can do to solve your problem is the following:

if len(decodel) % 3 == 1:
    #Numbers with remainder 1 when dividing by 3 (i.e. 1, 4, 7...)
elif len(decodel) % 3 == 2:
    #Numbers with remainder 2 when dividing by 3 (i.e. 2, 5, 8...)
elif len(decodel) % 3 == 0:
    #Numbers that have no remainder when dividing by 3 (i.e. 3, 6, 9...)

Just as a side note, you don't need to know this while programming, but what we found here are called in Discrete Mathematics "congruence class modulo n", in our case the 3 possible congruence classes modulo 3: [0], [1] and [2].

If you want to remove the non-alpha characters, this would be useful.

Also, we don't have to compute the remainder in every if checks!

import re
a= 'how good is this?'

a = re.sub("[^a-zA-Z ]","", a)
print(a)

#compute the remainder
rem=len(a)%3

if rem == 1: 
    print('2 + 3n')
elif rem == 2: 
    print('2 + 3n')
else: 
    print('3n')

output:

how good is this
2 + 3n
Related