Guest User

Untitled

a guest
Feb 22nd, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. array = [] #DEFINES THE ARRAY
  2.  
  3. array[0] = rand(10) #THIS CHOOSES FIRST NUMBER (FN) AND PUTS IT IN THE FIRST ARRAY SPOT
  4. p "fn = #{array[0]}"
  5.  
  6. for i in 1..6 #START OF FOR LOOP TO GET 6 NUMBERS IN THE ARRAY - WHY 6? THE FIRST NUMBER THAT POPPED IN MY HEAD
  7.  
  8. p "Iteration #{i} -------------------------" # THIS IS JUST SO I KNOW WHERE I AM IN THE LOOPS
  9.  
  10. @x = rand(10) #THIS CHOOSES THE NEXT NUMBER AND ALL NUMBERS AFTER
  11.  
  12. array.each do |uc| # THIS IS THE LOOP THAT COMPARES ALL NUMBERS
  13.  
  14. @type = @x == uc ? "yes" : "no" #DOES THE CHOSEN NUMBER EQUAL ANY NUMBER IN THE ARRAY
  15.  
  16. p "does #{uc} = #{@x}? #{@type}"
  17. if @type == "yes" # IF THE COMPARE IS TRUE, I DON'T WANT ANYTHING DONE. IT WILL CYCLE THRU AND GET A NEW NUMBER
  18.  
  19. i = 1
  20. p "YES #{@x} = #{uc}"
  21. break
  22. end #END OF IF YES
  23. end #END OF ARRAY EACH
  24.  
  25. if @type == "no" #IF NO, PUT NEXT NUMBER (@X) INTO THE NEXT ARRAY SPOT.
  26.  
  27. p "in last if type= #{@type}" #THESE STATEMENTS JUST PRINT OUT THE DIFFERENT VARIABLES SO I KNOW I AM GETTING WHAT I EXPECT
  28.  
  29. p "in last if x = #{@x}"
  30. p "in last if i = #{i}"
  31. @x = array[i] #THIS "SHOULD" FILL THE NEXT ARRAY SPOT - BUT DOESN'T SEEM TO
  32.  
  33. p "#{array[i]} is in the array" #THIS PRINT OUT IS BLANK - STATEMENT ABOVE DID NOT WORK.
  34.  
  35. p array[i]
  36. end #END OF IF NO
  37.  
  38. end #END OF FOR LOOP
  39.  
  40. p array #PRINTS OUT THE CONTENTS OF THE ARRAY
  41.  
  42. (1..10).to_a.shuffle
  43. #=> [4, 10, 1, 7, 3, 5, 8, 2, 9, 6]
  44.  
  45. (1..10).to_a.sample(7) #=> [2, 9, 1, 6, 8, 10, 4]
  46.  
  47. array = []
  48. array[0] = rand 10
  49. puts "fn = #{array[0]}"
  50. for i in 1..6
  51. puts "Iteration #{i} -------------------------"
  52. x = rand 10
  53. array.each do |uc|
  54. $type = x == uc ? "yes" : "no"
  55. puts "does #{uc} = #{x}? #{$type}"
  56. if $type == "yes"
  57. puts "YES #{x} = #{uc}"
  58. break
  59. end
  60. end
  61. if $type == "no"
  62. puts "in last if type= #{$type}"
  63. puts "in last if x = #{x}"
  64. puts "in last if i = #{i}"
  65. array[i] = x
  66. puts "#{array[i]} is in the array"
  67. p array[i]
  68. else
  69. redo
  70. end
  71. end
  72. p array
  73.  
  74. array = []
  75. array[0] = rand 10
  76. puts "fn = #{array[0]}"
  77. for i in 1..6
  78. puts "Iteration #{i} -------------------------"
  79. x = rand 10
  80. type = array.include?(x) ? "yes" : "no"
  81. puts "does array include #{x}? #{type}"
  82. if type == "no"
  83. puts "in last if type=#{type}, x=#{x}, i=#{i}"
  84. array[i] = x
  85. puts "#{array[i]} is in the array"
  86. else
  87. redo
  88. end
  89. end
  90. p array
  91.  
  92. array = []
  93. array[0] = rand 10
  94. puts "fn = #{array[0]}"
  95. for i in 1..6
  96. puts "Iteration #{i} -------------------------"
  97. x = rand 10
  98. if (type = array.include? x).tap{ puts "does array include #{x}? #{type : "yes" : "no"}" }
  99. redo
  100. else
  101. puts "in last if type=#{type}, x=#{x}, i=#{i}"
  102. array[i] = x
  103. puts "#{array[i]} is in the array"
  104. end
  105. end
  106. p array
  107.  
  108. array = []
  109. 7.times do |i|
  110. puts "Iteration #{i} -------------------------"
  111. x = rand 10
  112. redo if array.include?(x).tap{ |type| puts "does array include #{x}? #{type ? "yes" : "no"}" }
  113. puts "x=#{x}, i=#{i}"
  114. array[i] = x
  115. puts "#{array[i]} is in the array"
  116. end
  117. p array
  118.  
  119. a = []
  120. while a.length < 7
  121. unless a.include? random_number = rand(10)
  122. a << random_number
  123. end
  124. end
  125. p a
Add Comment
Please, Sign In to add comment