๐ Python Problem 0ne
Problem: Square every digit of a number and concatenate them.
Example:
-
-
---
My Solution:
AI's Solution (via ChatGPT):
---
๐ก Key Takeaway:
The AI uses
๐ Sources: Codewars, ChatGPT
๐ Try it yourself!
How would YOU approach this problem?
#Python #Codewars #Coding #LearnPython
Problem: Square every digit of a number and concatenate them.
Example:
-
9119
โ 811181
(since 9ยฒ=81, 1ยฒ=1, 1ยฒ=1, 9ยฒ=81 โ "81"+"1"+"1"+"81") -
765
โ 493625
(49-36-25) ---
My Solution:
def square_every_digit(num):
num_str = str(num)
result = ""
for digit in num_str:
squared = int(digit) ** 2
result += str(squared)
return int(result)
AI's Solution (via ChatGPT):
def square_every_digit(num):
squared_digits = ''.join(str(int(digit) ** 2) for digit in str(num))
return int(squared_digits)
---
๐ก Key Takeaway:
The AI uses
join()
with a generator expression to simplify the loop, making the code shorter and more "Pythonic". Both methods work, but join()
is cleaner for string concatenation!๐ Sources: Codewars, ChatGPT
๐ Try it yourself!
How would YOU approach this problem?
#Python #Codewars #Coding #LearnPython
๐ Day 1: Kickstart Your Python Journey!
Objective: Set up Python, write your first program, and grasp core basics.
---
๐ What is Python?
- A versatile, beginner-friendly language used for:
_Web dev โข Data science โข AI/ML โข Automation โข and more!_
- Known for clean syntax and readability.
---
๐ Setup Guide
1. Install Python:
- Download from [python.org](https://www.python.org/downloads/)
- โ๏ธ Check "Add Python to PATH" during installation.
2. Choose an Editor:
- VS Code (recommended) or PyCharm.
3. Verify Installation:
---
โจ Your First Python Program
1. Create
2. Run it in the terminal:
Output:
---
๐ Key Concepts
- Comments: Use
- Printing: Display output with
---
๐ Try the Python Shell
Type
---
๐ป Practice Tasks
1. Print your name:
2. Math operations:
---
### ๐ฏ Day 1 Goals
- [x] Understand Pythonโs purpose.
- [x] Set up your coding environment.
- [x] Write/Run your first script.
- [x] Master
Letโs crush Day 1! ๐ช
๐ *Share your code screenshots in the comments!*
#PythonBasics #LearnToCode #CodingJourney #Python101
Objective: Set up Python, write your first program, and grasp core basics.
---
๐ What is Python?
- A versatile, beginner-friendly language used for:
_Web dev โข Data science โข AI/ML โข Automation โข and more!_
- Known for clean syntax and readability.
---
๐ Setup Guide
1. Install Python:
- Download from [python.org](https://www.python.org/downloads/)
- โ๏ธ Check "Add Python to PATH" during installation.
2. Choose an Editor:
- VS Code (recommended) or PyCharm.
3. Verify Installation:
bash
python --version
# Should display e.g., "Python 3.11.4"
---
โจ Your First Python Program
1. Create
day1.py
and add: python
print("Hello, World!")
2. Run it in the terminal:
bash
python day1.py
Output:
Hello, World!
---
๐ Key Concepts
- Comments: Use
#
to explain code (ignored by Python). python
# This prints a message
print("Comments are useful!")
- Printing: Display output with
print()
. python
print("Python is fun! ๐")
---
๐ Try the Python Shell
Type
python
in your terminal to start experimenting: python
>>> print("I'm coding live!")
>>> 5 * 3
>>> 100 / 4
---
๐ป Practice Tasks
1. Print your name:
python
print("My name is [Your Name]")
2. Math operations:
python
print(8 + 4)
print(15 * 2)
---
### ๐ฏ Day 1 Goals
- [x] Understand Pythonโs purpose.
- [x] Set up your coding environment.
- [x] Write/Run your first script.
- [x] Master
print()
and comments. Letโs crush Day 1! ๐ช
๐ *Share your code screenshots in the comments!*
#PythonBasics #LearnToCode #CodingJourney #Python101
Python.org
Download Python
The official home of the Python Programming Language
๐ Problem 2
Problem: Determine if a string is an isogram (no repeating letters, case-insensitive).
Examples:
- "Dermatoglyphics" โ True
- "moOse" โ False (duplicate 'o' when lowercased)
- Empty string โ True
๐ง๐ป My Approach:
โ Works butโฆ
- Checks each character's count, leading to O(nยฒ) time complexity.
- Early exit if duplicates found, but inefficient for large strings.
๐ค AI's Optimized Approach:
๐ฅ Why Better?
1. O(n) Time: Converts string to a set (auto-removes duplicates).
2. Concise: One-liner using Python's built-in functions.
3. Readable: Leverages set properties for uniqueness check.
๐ก Key Takeaways:
- Use Sets for Uniqueness: Perfect for checking duplicates.
- Case Handling: Always lowercase the string first.
- Edge Cases: Empty string is handled gracefully.
๐ Compare Efficiency:
| Method | Time Complexity | Lines of Code |
|--------------|-----------------|---------------|
| Loop & Count | O(nยฒ) | 5 |
| Set & Length | O(n) | 1 |
*Credits: Codewars problem + ChatGPT optimization.*
Try both methods and share your thoughts! ๐
#Python #CodingTips #Algorithms #Programming
Problem: Determine if a string is an isogram (no repeating letters, case-insensitive).
Examples:
- "Dermatoglyphics" โ True
- "moOse" โ False (duplicate 'o' when lowercased)
- Empty string โ True
๐ง๐ป My Approach:
def is_isogram(string):
string = string.lower()
for i in string:
if string.count(i) > 1:
return False
return True
โ Works butโฆ
- Checks each character's count, leading to O(nยฒ) time complexity.
- Early exit if duplicates found, but inefficient for large strings.
๐ค AI's Optimized Approach:
def is_isogram(string):
lowered = string.lower()
return len(set(lowered)) == len(lowered)
๐ฅ Why Better?
1. O(n) Time: Converts string to a set (auto-removes duplicates).
2. Concise: One-liner using Python's built-in functions.
3. Readable: Leverages set properties for uniqueness check.
๐ก Key Takeaways:
- Use Sets for Uniqueness: Perfect for checking duplicates.
- Case Handling: Always lowercase the string first.
- Edge Cases: Empty string is handled gracefully.
๐ Compare Efficiency:
| Method | Time Complexity | Lines of Code |
|--------------|-----------------|---------------|
| Loop & Count | O(nยฒ) | 5 |
| Set & Length | O(n) | 1 |
*Credits: Codewars problem + ChatGPT optimization.*
Try both methods and share your thoughts! ๐
#Python #CodingTips #Algorithms #Programming
๐ Day 2: Python Basics - Variables & Data Types
Store data like a pro!
---
๐ฆ What are Variables?
Variables = Containers for storing data
Example output:
---
๐ Variable Rules
1. ๐ซ Never starts with numbers/symbols (except
2. ๐ Case-sensitive:
3. โ Allowed: letters, numbers, underscores
Good:
Bad:
---
๐ค Data Type Zoo
| Type | Example | Use Case |
|------------ |-----------------โ-|------------------------โโโ--|
|
|
|
|
Check types with:
---
๐ฏ Pro Tips
1. Swap variables without temp:
2. Assign multiple values:
---
๐ป Practice Time
1. Profile Maker
2. Advanced Swap Challenge
Swap values without using a 3rd variable (Hint: Use math!)
Starting values:
---
๐ฏ Today's Goal
- Create variables confidently โ
- Name them properly ๐ท
- Understand 4 core data types ๐ค
๐ฌ *Stuck? Drop your code in comments!*
#PythonForBeginners #LearnToCode #Day2
Store data like a pro!
---
๐ฆ What are Variables?
Variables = Containers for storing data
python
name = "Neo" # Text in quotes
age = 30 # Whole number
height = 6.1 # Decimal number
Example output:
Neo
, 30
, 6.1
---
๐ Variable Rules
1. ๐ซ Never starts with numbers/symbols (except
_
) 2. ๐ Case-sensitive:
Age โ age โ AGE
3. โ Allowed: letters, numbers, underscores
Good:
user_name
, price2
Bad:
2nd_user
, my-name
---
๐ค Data Type Zoo
| Type | Example | Use Case |
|------------ |-----------------โ-|------------------------โโโ--|
|
str
| "Hello"
| Text data | |
int
| 25
| Ages, counts | |
float
| 3.14
| Measurements, prices | |
bool
| True
/False
| Switches, conditions | Check types with:
python
print(type("Python")) # Output: <class 'str'>
---
๐ฏ Pro Tips
1. Swap variables without temp:
python
a, b = 5, 10
a, b = b, a # Magic!
2. Assign multiple values:
python
x, y, z = 10, 20, 30
---
๐ป Practice Time
1. Profile Maker
python
name = "Your Name"
age = your_age
height = your_height
# Print all in one message
2. Advanced Swap Challenge
Swap values without using a 3rd variable (Hint: Use math!)
Starting values:
a = 5
, b = 10
---
๐ฏ Today's Goal
- Create variables confidently โ
- Name them properly ๐ท
- Understand 4 core data types ๐ค
๐ฌ *Stuck? Drop your code in comments!*
#PythonForBeginners #LearnToCode #Day2
๐ท Problem 3: A Square of Squares ๐ท
๐ท You love building blocksโespecially square ones! But can you tell if the total number of blocks you have can form a perfect square?
๐ฏ Task:
Given an integer
๐ง Examples:
---
๐งช My Initial Solution (Not Efficient for Large Numbers)
โ ๏ธ *Why this fails on Codewars:*
While logically correct, itโs too slow for large values of
---
โก๏ธ AI/Optimal Solution (Codewars Accepted โ )
๐ Key Notes:
-
- Checking
- Negative numbers can't be perfect squares, so return
โ Best Practice: Use mathematical functions when available. They are optimized and make your code cleaner and faster.
---
๐ก *Follow for more real-world coding challenges and clean solutions!*
#Python #Codewars #Math #PerfectSquare #LearnToCode #ProgrammingTips
๐ท You love building blocksโespecially square ones! But can you tell if the total number of blocks you have can form a perfect square?
๐ฏ Task:
Given an integer
n
, determine whether it's a *perfect square* (i.e., the square of an integer).๐ง Examples:
-1 => False
0 => True
3 => False
4 => True
25 => True
26 => False
---
๐งช My Initial Solution (Not Efficient for Large Numbers)
def is_square(n):
def find_factors(n):
factors = []
for i in range(1, n + 1):
if n % i == 0:
factors.append(i)
return factors
factors = find_factors(n)
for i in factors:
if i * i == n:
return True
if n == 0:
return True
return False
โ ๏ธ *Why this fails on Codewars:*
While logically correct, itโs too slow for large values of
n
, because it checks all factors up to n
.---
โก๏ธ AI/Optimal Solution (Codewars Accepted โ )
import math
def is_square(n):
if n < 0:
return False
sqrt_n = math.isqrt(n) # Efficient integer square root
return sqrt_n * sqrt_n == n
๐ Key Notes:
-
math.isqrt(n)
returns the integer square root efficiently (no floating point issues).- Checking
sqrt_n * sqrt_n == n
is both fast and accurate.- Negative numbers can't be perfect squares, so return
False
early.โ Best Practice: Use mathematical functions when available. They are optimized and make your code cleaner and faster.
---
๐ก *Follow for more real-world coding challenges and clean solutions!*
#Python #Codewars #Math #PerfectSquare #LearnToCode #ProgrammingTips
๐ Day 3: Basic Arithmetic & Input/Output in Python
Letโs build the foundation of your programming skills with math operations and user input handling!
---
๐ข Basic Arithmetic Operations
---
๐งฎ Order of Operations (PEMDAS)
---
๐ฃ User Input and Output
---
### ๐ Convert Input to Numbers
---
โ Practice Problems
1๏ธโฃ Area of a Rectangle
2๏ธโฃ Square of a Number
3๏ธโฃ Basic Arithmetic Calculator
---
๐ฏ Goal for Day 3
โ Use Pythonโs arithmetic operators confidently
โ Understand input/output with
โ Practice writing small interactive programs
โ
๐จโ๐ป Keep coding! Day 4 coming soon...
#Python #Day3 #LearnToCode #ProgrammingBasics #BeginnerFriendly
Letโs build the foundation of your programming skills with math operations and user input handling!
---
๐ข Basic Arithmetic Operations
print(5 + 3) # Addition โ 8
print(10 - 4) # Subtraction โ 6
print(7 * 2) # Multiplication โ 14
print(15 / 3) # Division โ 5.0
print(15 // 2) # Floor Division โ 7
print(15 % 4) # Modulus โ 3
print(2 ** 3) # Exponentiation โ 8
---
๐งฎ Order of Operations (PEMDAS)
print(2 + 3 * 4) # Output: 14
print((2 + 3) * 4) # Output: 20
---
๐ฃ User Input and Output
name = input("What is your name? ")
print("Hello, " + name + "!")
---
### ๐ Convert Input to Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
---
โ Practice Problems
1๏ธโฃ Area of a Rectangle
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area:", area)
2๏ธโฃ Square of a Number
num = int(input("Enter a number: "))
print("Square:", num ** 2)
3๏ธโฃ Basic Arithmetic Calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
print("Division:", num1 / num2)
---
๐ฏ Goal for Day 3
โ Use Pythonโs arithmetic operators confidently
โ Understand input/output with
input()
and print()
โ Practice writing small interactive programs
โ
๐จโ๐ป Keep coding! Day 4 coming soon...
#Python #Day3 #LearnToCode #ProgrammingBasics #BeginnerFriendly
๐ถโโ๏ธ Problem 4: Cartesia City Walk
You live in the city of Cartesia, where roads form a perfect grid.
Your walk app gives you directions like
๐ Youโve got 10 minutes and must return exactly to where you started.
โ Return
โ Otherwise, return
---
๐ง Your Idea (Step-by-Step Position Tracking)
๐ Why it works:
You simulate the entire walk and check if youโve come back to the original (0,0) point.
---
โก๏ธ AIโs Optimized Version (Count Matching)
โ ๏ธ Key Note:
This version does not simulate movement. It cleverly checks balance:
-
-
If thatโs true and the walk is 10 steps long โ โ you're back on time and at the start.
---
๐งช Example Tests
---
โ Key Learning:
- Use coordinate tracking for clarity and understanding
- Use frequency counting for efficiency and elegance
- Always validate input length before logic
โ
๐จโ๐ป Keep walking (and coding)!
#Python #Codewars #GridWalk #LearnToCode #DailyChallenge
You live in the city of Cartesia, where roads form a perfect grid.
Your walk app gives you directions like
['n', 's', 'w', 'e']
, each taking 1 minute and 1 block. ๐ Youโve got 10 minutes and must return exactly to where you started.
โ Return
True
if the walk takes 10 minutes and ends at the starting point. โ Otherwise, return
False
.---
๐ง Your Idea (Step-by-Step Position Tracking)
def is_valid_walk(walk):
if len(walk) != 10:
return False
x, y = 0, 0
for drn in walk:
if drn == 'n':
y += 1
elif drn == 's':
y -= 1
elif drn == 'e':
x += 1
elif drn == 'w':
x -= 1
return x == 0 and y == 0
๐ Why it works:
You simulate the entire walk and check if youโve come back to the original (0,0) point.
---
โก๏ธ AIโs Optimized Version (Count Matching)
def is_valid_walk(walk):
if len(walk) != 10:
return False
return walk.count('n') == walk.count('s') and walk.count('e') == walk.count('w')
โ ๏ธ Key Note:
This version does not simulate movement. It cleverly checks balance:
-
'n'
must equal 's'
-
'e'
must equal 'w'
If thatโs true and the walk is 10 steps long โ โ you're back on time and at the start.
---
๐งช Example Tests
print(is_valid_walk(['n','s','n','s','n','s','n','s','n','s'])) # โ True
print(is_valid_walk(['w','e','w','e','w','e','w','e','w','e'])) # โ True
print(is_valid_walk(['n','n','n','s','s','s','e','w','e','w'])) # โ True
print(is_valid_walk(['n','n','n','s'])) # โ False
---
โ Key Learning:
- Use coordinate tracking for clarity and understanding
- Use frequency counting for efficiency and elegance
- Always validate input length before logic
โ
๐จโ๐ป Keep walking (and coding)!
#Python #Codewars #GridWalk #LearnToCode #DailyChallenge
๐ Day 4: Conditional Statements in Python ๐
๐ฏ Objective:
Learn how to use
---
๐ What Are Conditional Statements?
They allow your program to make decisions based on certain conditions.
---
โ `if` Statement
Checks a condition. If
๐งญ `else` Statement
Runs when the
๐ `elif` Statement
Use it to check multiple conditions.
โ ๏ธ Important: Python uses indentation (4 spaces) to define blocks of code. Be consistent!
---
๐งช Practice Exercises:
1๏ธโฃ Check Even or Odd
2๏ธโฃ Grade Calculator
3๏ธโฃ Positive, Negative or Zero
---
๐ Goal for Today:
โ๏ธ Understand how
โ๏ธ Write decision-based programs
โ๏ธ Master proper indentation
#ConditionalStatements #IfElse #PythonBasics #PythonTutorial #CodingLessons #TechEducation #Day4
๐ฏ Objective:
Learn how to use
if
, elif
, and else
statements to make decisions in your code.---
๐ What Are Conditional Statements?
They allow your program to make decisions based on certain conditions.
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
---
โ `if` Statement
Checks a condition. If
True
, it runs the code inside.num = 10
if num > 5:
print("The number is greater than 5.")
๐งญ `else` Statement
Runs when the
if
condition is False
.num = 3
if num > 5:
print("The number is greater than 5.")
else:
print("The number is not greater than 5.")
๐ `elif` Statement
Use it to check multiple conditions.
num = 0
if num > 0:
print("The number is positive.")
elif num == 0:
print("The number is zero.")
else:
print("The number is negative.")
โ ๏ธ Important: Python uses indentation (4 spaces) to define blocks of code. Be consistent!
---
๐งช Practice Exercises:
1๏ธโฃ Check Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
2๏ธโฃ Grade Calculator
score = int(input("Enter your score: "))
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
elif score >= 60:
print("Grade: D")
else:
print("Grade: F")
3๏ธโฃ Positive, Negative or Zero
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
elif num == 0:
print("The number is zero.")
else:
print("The number is negative.")
---
๐ Goal for Today:
โ๏ธ Understand how
if
, elif
, and else
work โ๏ธ Write decision-based programs
โ๏ธ Master proper indentation
#ConditionalStatements #IfElse #PythonBasics #PythonTutorial #CodingLessons #TechEducation #Day4