|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Any1 help (simple problem)!
Hi,
I'm trying to make a program that is a puzzle game. It's pretty simple, but I'm new to Ruby so am caught up here. The program has a string that gets modified as the game progresses. The rule I'm stuck on should iterate through the string, and each time three "I"'s appear in order, it should output them with *'s around them (to draw the player's attention). For example, assume the string was 'MIIIIUIIUIII' The output should be: M*III*IUIIUIII MI*III*UIIUIII MIIIIUIIU*III*I The logic I'm thinking to use would be (in english) starting from the first to the last character, if there are any occurences of "III" output the first one surrounded by "*", then move to the character immediately after the first group of III's and repeat until the end of the string is reached, or until a position is reached from which that position to the end of the string contain no more instances of "III". Here's my work in progress on the part of the game relating to that question (again, I'm very new): $my_string = 'MIIIIUIIUIII' count = 0 ending = $my_string.length while $my_string[ count...ending ].include? 'III' count = 0 display = $my_string display['III'] = '*III*' puts display count += 1 end Which is outputing (not what I want): M*III*IUIIUIII M**III**IUIIUIII M***III***IUIIUIII M****III****IUIIUIII M*****III*****IUIIUIII M******III******IUIIUIII M*******III*******IUIIUIII M********III********IUIIUIII M*********III*********IUIIUIII THANKS! ![]() |
|
#2
|
|||
|
|||
|
I got it!!
![]() length = $my_string.length count = 0 index = 0 while index < length if $my_string[index..(index + 2)] == 'III' display = $my_string.dup display[ index..(index + 2) ] = ' *III* ' puts display count += 1 end index += 1 end |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Any1 help (simple problem)! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|