Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

problem in Ruby code in finding permutations

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 7604
522
Mac OS X VI Editor BSD Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 7604
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me try it on my machine to see what happens.
 
Liutauras Vilda
Marshal
Posts: 7604
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • 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
 
Oh. Hi guys! Look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic