Re: [Tutor] how to use 2to3

2014-01-13 Thread Mark Lawrence

On 13/01/2014 17:30, S Tareq wrote:

can you please help me how to use 2to3 on 3.3. i went to the web site
follow instraction and it did not work.



Providing a sizeable wedge of code and stating "it did not work" is 
pretty useless.  But I'd guess that you've run the code *WITHOUT* the -w 
flag.  Am I correct?  You could have found this out for yourself by 
using this command:-


2to3 --help

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Keith Winston
s*** just got real.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Peter Otten
Alan Gauld wrote:

> On 13/01/14 18:22, Peter Otten wrote:
>> Peter Otten wrote:
> 
>> In the mean time here is my candidate:
>>
>> def test(a, b):
>>  a = iter(a)
>>  return all(c in a for c in b)
> 
> That's pretty close to my original thoughts. But one question.
> Why explicitly convert string a to an iter? The 'in' test
> would work with the original string. What extra magic does
> iter confer? Or are you extending reuse beyond strings?

No, I wanted to give a solution for the problem as originally stated and as 
attacked by Emile, where all characters have to occur in a in the same order 
as in b, but with arbitrary garbage allowed in between. Compare:

>>> for debris, product in [("alph", "alpha"), ("alpha", "alpha"), 
("axlypzha", "alpha"), ("alpha", "alpha"[::-1])]:
... print("debris: {}, product: {} --> test(): {}, (...): {}".format(
... debris, product, test(debris, product), all(c in debris for c in 
product)))
... 
debris: alph, product: alpha --> test(): False, (...): True
debris: alpha, product: alpha --> test(): True, (...): True
debris: axlypzha, product: alpha --> test(): True, (...): True
debris: alpha, product: ahpla --> test(): False, (...): True

The all(...) expression alone gets neither the count nor the order right.
The iter() call causes the `c in a` expression to search for the current c 
only after the occurence of the previous c. 

> And of course the original challenge was not for a
> boolean result but a specific string result so I'd
> go with:
> 
> def test(a,b):
> return {True: b,
> False: 'Give me something that's not useless next time.'
>}[all(c in a for c in b)]
> 
> or even
> 
> def test(a,b)
>  return b if all(c in a for c in b) else "Give me something that's
> not useless next time."
> 
> Are we there yet? :-)

Where's there? When's yet?

;)

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


Re: [Tutor] another better way to do this ?

2014-01-13 Thread eryksun
On Mon, Jan 13, 2014 at 1:36 PM, Keith Winston  wrote:
> Yikes, Peter, that's scary. Wow.

Yikes, watch the top posting. :)

>> In the mean time here is my candidate:
>>
>> def test(a, b):
>> a = iter(a)
>> return all(c in a for c in b)

Refer to the language reference discussion of comparison expressions:

http://docs.python.org/3/reference/expressions.html#not-in

For user-defined classes which do not define __contains__() but
do define __iter__(), x in y is true if some value z with
x == z is produced while iterating over y.

Since the iterator returned by `iter(a)` doesn't implement
__contains__, the interpreter iterates it looking for a match for `c`.
In effect, Peter smuggled a state variable into the expression,
equivalent to `index` in your code.

CPython Implementation
The "in" comparison operator is implemented abstractly by
PySequence_Contains. If the type doesn't define sq_contains, then the
call is routed to _PySequence_IterSearch, which calls PyIter_Next
until it either finds a match or the iterator is exhausted.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Alan Gauld

On 13/01/14 18:22, Peter Otten wrote:

Peter Otten wrote:



In the mean time here is my candidate:

def test(a, b):
 a = iter(a)
 return all(c in a for c in b)


That's pretty close to my original thoughts. But one question.
Why explicitly convert string a to an iter? The 'in' test
would work with the original string. What extra magic does
iter confer? Or are you extending reuse beyond strings?

And of course the original challenge was not for a
boolean result but a specific string result so I'd
go with:

def test(a,b):
   return {True: b,
   False: 'Give me something that's not useless next time.'
  }[all(c in a for c in b)]

or even

def test(a,b)
return b if all(c in a for c in b) else "Give me something that's 
not useless next time."


Are we there yet? :-)
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
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] how to use 2to3

2014-01-13 Thread Danny Yoo
On Mon, Jan 13, 2014 at 9:30 AM, S Tareq  wrote:
> can you please help me how to use 2to3 on 3.3. i went to the web site follow
> instraction and it did not work.

When asking for debugging help, it is usually a very good idea to
provide as much detail about what you exactly did as you can.
Specifically, if you see an error message, copy and paste the error
message for people to see.  If you are running a set of commands, copy
and paste exactly what commands you are running.

Why is this important?  Because the people who want to help you will
need to _replicate_ the steps that you are doing.  Maybe you have made
a mistake in typing something.  Or maybe you've forgotten a step.
Without seeing what steps you have taken, we can not tell.

Or, in some cases, maybe the software itself is at fault.  Maybe the
problem is something entirely external.  Computers are complicated,
and a lot of things can break.  Again, we can not know that without
seeing the error message you are encountering as well.

So please provide more information.  Otherwise, we can not help you:
we simply don't have enough information!


According to:

http://docs.python.org/2/library/2to3.html

you should be able to transform Python 2 programs to Python 3 programs
using the 2to3 program.


When I run the program 2to3 on your sample program, everything appears
to work fine.


dannyyoo@melchior:~$ 2to3 -w program.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored foo.py
... [output omitted]
#

and the resulting program.py does appear to use Python 3 conventions.


So I can not replicate or verify what you are seeing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Roelof Wobben


> From: keithw...@gmail.com
> Date: Mon, 13 Jan 2014 12:56:45 -0500
> Subject: Re: [Tutor] another better way to do this ?
> To: rwob...@hotmail.com
> CC: tutor@python.org
> 
> On Mon, Jan 13, 2014 at 1:14 AM, Roelof Wobben  wrote:
> > I have read all comments and im a little bit confused.
> > About which script are we talkimng about. I have seen a lot.
> 
> 
> I am talking about the script/approach I posted. Others have posted
> other scripts. Hopefully you have the capacity, with whatever approach
> to reading email you have, to go back and look over messages?
> 
> There is some confusion because your original message specified that
> order was important, though the examples you gave did not indicate
> that (in fact, contradicted it). Also, there was never anything
> specified about repeated letters: what would be the result of
> fix_machine("letr", "letter") (not to be confused with
> fix_machine("letter", "letr"), which would definitely be "letr").
> 
> -- 
> Keith

Oke, 

I think you mean this script :

def fix_machine(debris, product):
index = 0
for letter in product:
test = debris.find(letter, index)
if test!= -1:
index = test
else:   # Failure
return "Give me something that's not useless next time."
return product   # Success
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] how to use 2to3

2014-01-13 Thread S Tareq
can you please help me how to use 2to3 on 3.3. i went to the web site follow 
instraction and it did not work. 


# Import statements
import random
import datetime
#Arrays to store the definitions and keywords read from the file
keywords=[];
definition=[];
correctAnswer=[];
#Counter for the wrong Answer and counter of number of definition in
correctAnswerCounter=0 
wrongAnswer=0;
counter=0;
# Taking User input for accepting the file name to be read
filename= raw_input("Enter the File Name with extension")
#Reading the file from start to the end
for line in open(filename,'r').readlines():
    if(counter%2==0):
        keywords.append(line);
        counter=counter+1;
        correctAnswer.append(0)
    else:
        definition.append(line);
        keys=[];
        keys=line.split(" ");
        counter=counter+1;
# Running two while loops to make the pattern recursive
while True:
# Starting the time for quiz and also, creating variables to make sure that 
same sequences and answers are not repeated
    a = datetime.datetime.now().replace(microsecond=0)
    prevWord=0
    prevSeq=0
    # While loop to run the code till each answer is correctly answered
    while correctAnswer.count(2)!=(counter/2):
        #While loop to generate an different random number from one the 
generated previously
        while True:        
            word=random.randint(0,(counter/2)-1)
            if(correctAnswer[word]!=2):
                break;
            if(prevWord==word):
                continue;
        # Displaying the new keyword each time.
        print "Please Select the number which is the correct definition of the 
word:" ,keywords[word]
        #Generating an new sequence each time different from previous one
        while True:
            sequences =random.randint(0,2)
            if(prevSeq==sequences):
                continue;
            else:
                break
        #Generating an new incorrect answer each time different from previous 
one
        while True:
            incorrectAnswer=random.randint(0,len(correctAnswer)-1)
            if(incorrectAnswer==word):
                continue;
            else :
                break
        #Generating an new incorrect answer  each time different from previous 
one
        while True:
            incorrectAnswerSecond=random.randint(0,len(correctAnswer)-1);
            if (incorrectAnswer==incorrectAnswerSecond):
                continue
            if(incorrectAnswerSecond==word):
                continue
            else:
                break
        # Displaying the options to the user based on the sequence number 
generated
        if (sequences==0):
            print "1.",definition[word]
            print "2.",definition[incorrectAnswer]
            print "3.",definition[incorrectAnswerSecond]
        elif (sequences==1):
            print "1.",definition[incorrectAnswer]
            print "2.",definition[incorrectAnswerSecond]
            print "3.",definition[word]
        elif (sequences==2):
            print "1.",definition[incorrectAnswerSecond]
            print "2.",definition[word]
            print "3.",definition[incorrectAnswer]
        #Taking the answer from user
        answer = raw_input("Enter the Correct Number between 1 to 3")
        # Assign the seq and word to preseq and word
        prevSeq=sequences
        prevWord=word
        #Checking the answer if they are corret.
        if(0 == sequences):
            if(answer == "1"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(1 == sequences):
            if(answer == "3"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1;
        elif(2 == sequences):
            if(answer == "2"):
                print "success"
                correctAnswer[word]=correctAnswer[word]+1
                correctAnswerCounter=correctAnswerCounter+1
            else:
                print "Wrong Answer"
                print "Correct Answer: " ,definition[word]
                wrongAnswer=wrongAnswer+1
    # Stopping the time of the clock
    b = datetime.datetime.now().replace(microsecond=0)
    # displaying number of wrong answer and total quiz time
    print "Total Number of Wrong Answer:", wrongAnswer
    print "Total Quiz Time", (b-a)
    print "Total Number of correct Answer", correctAnswerCounter
    #asking user to reenter
    restart= raw_input("Do You want to start the quiz again Yes or No")
    if(restart=="no"):
        print "Thanks for quiz"
        break;
    el

Re: [Tutor] another better way to do this ?

2014-01-13 Thread Keith Winston
Yikes, Peter, that's scary. Wow.

On Mon, Jan 13, 2014 at 1:22 PM, Peter Otten <__pete...@web.de> wrote:
> Peter Otten wrote:
>
>> Emile van Sebille wrote:
>>
>>> On 01/12/2014 12:21 PM, Peter Otten wrote:
>>>
>>> test("axbxc", "abc")
 True
>>> test("abbxc", "abc")
 False

 Is the second result desired?
>>>
>>> No -- the second should match -- you found a test case I didn't...
>>>
>>> def test(a,b):
>>>for ii in a:
>>>  if ii not in b: a=a.replace(ii,"")
>>>  while ii+ii in a: a=a.replace(ii+ii,ii)
>>>return b in a
>>>
>>> Show me another.  :)
>>
> def test(a,b):
>> ...for ii in a:
>> ...  if ii not in b: a=a.replace(ii,"")
>> ...  while ii+ii in a: a=a.replace(ii+ii,ii)
>> ...return b in a
>> ...
> test("abac", "abc")
>> False
>
> In the mean time here is my candidate:
>
> def test(a, b):
> a = iter(a)
> return all(c in a for c in b)
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



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


Re: [Tutor] Euler Spoiler

2014-01-13 Thread Keith Winston
Ah, I got through it. Yes, I started down this path, but didn't dot
the i's. Thanks. I'm going to do some more reading on dynamic
programming...

Keith

On Mon, Jan 13, 2014 at 12:51 PM, Keith Winston  wrote:
> Danny, thanks for that exposition. I don't have time to absorb it
> yet,though I will attempt to soon, but I wanted to thank you for your
> effort in the meantime.
>
> Keith



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


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Peter Otten
Peter Otten wrote:

> Emile van Sebille wrote:
> 
>> On 01/12/2014 12:21 PM, Peter Otten wrote:
>> 
>> test("axbxc", "abc")
>>> True
>> test("abbxc", "abc")
>>> False
>>>
>>> Is the second result desired?
>> 
>> No -- the second should match -- you found a test case I didn't...
>> 
>> def test(a,b):
>>for ii in a:
>>  if ii not in b: a=a.replace(ii,"")
>>  while ii+ii in a: a=a.replace(ii+ii,ii)
>>return b in a
>> 
>> Show me another.  :)
> 
 def test(a,b):
> ...for ii in a:
> ...  if ii not in b: a=a.replace(ii,"")
> ...  while ii+ii in a: a=a.replace(ii+ii,ii)
> ...return b in a
> ...
 test("abac", "abc")
> False

In the mean time here is my candidate:

def test(a, b):
a = iter(a)
return all(c in a for c in b)


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


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Keith Winston
On Mon, Jan 13, 2014 at 1:14 AM, Roelof Wobben  wrote:
> I have read all comments and im a little bit confused.
> About which script are we talkimng about. I have seen a lot.


I am talking about the script/approach I posted. Others have posted
other scripts. Hopefully you have the capacity, with whatever approach
to reading email you have, to go back and look over messages?

There is some confusion because your original message specified that
order was important, though the examples you gave did not indicate
that (in fact, contradicted it). Also, there was never anything
specified about repeated letters: what would be the result of
fix_machine("letr", "letter") (not to be confused with
fix_machine("letter", "letr"), which would definitely be "letr").

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


Re: [Tutor] Euler Spoiler

2014-01-13 Thread Keith Winston
Danny, thanks for that exposition. I don't have time to absorb it
yet,though I will attempt to soon, but I wanted to thank you for your
effort in the meantime.

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


Re: [Tutor] another better way to do this ?

2014-01-13 Thread Roelof Wobben
I have read all comments and im a little bit confused.
About which script are we talkimng about. I have seen a lot.
 
Roelof
 
> From: keithw...@gmail.com
> Date: Sun, 12 Jan 2014 16:43:40 -0500
> CC: tutor@python.org
> Subject: Re: [Tutor] another better way to do this ?
> 
> On Sun, Jan 12, 2014 at 2:22 PM, Keith Winston  wrote:
> > There's another approach, I think, that's quite easy if order IS important.
> 
> Alas, there's one further problem with my script, relating to testing
> multiple sequential letters in product... but I'm not going to say
> more, I'll leave it as a problem for the OP. It's an easy fix once you
> understand the issue.
> 
> -- 
> Keith
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor