Created
May 14, 2026 06:07
-
-
Save igavrysh/9541e3363d672b53b1745eaba8981c4b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| token transform | |
| Write a function, token_transform, that takes in a dictionary of tokens and a string. In the dictionary, the replacement values for a token may reference other tokens. The function should return a new string where tokens are replaced with their fully evaluated string values. | |
| Tokens are enclosed in a pair of '$'. You can assume that the input string is properly formatted and "$" is not used in the string except to enclose a token. | |
| You may assume that there are no circular token dependencies. | |
| ''' | |
| def token_transform(s, tokens): | |
| output = [] | |
| i = 0 | |
| j = 1 | |
| while i < len(s): | |
| if s[i] != '$': | |
| output += s[i] | |
| i += 1 | |
| j = i + 1 | |
| elif s[j] != '$': | |
| j += 1 | |
| else: | |
| key = s[i:j + 1] | |
| value = tokens[key] | |
| evaluated_value = token_transform(value, tokens) | |
| tokens[key] = evaluated_value | |
| output.append(evaluated_value) | |
| i = j + 1 | |
| j = i + 1 | |
| return ''.join(output) | |
| tokens = { | |
| '$LOCATION$': '$ANIMAL$ park', | |
| '$ANIMAL$': 'dog', | |
| } | |
| token_transform('Walk the $ANIMAL$ in the $LOCATION$!', tokens) | |
| # -> 'Walk the dog in the dog park!' | |
| tokens = { | |
| '$ADJECTIVE_1$': "quick", | |
| '$ADJECTIVE_2$': "eager", | |
| '$ADVERBS$': "$ADJECTIVE_1$ly and $ADJECTIVE_2$ly", | |
| '$VERB$': "hopped $DIRECTION$", | |
| '$DIRECTION$': "North", | |
| } | |
| token_transform("the $ADJECTIVE_1$ fox $ADVERBS$ $VERB$ward", tokens) | |
| # -> 'the quick fox quickly and eagerly hopped Northward' | |
| tokens = { | |
| '$B$': "epicly $C$", | |
| '$A$': "pretty $B$ problem $D$", | |
| '$D$': "we have", | |
| '$C$': "clever", | |
| } | |
| token_transform("What a $A$ here!", tokens) | |
| # -> 'What a pretty epicly clever problem we have here!' | |
| tokens = { | |
| '$B$': "epicly $C$", | |
| '$A$': "pretty $B$ problem $D$", | |
| '$D$': "we have", | |
| '$C$': "clever", | |
| } | |
| token_transform("What a $A$ here!", tokens) | |
| # -> 'What a pretty epicly clever problem we have here!' | |
| tokens = { | |
| '$0$': "$1$$1$$1$$1$$1$$1$$1$$1$$1$$1$$1$$1$", | |
| '$1$': "$2$$2$$2$$2$$2$$2$$2$$2$$2$", | |
| '$2$': "$3$$3$$3$$3$$3$$3$$3$", | |
| '$3$': "$4$$4$$4$$4$$4$$4$", | |
| '$4$': "$5$$5$$5$$5$$5$", | |
| '$5$': "$6$$6$$6$$6$", | |
| '$6$': "$7$$7$$7$", | |
| '$7$': "$8$$8$", | |
| '$8$': "", | |
| } | |
| token_transform("z$0$z$0$z$0$z$0$z$0$z$0$z", tokens) | |
| # -> 'zzzzzzz' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment