Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-19 Thread Peter Otten
Adam Jensen wrote:

 Side note: if one were to only import specific functions from a module,
 would the load time and memory consumption be smaller? Example, is:
 
 from random import randint, seed
 
 smaller and faster than:
 
 import random

Basically

from random import randint, seed

is equivalent to 

import random
randint = random.randint
seed = random.seed
del random

From that you can deduce that the whole random module is loaded into memory 
in both cases. A small speed advantage may be caused when the attribute 
lookup is avoided in a tight loop
 
$ python3 -m timeit -s 'import random' 'random.randint'
1000 loops, best of 3: 0.0925 usec per loop
$ python3 -m timeit -s 'from random import randint' 'randint'
1000 loops, best of 3: 0.0356 usec per loop

but the actual randint() function call is so heavy that this speedup is 
lost in the noise when you actually invoke the function:

$ python3 -m timeit -s 'from random import randint' 'randint(0, 42)'
10 loops, best of 3: 3.73 usec per loop
$ python3 -m timeit -s 'import random' 'random.randint(0, 42)'
10 loops, best of 3: 3.82 usec per loop

 Side side note: since 'i' isn't being used, is there a way to loop (within
 the list comprehension) without the 'i'? For example, to generate three
 random numbers:
 
 [randint(1,10) for i in range(3)]  # This works.
 [randint(1,10) for range(3)]  # This does not work.

No, but in numpy you can express it directly

numpy.random.randint(1, 10, 3)

or -- if you go back to the original problem -- with some effort:

 N = 3
 numpy.random.randint(1, 10, N) + numpy.arange(0, N*10, 10)
array([ 5, 11, 27])

In return the latter is likely significantly more efficient for large N than 
the generic list comprehension.

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


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-19 Thread Adam Jensen
On Fri, 19 Dec 2014 10:32:15 +0100
Peter Otten __pete...@web.de wrote:

 Basically
 
 from random import randint, seed
 
 is equivalent to 
 
 import random
 randint = random.randint
 seed = random.seed
 del random
 
 From that you can deduce that the whole random module is loaded into memory 
 in both cases. A small speed advantage may be caused when the attribute 
 lookup is avoided in a tight loop

Thanks for the clarification, that's really helpful. So I guess the import 
style is more about name space management than controlling the details of what 
gets loaded...

 or -- if you go back to the original problem -- with some effort:
 
  N = 3
  numpy.random.randint(1, 10, N) + numpy.arange(0, N*10, 10)
 array([ 5, 11, 27])
 
 In return the latter is likely significantly more efficient for large N than 
 the generic list comprehension.

Ha! That's fun. Seven seems to be a magic number in the original problem, so 
maybe a little tweak:

import numpy as np
N=7
(np.random.randint(1,N,N) + np.arange(0,N*N,N)).tolist()

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


[Tutor] lottery problem (Was Re: (no subject))

2014-12-18 Thread Alan Gauld

On 18/12/14 21:10, Abdullahi Farah Mohamud wrote:

hello i need help with a program and i dont understand what is wrong


Please always add a meaningful subject line.


when the computers asks the user if he would like to go again

 and the user says yes, it asks for the number of lines
 and then if the user clicks 3 it will only give

me one line.



import random
abz = 0
lines = int(input('How many lines would you like?'))
loop = lines
if lines 7:


You might want to check fpor negative numbers too
or you will loop forever.


 print('Too many lines saaxib')
 exit()
else:
 print()
while lines != 0:
 line1 = random.randint (1,7)
 line2 = random.randint (8,14)
 line3 = random.randint (15,21)
 line4 = random.randint (22,28)
 line5 = random.randint (29,35)
 line6 = random.randint (36,42)
 line7 = random.randint (43,49)

  lines = lines - 1
  print(line1, line2, line3, line4, line5, line6,line7)

Down to here basically works. But...

You could have used a list instead of all the
individual variables

line[0] = ...
line[1] = ...

But then you could get clever and use a loop:

while lines != 0:
   start = 1
   period = 7
   for lineNum in range(7):
   line[lineNum] = random(start,period)
   start += period
   period += period
   print (*line)
   lines -=1



while abz == 0:
 again = input('Would you like to go again?')
 if again == 'yes':
 lines = int(input('How many lines would you like?'))
 line1 = random.randint (1,7)


Notice this is outside the if statement so will execute
regardless of the answer.

But unlike the equivalent section above it is NOT in a
while loop so will only execute once per question.
Which is what you were seeing.


 line2 = random.randint (8,14)
 line3 = random.randint (15,21)
 line4 = random.randint (22,28)
 line5 = random.randint (29,35)
 line6 = random.randint (36,42)
 line7 = random.randint (43,49)
 lines = lines - 1
 print(line1, line2, line3, line4, line5, line6,line7)


Since you are duplicating code you could put it in a function - have you 
seen functions yet?


def getLine(start=1, period=7):
   line = []
   for lineNum in range(7):
   line[lineNum] = random(start,period)
   start += period
   period += period
   return line

then the while loop becomes:

while lines != 0:
line = getLine()
print(*line)
lines -= 1


 if again == 'no':
 print('Okay the program is finished saaxib')
 exit()


HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-18 Thread Adam Jensen
On Fri, 19 Dec 2014 00:55:49 +
Alan Gauld alan.ga...@btinternet.com wrote:

 You could have used a list instead of all the
 individual variables
 
 line[0] = ...
 line[1] = ...
 
 But then you could get clever and use a loop:
 
 while lines != 0:
 start = 1
 period = 7
 for lineNum in range(7):
 line[lineNum] = random(start,period)
 start += period
 period += period
 print (*line)
 lines -=1
 

A list comprehension might be fun. 
https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions

For example:

 [random.randint(x,x+6) for x in range(1,50,7)]
[4, 9, 15, 27, 33, 36, 49]

And to build the 'lines' list (although, this is getting rather ugly):

 lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)]
 lines
[[2, 13, 18, 27, 35, 37, 47], [1, 11, 21, 24, 34, 37, 49], [7, 12, 16, 24, 29, 
36, 44], [4, 9, 16, 22, 32, 37, 46], [2, 13, 20, 22, 29, 40, 46], [7, 14, 19, 
26, 35, 42, 43], [4, 12, 16, 22, 34, 40, 46]]

It might also be a good idea to execute random.seed() before calling randint() 
- https://docs.python.org/3.4/library/random.html#random.seed
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lottery problem (Was Re: (no subject))

2014-12-18 Thread Adam Jensen
On Thu, 18 Dec 2014 20:27:03 -0500
Adam Jensen han...@riseup.net wrote:

 And to build the 'lines' list (although, this is getting rather ugly):
 
  lines = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(7)]

Oops, in the context of the original program this might make more sense if 
written as:

data = [[random.randint(x,x+6) for x in range(1,50,7)] for i in range(lines)]

Side note: if one were to only import specific functions from a module, would 
the load time and memory consumption be smaller? Example, is:

from random import randint, seed

smaller and faster than:

import random


Side side note: since 'i' isn't being used, is there a way to loop (within the 
list comprehension) without the 'i'? For example, to generate three random 
numbers:

[randint(1,10) for i in range(3)]  # This works. 
[randint(1,10) for range(3)]  # This does not work. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor