mersenneforum.org  

Go Back   mersenneforum.org > Fun Stuff > Puzzles

Reply
 
Thread Tools
Old 2022-04-27, 14:54   #1
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

11001100111102 Posts
Question 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)
(inf-infj)
(inf+nanj)
(-inf+infj)
(-inf-infj)
(-inf+nanj)
(nan+infj)
(nan-infj)
(nan+nanj)
>>>
This puzzle would be too easy if things like functions or loops were permitted, so they aren't allowed. That means no print or for etc.

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
>>>
Trying to use strings directly won't help because of the extra '.
Code:
>>> '(inf+infj)'
'(inf+infj)'
retina is offline   Reply With Quote
Old 2022-04-27, 17:27   #2
uau
 
Jan 2017

149 Posts
Default

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...
uau is offline   Reply With Quote
Old 2022-04-27, 18:02   #3
Gimarel
 
Apr 2010

22810 Posts
Default

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'))
Gimarel is offline   Reply With Quote
Old 2022-04-27, 19:00   #4
nordi
 
Dec 2016

23×3×5 Posts
Default

Quote:
Originally Posted by retina View Post
This puzzle would be too easy if things like functions or loops were permitted, so they aren't allowed. That means no print or for etc.
In Python 2.7, "print" is actually not a function, it is a statement, i.e. a part of the language's grammar. Python 3 changed "print()" to be a function that just happens to be defined by default.

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'>



nordi is offline   Reply With Quote
Old 2022-04-27, 21:26   #5
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

199E16 Posts
Default

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:
Originally Posted by Gimarel View Post
I don't know much about python. Just from reading your post, is this the intended solution:
complex is a function (or statement thingy).

Quote:
Originally Posted by nordi View Post
In Python 2.7, "print" is actually not a function, it is a statement, i.e. a part of the language's grammar. Python 3 changed "print()" to be a function that just happens to be defined by default.

This works with Python 2 and 3:
starmap and some other things you used are functions.

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
retina is offline   Reply With Quote
Old 2022-04-27, 21:38   #6
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

2·3·1,093 Posts
Default

Quote:
Originally Posted by uau View Post
You say that print() is not permitted. Then what is allowed?
Anything without ( and ), and not a loop construct.
Quote:
Originally Posted by uau View Post
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...
That is not correct.
retina is offline   Reply With Quote
Old 2022-04-27, 21:48   #7
nordi
 
Dec 2016

7816 Posts
Default

Quote:
Originally Posted by retina View Post
A function is something that needs parentheses ( and ).
In your initial post, you suggested that people use float('nan') to get a NaN value, but now you say that's forbidden. Please make up your mind.
nordi is offline   Reply With Quote
Old 2022-04-27, 21:52   #8
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

655810 Posts
Default

Quote:
Originally Posted by nordi View Post
In your initial post, you suggested that people use float('nan') to get a NaN value, but now you say that's forbidden. Please make up your mind.
float is not allowed because of the parenthesis. I said "Hope that helps" which means it won't help.
retina is offline   Reply With Quote
Old 2022-04-28, 07:37   #9
Gimarel
 
Apr 2010

22×3×19 Posts
Default

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
Gimarel is offline   Reply With Quote
Old 2022-04-28, 07:50   #10
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

2·3·1,093 Posts
Thumbs up

Quote:
Originally Posted by Gimarel View Post
Ok, your definition is still not good enough because you can use print without parenthesis, at least in python2.
But the code must work for all 2.7 and up, so I think that covers it.
Quote:
Originally Posted by Gimarel View Post
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
You are better than any of my minions. Well done.

I guess it wasn't as hard as I anticipated. :(
retina is offline   Reply With Quote
Old 2022-04-29, 06:57   #11
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

2×3×1,093 Posts
Default

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
retina is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
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

All times are UTC. The time now is 16:15.


Sat Aug 13 16:15:08 UTC 2022 up 37 days, 11:02, 2 users, load averages: 1.45, 1.17, 1.10

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.

≠ ± ∓ ÷ × · − √ ‰ ⊗ ⊕ ⊖ ⊘ ⊙ ≤ ≥ ≦ ≧ ≨ ≩ ≺ ≻ ≼ ≽ ⊏ ⊐ ⊑ ⊒ ² ³ °
∠ ∟ ° ≅ ~ ‖ ⟂ ⫛
≡ ≜ ≈ ∝ ∞ ≪ ≫ ⌊⌋ ⌈⌉ ∘ ∏ ∐ ∑ ∧ ∨ ∩ ∪ ⨀ ⊕ ⊗ 𝖕 𝖖 𝖗 ⊲ ⊳
∅ ∖ ∁ ↦ ↣ ∩ ∪ ⊆ ⊂ ⊄ ⊊ ⊇ ⊃ ⊅ ⊋ ⊖ ∈ ∉ ∋ ∌ ℕ ℤ ℚ ℝ ℂ ℵ ℶ ℷ ℸ 𝓟
¬ ∨ ∧ ⊕ → ← ⇒ ⇐ ⇔ ∀ ∃ ∄ ∴ ∵ ⊤ ⊥ ⊢ ⊨ ⫤ ⊣ … ⋯ ⋮ ⋰ ⋱
∫ ∬ ∭ ∮ ∯ ∰ ∇ ∆ δ ∂ ℱ ℒ ℓ
𝛢𝛼 𝛣𝛽 𝛤𝛾 𝛥𝛿 𝛦𝜀𝜖 𝛧𝜁 𝛨𝜂 𝛩𝜃𝜗 𝛪𝜄 𝛫𝜅 𝛬𝜆 𝛭𝜇 𝛮𝜈 𝛯𝜉 𝛰𝜊 𝛱𝜋 𝛲𝜌 𝛴𝜎𝜍 𝛵𝜏 𝛶𝜐 𝛷𝜙𝜑 𝛸𝜒 𝛹𝜓 𝛺𝜔