diff --git a/README.md b/README.md index b513272..3c3e8da 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,10 @@ This repository is a growing collection of clear, practical, engineer-friendly t ## Tutorials Index -| Filename | Description | -|----------|-------------| -| 001_hello_world.py | Basic Hello World Program in Python - This script prints "Hello, World!" to the console. | - - +| Script | Description | +|--------|-------------| +| [001_hello_world.py](basic/001_hello_world.py) | Basic Hello World Program in Python - This script prints "Hello, World!" to the console. | +| [002_fstrings.py](basic/002_fstrings.py) | Example of using f-strings for formatted string literals in Python. | +| [003_inplace_swap.py](basic/003_inplace_swap.py) | Basic In-Place Swap Program in Python - This script swaps the values of two variables without using a temporary variable. | +| [004_vowel_consonants.py](basic/004_vowel_consonants.py) | Basic example demonstrating vowel and consonant identification in a string. | +| [005_simple_list.py](basic/005_simple_list.py) | This script takes a list of hobbies from user input,prints each hobby on a new line prefixed with * | diff --git a/basic/001_hello_world.py b/basic/001_hello_world.py index 888b0f5..563f8ff 100644 --- a/basic/001_hello_world.py +++ b/basic/001_hello_world.py @@ -4,3 +4,6 @@ def hello_world(): print("Hello, World!") + +if __name__ == "__main__": + hello_world() \ No newline at end of file diff --git a/basic/002_fstrings.py b/basic/002_fstrings.py new file mode 100644 index 0000000..d2dab2b --- /dev/null +++ b/basic/002_fstrings.py @@ -0,0 +1,12 @@ +""" +Example of using f-strings for formatted string literals in Python. +""" + +name = input("Enter your name: ") +color = input("Enter your favorite color: ") + +def greet(name, color): + print(f"Hello, my name is {name} and I like the color {color}") + +if __name__ == "__main__": + greet(name, color) \ No newline at end of file diff --git a/basic/003_inplace_swap.py b/basic/003_inplace_swap.py new file mode 100644 index 0000000..0bf6db4 --- /dev/null +++ b/basic/003_inplace_swap.py @@ -0,0 +1,15 @@ +""" +Basic In-Place Swap Program in Python - This script swaps the values of two variables without using a temporary variable. +""" + +a = 10 +b = 20 + +def swap(a, b): + print(f"before swap: {a, b}") + a, b = b, a + print(f"after swap: {a, b}") + return a, b + +if __name__ == "__main__": + swap(a, b) \ No newline at end of file diff --git a/basic/004_vowel_consonants.py b/basic/004_vowel_consonants.py new file mode 100644 index 0000000..afe6687 --- /dev/null +++ b/basic/004_vowel_consonants.py @@ -0,0 +1,23 @@ +""" +Basic example demonstrating vowel and consonant identification in a string. +""" + +def count_vowels_and_consonants(input_string): + """Counts the number of vowels and consonants in the given string.""" + vowels = "aeiouAEIOU" + vowel_count = 0 # Initialize vowel count + consonant_count = 0 # Initialize consonant count + + for char in input_string: + if char.isalpha(): # Check if the character is a letter + if char in vowels: + vowel_count += 1 # Increment vowel count + else: + consonant_count += 1 # Increment consonant count + + print(f"Vowels count: {vowel_count}, Consonants count: {consonant_count}") + return vowel_count, consonant_count + +if __name__ == "__main__": + test_string = input("Enter a test string: ") + count_vowels_and_consonants(test_string) \ No newline at end of file diff --git a/basic/005_simple_list.py b/basic/005_simple_list.py new file mode 100644 index 0000000..5b0c9dc --- /dev/null +++ b/basic/005_simple_list.py @@ -0,0 +1,13 @@ +""" +This script takes a list of hobbies from user input,prints each hobby on a new line prefixed with * +""" + +def print_list_items(hobbies): + for hobby in hobbies: + print(f"* {hobby}") # Print each hobby with * prefix + +if __name__ == "__main__": + hobbies = input("Enter a list of hobbies separated by commas (e.g., reading,cycling,cooking): ").split(",") + # print("Hobby List:", hobbies) # Debugging line to check the list + print("Your hobbies are:") + print_list_items(hobbies) \ No newline at end of file diff --git a/list_script.py b/list_script.py index 69548d5..6aebb44 100644 --- a/list_script.py +++ b/list_script.py @@ -1,5 +1,6 @@ import os import ast +import re from typing import List, Dict BASE_DIRS = ["basic", "intermediate", "advanced"] @@ -13,6 +14,20 @@ def get_py_files(base_dirs: List[str]) -> List[str]: for file in files: if file.endswith(".py"): py_files.append(os.path.join(root, file)) + # Deterministic ordering: sort by base-dir order, numeric filename prefix, then filename + def sort_key(path: str): + parts = os.path.normpath(path).split(os.sep) + base = parts[0] if parts else path + try: + base_index = base_dirs.index(base) + except ValueError: + base_index = len(base_dirs) + filename = os.path.basename(path) + m = re.match(r"^(\d+)", filename) + num = int(m.group(1)) if m else float('inf') + return (base_index, num, filename) + + py_files.sort(key=sort_key) return py_files def extract_header(file_path: str) -> str: @@ -46,15 +61,18 @@ def main(): with open(readme_path, "r", encoding="utf-8") as f: readme = f.read() - # Replace or insert Tutorials Index section + # Replace or insert Tutorials Index section. + # Match from the '## Tutorials Index' header up to the next top-level '## ' header or EOF. import re - pattern = r'(## Tutorials Index\n)([\s\S]*?)(\n\n|\Z)' - replacement = f"## Tutorials Index\n\n{table}\n" + pattern = r'(?ms)^## Tutorials Index\b.*?(?=^##\s|\Z)' + replacement = f"## Tutorials Index\n\n{table}" if re.search(pattern, readme): readme = re.sub(pattern, replacement, readme) else: - # If not found, append at the end - readme += f"\n{replacement}" + # Ensure file ends with a newline before appending + if not readme.endswith("\n"): + readme += "\n" + readme += f"\n{replacement}\n" with open(readme_path, "w", encoding="utf-8") as f: f.write(readme)