Printing a Chunk Of Words

Peter Otten __peter__ at web.de
Wed Sep 27 09:48:12 EDT 2017


Matt Wheeler wrote:

> With deepest apologies to all involved...
> 
> On Tue, 26 Sep 2017 at 08:42 Gregory Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
> 
>> Ben Bacarisse wrote:
>> > Think functional!  This is 257 characters:
>>
>> 250 chars, 17 shorter than the text it produces:
>>
>> a=[];o=[];n=[];A=list.append
>> for b in range(3,-1,-1):
>>   x=bool(b>>1);y=bool(b&1);A(a,"%s and %s is %s"%(x,y,x and y));A(o,"%s
>>   or
>> %s is
>> %s"%(x,y,x or y))
>>   if x:A(n,"not %s is %s"%(y,not y))
>> print("    Boolean Operators\n"+"-"*24+"\n"+"\n".join(a+o+n))
>>
> 
> Cutting the same (quite reasonable) corners as you, I've got it down 212
> characters:
> 
> t,f,a,o,n='True','False','and','or','not'
> l=["     Boolean Operators\n"+"-"*24]
> for x in [(l,p,r)for p in(a,o)for l in(t,f)for r
> in(t,f)]+[(n,t),(n,f)]:x=' '.join(x);l+=[x[0].upper()+x[1:]+" is
> "+str(eval(x))] for i in 12,9,5,0:l.insert(i,'')
> print('\n'.join(l))
> 
> Reproducing the original string exactly the best I've managed is 260:
> 
> t,f,a,o,n='True','False','and','or','not'
> l=["     Boolean Operators\n"+"-"*24]
> for x in [(l,p,r)for p in(a,o)for l in(t,f)for r
> in(t,f)]+[(n,t),(n,f)]:x=' '.join(x);l+=[x[0].upper()+x[1:]+" is
> "+str(eval(x))] for i in 12,9,5,0:l.insert(i,'')
> print('\n'.join(l))
> 

That's a bit long, don't you think, as it can be beaten even by plain old 
zipping:

$ cat booltable2.py
from codecs 
import*;print(decode(decode(b'eJzjUgABp/z8nNTEPAX/gtSixJL8omIuXRwArFyBK6SoNFUhMS9FAczILAbTCFG3xJxisDCYwQXh\nIitHF0fTADEpvwiL8UBBuGKwKISHrhYuim6yX34JmitAIqhGcgEAEnJWfA==\n',"base64"),"zip").decode())$ 
$ wc -c booltable2.py
210 booltable2.py
$ python3 booltable2.py 

     Boolean Operators
------------------------      
True and True is True
True and False is False
False and True is False
False and False is False

True or True is True
True or False is True
False or True is True
False or False is False

Not True is False
Not False is True


$ 

:)




More information about the Python-list mailing list