Re: [Tutor] Writing decorators?
Michael Welle writes: > Somewhere in this thread (or the one talking about decorators after > this thread) it was said that a decorator 'changes a function'. I not > a native English speaker, so it could just be a language problem. But > to me it seems the function is replaced, not changed? That's correct. Don't be surprised, though, if the concept “replace the object referenced by ‘foo’ with a different object and discard the prior object at that reference“ is glossed to “change ‘foo’” in casual usage :-) -- \ “Hey Homer! You're late for English!” “Pff! English, who needs | `\ that? I'm never going to England!” —Barney & Homer, _The | _o__)Simpsons_ | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need Your help
>I'm creating a mobile application [ http://e-aadhaarcard.in ] and I'm using >python for a desktop server. However, I don't have access to a static IP on >the desktop, but do have a website. Is it possible to connect from mobile http >website -> desktop server >and back? Try using a LAN so all your devices have a 192.168.X.X address. You can set it in your PC and Mobile manually or through DHCP. You will be communicating through IP base between your desktop and mobile. That way you can test your application. Also you can ask your IT department to assign a permanent IP if you want to test real live. This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python cgi single double quotes
Hi Alan, vimal@Ubuntu-1404-trusty-64-minimal:~$ python Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> print sys.version 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] >>> Should I test the above from within html code too ? Also, for testing and configuration purpose, I have set 755 / 777 permissions to dir / files respectively. Thank you, Nitin Chandra On 20 July 2016 at 00:46, Alan Gauld via Tutor wrote: > On 19/07/16 19:43, nitin chandra wrote: > >> Now I have taken a VPS, using command line, I installed apache2.4, >> python 2.7, but I am not able to use the same code with triple quotes >> (""") to open and close the code block. >> >> I am forced to use >> >> print "Content-type:text/html\r\n\r\n" >> print '' >> print '' > >> Do I need to do some thing to apache config such that I can use Triple >> quote to embed. > > Triple quotes should work anywhere Python works. > But first I'd check your python environment. > Which interpreter is actually running for example? > > Try > > import sys > ... > print " + "sys.version + "" > ... > > The 500 error sounds like an apache setup or maybe permissions > issue. Are your files readable/executable by Apache? > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > 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
Re: [Tutor] Writing decorators?
On 20/07/16 09:08, Michael Welle wrote: >> Don't be surprised, though, if the concept “replace the object >> referenced by ‘foo’ with a different object and discard the prior object >> at that reference“ is glossed to “change ‘foo’” in casual usage :-) > I'm a bit surprised to see that kind of sloppy use of language on a > Python list ;). But you are right, human language is imprecise. Its not really sloppy. In English change means alter and the function referenced by foo is altered by a decorator. For example we could reasonably say that foo = lambda x: x+1 creates a function called foo And we can therefore also say that foo = lambda x: x+2 changes the function foo. Decorators change the function in a similar way. It depends on whether you are referring to the function name or the function object. So the use of change is not necessarily sloppy but it could be imprecise. :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: 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] python cgi single double quotes
On 20/07/16 09:23, nitin chandra wrote: > vimal@Ubuntu-1404-trusty-64-minimal:~$ python > Python 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. import sys print sys.version > 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] > > Should I test the above from within html code too ? Yes, that's what I meant to imply by putting it inside markers: >> import sys >> ... >> print " + "sys.version + "" >> ... The idea is to make sure that the web server is running the same version of Python that you are. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: 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] python cgi single double quotes
On inserting the line ... print " + "sys.version + "" required slight correction print "" + sys.version + "" and the following script and its output are below :- #!/usr/bin/env python import sys import cgi import psycopg2 print "Content-type:text/html\r\n\r\n" print '' print '' print 'Hello Word - First CGI Program' print '' print '' print 'Hello Word! This is my first CGI program' print ""+ sys.version + "" print 'First name: ' print '' print '' --- Hello Word! This is my first CGI program 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] First name: On 20 July 2016 at 14:14, Alan Gauld via Tutor wrote: > On 20/07/16 09:23, nitin chandra wrote: > >> vimal@Ubuntu-1404-trusty-64-minimal:~$ python >> Python 2.7.6 (default, Jun 22 2015, 17:58:13) >> [GCC 4.8.2] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. > import sys > print sys.version >> 2.7.6 (default, Jun 22 2015, 17:58:13) >> [GCC 4.8.2] > >> >> Should I test the above from within html code too ? > > Yes, that's what I meant to imply by putting it > inside markers: > >>> import sys >>> ... >>> print " + "sys.version + "" >>> ... > > The idea is to make sure that the web server is > running the same version of Python that you are. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > 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
Re: [Tutor] python cgi single double quotes
nitin chandra wrote: > On inserting the line ... > > print " + "sys.version + "" > > required slight correction > > print "" + sys.version + "" > > and the following script and its output are below :- > > #!/usr/bin/env python > > import sys > import cgi > import psycopg2 > > print "Content-type:text/html\r\n\r\n" > print '' > print '' > print 'Hello Word - First CGI Program' > print '' > print '' > print 'Hello Word! This is my first CGI program' > print ""+ sys.version + "" > print 'First name: ' > print '' > print '' > > --- > > Hello Word! This is my first CGI program > > 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] > > First name: If you got that from your server like in the session below... $ cat first.py #!/usr/bin/env python import sys import cgi import psycopg2 print "Content-type:text/html\r\n\r\n" print '' print '' print 'Hello Word - First CGI Program' print '' print '' print 'Hello Word! This is my first CGI program' print ""+ sys.version + "" print 'First name: ' print '' print '' $ curl http://myhost/somewhere/first.py Hello Word - First CGI Program Hello Word! This is my first CGI program 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] First name: ... the following should also work: $ cat second.py #!/usr/bin/env python import sys import cgi import psycopg2 print """Content-type:text/html\r\n\r\n Hello Word - First CGI Program Hello Word! This is my first CGI program {version} First name: """.format(version=sys.version) $ curl http://myhost/somewhere/second.py Hello Word - First CGI Program Hello Word! This is my first CGI program 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] First name: If it does you can start looking for the actual problem. I don't expect it to have anything to do with your choice of quoting characters, as long as you write legal Python 2. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python cgi single double quotes
Ran both the method #!/usr/bin/env python import cgi import psycopg2 import sys print """Content-type:text/html\r\n\r\n""" print """ Hello Word - First CGI Program Hello Word! This is my first CGI program {version} First name: """.format(version=sys.version) and its output (below) nitin@nitin-Ideapad-Z570:~$ curl http://passtms.in/vimal.cgi Hello Word - First CGI Program Hello Word! This is my first CGI program 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] First name: nitin@nitin-Ideapad-Z570:~$ In the first instance also it the same : -- #!/usr/bin/env python import sys import cgi import psycopg2 print "Content-type:text/html\r\n\r\n" print '' print '' print 'Hello Word - First CGI Program' print '' print '' print 'Hello Word! This is my first CGI program' print ""+ sys.version + "" print 'First name: ' print '' print '' output (below):- nitin@nitin-Ideapad-Z570:~$ curl http://passtms.in/vimal.py Hello Word - First CGI Program Hello Word! This is my first CGI program 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] First name: On 20 July 2016 at 15:15, Peter Otten <__pete...@web.de> wrote: > nitin chandra wrote: > >> On inserting the line ... >> >> print " + "sys.version + "" >> >> required slight correction >> >> print "" + sys.version + "" >> >> and the following script and its output are below :- >> >> #!/usr/bin/env python >> >> import sys >> import cgi >> import psycopg2 >> >> print "Content-type:text/html\r\n\r\n" >> print '' >> print '' >> print 'Hello Word - First CGI Program' >> print '' >> print '' >> print 'Hello Word! This is my first CGI program' >> print ""+ sys.version + "" >> print 'First name: ' >> print '' >> print '' >> >> --- >> >> Hello Word! This is my first CGI program >> >> 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] >> >> First name: > > If you got that from your server like in the session below... > > $ cat first.py > #!/usr/bin/env python > > import sys > import cgi > import psycopg2 > > print "Content-type:text/html\r\n\r\n" > print '' > print '' > print 'Hello Word - First CGI Program' > print '' > print '' > print 'Hello Word! This is my first CGI program' > print ""+ sys.version + "" > print 'First name: ' > print '' > print '' > $ curl http://myhost/somewhere/first.py > > > > Hello Word - First CGI Program > > > Hello Word! This is my first CGI program > 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] > First name: > > > > ... the following should also work: > > $ cat second.py > #!/usr/bin/env python > > import sys > import cgi > import psycopg2 > > print """Content-type:text/html\r\n\r\n > > > Hello Word - First CGI Program > > > Hello Word! This is my first CGI program > {version} > First name: > > > """.format(version=sys.version) > $ curl http://myhost/somewhere/second.py > > > > Hello Word - First CGI Program > > > Hello Word! This is my first CGI program > 2.7.6 (default, Jun 22 2015, 17:58:13) > [GCC 4.8.2] > First name: > > > > If it does you can start looking for the actual problem. I don't expect it > to have anything to do with your choice of quoting characters, as long as > you write legal Python 2. > > ___ > 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
Re: [Tutor] Writing decorators?
Michael Welle writes: > so 'the function has changed' really means 'the reference has > changed'? Strange. Humans think in strage ways :-) Really, though, it shouldn't be too surprising. The *perception* is that the reference (a name, or an index in a sequence, or whatever) remains unchanged; at least, you still address the reference exactly the same way. ‘foo’ in the code remains ‘foo’. But what you get from that reference is different. So, because what I get when I refer to ‘foo’ is different after some operation than what it was prior to that operation, it is natural to speak loosely about “this operation has changed foo”. > If you hear 'function foo', do you think of the reference 'foo' or do > you think of the referenced thing, the function object? It might be > context dependent, but usually I think about the latter. It is normal for us to think of them as one, because in Python the *only* way to get an object is through some specific reference. Our natural language doesn't easily handle the separable but linked concepts. > But it might just be a language problem. Which is another way of saying thta it's a human thinking problem. Try not to have overly strict expectations of how people think about it, while also striving to express ourselves precisely. -- \ “Dvorak users of the world flgkd!” —Kirsten Chevalier, | `\rec.humor.oracle.d | _o__) | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python cgi single double quotes
nitin chandra wrote: > Ran both the method So everything seems to be working as expected. When you go back to your original script you can enable tracebacks rendered as html with #!/usr/bin/env python import cgitb cgitb.enable() ... # your code Provided there are no syntax errors in the script this should simplify detecting any exceptions the code may raise. If there is a traceback that doesn't make sense to you post it here to see if we can help. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing decorators?
On Wed, Jul 20, 2016 at 09:33:19AM +0200, Michael Welle wrote: > Somewhere in this thread (or the one talking about decorators after this > thread) it was said that a decorator 'changes a function'. I not a > native English speaker, so it could just be a language problem. But to > me it seems the function is replaced, not changed? It might have been me that used the term "changes a function". A decorator can do anything. It can replace the function with a completely new one, ignoring the original function. It can wrap the original function in a closure, returning the wrapper. (The wrapper then calls the original function.) It can turn the function into a class, or a class into a function, or return something altogether different. It can modify the function and return it, or cause some side-effect and then return the original function with no changes made. For example, here is a decorator that ensures that the function has a doc string, and inserts one if it doesn't: def ensure_docstring(func): if func.__doc__ is None: func.__doc__ = "Please see the Fine Manual for '%s'" % func.__name__ return func @ensure_docstring def myfunc(args): pass In this case, the function object is actually changed, not replaced. The most common form of decorator wraps the original function inside a new function, as a closure, and returns the wrapper: def decorate(func): @functools.wraps(func) def inner(*args): print("inside the wrapper") result = func(*args) print("original returns %r" % result) return result # can modify the result return inner Even though the decorator is returning a new function, the original is still hidden deep inside that "inner" function, as part of the closure, so in a sense, it is just a change to that original: it is *wrapped* in another function, which does some pre-processing or post-processing, but the original still does most of the work. Does that help explain matters? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python cgi single double quotes
Me a little embarrassed :P ... but now when I retyped the code ... it seems to be working Alan, Peter .. .Thank you. On 20 July 2016 at 17:24, Peter Otten <__pete...@web.de> wrote: > nitin chandra wrote: > >> Ran both the method > > So everything seems to be working as expected. When you go back to your > original script you can enable tracebacks rendered as html with > > #!/usr/bin/env python > import cgitb > cgitb.enable() > > ... # your code > > Provided there are no syntax errors in the script this should simplify > detecting any exceptions the code may raise. If there is a traceback that > doesn't make sense to you post it here to see if we can help. > > ___ > 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
Re: [Tutor] strange behavior of matrix**matrix
On Wed, Jul 20, 2016 at 10:27:50AM +0200, AB wrote: > Hello > > W dniu 2016-07-17 o 17:23, Steven D'Aprano pisze: > >[...] > >What result did you expect? 2**-1 as an int32 cannot be 0.5, as that's a > >float. > > I expected 0.5: as 2^(-1) is in fact 1/2, and as in Python 3 division of > two integers 1/2 produces float 0.5, I naturally expected the case of > arrays to be consistent with this behavior. Ah, but the exponentiation operator ** is not the division operator / and is not guaranteed to give the same results. It looks to me that numpy has array[ints]/int coerce to array[floats], but array[ints]**int remains an array[ints]. In that case, they have to choose between 2**-1 rounds down to 0 or rounds up to 1, and they chose rounding down. Would I make the same decision? Probably not. > >I'm not really sure about the rules that numpy uses for coercing from > >one type to another, but I'm not surprised by this result. I don't know > >if it is documented anywhere, but it seems like the sort of thing numpy > >would do. > > I'm only learning Python, so the behavior of version 3 is natural to me. That's why it was changed :-) > >Here's another similar example: > > > >py> np.array([0])**-1 > >__main__:1: RuntimeWarning: divide by zero encountered in power > >__main__:1: RuntimeWarning: invalid value encountered in power > >array([-2147483648]) > > In my eye it's not similar - 1/0 should always produce an error, so the > behavior is exactly as expected. The point is that the numpy functions seem to me to be designed to be as fast as possible, not as correct as possible. There's no integer value to represent INFINITY, like for floats, and for some reason the numpy people didn't want to halt the calculation with an error, so they have to return *something*, and it has to be a 32-bit signed integer: -2147483648 is 32 "1" bits (including the sign), so that makes a good error value, at least if you think like a C programmer. [...] > I thought that this may be a 'sin of omission', to be corrected in some > future versions of numpy/scipy. If you report it as a bug on the numpy bug tracker, they will hopefully either explain why they think its not a bug, or fix it. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing decorators?
On 20/07/16 14:30, Michael Welle wrote: > Now it gets interesting ;). Can you give me a hint on how to modify the > code of the function in a decorator or even give a small example, > please? Would I take the route with the bytecode attribute __code__ > (IIRC)? Or use the inspect module? Steven changed the function object by modifying the __doc__ attribute. It is probably possible to modify the __code__ too but I'd strongly recommend that you don't. It's very likely to result in something so "clever"/complex that it will never be maintainable, and maintainability beats nearly everything else in programming priorities. If you cannot do what you want by wrapping the original function you should probably just rewrite it to do what you want. Or, if you own the code, refactor it into chunks and combine the chunks into a new high level function. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: 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] Writing decorators?
On Wed, Jul 20, 2016 at 03:30:43PM +0200, Michael Welle wrote: > > It [a decorator] > > can modify the function and return it, > > Now it gets interesting ;). Can you give me a hint on how to modify the > code of the function in a decorator or even give a small example, > please? Would I take the route with the bytecode attribute __code__ > (IIRC)? Or use the inspect module? The inspect module is not really designed for changing objects, only for inspecting them. (Reading, not writing.) Function code objects are immutable, so you cannot change them in place, only replace them with a new code object: py> def f(): ... print("f") ... py> def g(): ... print("g") ... py> f() f py> f.__code__ = g.__code__ py> f() g The code object itself is very complex, and badly documented: py> help(f.__code__) Help on code object: class code(object) | code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring, |constants, names, varnames, filename, name, firstlineno, |lnotab[, freevars[, cellvars]]) | | Create a code object. Not for the faint of heart. so in practice the way to create them is by actually defining a function, then extracting its __code__ object. But if you're going to do that, why not just use the function? There are a couple of projects designed to let you manipulate the byte-code of functions, but because the byte-code format is not part of the public Python API, it tends to change from version to version. If you use the wrong byte-code, you can crash the interpeter and cause a segmentation fault or core dump. However, there is some talk about creating a interface to modify a function's abstract syntax tree. At the moment consider that to be just talk, but its more likely than a supported interface to edit byte-code. But for those brave, or silly, enough, here are some resources for editing byte-code to get you started: http://www.voidspace.org.uk/python/articles/code_blocks.shtml https://wiki.python.org/moin/ByteplayDoc There are ways to modify functions apart from changing their code. For example, you can change argument defaults, add attributes to the function object, change the global namespace that the function works with. I have an experimental project that modifies functions so that instead of searching for variables in this order: locals nonlocals globals builtins it uses: locals nonlocals custom namespace set by the user globals builtins (Technically, I don't "modify" the function, since some parts of the function are immutable and cannot be changed. Instead I replace it with a new function, copied from the original but with a slight modification.) If you are interested in that, see the discussion that starts here: https://mail.python.org/pipermail/python-list/2016-July/711177.html The original version of the code I gave uses a metaclass; the version I have now also supports being called as a decorator. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python programmin problem
On 20/07/16 22:11, monik...@netzero.net wrote: > ... if not in python, then in pseudo code. The first question to ask is can you do it without a computer? In other words given input [1,7,2,3,5,4,6] Can you first of all produce a list of all valid runs? [1,2,3,5,6] and [1,2,3,4,6] both have length 5 I also see shorter runs: [7] and [4,6] for example. Can you manually create a list of all valid runs? Once you can do that can you write a program to generate that as a list of lists in Python? If so then the answer to your question is a matter of finding the length of the longest valid run which should be fairly easy. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: 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] python programmin problem
On 21/07/16 00:14, monik...@netzero.net wrote: > IM not able to figure out algorithm to find the runs. > Here is the code I have: OK, Forget about code for now. just focus on what is being asked. > > The first question to ask is can you do it without a computer? > > In other words given > > > > input [1,7,2,3,5,4,6] > > > > Can you first of all produce a list of all valid runs? Lets try it manually. Start with 1 run = [] 1 > run[-1] so add it to the run -> [1] 1 is followed by 7 which >run[-1] so add it to the run -> [1,7] 7 is followed by 2 which [1] 2 is now greater than run[-1] so add it to the run -> [1,2] 2 is followed by 3 which is > run[-1] so add it to the run -> [1,2,3] 3 is followed by 5 which is > run[-1] so add it to the run -> [1,2,3,5] 5 is followed by 4 which is run[-1] so add it to the run -> [1,2,3,4] 4 is followed by 6 which is > run[-1] so add it to the run -> [1,2,3,4,6] 6 is not followed by anything, run complete. Can you see an algorithm there that you can code? Its not quite complete because it never catches the [1,2,3,5,6] case but its a start. And there are probably more efficient algorithms too, but we are interested in easy to code here not speed. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] python programmin problem
Hi: Can somebody please provide answer to following python programming question? I have spent days on it and cannot come up with any code that would make any sense for it so I cannot provide you any code. But I would appreciate the answer very much, if not in python, then in pseudo code. Thank you very much Monika Here is the problem: Given a list of integers, return the progress of the test The progress is defined as the length of the longest run of strictly increasing scores "with gaps allowed". That is the length of the longest group of numbers such that each number in that block is strictly larger than the previous number, and the group can be formed by choosing some subset of values in the order they appear in the original list. All values are integers. input [1,7,2,3,5,4,6] returns 5 [1,2,3,5,6] and [1,2,3,4,6] both have length 5 and are longer than any other runs Returning 7 is incorrect for [1,2,3,4,5,6,7] as it changes the ordering of the original sequence. Input [1,2,3,4] the run [1,2,3,4] is the entire test and has lenght 4. returns 4 Input [4, 3, 2, 1] returns 1 each result is a run of 1. so we return 1 Input [1,2,0,4,5] return 5 longest run [1,2,4,5] has lenght 4 Affordable Wireless Plans Set up is easy. Get online in minutes. Starting at only $9.95 per month! www.netzero.net?refcd=nzmem0216 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python programmin problem
IM not able to figure out algorithm to find the runs. Here is the code I have: def ProgressCalc(items): counts = [items[0]] for i in range(1, len(items)-1): print "for loop", items[i], items[i + 1] if counts[- 1] < items[i]: counts += [items[i]] print "inside", items[i], items[i - 1], counts print counts counts = [items[0]] for i in range(1, len(items) - 1): print "for loop", items[i], items[i + 1] if counts[- 1] <= items[i] and items[i] < items[i + 1]: counts += [items[i]] print "inside", items[i], items[i - 1], counts elif counts[- 1] <= items[i] and items[i] > items[i + 1]: counts += [items[i]] i += 2 print counts ProgressCalc(items) -- Original Message -- From: Alan Gauld To: "monik...@netzero.net" Cc: tutor@python.org Subject: Re: python programmin problem Date: Thu, 21 Jul 2016 00:11:24 +0100 On 20/07/16 22:11, monik...@netzero.net wrote: > ... if not in python, then in pseudo code. The first question to ask is can you do it without a computer? In other words given input [1,7,2,3,5,4,6] Can you first of all produce a list of all valid runs? [1,2,3,5,6] and [1,2,3,4,6] both have length 5 I also see shorter runs: [7] and [4,6] for example. Can you manually create a list of all valid runs? Once you can do that can you write a program to generate that as a list of lists in Python? If so then the answer to your question is a matter of finding the length of the longest valid run which should be fairly easy. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos Affordable Wireless Plans Set up is easy. Get online in minutes. Starting at only $9.95 per month! www.netzero.net?refcd=nzmem0216 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python programmin problem
On Wed, Jul 20, 2016 at 2:11 PM, monik...@netzero.net wrote: > Hi: > Can somebody please provide answer to following python programming question? > I have spent days on it and cannot come up with any code that would make any > sense for it so I cannot provide you any code. But I would appreciate the > answer very much, if not in python, then in pseudo code. > Thank you very much > Monika > > Here is the problem: > Given a list of integers, return the progress of the test > The progress is defined as the length of the longest run of strictly > increasing scores "with gaps allowed". If I understand the question being asked, this is often presented as an exercise for the technique of "dynamic programming". https://en.wikipedia.org/wiki/Dynamic_programming However, your question hasn't used the term "dynamic programming". Are you familiar with this term? If so, have you been exposed to other problems that can be solved with "dynamic programming"? Your problem is a rough restatement of the "longest increasing subsequence" problem. https://en.wikipedia.org/wiki/Longest_increasing_subsequence Unfortunately, solving this problem is a bit out of scope for Python tutor because it's more advanced than the material we typically talk about here (basic Python programming). I don't think we can help very much. You may get better help by talking with your instructor or study group. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help me out please
On Tue, Jul 19, 2016 at 4:31 AM, Marc Sànchez Quibus wrote: > Hi, > First of all I'm gonan introduce myself. My name is Marc and I'm a student > and also a python's programmer begginer. I've been studying/learning python > and now I need some help to finish my project. > I have two scripts, one of them in python (the main script) and the other > one written in html. Well, is just a brief javascript (an app) that I took > by Github. I need to change a variable inside this html script. I was > wondering wether I could change this variable from my python script or not. > Is there some way to do it? Hi Marc, Yes. You might want to read something like this to get some background. Phil Greenspun's Guide to Web Publishing: http://philip.greenspun.com/panda/ Specifically, the chapter "Sites that are really progarms". http://philip.greenspun.com/panda/server-programming You mentioned that you have two scripts, one in Python and the other in HTML. A web site can be seen as this: something that (1) takes in a web request sent by a browser, and (2) spits out a web response. A static web site takes in a web request, looks for an appropriate file, and prints that file back as a web response. But that's not the only way we can build web responses. A programmatic web site can take that request and *generate* a web page on the fly. A web site can actually be a program: not just a plain text file. There are a lot of resources to teach how to write programs that serve web sites. Another by the same author is http://philip.greenspun.com/seia/, which goes into a lot more detail. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor