mersenneforum.org Python inf & nan complexity
 User Name Remember Me? Password
 Register FAQ Search Today's Posts Mark Forums Read

 2022-04-27, 14:54 #1 retina Undefined     "The unspeakable one" Jun 2006 My evil lair 22×13×127 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. >>> (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 "", line 1, in NameError: name 'inf' is not defined >>> nan+nanj Traceback (most recent call last): File "", line 1, in 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)'
 2022-04-27, 17:27 #2 uau   Jan 2017 2×3×52 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...
 2022-04-27, 18:02 #3 Gimarel   Apr 2010 22·59 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'))
2022-04-27, 19:00   #4
nordi

Dec 2016

23×3×5 Posts

Quote:
 Originally Posted by retina 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'>

2022-04-27, 21:26   #5
retina
Undefined

"The unspeakable one"
Jun 2006
My evil lair

22·13·127 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:
 Originally Posted by Gimarel 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 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

2022-04-27, 21:38   #6
retina
Undefined

"The unspeakable one"
Jun 2006
My evil lair

147148 Posts

Quote:
 Originally Posted by uau You say that print() is not permitted. Then what is allowed?
Anything without ( and ), and not a loop construct.
Quote:
 Originally Posted by uau 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.

2022-04-27, 21:48   #7
nordi

Dec 2016

12010 Posts

Quote:
 Originally Posted by retina 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.

2022-04-27, 21:52   #8
retina
Undefined

"The unspeakable one"
Jun 2006
My evil lair

11001110011002 Posts

Quote:
 Originally Posted by nordi 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.

 2022-04-28, 07:37 #9 Gimarel   Apr 2010 22·59 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
2022-04-28, 07:50   #10
retina
Undefined

"The unspeakable one"
Jun 2006
My evil lair

19CC16 Posts

Quote:
 Originally Posted by Gimarel 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 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. :(

 2022-04-29, 06:57 #11 retina Undefined     "The unspeakable one" Jun 2006 My evil lair 22·13·127 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

 Similar Threads Thread Thread Starter Forum Replies Last Post carpetpool Miscellaneous Math 4 2017-02-09 19:26 Mr. P-1 Computer Science & Computational Number Theory 5 2013-04-02 15:47 kurtulmehtap Math 10 2013-03-20 14:15 T.Rex Math 9 2007-05-29 21:15 ixfd64 Math 14 2005-12-01 22:50

All times are UTC. The time now is 11:19.

Tue Oct 4 11:19:31 UTC 2022 up 47 days, 8:48, 0 users, load averages: 1.32, 1.28, 1.23

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.

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