![]() |
![]() |
#1 |
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·3·5·7·31 Posts |
![]()
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) (inf-infj) (inf+nanj) (-inf+infj) (-inf-infj) (-inf+nanj) (nan+infj) (nan-infj) (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)' |
![]() |
![]() |
![]() |
#2 |
Jan 2017
2×73 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...
|
![]() |
![]() |
![]() |
#3 |
Apr 2010
22·3·19 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')) |
![]() |
![]() |
![]() |
#4 | |
Dec 2016
23×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'> ![]() |
|
![]() |
![]() |
![]() |
#5 | ||
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·3·5·7·31 Posts |
![]()
I may have mis-characterised 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 2022-04-27 at 21:34 |
||
![]() |
![]() |
![]() |
#6 | |
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2×3×5×7×31 Posts |
![]()
Anything without ( and ), and not a loop construct.
Quote:
|
|
![]() |
![]() |
![]() |
#7 |
Dec 2016
7816 Posts |
![]() |
![]() |
![]() |
![]() |
#8 |
Undefined
"The unspeakable one"
Jun 2006
My evil lair
145568 Posts |
![]() |
![]() |
![]() |
![]() |
#9 |
Apr 2010
22×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;1e400-1e400j;1e400+1e400j-1e400j;-1e400+1e400j;-1e400-1e400j;-1e400+1e400j-1e400j;1e400-1e400+1e400j;1e400-1e400-1e400j;1e400-1e400+1e400j-1e400j Last fiddled with by Gimarel on 2022-04-28 at 07:39 |
![]() |
![]() |
![]() |
#10 | ||
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2×3×5×7×31 Posts |
![]() Quote:
Quote:
![]() I guess it wasn't as hard as I anticipated. :( |
||
![]() |
![]() |
![]() |
#11 |
Undefined
"The unspeakable one"
Jun 2006
My evil lair
2·3·5·7·31 Posts |
![]()
Another possible solution I had was this:
1e666+1e666j;1e666-1e666j;1e666j*-1j;-1e666+1e666j;-1e666-1e666j;1e666j*1j;1e666+1e666*1j;1e666-1e666*1j;1e666-1e666j*-1j It shows some weird occurrences that Python arithmetic exhibits. Last fiddled with by retina on 2022-04-29 at 07:07 Reason: Some bad analysis |
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Complexity of Chinese Remainder Theorem | carpetpool | Miscellaneous Math | 4 | 2017-02-09 19:26 |
What is the time complexity of base conversion? | Mr. P-1 | Computer Science & Computational Number Theory | 5 | 2013-04-02 15:47 |
Complexity analysis of 3 tests | kurtulmehtap | Math | 10 | 2013-03-20 14:15 |
Complexity of LLT | T.Rex | Math | 9 | 2007-05-29 21:15 |
complexity of Pepin's test | ixfd64 | Math | 14 | 2005-12-01 22:50 |