We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I wrote a little python script that will draw out the terrian, just because I thought it would be fun.
classAsciiArt:art=list(list())width=int()height=int()def__init__(self,width):self.width=widthself.height=1self.art=[[' 'for_inrange(width)]]defplaceArt(self,symbol,x,y):if(y>=self.height):numberOfLinesToAdd=abs(y-self.height)+1for_inrange(numberOfLinesToAdd):self.art+=[[' 'for_inrange(self.width)]]self.height+=numberOfLinesToAddself.art[y][x]=symboldefprintArt(self):forlineinself.art:print(str.join("",line))defgetAsciiForStep(step):return'/'ifstepis1else'\\'defgetMaxHeight(string_steps):mapStepsToInt=lambdax:1ifxis'U'else-1steps=list(map(mapStepsToInt,list(string_steps)))currentLevel=0# zero is sea levelmaxHeight=0forstepinsteps:currentLevel+=stepif(currentLevel>maxHeight):maxHeight=currentLevelreturnmaxHeightdefgetAsciiArtForSteps(string_steps):print(string_steps)# Create our display *steps* widenumberOfSteps=len(string_steps)OFFSET=getMaxHeight(string_steps)display=AsciiArt(numberOfSteps+2)display.placeArt('_',0,OFFSET)# Map our step values to integersmapStepsToInt=lambdax:1ifxis'U'else-1steps=list(map(mapStepsToInt,list(string_steps)))currentLevel=0# zero is sea levelforstep_iinrange(numberOfSteps):step=steps[step_i]symbol=getAsciiForStep(step)y=((OFFSET-currentLevel)-(1ifstepis1else0))+1currentLevel+=stepdisplay.placeArt(symbol,step_i+1,y)display.placeArt('_',numberOfSteps+1,OFFSET)returndisplaydefmain():display=getAsciiArtForSteps("UDUDDUUUDUDUDUUDUUDDDDDUDUDDDDUUDDUDDUUUUDUUDUDDDDUDUDUUUDDDUUUDUDDUUDDDUUDDUDDDUDUUDUUDUUDUDDDUUUUU")display.printArt()if__name__=="__main__":main()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Valleys
You are viewing a single comment's thread. Return to all comments →
I wrote a little python script that will draw out the terrian, just because I thought it would be fun.