Reloading will discard the content you have written here and load the latest
version of this station. Copy anything you want to keep first.
Warning
SUPERSET
POSTERIOR
Python Proficiency Test
The questionnaire to test Python programming language skills.
Time Limit: 60 minutes
Questionnaire Completed Successfully
Thank you for completing this questionnaire. Your results have been saved.
Ready to begin?
This questionnaire contains 2 problems.
Time Limit: 60 minutes
Provide Your Information
Questionnaire Completed Successfully!
Thank you for your responses. Your questionnaire has been successfully completed and submitted.
Problem 1 of 2
60:00
Note: Click and hold to view problem and solutions.
Word Frequency Counter
Answer
Note: Click and hold to view problem and solutions.
Matrix Rotation
Answer
Word Frequency Counter
Givens:
Example:
12
text = "The cat chased the dog. The dog chased the cat."
# Output: "cat"
Requirements:
Write a Python function that takes a string of text and returns the most frequent word (ignoring case and punctuation). If multiple words have the same frequency, return the lexicographically smallest one.
Word Frequency Counter Solution
Solution Steps
Step 1:
123456
def most_frequent_word(text):
words = text.split()
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
return max(freq, key=freq.get)
Word Frequency Counter Solution
Solution Steps
Step 1:
1234567
def most_frequent_word(text):
import re
words = re.findall(r'\w+', text.lower())
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
return max(freq, key=freq.get) # doesn’t handle tie-breaking
Word Frequency Counter Solution
Solution Steps
Step 1:
123456789
def most_frequent_word(text):
import re
words = re.findall(r'\w+', text.lower())
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
max_freq = max(freq.values())
candidates = [w for w, c in freq.items() if c == max_freq]
return min(candidates) # lexicographically smallest on tie
Write a function to rotate an n x n matrix (list of lists) 90 degrees clockwise in-place.
Matrix Rotation Solution 1
Solution Steps
Step 1:
1234567
def rotate(matrix):
n = len(matrix)
rotated = [[0]*n for _ in range(n)]
for i in range(n):
for j in range(n):
rotated[j][n-i-1] = matrix[i][j]
return rotated
Matrix Rotation Solution 2
Solution Steps
Step 1:
12345
def rotate(matrix):
n = len(matrix)
for i in range(n):
for j in range(n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
Matrix Rotation Solution 3
Solution Steps
Step 1:
123456789
def rotate(matrix):
n = len(matrix)
# Step 1: Transpose
for i in range(n):
for j in range(i, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Step 2: Reverse each row
for row in matrix:
row.reverse()