Re: [Tutor] Tutor Digest, Vol 95, Issue 55

2012-01-21 Thread Shreesh bhat
*Lucky Numbers*
A number is called lucky if the sum of its digits, as well as the sum of
the squares of its digits is a prime number. How many numbers between A and
B are lucky?
Input:
The first line contains the number of test cases T. Each of the next T
lines contains two integers, A and B.
Output:
Output T lines, one for each case containing the required answer for the
corresponding case.

Constraints:
1 <= T <= 1
1 <= A <= B <= 10^18
Sample Input:
2
1 20
120 130
Sample Output:
4
1
Explanation:
For the first case, the lucky numbers are 11, 12, 14, 16.
For the second case, the only lucky number is 120.

---
My solution:

def isprime(n):
  n=abs(int(n))
  if n<2:
return False
  if n==2:
return True
  if not n & 1:
return False
  for x in range(3,int(n**0.5)+1,2):
if n % x == 0:
  return False
  return True

def islucky(n):
  sum1=0
  sum2=0
  while n!=0:
r=n%10
sum1+=r
sum2+=r*r
n=n/10
  if isprime(sum1) & isprime(sum2):
return True
  return False

number=raw_input()


for i in range(int(number)):
inp=raw_input()
a=inp.split()
startnum=int(a[0])
endnum=int(a[1])
li=map(islucky,xrange(startnum, endnum))
count=0
for j in li:
if j:
count+=1
print count
---

Traceback (most recent call last): File
"/run-1327085301-1965755690/solution.py",
 line 35, in li=map(islucky,xrange(startnum, endnum))
OverflowError: Python int too large to convert to C long

---
It shows this error for very large numbers or slows down with large numbers.
I m using Ubuntu 32-bit.

On Sun, Jan 22, 2012 at 4:24 AM,  wrote:

> Send Tutor mailing list submissions to
>tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>tutor-requ...@python.org
>
> You can reach the person managing the list at
>tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. OverFlow Error (Shreesh bhat)
>   2. Re: OverFlow Error (Alan Gauld)
>   3. Re: Tutor Digest, Vol 95, Issue 53 (George Nyoro)
>   4. Re: Tutor Digest, Vol 95, Issue 53 (Steven D'Aprano)
>   5. Re: delete an object from method (was Tutor Digest) (Dave Angel)
>   6. checking return status of 'ping' in windows (Nikunj Badjatya)
>   7. Re: checking return status of 'ping' in windows (Hugo Arts)
>
>
> --
>
> Message: 1
> Date: Sat, 21 Jan 2012 18:40:28 +0530
> From: Shreesh bhat 
> To: tutor@python.org
> Subject: [Tutor] OverFlow Error
> Message-ID:
> >
> Content-Type: text/plain; charset="iso-8859-1"
>
> How to correct this error?
>
> * OverflowError: Python int too large to convert to C long*
> -- next part --
> An HTML attachment was scrubbed...
> URL: <
> http://mail.python.org/pipermail/tutor/attachments/20120121/f7b86624/attachment-0001.html
> >
>
> --
>
> Message: 2
> Date: Sat, 21 Jan 2012 13:57:20 +
> From: Alan Gauld 
> To: tutor@python.org
> Subject: Re: [Tutor] OverFlow Error
> Message-ID: 
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 21/01/12 13:10, Shreesh bhat wrote:
> > How to correct this error?
> >
> > * OverflowError: Python int too large to convert to C long*
>
>
> Could we have some context?
>
> What version of Python? What OS?
> What does your code look like?
> Can we see the full error trace please?
>
> Otherwise, based only on what you posted, the only advice
> I can give you is to use a smaller int!
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> --
>
> Message: 3
> Date: Sat, 21 Jan 2012 17:58:17 +0300
> From: George Nyoro 
> To: tutor@python.org
> Subject: Re: [Tutor] Tutor Digest, Vol 95, Issue 53
> Message-ID:
> >
> Content-Type: text

Re: [Tutor] checking return status of 'ping' in windows

2012-01-21 Thread Alan Gauld

On 21/01/12 18:47, Nikunj Badjatya wrote:


Normal ping operation on windows cmd prompt can give 3 outputs.
1) "destination host unreachable
2) "request timed out"
3) "Reply from 192.168.1.1 : bytes=32 time=3ms TTL=64"

Now,
I was expecting the "status" in above snippet to hold '0' only in case
of no. 3)
But even when we have case 1), 'status' is holding '0'.
i.e. The exit status of ping is 0, even when destination host is
unreachable.!


The exit status tells you whether the program worked ok,
which could be interpreted, as it apparemtly is in the
case of Windows ping not to include whether it found
a host. If the ping program executed with no errors
you will get no errors. Its kind of arbitrary how you
define an 'error'...

So it seems like you will need to examine the actual output
of ping by looking at the content of stdout/err.

The good news is that this is fairly easy in the case of your ping 
because you can tell from the first word.


def pingStatus(resultString):
key = resultString.split()[0]
if key.lower().startswith('rep'): return 0
elif key.lower().startswith('req'): return 1
elif key.lower().startswith('des'): return 2
else: return -1

should give you something like what you were looking for.
Reading Popen stdout I leave as an exercise for the reader :-)

--
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] checking return status of 'ping' in windows

2012-01-21 Thread Hugo Arts
On Sat, Jan 21, 2012 at 7:47 PM, Nikunj Badjatya
 wrote:
> Hi All,
>
> I am using the following snippet to check the availability of an IP address.
> If that IP addr is found free than it can be used later on for further
> operations.
> Python ver 3.2
> Windows OS
>
> {{{
> pingret = subprocess.Popen('ping {0}'.format(IPaddr),
> shell=True,universal_newlines=True, \
>                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
> status = pingret.wait()
> if status == 0:
>     print("WARN  The IP given in the input is not free")
> .
> .
> }}}
>
> Normal ping operation on windows cmd prompt can give 3 outputs.
> 1) "destination host unreachable
> 2) "request timed out"
> 3) "Reply from 192.168.1.1: bytes=32 time=3ms TTL=64"
>
> Now,
> I was expecting the "status" in above snippet to hold '0' only in case of
> no. 3)
> But even when we have case 1), 'status' is holding '0'.
> i.e. The exit status of ping is 0, even when destination host is
> unreachable.!
>

This appears to be windows specific. Linux ping will return an exit
code of 1 if either zero responses are received or a packetcount and
deadline are specified and not met. I'm not sure why it doesn't work
that way on windows, but testing a bit for myself it seems to be the
platform's fault and not python's, since calling EXIT 1 will correctly
return a status code of 1.

> How do I make my snippet to work as desired. i.e even if destination host is
> unreachable, 'status' should hold '1'  and hold '0' only when it gets reply
> from that ip address.??
>

rather than using the wait() call, use Popen.communicate() which
returns the output of the program, and check that directly for your
three cases.

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


[Tutor] checking return status of 'ping' in windows

2012-01-21 Thread Nikunj Badjatya
Hi All,

I am using the following snippet to check the availability of an IP
address. If that IP addr is found free than it can be used later on for
further operations.
Python ver 3.2
Windows OS

{{{
pingret = subprocess.Popen('ping {0}'.format(IPaddr),
shell=True,universal_newlines=True, \
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
status = pingret.wait()
if status == 0:
print("WARN  The IP given in the input is not free")
.
.
}}}

Normal ping operation on windows cmd prompt can give 3 outputs.
1) "destination host unreachable
2) "request timed out"
3) "Reply from 192.168.1.1: bytes=32 time=3ms TTL=64"

Now,
I was expecting the "status" in above snippet to hold '0' only in case of
no. 3)
But even when we have case 1), 'status' is holding '0'.
i.e. The exit status of ping is 0, even when destination host is
unreachable.!

How do I make my snippet to work as desired. i.e even if destination host
is unreachable, 'status' should hold '1'  and hold '0' only when it gets
reply from that ip address.??


Thanks,

Nikunj


--
*7*Switch off as you go |*q*Recycle always | P Save Paper - Save Trees | Go
Green
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] delete an object from method (was Tutor Digest)

2012-01-21 Thread Dave Angel

On 01/21/2012 09:58 AM, George Nyoro wrote:

Hey guys,
I've been making an application and have made a delete method where the
user can delete the instance of that application. e.g. if I have a table
object, I need to be able to delete that instance from within the class and
then it becomes accessible.



If you're going to misuse so many terms in one query, you'll need to 
supply some code, and tell us in what way it doesn't serve your needs.


In the meantime,

1) Post a query by addressing it to "tutor@python.org," not by replying 
to  a digest message.  And if you must reply to a digest message, at 
least change the subject.  Thanks, though for deleting the digest content.


2) How do you expect to delete the instance of the application?  It's a 
funny term, but the only meaning I can come up with is you want to kill 
the application's process.


3) What's a table object?  If table is the name of your class, it really 
ought to be uppercase.


4) "from within the class" --  I'm guessing you mean from within a 
method of the class.


5) "then it becomes accessible" -- perhaps you mean inaccessible.

6) Please tell us the version of Python you're using and the operating 
system you're running on.




--

DaveA

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


Re: [Tutor] Tutor Digest, Vol 95, Issue 53

2012-01-21 Thread Steven D'Aprano

George Nyoro wrote:

Hey guys,
I've been making an application and have made a delete method where the
user can delete the instance of that application. e.g. if I have a table
object, I need to be able to delete that instance from within the class and
then it becomes accessible.


Did you want to ask a question, or are you just sharing?


--
Steven

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


Re: [Tutor] Tutor Digest, Vol 95, Issue 53

2012-01-21 Thread George Nyoro
Hey guys,
I've been making an application and have made a delete method where the
user can delete the instance of that application. e.g. if I have a table
object, I need to be able to delete that instance from within the class and
then it becomes accessible.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] OverFlow Error

2012-01-21 Thread Alan Gauld

On 21/01/12 13:10, Shreesh bhat wrote:

How to correct this error?

* OverflowError: Python int too large to convert to C long*



Could we have some context?

What version of Python? What OS?
What does your code look like?
Can we see the full error trace please?

Otherwise, based only on what you posted, the only advice
I can give you is to use a smaller int!


--
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] OverFlow Error

2012-01-21 Thread Shreesh bhat
How to correct this error?

* OverflowError: Python int too large to convert to C long*
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor