20220427, 14:54  #1 
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·17·193 Posts 
Python inf & nan complexity
Write a single line input for Python (2.7 and up) that can output all nine possibilities of ±inf and nan as a complex number.
That is, the output should look like this: Code:
Python 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> <your line goes here> (inf+infj) (infinfj) (inf+nanj) (inf+infj) (infinfj) (inf+nanj) (nan+infj) (naninfj) (nan+nanj) >>> For those that aren't familiar, complex numbers in python can be entered like this: 3+4j and Python displays them in the same format surrounded by parentheses. But it isn't possible to simply enter inf+infj or nan+nanj those will show an error. And in case you might be wondering, you can define both inf and nan like this: float('inf') and float('nan'). Hope that helps. Code:
>>> 3+4j (3+4j) >>> inf+infj Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'inf' is not defined >>> nan+nanj Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'nan' is not defined >>> float('inf') inf >>> float('inf'); float('nan') inf nan >>> Code:
>>> '(inf+infj)' '(inf+infj)' 
20220427, 17:27  #2 
Jan 2017
95_{16} Posts 
You say that print() is not permitted. Then what is allowed? If the only allowed thing is to return some object and have Python display it in the default manner, that does not seem to enable the desired kind of output. It looks like 9 distinct objects on separate lines (not a list or tuple of objects), and you can't have 9 independent return values from a single line...

20220427, 18:02  #3 
Apr 2010
E4_{16} Posts 
I don't know much about python. Just from reading your post, is this the intended solution:
complex(float('inf'),float('inf'));complex(float('inf'),float('inf'));complex(float('inf'),float('nan'));complex(float('inf'),float('inf'));complex(float('inf'),float('inf'));complex(float('inf'),float('nan'));complex(float('nan'),float('inf'));complex(float('nan'),float('inf'));complex(float('nan'),float('nan')) 
20220427, 19:00  #4  
Dec 2016
2^{3}·3·5 Posts 
Quote:
This works with Python 2 and 3: import itertools as i;x=1e999,+1e999,float("nan");tuple(i.starmap(complex,i.product(x,x))) None of these is a function, btw: >>> type(i.starmap) <type 'type'> >>> type(i.product) <type 'type'> 

20220427, 21:26  #5  
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·17·193 Posts 
I may have mischaracterised what a function is, but for this purpose I define it as:
A function is something that needs parentheses ( and ). So I stated print explicitly, but this also includes complex and product etc. Quote:
Quote:
A solution should work in Python 2.7 and up so even if print is a statement in 2.7, it would be a function in 3.0+ so print is not going to work. That would be just too easy anyway. Easy puzzles are not fun. Last fiddled with by retina on 20220427 at 21:34 

20220427, 21:38  #6  
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·17·193 Posts 
Anything without ( and ), and not a loop construct.
Quote:


20220427, 21:48  #7 
Dec 2016
2^{3}·3·5 Posts 

20220427, 21:52  #8 
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2×17×193 Posts 

20220428, 07:37  #9 
Apr 2010
2^{2}·3·19 Posts 
Ok, your definition is still not good enough because you can use print without parenthesis, at least in python2.
But I think you want something like this: 1e400+1e400j;1e4001e400j;1e400+1e400j1e400j;1e400+1e400j;1e4001e400j;1e400+1e400j1e400j;1e4001e400+1e400j;1e4001e4001e400j;1e4001e400+1e400j1e400j Last fiddled with by Gimarel on 20220428 at 07:39 
20220428, 07:50  #10  
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2×17×193 Posts 
Quote:
Quote:
I guess it wasn't as hard as I anticipated. :( 

20220429, 06:57  #11 
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2×17×193 Posts 
Another possible solution I had was this:
1e666+1e666j;1e6661e666j;1e666j*1j;1e666+1e666j;1e6661e666j;1e666j*1j;1e666+1e666*1j;1e6661e666*1j;1e6661e666j*1j It shows some weird occurrences that Python arithmetic exhibits. Last fiddled with by retina on 20220429 at 07:07 Reason: Some bad analysis 
Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Complexity of Chinese Remainder Theorem  carpetpool  Miscellaneous Math  4  20170209 19:26 
What is the time complexity of base conversion?  Mr. P1  Computer Science & Computational Number Theory  5  20130402 15:47 
Complexity analysis of 3 tests  kurtulmehtap  Math  10  20130320 14:15 
Complexity of LLT  T.Rex  Math  9  20070529 21:15 
complexity of Pepin's test  ixfd64  Math  14  20051201 22:50 