0

For example, I have a reference number a = 15 and b= 3.

  • If x=2, f(a,b,x) = 1 because if one divide 15 into 3 parts, the number 2 is in the first part.
  • If x=7, f(a,b,x) = 2 because if one divide 15 into 3 parts, the number 7 is in the second part.
  • If x=15, f(a,b,x) = 3 because if one divide 15 into 3 parts, the number 15 is in the third part.
  • If x<0 or >15 the results are irrelevant to me.

Is there any built-in function like this?

kojiro
  • 74,557
  • 19
  • 143
  • 201
David Sousa
  • 383
  • 2
  • 13

2 Answers2

7

I can't think of a single built-in function which would do exactly that. It is however not difficult to write one:

def f(a, b, x):
  return (x - 1) * b // a + 1

for i in range(1, 16):
  print i, f(15, 3, i)

This prints out

1 1
2 1
3 1
4 1
5 1
6 2
7 2
8 2
9 2
10 2
11 3
12 3
13 3
14 3
15 3

(It is not entirely clear from the question how, and if, x=0 needs to be handled; this answer considers it to be outside the valid range.)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

No, there is no such built-in function. You can certainly write your own, however.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 2
    Although true, this could have been a comment; why not *write that function*? As it stands, this isn't very helpful. – Martijn Pieters Mar 10 '14 at 19:53
  • 1
    OP didn't ask for a function definition; explicitly asked for a built-in one. Thus, this is PRECISELY answering the question as asked. – Scott Hunter Mar 10 '14 at 19:54
  • 2
    Yup, but that means, in my experience, that the OP is really asking for such a function. If there is no such function, *why* isn't there; is it impossible to write it? Etc. A simple 'no, there is not' is rarely actually helpful. – Martijn Pieters Mar 10 '14 at 19:58
  • 1
    And yet the author comments above "aga, the question is about a built-in function and not about how to write a function." – Scott Hunter Mar 10 '14 at 20:10