Re: [Tutor] Creating Sudoku

2008-04-07 Thread Andrei Petre
A good place to look at : http://www.norvig.com/sudoku.html

On Mon, Apr 7, 2008 at 6:53 PM, Luke Paireepinart <[EMAIL PROTECTED]>
wrote:

> W W wrote:
> > On 4/7/08, Luke Paireepinart <[EMAIL PROTECTED]> wrote:
> >
> >> W W wrote:
> >>  What are you talking about?  I don't understand what you mean by
> "ignores
> >> whitespace between dictionary elements."
> >>
> >>
> >>> foo = {'1a': 1, '1b':2, '1c':3,
> >>>   '2a': 0, '2b': 9, '2c': 6}
> >>>
> >
> > Exactly that. If you were to write:
> >
> > foo = {'1a': 1, '1b':2, '1c':3}
> > foo['2a'] = 0
> >
> > You would get a nifty error.
> >
> You mean that the dictionary _definition_ ignores whitespace between
> elements?
> That's quite different than the dictionary itself ignoring whitespace.
> That implies that
> foo['1b'] is the same element as foo['1 b'], hence the source of  my
> confusion.
>
> That's not a feature of dictionaries, but of the comma.
> You can easily do the following:
> x = [1, 2,
>   3, 4]
> if you want.
> Same with tuples and various other things.
> Python just realizes that if it doesn't see a closing brace/bracket, but
> sees a comma, that more will probably be coming on the next line.
> You can do the same thing with backslash, if your statement does not end
> in a comma: for example,
> x = 1 + 1 + \
>  2 + 3 + 5 \
>  + 8 + 13
>
> Also, did you test the code that "generates an error?"
> It works fine for me.
>  >>> foo = {'1a': 'b'}
>  >>> foo['2b'] = 0
>  >>> print foo['2b']
> 0
>  >>>
>
> Hope that helps,
> -Luke
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] crashed with Runtime Error: NZEC (non-zero exit code)

2008-02-27 Thread Andrei Petre
i'm  thinking the same way Eric do.

On Wed, Feb 27, 2008 at 11:18 PM, Eric Brunson <[EMAIL PROTECTED]> wrote:

>  Alan Gauld wrote:
>
> "bob gailer" <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> wrote
>
>
>
>  i don't really understand that. you are trying to say that the
> tests
> from the online judge are not "exactly good" ?
>
>
>  Yes that's exactly what I'm saying. At least one test case has a
> non-decimal character.
>
>
>  Which actually makes it a very good test since thats
> exactly the kind of thing you should be testing for :-)
> A test suite that only checks valid data is a bad test.
>
>
>
>
> That's fine if writing a test suite for production software, but from the
> problem description:  "There is a string containing only decimal digit
> characters."
>
> Testing for valid input seems to be outside the scope of the problem
> definition.  :-)
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] crashed with Runtime Error: NZEC (non-zero exit code)

2008-02-24 Thread Andrei Petre
Hello,

I wrote a code for a problem and submitted to an online judge, a program
that runs my program against a set of predefined input data.

I wrote it for exercise my python writing. In don't want to discuss the
algorithm here!

I dare to ask for help because the answer was : crashed with Runtime Error:
NZEC (non-zero exit code). I heard that is a general error for most
interpreters and elsewhere that NZEC error code has to do with exceptions.

So, i'm seeking for a possible problem with the coding, not with the
algorithm.

Here, the code. (l also attached it in a file)
Using characters of the string, the program should construct the maximum
number which divides by fifteen without remainder.

example

*Input:*
1
02041

*Output:*
4200

[code]
import sys

def write(number): # assamble the number
# assure the 5 divisibility
if number[0] != 0:
number[0] -= 1
last_d = 0
else:
number[5] -= 1
last_d = 5

zero = True
for i in reversed(range(1,10)):
 for j in range(digits[i]):
 sys.stdout.write(i)
 zero = False

# leading zeroes should be omitted
if zero == False:
for i in range(digits[0]):
sys.stdout.write(0)

print last_d

def remove_item(d,item):
if type(item) != list:
d[item] -= 1
else:
d[item[0]] -= 1
d[item[1]] -= 1

def case5(d,item):
# return True if i try to remove the only five that assure the
divisibility with 5
return item == 5 and d[0] == 0 and d[item] == 1

