[Tutor] To find the least number divisible by all numbers from 1-20

2013-08-13 Thread #PATHANGI JANARDHANAN JATINSHRAVAN#
Hello All,

Sorry for the earlier mail without subject. I was in a hurry so I missed that

I am solving problem number 5 in project euler. I think my solution seems 
logically correct but it is not giving an answer as it is taking too long to 
execute. So can someone suggest an alternative solution? Here is my source code:

import sys
def check(num):
  lis=[20,19,18,17,16,14,13,11]  #because a no. divisible by 20 is divisible by 
2,4,5 and so on for the values omitted
  for i in lis:
if num%i==0:
  continue
else:
  return False
  break

  return True

def main():
  num=2520  # Because we can start from the no. divisible by 1-10
  while not check(num):
print(num)
num+=2# Because num has to be even
  print(num)


if __name__ == '__main__':
  main()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] dbus.Array to string

2013-08-13 Thread Marc Tompkins
On Tue, Aug 13, 2013 at 8:59 AM, Amit Saha  wrote:


> What does it mean (and will it always work?) when I don't specify any
> encoding:
>
> >>> bytearray(ssid).decode()
> u'BigPond679D85'
>

If you don't specify an encoding, then the default encoding is used; as you
point out a bit later, your local default is ascii.

Will it always work?  NO.  If there are any characters in the input stream
(the SSID in this case), .decode will fail (probably with
UnicodeDecodeError, but I can't test it at the moment.)

I don't know the WiFi spec well enough to know whether you're ever going to
run into non-ASCII characters in an SSID; I'm guessing that people in e.g.
Russia name their networks in Cyrillic, but (despite living in a Russian
neighborhood of Los Angeles) I've never seen any SSIDs that weren't pure
ASCII.  Does anybody out there know the rules for this?  Just now I tried
to change the SSID of my cell phone's mobile hotspot to Cyrillic, but the
configuration utility wouldn't even let me change keyboards; I don't know,
though, whether this is a limitation of the spec or just the local
implementation.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] (no subject)

2013-08-13 Thread #PATHANGI JANARDHANAN JATINSHRAVAN#
Hello All,

I am solving problem number 5 in project euler. I think my solution seems 
logically correct but it is not giving an answer as it is taking too long to 
execute. So can someone suggest an alternative solution? Here is my source code:

import sys
def check(num):
  lis=[20,19,18,17,16,14,13,11]  #because a no. divisible by 20 is divisible by 
2,4,5 and so on for the values omitted
  for i in lis:
if num%i==0:
  continue
else:
  return False
  break

  return True

def main():
  num=2520  # Because we can start from the no. divisible by 1-10
  while not check(num):
print(num)
num+=2# Because num has to be even
  print(num)


if __name__ == '__main__':
  main()

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


Re: [Tutor] To find the least number divisible by all numbers from 1-20

2013-08-13 Thread Marc Tompkins
On Tue, Aug 13, 2013 at 9:45 AM, #PATHANGI JANARDHANAN JATINSHRAVAN# <
jatinshr...@e.ntu.edu.sg> wrote:

>  Hello All,
>
> Sorry for the earlier mail without subject. I was in a hurry so I missed
> that
>
>  I am solving problem number 5 in project euler. I think my solution
> seems logically correct but it is not giving an answer as it is taking too
> long to execute. So can someone suggest an alternative solution?
>

My approach: factor each number from 1 to 20 (for example, 20 factors to 1,
2, 2, 5) and build a list of factors; for each number, check to see that
the list contains enough copies of all of the current number's factors
(e.g. four 2s and two 3s) and add them to the list if not; at the end,
multiply the list of factors.

It took me about a minute in Excel; about 45 minutes to work out my
algorithm in Python - but it executed in less than a second.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] To find the least number divisible by all numbers from 1-20

2013-08-13 Thread Alan Gauld

On 13/08/13 17:45, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote:


def check(num):
   lis=[20,19,18,17,16,14,13,11]  #because a no. divisible by 20 is
   for i in lis:
 if num%i==0:
   continue
 else:
   return False
   break

   return True


This is a bit convoluted. All you need is

for i in lis:
  if num%i != 0:
return False
  return True


def main():
   num=2520  # Because we can start from the no. divisible by 1-10
   while not check(num):
 print(num)
 num+=2# Because num has to be even


