{}
See how a CS professor is using our compiler for class assignment.
Try Programiz PRO for Educators!
Learn DSA with step-by-step code visualization.
Try Programiz PRO for Educators!
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. import re from collections import Counter # ---------------------------- # Part 1: Most frequent word in a paragraph # ---------------------------- paragraph = '''I love teaching. If you do not love teaching what else can you love. I love Python if you do not love something which can give you all the capabilities to develop an application what else can you love.''' def most_frequent_in_paragraph(text): # Clean and normalize words = re.findall(r'\b\w+\b', text.lower()) count = Counter(words) return count.most_common(1)[0] # Return most frequent word most_common_word = most_frequent_in_paragraph(paragraph) print("Most frequent word in paragraph:", most_common_word) # ---------------------------- # Part 2: Distance between furthest particles # ---------------------------- points = ['-12', '-4', '-3', '-1', '0', '4', '8'] points = list(map(int, points)) points.sort() distance = points[-1] - points[0] print("Distance between furthest particles:", distance) # ---------------------------- # Part 3: Valid Python variable name checker # ---------------------------- def is_valid_variable(name): return name.isidentifier() # Test cases print("is_valid_variable('first_name'):", is_valid_variable('first_name')) print("is_valid_variable('first-name'):", is_valid_variable('first-name')) print("is_valid_variable('1first_name'):", is_valid_variable('1first_name')) print("is_valid_variable('firstname'):", is_valid_variable('firstname')) # ---------------------------- # Part 4: Clean text and get top 3 most frequent words # ---------------------------- sentence = '''%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?''' def clean_text(text): # Remove special characters return re.sub(r'[^a-zA-Z\s]', '', text) def most_frequent_words(text, n=3): words = text.lower().split() return Counter(words).most_common(n) cleaned = clean_text(sentence) print("Cleaned sentence:", cleaned) top_words = most_frequent_words(cleaned) print("Top 3 frequent words:", top_words)
Output