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.
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
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:
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With