Bài tập String - Nâng cao
- Viết hàm
validate_emailkiểm tra email hợp lệ (phải có @, kết thúc bằng .com, .vn, hoặc .net).
def validate_email(email): # Code của bạn ở đây pass
# Testprint(validate_email("invalid-email")) # False- Viết hàm
clean_phone_numberchuẩn hóa số điện thoại về dạng: 0xxx-xxx-xxx.
def clean_phone_number(phone): # Loại bỏ tất cả ký tự không phải số, format lại pass
# Testprint(clean_phone_number("090 123 4567")) # "0901-234-567"print(clean_phone_number("(090) 123-4567")) # "0901-234-567"- Viết hàm
title_casechuyển chuỗi thành Title Case nhưng giữ nguyên các từ đặc biệt (and, or, the, of) ở chữ thường.
def title_case(text): # Code của bạn ở đây pass
# Testprint(title_case("the lord of the rings"))# "The Lord of the Rings"- Viết hàm
word_frequencyđếm tần suất xuất hiện của mỗi từ trong văn bản.
def word_frequency(text): # Code của bạn ở đây pass
# Testtext = "python is fun and python is easy"print(word_frequency(text))# {'python': 2, 'is': 2, 'fun': 1, 'and': 1, 'easy': 1}- Viết hàm
longest_wordtìm từ dài nhất trong chuỗi.
def longest_word(text): # Code của bạn ở đây pass
# Testprint(longest_word("The quick brown fox jumps")) # "quick" hoặc "brown"- Viết hàm
reverse_wordsđảo ngược thứ tự các từ (không phải đảo ngược từng từ).
def reverse_words(text): # Code của bạn ở đây pass
# Testprint(reverse_words("Hello World Python"))# "Python World Hello"- Viết hàm
caesar_ciphermã hóa chuỗi bằng Caesar cipher (dịch mỗi chữ cái đi n vị trí).
def caesar_cipher(text, shift): # Code của bạn ở đây pass
# Testprint(caesar_cipher("abc", 3)) # "def"print(caesar_cipher("xyz", 3)) # "abc"💡 Gợi ý: Dùng
ord()vàchr()để chuyển đổi ký tự sang mã ASCII và ngược lại
- Viết hàm
create_acronymtạo từ viết tắt từ các chữ cái đầu.
def create_acronym(text): # Code của bạn ở đây pass
# Testprint(create_acronym("Application Programming Interface")) # "API"print(create_acronym("as soon as possible")) # "ASAP"- Viết hàm
snake_to_camelchuyển snake_case thành camelCase.
def snake_to_camel(text): # Code của bạn ở đây pass
# Testprint(snake_to_camel("hello_world_python")) # "helloWorldPython"- Viết hàm
camel_to_snakechuyển camelCase thành snake_case.
def camel_to_snake(text): # Code của bạn ở đây pass
# Testprint(camel_to_snake("helloWorldPython")) # "hello_world_python"- Viết hàm
wrap_textngắt dòng văn bản theo độ dài tối đa.
def wrap_text(text, max_length): # Code của bạn ở đây pass
# Testtext = "This is a very long sentence that needs to be wrapped"print(wrap_text(text, 20))# This is a very long# sentence that needs# to be wrapped- Viết hàm
remove_html_tagsloại bỏ các thẻ HTML từ chuỗi.
def remove_html_tags(html): # Code của bạn ở đây pass
# Testhtml = "<p>Hello <b>World</b>!</p>"print(remove_html_tags(html)) # "Hello World!"- Viết hàm
format_currencyformat số tiền với dấu phẩy và ký hiệu tiền tệ.
def format_currency(amount, currency="VND"): # Code của bạn ở đây pass
# Testprint(format_currency(1234567)) # "1,234,567 VND"print(format_currency(1234.56, "USD")) # "$1,234.56"- Viết hàm
create_slugtạo URL slug từ tiêu đề (chuyển thành chữ thường, thay khoảng trắng bằng-, loại bỏ ký tự đặc biệt).
def create_slug(title): # Code của bạn ở đây pass
# Testprint(create_slug("Hello World! Python 2024"))# "hello-world-python-2024"- Viết hàm
highlight_keywordsbọc các từ khóa trong chuỗi bằng ** (markdown bold).
def highlight_keywords(text, keywords): # Code của bạn ở đây pass
# Testtext = "Python is a programming language"keywords = ["Python", "language"]print(highlight_keywords(text, keywords))# "**Python** is a programming **language**"- Viết hàm
extract_numberstrích xuất tất cả các số từ chuỗi.
def extract_numbers(text): # Code của bạn ở đây pass
# Testprint(extract_numbers("I have 3 apples and 5 oranges"))# [3, 5]- Viết hàm
format_tableformat dữ liệu thành bảng ASCII.
def format_table(data, headers): # Code của bạn ở đây pass
# Testdata = [ ["Alice", 25, "Hanoi"], ["Bob", 30, "HCMC"], ["Charlie", 35, "Danang"]]headers = ["Name", "Age", "City"]print(format_table(data, headers))# +----------+-----+--------+# | Name | Age | City |# +----------+-----+--------+# | Alice | 25 | Hanoi |# | Bob | 30 | HCMC |# | Charlie | 35 | Danang |# +----------+-----+--------+- Viết hàm
compress_whitespacethay thế nhiều khoảng trắng liên tiếp bằng một khoảng trắng.
def compress_whitespace(text): # Code của bạn ở đây pass
# Testprint(compress_whitespace("Hello World Python"))# "Hello World Python"- Viết hàm
mask_sensitive_datache giấu dữ liệu nhạy cảm (email, số điện thoại, số thẻ).
def mask_email(email): # Hiển thị 2 ký tự đầu và domain # [email protected] -> us***@example.com pass
def mask_phone(phone): # 0901234567 -> 090***4567 pass
# Testprint(mask_phone("0901234567")) # "090***4567"- Viết hàm
parse_csv_lineparse một dòng CSV (xử lý trường hợp có dấu phẩy trong quotes).
def parse_csv_line(line): # Code của bạn ở đây pass
# Testline = 'Alice,25,"Hanoi, Vietnam"'print(parse_csv_line(line))# ["Alice", "25", "Hanoi, Vietnam"]- Viết hàm
levenshtein_distancetính khoảng cách Levenshtein giữa hai chuỗi (số thay đổi tối thiểu để biến chuỗi này thành chuỗi kia).
def levenshtein_distance(s1, s2): # Code của bạn ở đây pass
# Testprint(levenshtein_distance("kitten", "sitting")) # 3- Viết hàm
generate_passwordtạo mật khẩu ngẫu nhiên với độ dài và yêu cầu cho trước.
def generate_password(length, include_numbers=True, include_symbols=True): # Code của bạn ở đây pass
# Testprint(generate_password(12)) # "aB3#xY9$mN2@"- Viết hàm
extract_urlstrích xuất tất cả URL từ văn bản.
def extract_urls(text): # Code của bạn ở đây pass
# Testtext = "Visit https://example.com and http://test.org for more info"print(extract_urls(text))# ["https://example.com", "http://test.org"]- Viết hàm
pluralizechuyển từ số ít sang số nhiều (đơn giản hóa tiếng Anh).
def pluralize(word, count): # Code của bạn ở đây pass
# Testprint(pluralize("apple", 1)) # "1 apple"print(pluralize("apple", 5)) # "5 apples"print(pluralize("box", 3)) # "3 boxes"- Viết hàm
format_durationformat thời gian (giây) thành chuỗi dễ đọc.
def format_duration(seconds): # Code của bạn ở đây pass
# Testprint(format_duration(125)) # "2 minutes, 5 seconds"print(format_duration(3665)) # "1 hour, 1 minute, 5 seconds"print(format_duration(90000)) # "1 day, 1 hour, 0 minutes, 0 seconds"