Re: [Tutor] Assigning range :p:

2011-07-28 Thread Christopher King
On Wed, Jul 27, 2011 at 9:11 PM, Thomas C. Hicks para...@pobox.com wrote:

 On Wed, 27 Jul 2011 20:16:31 -0400
 Alexander Quest alexxqu...@gmail.com wrote:

 x=range(1,50)
 mid=x[len(x)/2]

 You would have to make sure there is a way to work around decimal points in
the division. Also, I would try it in practice. (remember range(1, 50) does
not include 50.)

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Assigning range

2011-07-27 Thread Alexander Quest
Does anyone know how to assign a certain numerical range to a variable, and
then choose the number that is the middle of that range? For example, I want
to assign the variable X a range between 1 and 50, and then I want to have
the middle of that range (25) return with some command when I call it
(perhaps rangemid or something like that?). In pseudocode, I am trying to
say X = range [1,50], return middle of range (which should return 25) but I
don't know how to code it. This is for a basic program I'm trying to write
where the player thinks of a number and the computer tries to guess the
number in as few tries as possible. Thanks for any help!

-Alex
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning range

2011-07-27 Thread Steven D'Aprano

Alexander Quest wrote:

Does anyone know how to assign a certain numerical range to a variable, and
then choose the number that is the middle of that range? For example, I want
to assign the variable X a range between 1 and 50, and then I want to have
the middle of that range (25) return with some command when I call it
(perhaps rangemid or something like that?). In pseudocode, I am trying to
say X = range [1,50], return middle of range (which should return 25) but I
don't know how to code it. This is for a basic program I'm trying to write
where the player thinks of a number and the computer tries to guess the
number in as few tries as possible. Thanks for any help!



Forget about using range, that just adds meaningless complexity.

What is important is that you have a lower bound, and a higher bound: 
two numbers, instead of how ever many (possible thousands, or millions!) 
in range(low, high).


middle = (low+high)//2



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning range

2011-07-27 Thread Alexander Quest
Thanks Steven- I'll try that out.

-Alex

On Wed, Jul 27, 2011 at 5:40 PM, Steven D'Aprano st...@pearwood.infowrote:

 Alexander Quest wrote:

 Does anyone know how to assign a certain numerical range to a variable,
 and
 then choose the number that is the middle of that range? For example, I
 want
 to assign the variable X a range between 1 and 50, and then I want to
 have
 the middle of that range (25) return with some command when I call it
 (perhaps rangemid or something like that?). In pseudocode, I am trying to
 say X = range [1,50], return middle of range (which should return 25) but
 I
 don't know how to code it. This is for a basic program I'm trying to write
 where the player thinks of a number and the computer tries to guess the
 number in as few tries as possible. Thanks for any help!



 Forget about using range, that just adds meaningless complexity.

 What is important is that you have a lower bound, and a higher bound: two
 numbers, instead of how ever many (possible thousands, or millions!) in
 range(low, high).

 middle = (low+high)//2



 --
 Steven

 __**_
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/**mailman/listinfo/tutorhttp://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning range :p:

2011-07-27 Thread Thomas C. Hicks
On Wed, 27 Jul 2011 20:16:31 -0400
Alexander Quest alexxqu...@gmail.com wrote:

 Does anyone know how to assign a certain numerical range to a
 variable, and then choose the number that is the middle of that
 range? For example, I want to assign the variable X a range between
 1 and 50, and then I want to have the middle of that range (25)
 return with some command when I call it (perhaps rangemid or
 something like that?). In pseudocode, I am trying to say X = range
 [1,50], return middle of range (which should return 25) but I don't
 know how to code it. This is for a basic program I'm trying to write
 where the player thinks of a number and the computer tries to guess
 the number in as few tries as possible. Thanks for any help!
 
 -Alex

There are probably better ways but this worked for me:

x=range(1,50)
mid=x[len(x)/2]

You do have to keep in mind the way python counts list indices for a
range call - i.e. x=range(1,50) will give you a list with all numbers 1
to 49 in it.

Hope that helps!

tom
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning range

2011-07-27 Thread Donald Wilson
You could start with an anonymous function using the lambda operator, such as:



mid_range = lambda x: x[len(x) // 2]

Note: If you use len(x) / 2 in python 3.x you will get a TypeError because the 
division operator / returns a float. Floor // returns an int in 2.x and 3.x.

Then use either:

x = range(1000, 4001)
mid_x = mid_range(x) # mid_x == 2500

or…

mid_x = mid_range(range(500, 751)) # mid_x == 625

etc. to retrieve the middle element.

You can extract the mid point of any sequence type, such as a string, using 
this function.

mid_x = mid_range(‘12345678987654321’) # mid_x == ‘9’



middle_number = lambda lo, hi: abs(lo - hi) // 2

will work if you just need the mid point of two numbers; either ints or floats.

mid_x = middle_number(0, 1000) # mid_x = 500

DW

On Jul 27, 2011, at 8:16 PM, Alexander Quest wrote:

 Does anyone know how to assign a certain numerical range to a variable, and 
 then choose the number that is the middle of that range? For example, I want 
 to assign the variable X a range between 1 and 50, and then I want to have 
 the middle of that range (25) return with some command when I call it 
 (perhaps rangemid or something like that?). In pseudocode, I am trying to say 
 X = range [1,50], return middle of range (which should return 25) but I don't 
 know how to code it. This is for a basic program I'm trying to write where 
 the player thinks of a number and the computer tries to guess the number in 
 as few tries as possible. Thanks for any help!
 
 -Alex
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Assigning range

2011-07-27 Thread Alexander Quest
Thanks for that Donald!

-Alex

On Wed, Jul 27, 2011 at 8:16 PM, Donald Wilson donald...@me.com wrote:

 You could start with an anonymous function using the lambda operator, such
 as:

 

 mid_range = lambda x: x[len(x) // 2]

 Note: If you use len(x) / 2 in python 3.x you will get a TypeError because
 the division operator / returns a float. Floor // returns an int in 2.x and
 3.x.

 Then use either:

 x = range(1000, 4001)
 mid_x = mid_range(x) # mid_x == 2500

 or…

 mid_x = mid_range(range(500, 751)) # mid_x == 625

 etc. to retrieve the middle element.

 You can extract the mid point of any sequence type, such as a string, using
 this function.

 mid_x = mid_range(‘12345678987654321’) # mid_x == ‘9’

 

 middle_number = lambda lo, hi: abs(lo - hi) // 2

 will work if you just need the mid point of two numbers; either ints or
 floats.

 mid_x = middle_number(0, 1000) # mid_x = 500

 DW

 On Jul 27, 2011, at 8:16 PM, Alexander Quest wrote:

  Does anyone know how to assign a certain numerical range to a variable,
 and then choose the number that is the middle of that range? For example, I
 want to assign the variable X a range between 1 and 50, and then I want to
 have the middle of that range (25) return with some command when I call it
 (perhaps rangemid or something like that?). In pseudocode, I am trying to
 say X = range [1,50], return middle of range (which should return 25) but I
 don't know how to code it. This is for a basic program I'm trying to write
 where the player thinks of a number and the computer tries to guess the
 number in as few tries as possible. Thanks for any help!
 
  -Alex
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor

 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor