Automatically translated.View original post

🐍 Daily Python Problem: Solve the classic FizzBuzz! 💖

Let's face the legendary FizzBuzz problem! This problem seems simple, but it is popular to use in interviews to see how well we understand Conditional Logic and Loops principles! ✨

Problem: Write the fizz _ buzz (n) function that will return a list of results from 1 to n according to the following rules:

• If that number is divisible by 3 (with a remainder of 0)\ rightarrow, use the word "Fisz."

• If that number is divisible by 5, use the word "Buzz."

• If the number is divisible by both 3 and 5, use the word "FizzBuzz."

• If you do not enter any conditions, use that number as usual.

🌟 Key Tips and 2 Solutions

1.Check order (very important!):

• We need to check the most complicated conditions (divisible 3 and 5 or num% 15 = = 0) before checking the sub-conditions (divisible 3 or divisible 5)! Because if we check divisible 3 first, the number divisible by 15 will be changed to "Fizz" and will never be able to enter the "FizzBuzz" condition again!

2. Solution (2 code patterns):

• Basic (Use If / Elif / Else): This method is straightforward. Use elif so that when you enter any conditions, you will not check any other conditions. Make sure the answer is correct in order.

• "Per String" Choice: This method does not need to check 15 first! We just create a string for free and use output _ string + = "Fizz" if 3 is divisible and output _ string + = "Buzz." If 5 is divisible, if the string is still empty (not in any condition), then use a number!

Example:

• Input: fizz _ buzz (15)

• Output: [1, 2, "Fizz," 4, "Buzz," "Fizz," 7, 8, "Fizz," "Buzz," 11, "Fizz," 13, 14, "FizzBuzz"]

It's a really good problem to practice Logic! Let's take both to practice writing! Keep going. 🥰

# Pytheseries # data # ai # programmer # learnonlemon8

2025/11/26 Edited to

... Read moreโจทย์ FizzBuzz เป็นหนึ่งในโจทย์ยอดนิยมที่มักใช้ในการสัมภาษณ์งานสำหรับตำแหน่งโปรแกรมเมอร์ เพราะช่วยทดสอบความเข้าใจพื้นฐานของ Conditional Logic และการใช้ Loop ได้เป็นอย่างดี หลักการของโจทย์นี้คือการสร้างฟังก์ชันที่วนลูปนับเลขตั้งแต่ 1 ถึง n และเปลี่ยนค่าที่ได้ตามเงื่อนไขเช็คหารด้วย 3, 5 หรือ 15 โดยเรียงลำดับการเช็คเงื่อนไขให้ถูกต้องเพื่อให้ผลลัพธ์ตรงตามโจทย์ ส่วนสำคัญของการเขียนโค้ด FizzBuzz คือการเช็คเงื่อนไขว่าเลขนั้นหารทั้ง 3 และ 5 ลงตัวก่อน (เช่น num % 15 == 0) เพราะถ้าเช็คทีหลังจะทำให้เลขที่ต้องแสดงผลเป็น "FizzBuzz" กลายเป็นแค่ "Fizz" หรือ "Buzz" ไปก่อนแล้ว ซึ่งจะทำให้ผลลัพธ์ผิดพลาด สิ่งนี้ถือเป็นหนึ่งในเคล็ดลับที่หลายๆ คนมองข้าม นอกจากนี้ยังมีวิธีเขียนโค้ดแบบต่อ String ที่ไม่จำเป็นต้องเช็คก่อนว่าเลขหาร 15 ลงตัวหรือไม่ วิธีนี้จะสร้างตัวแปร String เปล่าขึ้นมาก่อนแล้วเติมคำว่า "Fizz" เมื่อหาร 3 ลงตัว และเติมคำว่า "Buzz" เมื่อหาร 5 ลงตัว หากไม่ได้เติมคำใดเลยให้เก็บค่าเป็นตัวเลขตัวเดิม วิธีนี้ช่วยลดความซับซ้อนของโค้ดและเข้าใจง่าย ในแง่ของการพัฒนาทักษะ โปรแกรมเมอร์ควรฝึกเขียนทั้งสองรูปแบบให้ชำนาญเพื่อเพิ่มความยืดหยุ่นในการแก้ปัญหาและเข้าใจหลักการของ Loop กับ Conditional มากขึ้น นอกจากนี้การเรียนรู้และเข้าใจข้อผิดพลาดที่อาจเกิดขึ้น เช่น การลำดับคำสั่งเช็คเงื่อนไข จะช่วยให้เขียนโค้ดได้แม่นยำและมีประสิทธิภาพมากขึ้น สุดท้าย การทำโจทย์แบบนี้เป็นประจำจะช่วยให้เข้าใจการออกแบบอัลกอริทึมอย่างง่ายๆ และเป็นพื้นฐานที่ดีเพื่อเตรียมตัวสำหรับโจทย์โปรแกรมที่ซับซ้อนขึ้นในอนาคต ถือเป็นการลงทุนเวลาที่คุ้มค่าอย่างยิ่งสำหรับผู้สนใจโปรแกรมมิ่งทุกระดับ

Related posts

My Programming Interest Just Skyrocketed! 🚀
I just discovered something that has made my interest in programming explode to 1000000000000000%! 🤯 🎮 Learning Programming is Like a Game Adventure! From Python, Java, JavaScript, to HTML, there are all kinds of programming languages available, along with systematic topic courses. The learning p
Valder

Valder

2140 likes

Data analyst - what to study? 👩‍💻📈
1. Learn Key Data Analysis Tools and Programming Languages • Tip: Master the core tools and languages that data analysts use. Excel, SQL, Python, and R are essential for data manipulation, analysis, and visualization. SQL is particularly important for querying databases, while Python
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

2526 likes

How to Learn Coding Fast Even as a Beginner
#codingforbeginners #programming #studymotivations #webdevelopment #learntocode
Aysha

Aysha

784 likes

Important beauty hacks 🎀
Knowing our skin, hair, and health is very important! These are some things to help us understand ourselves better and know how to solve certain issues with our body. #beautyhacks #hairhealth #skinhealth #beautytips #makeuptips #skincare #lifestyle #Lemon8Diary #healthylife
sondos e

sondos e

2295 likes

Learning Websites to Level Up Your Skills
They make the learning journey feel less like a lecture and more like a chat with a knowledgeable friend. Codecademy – my coding guru! 🤓✨ Forget the boring textbooks; this platform brings the coding game to life with a user-friendly vibe and hands-on lessons. Learning Python, Java, or HTML? They
Reverelia

Reverelia

1437 likes

8 Free Online Certifications to Boost Your Resume
Hi, lemons! In today's competitive job market, having the right certifications can make a significant difference in showcasing your skills and expertise. Online certifications are not only convenient but also cost-effective. Here are six free online certifications that can give your resume a va
Lifestyle Babe

Lifestyle Babe

1214 likes

Career Advice: 5 Job Skills in Demand
Keeping skills up-to-date and relevant to your industry is key to staying competitive in the job market. Continuous learning and professional development are important to adapting to changing demands and technologies - here are 5 skills in demand right now to help with career growth. #lemon8partn
thewerklife

thewerklife

1503 likes

Major-Specific Strategies to Achieve a ✨4.0 GPA✨🎓🧠
🎓1. Business & Management Majors (Business Management, Accounting, Finance, Economics) ✅Active Participation: Example: In a Marketing class, when discussing a case study like Coca-Cola’s rebranding, share your analysis of the marketing mix (4Ps). This not only engages you in the materi
Chalie_Baker

Chalie_Baker

6986 likes

BOOST Your Career: 4 FREE Online Courses for You
Are you wanting to up your resume and LinkedIn profile? There are so many ways to go about it, but free courses can really help! Here are a few sites with free courses to take. Harvard + EdX Price: Free Course Topics: Computer Science, Programming with Python, Artificial Intelligence with Pyth
Itsleilahclaire

Itsleilahclaire

1739 likes

📌 Save! Top 3 Free Harvard Courses For Your Career
Hey everyone! 🌟 Ever dreamed of boosting your skills with courses from one of the world's leading universities? Well, guess what? Harvard offers some incredible courses for FREE, and I’m here to give you the scoop on the top three you shouldn’t miss. Whether you're looking to enhance yo
Pradeep M

Pradeep M

1041 likes

Becoming a tech girly 👩🏾‍💻: Learning How to Code
Hi! I'm new to Lemon8 ✨ and this is my first post ☺️ I'm currently working as a software engineer and thought I'd share some of my ideas about how you can get started with programming. I've invested a lot of time getting underrepresented groups into the field of tech through
Lauren Williams

Lauren Williams

462 likes

math apps for school 🧮
crying over math? (。•́︿•̀。) 💔 ⭐️ here’s some apps that can help ⭐️ ✖️photomath (scans problems and has step by step) - ⭐️FAV APP⭐️ ✖️ symbolab (over 10 diff calculators and scans problems) ✖️ solvely - Ai math solver (diff subjects that involve equations, uses ChatGPT) ✖️ MathPapa - ( gra
juanita ☆

juanita ☆

893 likes

study apps for A+ students 🫧💫💻
my top school apps 🌟 💫 for notes: Notion has an amazing organizational system, which is really helpful for taking notes that you know you are going to be using or adding onto for long periods of time. Notability is my go-to for when I want to take handwritten notes or have to work on lab as
may

may

675 likes

Maybe they don’t love, but to gain their trust is 🥰
#ballpython #ballpythonmorphs
jettt.black_

jettt.black_

174 likes

Why should girls learn to code?
Hey girls, have you ever thought about learning coding like I did? #study #learning #studywithme 👩‍💻Coding is a way to self-expression, creativity, and fostering intellectual confidence. Even if you're not planning to pursue a career in tech, learning to code can help you develop a
Aura✨

Aura✨

363 likes

Sites for CS Students!
Learning computer science can feel like drinking from a firehose sometimes, but the right resources make all the difference. Whether you’re debugging code, prepping for interviews, or diving into data science, these websites are absolute must-haves. GitHub is your go-to for managing projects and
CompSkyy

CompSkyy

211 likes

🔥 Assign Multiple Values in Python (Easy!)
🔥 Assign Multiple Values in Python (Easy!) Assigning multiple values in Python makes your code shorter, cleaner, and more readable 💻 Perfect trick for beginners and interview prep 🚀 Save this post and follow for daily Python tips 🐍 #Python #LearnPython #PythonBasics #PythonTips
TechKeysX

TechKeysX

0 likes

my favorite app for learning python 🫶
Plenty of people enjoy picking up a new language as a hobby, like Spanish or French... So, why not give learning a programming language a whirl? It could prove useful for work and also flex those computational thinking muscles. I stumbled upon this really interesting app. It's geared toward Pyt
Aura✨

Aura✨

526 likes

💰apps to be a rich girl
If you want to become richer, start by becoming richer in spirit! What you need to do is to develop good habits and take an hour out of your entertainment every day for self-improvement. 🌈And after a month, you'll find yourself with more competitive skills, a broader perspective, and a more
Aura✨

Aura✨

2030 likes

Second Brain Apps for knowledge and Ideas
These tools aren't just apps; they're your creative partners, making every idea, task, or project an exciting adventure. 📚 Roam Research is your go-to for visualizing relationships between concepts effortlessly. 🌐 Combining the simplicity of documents with the power of graph databases, l
Reverelia

Reverelia

258 likes

6 Free Online Certifications to Boost Your Resume
Hi, lemons! In a rapidly evolving job market, standing out can be a daunting task. Thankfully, online platforms have leveled the playing field, offering a wealth of world-class certifications—for free! Did your eyes just light up? They should have! Today, I'm happy to reveal six free online cer
Lifestyle Babe

Lifestyle Babe

390 likes

Tiger Albino Lavender Reticulated Python
Tiger Albino Lavender Reticulated Python Reticulated Python Size: 120 cm Sex: 0.1 Price: $830.86 #ballpython #ballpythons #reptile #reptiles
Ball Python Master 🐍

Ball Python Master 🐍

6 likes

A day in the life of a Artificial Intelligence (AI) Engineer 👩🏽‍💻 #vlog #dayinthelife #wfh #blackgirlmagic #blackwomenintech #womenintech #tech #techtok #fyp
Faïkat M.

Faïkat M.

171 likes

my favorite study websites ✨🫶🖥️
some of my favorite study websites 🫶 ☁️Miro - miro is a digital whiteboard platform that has a ton of cool templates for everything, from scrapbooking to scheduling. what i really like about this is that it is built for multiple people to be able to work on a whiteboard together, making it perfe
may

may

4080 likes

Apps to become a confident girl
For those seeking confidence and balance, there are now more options available. Whether you want to relax through meditation or enhance yourself by learning new skills, these 5 apps are just for you. 1️⃣ Headspace: This app focuses on meditation and relaxation, helping you reduce stress, impro
Aura✨

Aura✨

636 likes

Say Goodbye to the Lazy Girl Era🌼
#lazygirlera #apps #lazygirlhacks #2024habits #healthylifestyle #lifehacks #selfimprovement #selfdevelopment #lazygirl Contemporary fast-paced life has made me, like many girls, so busy every day that when I get home all I want to do is lie down flat. I don't want to go to s
Aura✨

Aura✨

218 likes

Braver than most adults.
#python #tour #lemon #wildlife #fyp
@mccarthyswildlife

@mccarthyswildlife

4 likes

Free Economics Courses from UC Berkley
If you are interested in economics, these FREE courses from UC Berkleya make learning easy and accessible for everyone 1. Fundamentals of Economics. In this course you are going to learn about: * Understand basic concepts of Economics through programming * Visualize Demand and Supply curves as
Ivy League Help

Ivy League Help

85 likes

✨ level up your skills with these platforms
‧°𐐪♡𐑂°‧₊ 🥯Looking to kickstart your coding journey for uni, work, or a side hustle? Codecademy is a personal favorite, offering interactive courses in Python, JavaScript, SQL, HTML/CSS, and even data science or web development paths. Programiz is perfect for beginners, with step-by-step tutorials
peachiesuga ♡

peachiesuga ♡

186 likes

Useful Python libraries for Data Analysts
Hello everyone! I wanted to highlight key Python libraries for data analysts that I find very useful! To start with, Pandas library excels in data manipulation with DataFrames and Series. NumPy supports efficient handling of large arrays and mathematical operations. Matplotlib offers versatile plot
Yun Jung

Yun Jung

61 likes

✨👩🏾‍💻🎒Websites EVERY STUDENT Should Have SAVED 💻👩🏾‍🎓✨
✨ Whether you’re in high school, college, or grad school, the right websites can make your study life 10x EASIER. These tools help with research, organization, studying, and even saving money. Bookmark them NOW! 🗂️ ✨ Must-Visit Student Sites: 1️⃣. Quizlet — Digital flashcards for memorizing ter
Margarita

Margarita

456 likes

Free Data Science Courses from Stanford
If you are interested in Data Science, these FREE courses from Stanford make learning easy and accessible for everyone: 1. Statistical Learning with R. In this course you are going to learn about: * Overview of statistical learning * Linear regression * Classification * Resampling methods *
Ivy League Help

Ivy League Help

106 likes

First Book of the Year!
if you're a fellow romance enjoyer, make sure to follow star.g.pages on insta for the journey and recommendations! #books #romancebooks #bookaesthetic #bookrecommendations #BookReview #bookstagram
annie grace

annie grace

189 likes

Resumé checklist: do NOT miss these! 📝
This may seem like basic advice, but I get a lot of DMs asking me to help review resumes and you’d be surprised how many people miss these key points! Keeping your resume updated is essential for reflecting your most recent skills, experiences, and achievements. Regular updates keep you in the r
Jane 🦧

Jane 🦧

84 likes

Why analysts should use Python for Data Processing
Hello Data Analysts! I wanted to highlight Python's efficiency in data cleaning and preprocessing. Python libraries like Pandas and NumPy offer methods to handle missing data, transform data types, and encode categorical variables like one-hot encoding. Tools such as drop_duplicates() ensure da
Yun Jung

Yun Jung

43 likes

Data Structures & Algorithms Was EASY With These!
When I first started learning DSA, finding the right resources felt overwhelming. That’s why I want to share the playlists that made a huge difference for me! Abdul Bari’s Algorithms Course was a lifesaver—his clear and simple explanations helped me truly understand complex concepts like sorting
CompSkyy

CompSkyy

27 likes

My reptiles & amphibian
#reptiles #reptilesoflemon8 #geckos #python #amphibian
M Darlene

M Darlene

2 likes

Building my own 3D engine from scratch.
The "Proud Developer" Vibe • "Turning lines of code into 3D art. 🐧💻" • "Finally got the wireframe rendering correctly. Worth the 1,000+ lines of Python!" • "Building my own 3D engine from scratch. Pygame is more powerful than people think ." Witty & Short •
ShadowRoot17

ShadowRoot17

1 like

Free Computer Science Courses
Free Computer Science Courses from Columbia If you are interested in Computer Science, these FREE courses from Columbia make learning easy and accessible for everyone 1. Analytics in Python. In this course you are going to learn about: * Python review * Data interchange formats: JSON and XML
Ivy League Help

Ivy League Help

16 likes

Python Variable Names Explained (Avoid These Mista
Confused by Python code? 😵‍💫 The problem might be bad variable names. In this post, you’ll learn: ✔ What variable names are ✔ Rules you must follow ✔ Good vs bad examples ✔ Pro tips to write clean Python code Save this post 📌 and follow for more Python basics made easy 🐍✨ #Python #LearnPy
TechKeysX

TechKeysX

0 likes

Data Analyst - Start a career today
1. Master Essential Tools and Skills - Why It’s Crucial: Employers prioritize technical proficiency for handling and visualizing data. - Focus Areas: - Excel: For basic data analysis and pivot tables. - SQL: To query and manage large datasets (a must-have skill). - Python/
vedha | career tips (tech) 👩‍

vedha | career tips (tech) 👩‍

213 likes

Albino Burmese python
#snake #reptiles #python
BS Reptiles

BS Reptiles

2 likes

The best comedies on Netflix 🎬
– School of Rock (2003) Genre:Comedy, Music Stars:Jack Black, Joan Cusack, Mike White Directed by Richard Linklater It’s been over two decades since School of Rock hit theaters, and it’s still one of Jack Black’s best movies. The story follows Dewey Finn (Black), a musically talented slacker
Rachelle Smith

Rachelle Smith

29 likes

Had this beautiful girl for 4 years now ❤️ #ballpython
dutch_dabbles🇺🇸

dutch_dabbles🇺🇸

0 likes

#piedballpythons #ballpython #pet #animal #reptile
Port City Reptiles

Port City Reptiles

4 likes

Excellent episode for anyone learning Python!!! 🎧
Podcast: Talk Python To Me! #hellolemon8 #programming #learningcode #machinelearning #DataScience #AI #DataEngineering #Python #BigData
DudeLearninCode

DudeLearninCode

2 likes

See more