Login Register


[Golfing] Collatz Conjecture filter_list
Author
Message
RE: [Golfing] Collatz Conjecture #11
(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
[Image: BXqGARG.png]

Reply

RE: [Golfing] Collatz Conjecture #12
(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.


[Image: tumblr_noac9s6rgw1tvnnaxo1_500.gif]
Tik Tak~! Time is up~!

Reply

RE: [Golfing] Collatz Conjecture #13
(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.

Reply

RE: [Golfing] Collatz Conjecture #14
(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


[Image: tumblr_noac9s6rgw1tvnnaxo1_500.gif]
Tik Tak~! Time is up~!

Reply

RE: [Golfing] Collatz Conjecture #15
Poor little JavaScript code being ignored...

Reply

RE: [Golfing] Collatz Conjecture #16
(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.

Reply

RE: [Golfing] Collatz Conjecture #17
(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()).
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #18
(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
[Image: CDUAq9d.png]

Reply

RE: [Golfing] Collatz Conjecture #19
(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.

Reply

RE: [Golfing] Collatz Conjecture #20
(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.

Reply







Users browsing this thread: 1 Guest(s)