-3

How from array with numbers:

a = [4,1,3]

make array:

s = ["****","*","***"]

So each number corresponds to amount of stars in a cell

  • This question doesn't show any effort on the OP's part to attempt to learn how to answer the question himself. The question asks for a coding solution but does not clearly specify what language to use other than in the tags. If this should be posted at all, it might be more appropriate at http://codegolf.stackexchange.com/ – mkrufky Jun 11 '16 at 19:27
  • 1
    Lack of effort is not a valid close reason. For that reason, I'm voting to leave open. See also [A Close Vote Is Not a Super Downvote](http://meta.gamedev.stackexchange.com/questions/2115/a-close-vote-is-not-a-super-downvote) – Wayne Conrad Jun 11 '16 at 22:25

2 Answers2

4

You can use map like so:

a = [4,1,3]
s = a.map { |count| '*' * count }
#=> ["****", "*", "***"]
spickermann
  • 100,941
  • 9
  • 101
  • 131
Surya
  • 15,703
  • 3
  • 51
  • 74
  • It can also be written as `a.map(&?*.method(:*))` (see http://stackoverflow.com/questions/23695653/can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby) – Mladen Jablanović Jun 11 '16 at 16:48
  • 1
    @MladenJablanović : Yes, it can be. However, it will look a bit complicated to someone who is new to Ruby. As it will require explanation of `?*` which will be char string and then calling multiplication method within `map` which would be applied on the element/value on a particular iteration. So, keeping it simple. Thank you for the link anyway. – Surya Jun 11 '16 at 18:23
0

If you are okay with array contents being replaced, then, following will do it:

a.fill {|i|  "*" * a[i]}
#=> ["****", "*", "***"]
Wand Maker
  • 18,476
  • 8
  • 53
  • 87