fromstrutilsimporttoLowerfromsequtilsimportfiltertypeWordType=enumvalue,multiply,globaltypeWord=objectValue:intType:WordType# a..z# [97, 122]constscore_mapping=[1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10]# Build a stack, tokenizing the characters# This will let us apply operations in a reverse orderproc buildStack(word:string):seq[refWord]=result=newSeq[refWord]()letlowerWord=toLower(word)foriin0..(len(lowerWord)-1):letcurrentWord=new(Word)letletter=lowerWord[i]letletterAsInt=int(letter)ifletterAsInt<97orletterAsInt>122:ifletter=='*':currentWord.Value=2currentWord.Type=WordType.multiplyifletter=='^':currentWord.Value=0currentWord.Type=WordType.multiplyifletter=='(':currentWord.Value=iflowerWord[i+1]=='t':3else:2currentWord.Type=WordType.globalresult.add(currentWord)# we reached the end of the stringbreakelse:letscoreMappingPosition=letterAsInt-97currentWord.Value=score_mapping[scoreMappingPosition]currentWord.Type=WordType.valueresult.add(currentWord)# Gets the value of the operationsproc parseOperations(value:int,operations:seq[refWord]):int=varmultiplier=1iflen(operations)>0:foroperationinoperations:ifoperation.Type==Wordtype.multiply:ifoperation.Value==0:return0else:multiplier+=1result+=value*multiplierproc getScore(stack:seq[refWord]):int=vars=stack# make mutablevaroperations:seq[refWord]varglobalMultiplier=1ifstack.filter(proc(p:refWord):bool=p.Type==Wordtype.value).len>=7:result+=50whilelen(s)>0:letitem=s.pop()ifitem.Type==WordType.value:result+=parseOperations(item.Value,operations)operations=@[]elifitem.Type==Wordtype.global:globalMultiplier=item.Valueelse:operations.add(item)result*=globalMultiplierechobuildStack("d**e*v^(d)").getScore()
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
My overly complex (nim) solution :)