def exist(d,item):
found = False
if type(item) != list:
if d[item] != 0 and case5(d,item) == False:
found = True
else:
p1 = d[item[0]] != 0 and case5(d,d[item[0]]) == False
d[item[0]] -= 1
p2 = d[item[1]] != 0 and case5(d,d[item[1]]) == False
d[item[0]] += 1
if p1 == True and p2 == True:
found = True
return found

def remove(d, mult):
found = False
for item in mult:
if exist(d,item) == True:
remove_item(d,item)
found = True
break
if found == False:
d = []
return d

def remove_all(digits,rest):
# if the number is the form 3k+1, i try to remove one 3k+1 digit or two
3k+2 digits
# if the number is the form 3k+2, i try to remove one 3k+2 digit or two
3k+2 digits
one = [1,4,7,[2,2],[2,5],[2,8],[5,5],[5,8],[8,8]]
two = [2,5,8,[1,1],[1,4],[1,7],[4,4],[4,7],[7,7]]
if rest == 1:
d = remove(digits, one)
else:
d = remove(digits, two)
return d

def resolve(digits):
suma = 0
for i in range(10):
suma += i*digits[i]
rest = suma % 3
if rest != 0: # if it's not divisible with 3 i try to remove some digits
digits = remove_all(digits,rest)
return digits

t = int(raw_input())
for tt in range(t):
line = raw_input()
digits = [0]*10
# counts the frequence of digits in the string
for i in range(len(line)):
digits[int(line[i])] += 1
# if it's not divisible with 5
if digits[0] == 0 and digits[5] == 0:
print("impossible")
else:
number = resolve(digits)
if(number == []):
print("impossible")
else:
write(number)
[/code]


Any alternatives for the personal rambling(if it's a wrong way to do it, of
course):

L = [1,2,[3,4],[5,5]]
for item in L:
   if type(item) == list:
 print L[item[0]], L[item[1]]
   else:
 print L[item]

Thanks,

Andrei
import sys

def write(number): # assamble the number
# assure the 5 divisibility
if number[0] != 0:
number[0] -= 1
last_d = 0
else:
number[5] -= 1
last_d = 5

zero = True
for i in reversed(range(1,10)):
 for j in range(digits[i]):
 sys.stdout.write(i)
 zero = False
 
# leading zeroes should be omitted 
if zero == False:
for i in range(digits[0]):
sys.stdout.write(0)

print last_d

def remove_item(d,item):
if type(item) != list:
d[item] -= 1
else:
d[item[0]] -= 1
d[item[1]] -= 1

def case5(d,item):
# return True if i try to remove the only five that assure the divisibility 
with 5
return item == 5 and d[0] == 0 and d[item] == 1

def exist(d,item):
found = False
if type(item) != list:
if d[item] != 0 and case5(d,item) == False:
found = True
else:
p1 = d[item[0]] != 0 and case5(d,d[item[0]]) == False
d[item[0]] -= 1
p2 = d[item[1]] != 0 and case5(d,d[item[1]]) == False
d[item[0]] += 1
if p1 == True and p2 == True:
found = True
return found

def remove(d, mult):
found = False
for item in mult:
if exist(d,item) == True:
remove_item(d,item)
found = True
break
if found == False:
d = []
return 

[Tutor] read from standard input

2008-02-13 Thread Andrei Petre
Hello,

I want to read from the standard input numbers until i reach a certain value
or to the end of the "file".
What is the simplest, straightforward, pythonic way to do it?

a sketch of how i tried to do it:
[code]
while 1 < 2:
x = raw_input()
if type(x) != int or x == 11:
break
else:
print x
[/code]

but don't work. and i'm interest in a general way to read until it is
nothing to read.


to ilustrate that in C:
[code]
int x;
while( scanf("%d",&x) == 1 && x != 11)
 printf("%d\n", x);
[/code]


Thanks!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] tokenizing a simple string with split()

2007-03-31 Thread Andrei Petre

I want to split a string like "C:\My\Doc\;D:\backup\" with two separators: \
and ;
I found that \ is handled with *raw string* notation r"". But the problem i
encountered is with split() function.
In the 2.5 reference is said that "The sep argument of the split() function
may consist of multiple characters". But I cannot figured out why this
simple example not working:

s = "spam;egg mail"
s.split("; ")

output: ['spam;egg mail']

instead of ['spam', 'egg', 'mail']

any suggestion is welcome,
andrei
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor