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
Solutions for this problem:
Word Frequency Counter Solution
Word Frequency Counter Solution
Word Frequency Counter Solution
Note: Click and hold to view problem and solutions.
Matrix Rotation
Solutions for this problem:
Matrix Rotation Solution 1
Matrix Rotation Solution 2
Matrix Rotation Solution 3
Word Frequency Counter
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.
Givens
Example:
text = "The cat chased the dog. The dog chased the cat."
# Output: "cat"
Word Frequency Counter Solution
Solution Steps
Step 1:
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:
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:
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
Matrix Rotation
Requirements
Write a function to rotate an n x n matrix (list of lists) 90 degrees clockwise in-place.
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:
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:
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()