Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. # Write a method that takes in a string and an array of indices in the
  2. # string. Produce a new string, which contains letters from the input
  3. # string in the order specified by the indices of the array of indices.
  4. #
  5. # Difficulty: medium.
  6.  
  7. def scramble_string(string, positions)
  8. i=0
  9. result=""
  10.  
  11. while i<string.length
  12. result+=string[positions[i]]
  13. i+=1
  14. end
  15.  
  16. return result
  17. end
  18.  
  19. # These are tests to check that your code is working. After writing
  20. # your solution, they should all print true.
  21.  
  22. puts("\nTests for #scramble_string")
  23. puts("===============================================")
  24. puts(
  25. 'scramble_string("abcd", [3, 1, 2, 0]) == "dbca": ' +
  26. (scramble_string("abcd", [3, 1, 2, 0]) == "dbca").to_s
  27. )
  28. puts(
  29. 'scramble_string("markov", [5, 3, 1, 4, 2, 0]) == "vkaorm"): ' +
  30. (scramble_string("markov", [5, 3, 1, 4, 2, 0]) == "vkaorm").to_s
  31. )
  32. puts("===============================================")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement