0
0
Prompt Engineering / GenAIml~10 mins

Context window and token limits in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the number of tokens in a text using a simple split.

Prompt Engineering / GenAI
text = "Hello world! This is a test."
tokens = text.[1]()
print(len(tokens))
Drag options to blanks, or click blank then click option'
Areplace
Bjoin
Cstrip
Dsplit
Attempts:
3 left
💡 Hint
Common Mistakes
Using join instead of split.
Trying to replace tokens instead of splitting.
2fill in blank
medium

Complete the code to limit the number of tokens to a maximum context window size.

Prompt Engineering / GenAI
text = "Hello world! This is a test."
max_tokens = 5
tokens = text.split()
limited_tokens = tokens[:[1]]
print(limited_tokens)
Drag options to blanks, or click blank then click option'
A5 + 1
Blen(tokens)
Cmax_tokens
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(tokens) which does not limit tokens.
Using 0 which results in an empty list.
3fill in blank
hard

Fix the error in the code that counts tokens but mistakenly uses a wrong method.

Prompt Engineering / GenAI
text = "Sample text for token count."
token_count = len(text.[1]())
print(token_count)
Drag options to blanks, or click blank then click option'
Asplit
Bcount
Cjoin
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which counts occurrences of a substring.
Using join() which combines strings.
4fill in blank
hard

Complete the code to create a dictionary of tokens and their counts, filtering tokens longer than 3 characters.

Prompt Engineering / GenAI
text = "Hello world! This is a test."
tokens = text.split()
token_counts = {token: tokens.count(token) for token in tokens if len(token) [1] 3}
print(token_counts)
Drag options to blanks, or click blank then click option'
A:
B>
C<
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of ':' in dictionary comprehension.
Using '<' which filters shorter tokens.
5fill in blank
hard

Fill both blanks to create a dictionary of tokens in uppercase, their counts, and filter tokens with length less than 6.

Prompt Engineering / GenAI
text = "Hello world! This is a test."
tokens = text.split()
result = { [1] : tokens.count(token) for token in tokens if len(token) [2] 6 }
print(result)
Drag options to blanks, or click blank then click option'
Atoken.upper()
B<
Ctoken
D:
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'token' instead of 'token.upper()' for keys.
Using '>' instead of '<' for filtering.
Forgetting ':' between key and value.