• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in Ruby code in finding permutations

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I have marked the code line which creates problem. Look It prints right value of str but its doesn't store the right value of str in global array $arr. Why so?

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.
 
Abhimanyu Aryan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the Ranch.

Please answer to the next questions, so someone could actually help you:

1. What it prints?
1.1 What you expect it to print?

2. What it stores?
2.1 What you expect it to store?

Please also give us example of input and output.



sorry I missed that part I am adding output

Output:

123
132
213
231
321
312
["123", "123", "123", "123", "123", "123"]

Expected Output:

123
132
213
231
321
312
["123", "132", "213", "231", "321", "312"]
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me try it on my machine to see what happens.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, clear. Strings are mutable in ruby. So, once you add them all in array they all referring to the same string object. After you add last element to it, your line 13 sets string to its initial form.

Add one extra print statement after line 6 to print an array and you'll see yourself ;) How to make it work in your way? We'll let you to think about it first
reply
    Bookmark Topic Watch Topic
  • New Topic