Surely you can increment by 20 since that's the next number that is 
divisible by 20.


eg if we start at 20 the next number divisible by 20 will be 40, not 
22... That should speed up the search considerably!


But my brain is nagging me that there should be a
cleverer mathematical trick too, but I can't remember
what it is... I'm sure one of our math gurus will tell us! :-)

--
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] To find the least number divisible by all numbers from 1-20

2013-08-13 Thread Alan Gauld

On 13/08/13 18:19, Alan Gauld wrote:

On 13/08/13 17:45, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote:


def check(num):
   lis=[20,19,18,17,16,14,13,11]  #because a no. divisible by 20 is
   for i in lis:
 if num%i==0:
   continue
 else:
   return False
   break

   return True


This is a bit convoluted. All you need is

 for i in lis:
   if num%i != 0:
 return False
   return True


Oops, that last return should be outside the loop, sorry...

--
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] [Spoiler alert] Re: To find the least number divisible by all numbers from 1-20

2013-08-13 Thread Oscar Benjamin
On Aug 13, 2013 7:21 PM, "Peter Otten" <__pete...@web.de> wrote:
>
> Peter Otten wrote:
>
> > I'll post a naive implementation of that idea in a follow-up.
>
> So there:
>
> def gcd(a, b):
> if a == b:
> return a
> elif a > b:
> return gcd(a-b, b)
> else:
> return gcd(a, b-a)
>
> N = 20
> numbers = range(2, N+1)
> while len(numbers) > 1:
> numbers.sort()
> a, b = numbers[:2]
> d = gcd(a, b)
> numbers[:2] = [a*b//d]
> result = numbers[0]
> assert all(result % i == 0 for i in range(1, N+1))
> print result
>
> It turns out I can only avoid the recursion limit by sorting the numbers
and
> picking the smallest. For greater N you need to come up with a smarter
> approach (the actual limit was N<=48) or at least a non-recursive
> implementation of gcd().

I think it's better to compute the lcm by finding its prime factorisation.
E.g. There are 4 twos in it since that's the maximum number of twos in the
prime factorisation of any of the integers up to 20.

I would post a snippet but isn't it generally a bit of a faux-pas to give
solutions to these problems (I mean this as a real question)?

Oscar

>
> ___
> 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] dbus.Array to string

2013-08-13 Thread Steven D'Aprano

On 14/08/13 02:49, Marc Tompkins wrote:

On Tue, Aug 13, 2013 at 8:59 AM, Amit Saha  wrote:



What does it mean (and will it always work?) when I don't specify any
encoding:


bytearray(ssid).decode()

u'BigPond679D85'



If you don't specify an encoding, then the default encoding is used; as you
point out a bit later, your local default is ascii.

Will it always work?  NO.  If there are any characters in the input stream
(the SSID in this case), .decode will fail (probably with
UnicodeDecodeError, but I can't test it at the moment.)


Careful -- you are confusing two distinct concepts here. ssid does not contain 
characters. It contains bytes. There are exactly 256 possible bytes, which are numbers 0, 
1, ... 255. They may *represent* characters, or sounds, or images, or motion video, or 
any other form of data you like, in which case you have to ask (e.g.) "how is the 
sound encoded into bytes? is it a WAV file, or MP3, or OGG, or something else?"

In this case, the ssid represents characters, but it contains bytes, and the 
same question applies -- how are the characters A, B, C, ... encoded into 
bytes? Unless you know which encoding is used, you have to guess. If you guess 
wrong, you'll get errors. If you're lucky you will get an exception, and know 
that you guessed wrong, but if you're unlucky you'll just get garbage 
characters.

Fortunately, there are a couple of decent guesses you can make which will often 
be correct, at least in Western European countries, Australia, the USA, and 
similar:

UTF-8
ASCII
Latin-1

Latin-1 should be considered the "last resort" encoding, since it will never 
fail. But it can return garbage. UTF-8 should be considered your first guess, since it is 
the standard encoding that everyone should use. (Any application that doesn't use UTF-8 
by default in the 21st century is, in my opinion, buggy.)




I don't know the WiFi spec well enough to know whether you're ever going to
run into non-ASCII characters in an SSID;


A little bit of googling shows that it definitely happens, and that UTF-8 is 
the standard encoding to use.




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