Re: [Tutor] \x00T\x00r\x00i\x00a\x00 ie I get \x00 breaking up every character ?

2011-11-21 Thread Steven D'Aprano

Dave Angel wrote:

On 11/20/2011 04:45 PM, Steven D'Aprano wrote:



Something in the tool chain before it reached Python has saved it 
using a wide (four byte) encoding, most likely UTF-16 as that is 
widely used by Windows and Java. With the right settings, it could 
take as little as opening the file in Notepad, then clicking Save.




UTF-16 is a two byte format.  That's typically what Windows uses for 
Unicode.  It's Unices that are more likely to use a four-byte format.


Oops, you're right of course, two bytes, not four:

py> u'M'.encode('utf-16BE')
'\x00M'

I was thinking of four hex digits:

py> u'M'.encode('utf-16BE').encode('hex')
'004d'




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


[Tutor] IndexError: list index out of range

2011-11-21 Thread John


Hi all,

I have wriiten the following code:
[Segment]


 def survivor(names, step):

index = step - 1
next = names
while len(next)>  1:
next.remove (next[index])



However when ever i run it i get this error message:

Traceback (most recent call last):
  File "", line 1, in
survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 4)
  File "", line 5, in survivor
next.remove (next[index])
IndexError: list index out of range

Any ideas about whats causing this error?

Big thanks,

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


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread James Reynolds
On Tue, Nov 22, 2011 at 12:28 AM, John  wrote:

>
> Hi all,
>
> I have wriiten the following code:
> [Segment]
>
>   def survivor(names, step):

>>>index = step - 1
>next = names
>while len(next)>  1:
>next.remove (next[index])
>
>
>
> However when ever i run it i get this error message:
>
> Traceback (most recent call last):
>  File "", line 1, in
>survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
> "Felicity", "Greg", "Harriet"], 4)
>  File "", line 5, in survivor
>next.remove (next[index])
> IndexError: list index out of range
>
> Any ideas about whats causing this error?
>
> Big thanks,
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>


I don't really know what it is you are trying to achieve, but I can tell
you this, if you add a print statement you can see very clearly why you are
getting the error:

def survivor(names, step):
index = step - 1
next = names
while len(next)>  1:
print next, index
next.remove (next[index])
survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 4)


['Andrew', 'Brenda', 'Craig', 'Deidre', 'Edward', 'Felicity', 'Greg',
'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Edward', 'Felicity', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Felicity', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig'] 3
Traceback (most recent call last):
  File "C:\Users\user\workspace\pricing\pricing\pricer\tests.py", line 40,
in 
"Felicity", "Greg", "Harriet"], 4)
  File "C:\Users\user\workspace\pricing\pricing\pricer\tests.py", line 38,
in survivor
next.remove (next[index])
IndexError: list index out of range


As you can see in this line: ['Andrew', 'Brenda', 'Craig'] 3

It looks for index "3" and there isn't one, so it throws an error. The
indexes available are 0,1 and 2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread Alan Gauld

On 22/11/11 05:28, John wrote:


def survivor(names, step):

index = step - 1
next = names
while len(next)> 1:
next.remove (next[index])



However when ever i run it i get this error message:
next.remove (next[index])
IndexError: list index out of range

Any ideas about whats causing this error?



You are doing the programming equivalent of sitting on the branch of a 
tree and cutting 2 feet off the end of the branch. But you never move 
your position so eventually you cut off the section of branch you are 
sitting on...


You never change index so eventually you remove enough of next that 
theres no longer any data at index.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread Dave Angel

On 11/22/2011 12:28 AM, John wrote:


Hi all,

I have wriiten the following code:


I can't provide any more clues about your code than the two excellent 
replies you've already gotten.  But it'd be really good if you could fix 
your time machine, so you're not posting in the future.  Really hard to 
follow a thread when the responses come before the question.


I suspect the date on your machine is set 12 hours or so into the future.

--

DaveA

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


[Tutor] Python code trouble!

2011-11-21 Thread John

Hi all,

I have attempted to create a programme which removes every Nth person 
from the list until there is only one name remaining. N is inputted by 
the user.


Here is the code:

def survivor(names, step):
next = names
while len(next) > 1:
 index = step - 1
 next.remove (next[index])
index = index + step - 1
while index > len(next):
index = index - len(next)
if index == len(next):
index = 0
return names[0]



To me is appears to be correct, however when i test it i get the 
following message:




>>> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", 
"Felicity", "Greg", "Harriet"], 4)
['Andrew', 'Brenda', 'Craig', 'Deidre', 'Edward', 'Felicity', 'Greg', 
'Harriet'] 3

['Andrew', 'Brenda', 'Craig', 'Edward', 'Felicity', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Felicity', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Greg', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig', 'Harriet'] 3
['Andrew', 'Brenda', 'Craig'] 3

Traceback (most recent call last):
  File "", line 1, in 
survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward", 
"Felicity", "Greg", "Harriet"], 4)

  File "", line 6, in survivor
next.remove (next[index])
IndexError: list index out of range


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


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread Steven D'Aprano

John wrote:


Hi all,

I have wriiten the following code:
[Segment]


 def survivor(names, step):

index = step - 1
next = names
while len(next)>  1:
next.remove (next[index])



What is the intention of this function? The name given doesn't mean 
anything to me. The parameters "names" and "step" don't seem meaningful.


I can see what the function does: it deletes bits of something, probably 
a list, in a convoluted way, eventually causing an error. But I can't 
tell what it is *supposed* to do.



Given the example you show later on:

survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 4)


what should the result be?



However when ever i run it i get this error message:

Traceback (most recent call last):
  File "", line 1, in
survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 4)
  File "", line 5, in survivor
next.remove (next[index])
IndexError: list index out of range

Any ideas about whats causing this error?



You attempt to delete an item that doesn't exist.




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


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread Nidian Job-Smith


To answer your questions  Steven
What is the intention of this function? The name given doesn't mean 
anything to me. The parameters "names" and "step" don't seem meaningful.

I can see what the function does: it deletes bits of something, probably 
a list, in a convoluted way, eventually causing an error. But I can't 
tell what it is *supposed* to do.


Given the example you show later on:

survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 4)


what should the result be?



To answer your questions Steven

 

What is the intention of this function? The name
given doesn't mean 

anything to me. The parameters "names" and "step" don't
seem meaningful.





The intention of the programme is to remove every Nth person from a
list. N (defined as steps) is inputted by the user.



I can see what the function does: it deletes bits of something,
probably 

a list, in a convoluted way, eventually causing an error. But I can't 

tell what it is *supposed* to do.





Given the example you show later on:



survivor(["Andrew", "Brenda", "Craig",
"Deidre", "Edward",

"Felicity", "Greg", "Harriet"], 4)





what should the result be?

 

In the example 

Survivor (["Andrew", "Brenda",
"Craig", "Deidre", "Edward",
"Felicity", "Greg", "Harriet"], 3)

He answer should be ‘Greg’
> Date: Tue, 22 Nov 2011 10:02:01 +1100
> From: st...@pearwood.info
> To: tutor@python.org
> Subject: Re: [Tutor] IndexError: list index out of range
> 
> John wrote:
> > 
> > Hi all,
> > 
> > I have wriiten the following code:
> > [Segment]
> > 
>   def survivor(names, step):
> > index = step - 1
> > next = names
> > while len(next)>  1:
> > next.remove (next[index])
> 
> 
> What is the intention of this function? The name given doesn't mean 
> anything to me. The parameters "names" and "step" don't seem meaningful.
> 
> I can see what the function does: it deletes bits of something, probably 
> a list, in a convoluted way, eventually causing an error. But I can't 
> tell what it is *supposed* to do.
> 
> 
> Given the example you show later on:
> 
> survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
>  "Felicity", "Greg", "Harriet"], 4)
> 
> 
> what should the result be?
> 
> 
> > However when ever i run it i get this error message:
> > 
> > Traceback (most recent call last):
> >   File "", line 1, in
> > survivor(["Andrew", "Brenda", "Craig", "Deidre", "Edward",
> > "Felicity", "Greg", "Harriet"], 4)
> >   File "", line 5, in survivor
> > next.remove (next[index])
> > IndexError: list index out of range
> > 
> > Any ideas about whats causing this error?
> 
> 
> You attempt to delete an item that doesn't exist.
> 
> 
> 
> 
> -- 
> Steven
> ___
> 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] Python code trouble!

2011-11-21 Thread Dave Angel

You're still posting from tomorrow.

On 11/22/2011 05:50 AM, John wrote:

Hi all,

I have attempted to create a programme which removes every Nth person 
from the list until there is only one name remaining. N is inputted by 
the user.


Here is the code:

def survivor(names, step):
next = names
while len(next) > 1:
 index = step - 1
 next.remove (next[index])
index = index + step - 1
while index > len(next):
index = index - len(next)
if index == len(next):
index = 0
return names[0]

Lots of things wrong with that code.  But first we need a complete 
description of what you want.  If you remove every nth item from a list, 
you'll end up with a somewhat shorter list, but not a single item.  For 
example, if you remove every 4th item from a 13 item list,  you'll end 
up with a 10-item list.


So presumably you're defining some kind of repeat, where you do it over 
and over.  But even then, if you try to remove every 4th item from a 
3-item list, you'll just end up with a 3-item list.


--

DaveA

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


[Tutor] Question on List Comprehensions

2011-11-21 Thread Charles Karl Becker
I'm trying to use a list comprehension to build a list with a variable
number of lists nested within it (ideally eventually going several
levels of nesting).  However I seem to be observing some strange
behavior and was wondering if anyone could take a look at this and
tell me if what I'm trying to do with list comps is possible, or is a
map() or for loop the best thing here?

I'm not worrying about incrementing the variables in the later
examples since I'm confused about their behavior (just simply adding
new elements to the list rather than nesting the lists; and then
setting the list to [none] in the last uncommented.  The last
commented one produces a syntax error, is it impossible to be
recursive with list comps like that or is my syntax just faulty?)
Thanks!
Charles

Here's the raw code with my comments :

board_size = 5
master_list = []

# this block produces the desired behavior
for c in range(board_size):
cell = ['', c+1]
master_list.append(cell)

print(master_list)

# I don't understand why this block behaves the way it does
master_list = []
master_list = [board_size * cell]
print(master_list)

# I also don't understand why this block behaves the way that it does
master_list = []
master_list = [master_list.append(cell)]
print(master_list)

# this block produces a syntax error, and I'm not sure why
'''
master_list = []
master_list = [x for c in range(board_size) master_list.append(cell)]
print(master_list)
'''
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python code trouble!

2011-11-21 Thread Steven D'Aprano

Hello John,


You are still posting from the future. Please fix your computer so that 
it is no longer set 12 hours in the future. Or perhaps your computer is 
just set in the wrong time zone.



John wrote:

Hi all,

I have attempted to create a programme which removes every Nth person 
from the list until there is only one name remaining. N is inputted by 
the user.


Ah, the Josephus problem! Trickier than it seems.

http://en.wikipedia.org/wiki/Josephus_permutation



Here is the code:

def survivor(names, step):
next = names


This line is pointless. It just assigns a new name to the same list. Why 
not just work directly with "names" instead of the misleading "next"? 
(Next what?)



while len(next) > 1:
 index = step - 1
 next.remove (next[index])


This does not do what you think it does, but even when it does, it 
doesn't it slowly and inefficiently.


You start of with an index. You want to delete the item at that index. 
So you start by retrieving the item at that index, then instruct the 
list to search from the beginning of the list for something which 
matches, then delete that. So consider what happens if you have a really 
huge list, with tens of thousands of items, and ask to delete the last 
one: even though you know you want to delete the last item, the remove() 
method has to check every single item first.


Worse, what if there are duplicates? The *first* duplicate found will be 
removed, instead of the one at the given index.


If you want to delete the name at an index, you should just directly 
delete the name at the index:


del names[index]



index = index + step - 1


This line is wrong. It means that you take one fewer step each time than 
you expect. For instance, if you take step = 4, you should step along 
indexes 3, 3+4=7, 7+4=11, 11+4=15, ... but instead you step along 
indexes 3, 3+4-1=6, 6+4-1=9, 9+4-1=12, ...




while index > len(next):
index = index - len(next)
if index == len(next):
index = 0


You can combine both of those with a single test:

while index >= len(names):
index = index - len(names)



return names[0]



To me is appears to be correct, however when i test it i get the 
following message:


Obviously it isn't correct. Here's a version which is not correct either:

def eliminate(names, step):
index = -1
while len(names) > 1:
print names
index += step
while index >= len(names):
index -= len(names)
del names[index]
return names[0]

but it is simple to read. Can you see what is wrong with it? Hint: 
perform the elimination by hand, eliminating every fourth person:


Starting names: Fred Wilma Barney Betty Scooby Shaggy Daphne Fred Wilma
1st round: Fred Wilma Barney - Scooby Shaggy Daphne Fred Wilma
2nd round: Fred Wilma Barney - Scooby Shaggy Daphne  Wilma
3rd round: Fred Wilma -- - Scooby Shaggy Daphne  Wilma
4th round: Fred Wilma -- - Scooby Shaggy Daphne  -
5th round: Fred Wilma -- - Scooby -- Daphne  -
6th round: Fred Wilma -- - -- -- Daphne  -
7th round: Fred Wilma -- - -- -- --  -
8th round: Fred - -- - -- -- --  -

But when I use my function:

py> eliminate(['FredF', 'Wilma', 'Barney', 'Betty', 'Scooby', 'Shaggy', 
'Daphne', 'Fred', 'Wilma'], 4)

'Daphne'

I get the wrong person. Why? Watch when I do this instead:

Starting names: Fred Wilma Barney Betty Scooby Shaggy Daphne Fred Wilma
1st round: Fred Wilma Barney Scooby Shaggy Daphne Fred Wilma
2nd round: Fred Wilma Barney Scooby Shaggy Daphne Fred
3rd round: Fred Wilma Barney Scooby Daphne Fred
4th round: Fred Wilma Scooby Daphne Fred
5th round: Fred Scooby Daphne Fred
6th round: Fred Daphne Fred
7th round: Fred Daphne
8th round: Daphne

Can you see why the two processes are different? The second one is much 
easier to program, but it is completely different from what you would do 
in real life.


I told you the problem was tricker than it first appears :)




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


Re: [Tutor] Question on List Comprehensions

2011-11-21 Thread Steven D'Aprano

Charles Karl Becker wrote:

I'm trying to use a list comprehension to build a list with a variable
number of lists nested within it (ideally eventually going several
levels of nesting).  However I seem to be observing some strange
behavior and was wondering if anyone could take a look at this and
tell me if what I'm trying to do with list comps is possible, or is a
map() or for loop the best thing here?



When in doubt, always use a for loop. List comps can't do anything that 
for loops can do, in fact they can do LESS.




I'm not worrying about incrementing the variables in the later
examples since I'm confused about their behavior (just simply adding
new elements to the list rather than nesting the lists; and then
setting the list to [none] in the last uncommented.  The last
commented one produces a syntax error, is it impossible to be
recursive with list comps like that or is my syntax just faulty?)


Your syntax is faulty. List comps are not full-blown replacements for 
for-loops, they are intentionally simple and straightforward.



board_size = 5
master_list = []

# this block produces the desired behavior
for c in range(board_size):
cell = ['', c+1]
master_list.append(cell)

print(master_list)

# I don't understand why this block behaves the way it does
master_list = []
master_list = [board_size * cell]
print(master_list)


You start of with master_list set to an empty list.

Then you immediately throw away that empty list, and set master_list to 
a list containing a single value, board_size * cell. Since by accident 
cell happens to equal a list left over from the previous part of code, 
you multiply a number 5 by a list ['', 5].


Multiplication of lists performs repetition:

py> ['a', 'b', 'c']*3
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

By the way, there is no list comprehension there. All you have is a list 
containing a single item.


[42] is not a list comp, it is a list containing a single item, 42.

[4*x] is not a list comp, it is a list containing a single item, 4*x 
(whatever x happens to be).


[4*x for x in (25, 36, 19, 5)] is a list comp.




# I also don't understand why this block behaves the way that it does
master_list = []
master_list = [master_list.append(cell)]
print(master_list)


You call master_list.append, which modifies master_list in place and 
returns None. So you append cell to master_list, then you create a new 
list [None], and set master_list to that new list, throwing away the 
work you did earlier.


Again, there is no list comprehension here either.



# this block produces a syntax error, and I'm not sure why
'''
master_list = []
master_list = [x for c in range(board_size) master_list.append(cell)]
print(master_list)
'''


Because you don't have a list comprehension. You can't put add arbitrary 
code inside a square brackets [ ]. You have to follow the syntax for a 
list comprehension:


listcomp = [expression for name in sequence]

not

listcomp = [expression for name in sequence another_command]



--
Steven

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


Re: [Tutor] How to get module name from ImportError

2011-11-21 Thread Steven D'Aprano

nikunj.badja...@emc.com wrote:

Hi All,

Please look at the following snippet.
{{{

# User defined modules
try:
from scripts import precheck
from scripts import validate
from scripts import constants
except ImportError:
print("ERROR: One of the modules (..scripts/precheck.py, validate.py, constants) 
is not present.")
print("INFO : Please verify the above modules, and restart the 
installation")
sys.exit(1)

}}}

See the red line.


Please remember that 8% of men, and 1% of women, are colour blind and 
may not be able to distinguish red. In the computer community, that 
figure is probably higher: I have heard credible reports that red-green 
colour blindness is more common among mathematicians, scientists and 
computer programmers than normal.


Please also remember that many people, especially programmers, disable 
HTML in email, as it is a security and privacy risk. Consequently they 
will not see colours, fancy fonts, dancing paperclips, coloured 
backgrounds, or any other superfluous junk used in HTML email.





I want to get the name of the particular module which is not available and 
hence causing ImportError.
One of the ways can be to get the STDERR and process it using re. !?


Absolutely not. The error message is not part of the API of Python, 
which means it could change without warning.


It should be safe to assume that the error message itself will describe 
the missing module in some fashion, but parsing the error is the wrong 
solution.


The way I would do this is:

# Untested
try:
from scripts import precheck, validate, constants
except ImportError as err:
msg = err.args[0]
msg += '\n whatever new message you want to add'
err.args = (msg,)  # note the comma is important
raise  # re-raise the exception


Hope that helps.



--
Steven

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


[Tutor] File vs. Database (possible off topic)

2011-11-21 Thread Ken G.
It occurred to me last week while reviewing the files I made in using 
Python, it could be somewhat similar to a database.


What would be a different between a Python files and Python databases?  
Granted, the access in creating them are different, I really don't see 
any different in the format of a file and a database.


Again, this may be off topic, but where can I review the basic concepts 
of creating a database/file.  For example, when is a back up file 
created, after inputting a new value?  Is sorting a consider a separate 
program after inputting a new value?  It has been some 30 years since I 
took a course in basic data processing and I am hazy in trying to 
remember the basic concepts.


If this is off topic, I apologized for posting here.

Ken, Kentucky, USA




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


Re: [Tutor] Python code trouble!

2011-11-21 Thread Dave Angel

On 11/21/2011 06:57 PM, Steven D'Aprano wrote:

Hello John,


You are still posting from the future. Please fix your computer so 
that it is no longer set 12 hours in the future. Or perhaps your 
computer is just set in the wrong time zone.



John wrote:

Hi all,

I have attempted to create a programme which removes every Nth person 
from the list until there is only one name remaining. N is inputted 
by the user.


Ah, the Josephus problem! Trickier than it seems.

http://en.wikipedia.org/wiki/Josephus_permutation



Here is the code:

def survivor(names, step):
next = names


This line is pointless. It just assigns a new name to the same list. 
Why not just work directly with "names" instead of the misleading 
"next"? (Next what?)



while len(next) > 1:
 index = step - 1
 next.remove (next[index])


This does not do what you think it does, but even when it does, it 
doesn't it slowly and inefficiently.


You start of with an index. You want to delete the item at that index. 
So you start by retrieving the item at that index, then instruct the 
list to search from the beginning of the list for something which 
matches, then delete that. So consider what happens if you have a 
really huge list, with tens of thousands of items, and ask to delete 
the last one: even though you know you want to delete the last item, 
the remove() method has to check every single item first.


Worse, what if there are duplicates? The *first* duplicate found will 
be removed, instead of the one at the given index.


If you want to delete the name at an index, you should just directly 
delete the name at the index:


del names[index]



index = index + step - 1


This line is wrong. It means that you take one fewer step each time 
than you expect. For instance, if you take step = 4, you should step 
along indexes 3, 3+4=7, 7+4=11, 11+4=15, ... but instead you step 
along indexes 3, 3+4-1=6, 6+4-1=9, 9+4-1=12, ...
Actually, this one is the one he got right.  Since he just deleted an 
item from the list, he should base the step off one *before* the present 
one.

index = (index-1) + step




while index > len(next):
index = index - len(next)
if index == len(next):
index = 0


You can combine both of those with a single test:

while index >= len(names):
index = index - len(names)


He could simplify that using the modulo operator.



return names[0]


I'm not familiar with the Josephus problem, and I don't have time right 
now to research it.  So I can't help with the rest.




--

DaveA

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


Re: [Tutor] File vs. Database (possible off topic)

2011-11-21 Thread Alan Gauld

On 22/11/11 00:24, Ken G. wrote:

It occurred to me last week while reviewing the files I made in using
Python, it could be somewhat similar to a database.


Depending on how broadly you define "database" you could be right.


What would be a different between a Python files and Python databases?


There is no such thing as a python database per-se. You can create 
databases in Python, as you can in any language.



Granted, the access in creating them are different, I really don't see
any different in the format of a file and a database.


A database in its most general sense describes a type of usage of a file 
so ther is no difference. However in the computing sense a database 
usually implies either a system or library that optimises the storage 
and retrieval of data in a way that is more efficient than simple 
sequential file access in Python. One such database is SqlLite which 
comes with Python. (But more primitive databases are also

provided like gdbms)


Again, this may be off topic, but where can I review the basic concepts
of creating a database/file.


Since its about programming basics its just about on topic.
But I'd start by reading wikipedia on the subject of databases. That 
will lead you into several different areas depending on interests. The 
theory of data storage/processing, the different available databases, 
object v relational data theory, and SQL.




For example, when is a back up file created, after inputting a new value?


When the programmer creates it. In most operating systems backups are 
not reated automatically (VAX VMS and IBM OS./390 and a few 
others(Pick?) being exceptions).


But that has nothing much to do with databases.

> Is sorting a consider a separate program after inputting a new value?

Yes, its a different operation that requires its own code - although 
Python does provide a sort method and most databases will provide a sort 
command.


> It has been some 30 years since I took a course in basic

data processing...


Things have moved on quite a long way! Check wikipedia.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Question on List Comprehensions

2011-11-21 Thread Alan Gauld

On 22/11/11 00:10, Steven D'Aprano wrote:


Because you don't have a list comprehension. You can't put add arbitrary
code inside a square brackets [ ]. You have to follow the syntax for a
list comprehension:

listcomp = [expression for name in sequence]

not

listcomp = [expression for name in sequence another_command]


And being picky you can add a conditional after the loop:

> listcomp = [expression for name in sequence if some_condition]

But it must be an if test, nothing else will do.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] File vs. Database (possible off topic)

2011-11-21 Thread Modulok
On 11/21/11, Ken G.  wrote:
> It occurred to me last week while reviewing the files I made in using
> Python, it could be somewhat similar to a database.
>
> What would be a different between a Python files and Python databases?
> Granted, the access in creating them are different, I really don't see
> any different in the format of a file and a database.
>
> Again, this may be off topic, but where can I review the basic concepts
> of creating a database/file.  For example, when is a back up file
> created, after inputting a new value?  Is sorting a consider a separate
> program after inputting a new value?  It has been some 30 years since I
> took a course in basic data processing and I am hazy in trying to
> remember the basic concepts.
>
> If this is off topic, I apologized for posting here.
>
> Ken, Kentucky, USA

Generally speaking,

If you ever even think the word 'database', your best choice 98% of the time is
sqlite. (The module is called 'sqlite3' in python.) Reading up on SQL on
wikipedia and sqlite3 in the python docs is a good place to start!

Eventually, if you start storing a massive number of records and need things
like concurrent access and load balancing across multiple physical machines and
so forth, sqlite isn't going to cut the mustard. In such cases people use a
dedicated database server like postgresql, mysql, etc; A totally separate
program that does nothing but database black magic. Your python programs will
talk to the database server through a module written for that server. The
database server will fetch, sort, update and store the actual data.

Python has bindings to these heavy weight database backends through third party
modules that abstract the details for you. The most popular module is probably
sqlalchemy. It talks to postgresql, mysql and a few others. It's a good idea to
get used to sqlite and general SQL concepts before you jump into sqlalchemy!

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


Re: [Tutor] IndexError: list index out of range

2011-11-21 Thread Asokan Pichai
On Tue, Nov 22, 2011 at 4:32 AM, Steven D'Aprano wrote:

> John wrote:
>
>>
>> Hi all,
>>
>> I have wriiten the following code:
>> [Segment]
>>
>>   def survivor(names, step):
>
index = step - 1
>>next = names
>>while len(next)>  1:
>>next.remove (next[index])
>>
>
>
> What is the intention of this function? The name given doesn't mean
> anything to me. The parameters "names" and "step" don't seem meaningful.
>
I guess he is trying the Josephus problem. I am guessing from the name!

If so,  you have to remember that when you delete an item you change the
positions of subsequent items.
For example, in a 11-element list, say A,  if you want to delete every
third, the index numbers you *should* delete are 2, 5, 8. But if you delete
A[2], then you will delete the original A[6] and then the original A[10].

So you have to think in terms of 'marking' for deletion and later deleting
or deleting from the other end.

A more interesting possibility is to replicate the original list and append
it to itself and "delete" all occurrences.

About 25 years back thats what I did in BASIC and won an honorable mention
in a coding contest in a magazine :-)
I replaced the cell contents with a space and in subsequent rounds counted
non-space items.

The first prize was won by a circular(linked-)list in Pascal, with the same
repace by Space idea

HTH

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


Re: [Tutor] Question on List Comprehensions

2011-11-21 Thread Charles Karl Becker
Steven and Alan,

Thank you for your comments!

Alan said:
>> Because you don't have a list comprehension. You can't put add arbitrary
>> code inside a square brackets [ ]. You have to follow the syntax for a
>> list comprehension:

This helps me understand a lot when looking back, I thought that any
operation done in place of defining the list literally was a list
comprehension.  I'm on wikipedia and a few tutorials now to refine and
will post back when I've come up with the solution I'm looking for
(and a comment as to if it's worth replacing a for loop with).

Thanks again!
Charles

On Mon, Nov 21, 2011 at 7:04 PM, Alan Gauld  wrote:
> On 22/11/11 00:10, Steven D'Aprano wrote:
>
>> Because you don't have a list comprehension. You can't put add arbitrary
>> code inside a square brackets [ ]. You have to follow the syntax for a
>> list comprehension:
>>
>> listcomp = [expression for name in sequence]
>>
>> not
>>
>> listcomp = [expression for name in sequence another_command]
>
> And being picky you can add a conditional after the loop:
>
>> listcomp = [expression for name in sequence if some_condition]
>
> But it must be an if test, nothing else will do.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> ___
> 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] Question on List Comprehensions

2011-11-21 Thread Charles Becker
Alan, Steve, future readers,

After some re-reading and hacking I was able to discover the solution.  Since I 
raised the question here it is :

[['{0}'.format(x+1), x+1] for x in range(size)]

This will create the list with nested lists for whatever number 'size' is set 
to.  This should be good enough to get anyone started on future problems in 
this area, and yes the redundancy present does make sense in the scope of what 
I'm working on. :)

Thanks!  I'll leave future questions on the backburner a little longer.
Charles

Sent from my iPhone

On Nov 21, 2011, at 7:04 PM, Alan Gauld  wrote:

> On 22/11/11 00:10, Steven D'Aprano wrote:
> 
>> Because you don't have a list comprehension. You can't put add arbitrary
>> code inside a square brackets [ ]. You have to follow the syntax for a
>> list comprehension:
>> 
>> listcomp = [expression for name in sequence]
>> 
>> not
>> 
>> listcomp = [expression for name in sequence another_command]
> 
> And being picky you can add a conditional after the loop:
> 
> > listcomp = [expression for name in sequence if some_condition]
> 
> But it must be an if test, nothing else will do.
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> 
> ___
> 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