Sinisterly
[Golfing] Collatz Conjecture - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: [Golfing] Collatz Conjecture (/Thread-Golfing-Collatz-Conjecture)

Pages: 1 2 3 4


RE: [Golfing] Collatz Conjecture - Equinox - 08-21-2015

(08-21-2015, 04:26 PM)Shebang Wrote: You're lucky Stuck has no while-loops ;P

Maybe you should hop on that. Wink

Also props to you, your little code golf actually taught me something about Python. Thankies to you. Smile


RE: [Golfing] Collatz Conjecture - Jolly - 08-21-2015

(08-21-2015, 04:44 PM)Shebang Wrote: Well, it is a code-golf ;P I'll un-golf a little and explain it though!

Taking input (obviously)
Code:
n=input()

Neat trick using complement and negation, ~-n is equivalent to n-1. Means I can remove the space between while and that. 0 is falsy, and 1-1 is zero, so once this hits one it will stop executing.
Code:
while~-n:

This is using a trick with list indexing. n%2 will either return a 0 or 1, so I make a list of length 2 which has what I want to do based on that. An even number will return 0, so the 0th element should be n/2. An odd number will return 1, so the 1th element should be 3*n+1.
Code:
n=[n/2,3*n+1][n%2]

Pretty obvious, prints whatever n is at.
Code:
print n

Also, @Eclipse, my score is 45 now Wink

This is quite clever to be honest!
However, why doesn't it get an error since Input() returns a string value, but you are operating with integers.


RE: [Golfing] Collatz Conjecture - Eclipse - 08-21-2015

(08-21-2015, 08:46 PM)Jolly Wrote: This is quite clever to be honest!
However, why doesn't it get an error since Input() returns a string value, but you are operating with integers.

Input doesn't require any arguments. That line takes input from the user and assigns it to n. Also, input has differences in python 2 and 3.


RE: [Golfing] Collatz Conjecture - Jolly - 08-21-2015

(08-21-2015, 08:48 PM)eclipse Wrote: Input doesn't require any arguments. That line takes input from the user and assigns it to n. Also, input has differences in python 2 and 3.

I see, thanks for that list indexing tip though, learned something new today Biggrin


RE: [Golfing] Collatz Conjecture - lux - 08-21-2015

Poor little JavaScript code being ignored...


RE: [Golfing] Collatz Conjecture - Eclipse - 08-21-2015

(08-21-2015, 08:58 PM)Lux Wrote: Poor little JavaScript code being ignored...

If you choose the JavaScript path, you walk down it alone.

(08-21-2015, 08:57 PM)Jolly Wrote: I see, thanks for that list indexing tip though, learned something new today Biggrin

No problem. It was Shebang who posted the indexing tip, not me.


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

(08-21-2015, 08:46 PM)Jolly Wrote: This is quite clever to be honest!
However, why doesn't it get an error since Input() returns a string value, but you are operating with integers.

So, here's the difference:

In Python 2, we have raw_input(), which reads a string. This is equivalent to Python 3's input(). In Python 2, we have input() too. However, ours essentially performs the operation eval(raw_input()), which interprets the string given and tries to figure out it's type, and casts the value to that. So, with input() in Python 2, you can interpret pretty much any type that exists in Python as input! You can replicate it in Python 3 with eval(input()), but at that point you may as well be using the appropriate name if it's shorter, like int(input()).


RE: [Golfing] Collatz Conjecture - Shebang - 08-21-2015

(08-21-2015, 07:59 PM)Stocking Wrote: Maybe you should hop on that. Wink

Also props to you, your little code golf actually taught me something about Python. Thankies to you. Smile

Done! Biggrin

This doesn't count for anything since I added it after the challenge started though Tongue

Code:
i"1>""_2%;_3*1+;2/?p"h



RE: [Golfing] Collatz Conjecture - Eclipse - 08-21-2015

(08-21-2015, 10:45 PM)Shebang Wrote: Done! Biggrin

This doesn't count for anything since I added it after the challenge started though Tongue

Code:
i"1>""_2%;_3*1+;2/?p"h

I'm thinking of adding stuck to my list of languages to learn. After C (in progress) and possibly Go.


RE: [Golfing] Collatz Conjecture - lux - 08-21-2015

(08-21-2015, 10:55 PM)eclipse Wrote: I'm thinking of adding stuck to my list of languages to learn. After C (in progress) and possibly Go.

+rep @lux for telling you about Go.