-7
\$\begingroup\$

Just for fun, I am trying to convert the following Python

from math import *

print map(lambda count: ceil( 6 * cos((count + i) * pi / 5) ), range(100))

Which yields something like this

[2.0, -1.0, -4.0, -6.0, -4.0, -1.0, 2.0, 5.0, 6.0, 5.0, 2.0, -1.0, -4.0, -6.0, -4.0, -1.0, 2.0, 5.0, 6.0, 5.0, 2.0, -1.0, -4.0, -6.0, -4.0, -1.0, 2.0, 5.0, 6.0, 5.0, 2.0, -1.0, -4.0, -6.0, -4.0, -1.0, 2.0, 5.0, 6.0, 5.0, 2.0, -1.0, -4.0, -6.0, -4.0, -1.0, 2.0, 5.0, 6.0, 5.0]

To Ruby. This is what I've got:

(0..50).select { |c| (6 * Math::sin(x * Math::PI/5)).ceil }

The following works, but the numbers are still rounded to integers.

\$\endgroup\$

closed as off-topic by Flambino, 23fc9a62-56de-47fb-97b4-737890, SirPython, Caridorc, 200_success Dec 26 '15 at 23:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Flambino, 23fc9a62-56de-47fb-97b4-737890, SirPython, Caridorc, 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Look at your code again. You're not doing the same operations in the two versions. In one you multiply the result of cos by 6; in the other you multiply the argument by 6. In one you add 3; in the other add i. And in one you multiply by pi/5; in the other you add pi/5. \$\endgroup\$ – Flambino Dec 26 '15 at 21:23
  • \$\begingroup\$ You are right. I made a crucial silly mistake. \$\endgroup\$ – Jorge Bucaran Dec 26 '15 at 21:32
  • 3
    \$\begingroup\$ Sorry, but broken code is off-topic here. \$\endgroup\$ – SirPython Dec 26 '15 at 21:57
  • 1
    \$\begingroup\$ @JorgeBucaran Even after fixing the typo (which you did after receiving answers, and that's also a no-no), the code still doesn't produce the result you want. Ergo, it's by definition "broken code" and thus off-topic. It's even more broken now, as you reference a variable x that doesn't exist. If x is just intended as a placeholder, your code is instead "hypothetical or stub code" - which is also off-topic. \$\endgroup\$ – Flambino Dec 26 '15 at 22:18
  • 1
    \$\begingroup\$ Correct. Code that is ready for a peer review is code that already works as intended; one could think of it as "I just committed / I'm about to commit this code, it does xyz, is there anything I could improve?" - we define broken code as "code that doesn't work as intended", because if you have a question about a specific issue with your code, it could probably be worded as a good Stack Overflow question. \$\endgroup\$ – Mathieu Guindon Dec 27 '15 at 4:14
4
\$\begingroup\$

Selecting Items from an Array¶ ↑

Elements can be selected from an array according to criteria defined in a block.

Collect - Invokes the given block once for each element of self.

Use collect

(0..100).collect{|x| (6 * Math::cos(x) * Math::PI/5).ceil()}
\$\endgroup\$
  • 6
    \$\begingroup\$ Good job on your first answer! However, in the future, please refrain from answering off-topic questions. \$\endgroup\$ – SirPython Dec 26 '15 at 21:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.