Start Coding By Solving Problems

Start Coding By Solving Problems

Programming is all about solving problems through well crafted code.

Learning to code is frustrating, but not impossible. With the right mindset and consistency, you can strengthen your coding muscles. In this tutorial series instead of teaching you Python concepts(which are available over the internet), I will focus on building programs with outline concepts. When building a program you will use different concepts like strings, variables, lists, operators, etc... So in our tutorial, I will first list the concepts we will use, and build the programs with them. But the over series will cover the core concepts of programming like loops, functions, conditions, etc...

This tutorial will start with the basics of Python, which I assume you know or can learn. The problems we will solve are from numerous coding challenge sites like Dmoj, Timus, and USACO. This is because the programming judges host different sets of problems on both sites, using them will allow us to cover all the topics we need. I have added the link for each question to read more on the problem description.

A Note: You will need to create an account before you can submit your code for validity on the systems.

So let's get started!!

In this problem set, we will be covering and using the concepts below:

  • Strings

  • Integer and Floating Point Numbers

  • Variables

  • Reading Inputs and Writing Outputs

  • Maths Operators and Type Conversion

Try Solving the problem by visiting the problem through the link provided, if you can't go through my solution, read the problem description, do it on paper, take a break, and come back later to try again. But don't ever give up.

Problem One : DMOJ problem dmopc15c7p2, Word Count

In This challenge, you have to write a program that can check the number of words in a sentence. We used the concepts of variables, input statements, string manipulations, and outputs

# Get a sentence from user
sentence = input("Enter a sentence:")

# Count the number of words from the input string using the count method
word_count = sentence.count(" ") + 1

# Display the Result to the user.
print(f"The Sentence: '{sentence}' has {word_count} words.")

# Sample Output: The Sentence: 'this is a test sentence' has 5 words.

Problem Two: DMOJ problem dmopc14c5p1, Cone Volume

In This Challenge, you have to write a program that calculates the volume of a cone using the formula:(π * r^2 * h)/3. We used the concepts of variables, math operators, and type conversion.

# Get math library
import math

# Get User Inputs, Radius and height
radius = eval(input("Enter radius:"))
height = eval(input("Enter height:"))

# Calculate the volume of the cone using the formula
volume = math.pi * (radius ** 2) * height / 3

# Display Results
print(f"The volume of a Cone with {radius} radius and {height} height is {volume}")

# Sample Output: The volume of a Cone with 12 radius and 23 height is 3468.318289563131

Problem Three: DMOJ problem wc17c1j2, How’s the Weather?

In This Challenge, you have to write a program that calculates the Celsius based on the Fahrenheit value given. Using the Formula: (5/9)*(Fahrenheit - 32)

# Get user input
fahrenheit = eval(input("Enter Fahrenheit:"))

# Calculate celsius
celsius = 5 / 9 * (fahrenheit - 32)

# Display Results
print(f"{fahrenheit} Fahrenheit is {celsius} Celsius.")

# Sample Output: 12 Fahrenheit is -11.11 Celsius

Problem Four: DMOJ problem wc16c1j1, A Spooky Season

In This Challenge, you have to write a program that repeats a set of characters in an input based on the user request(input).

# Get the number of characters you want
number = int(input("Enter a number:"))

# Generate the word with the number of characters 
word = f"sp{'o' * number}ky"

# Display Result

# Sample Output: spooooooky

Problem Five: DMOJ problem wc15c2j1, A New Hope

In This Challenge, you have to write a program that will print "far" N times.

# Get user input
number = int(input("Enter a Number:"))

# Generate the text with "far" keyword
text = f"A long time ago in a galaxy {'far, ' * (number - 1)}far away..."

# Display result

# Sample Output: A long time ago in a galaxy far, far, far, far, far away...

Problem Six: DMOJ problem ccc13j1, Next in Line

In This Challenge, you have to write a program that get the age of the oldest given the youngest and the middle

# Get User Input
youngest_age = int(input("Enter the age of the youngest child:"))
middle_age = int(input("ENter the age of the middle child:"))

# Calculate Oldest Age
oldest_age = 2 * middle_age - youngest_age

# Display Result
print(f"The Oldest Child is {oldest_age} years old.")

# Sample Output: The Oldest Child is 16 years old.

Problem Seven: DMOJ problem wc18c3j1, An Honest Day’s Work

In This Challenge, you have to write a program that calculates the total money of creating a bottle cap.

# Get User Inputs
paint_litres = int(input("Enter the amount of paint(litres):"))
bottle_caps = int(input("Enter the amount of litres(per badge):"))
poke_dollars = int(input("Enter the cost of each badge:"))

# Calculate the total money
total_money = (paint_litres // bottle_caps * poke_dollars) + (paint_litres % bottle_caps)

# Display Result
print(f"The Total Money is ${total_money} Dollars")

# Sample Output: The Total Money is $10 Dollars

Problem X-1: DMOJ Problem sac21ccp1, SAC '21 Code Challenge P1 - Shrinkage

In This Challenge, you have to write a program that find the area of the rectangle after shrinking it through the telescope!

# Get User Input
original_area = eval(input("Enter original area:"))

# Calculate Area
new_area = original_area // 40

# Display Result

# Sample Output: 20

Problem X-2: DMOJ Problem sac22cc1p1, SAC '22 Code Challenge 1 P1 - That Teacher

In This Challenge, you have to write a program that gets the number of candy bars will be left after they are being shared.

# Get user input
number_of_trick_or_treat = eval(input("Enter the number of trick_or_treaters:"))
number_of_candy_bars_to_give = eval(input("Enter the Number of Candy Bars to give each treaters:"))
number_of_candy_bars = eval(input("Enter the number of candy bars:"))

# Calculate the number of candies left
number_left = number_of_candy_bars - (number_of_trick_or_treat * number_of_candy_bars_to_give)

# Display Result

# Sample Output: 5

Problem X-3: DMOJ Problem ccc22j1, CCC '22 J1 - Cupcake Party

In This Challenge, you have to write a program that returns the number of cupcakes that are left over.

# Get User Input
number_of_regular_boxes = eval(input("Enter the number of regular boxes:"))
number_of_small_boxes = eval(input("Enter the number of small boxes:"))

# Calculate the number of left overs
number_of_leftover = (number_of_regular_boxes * 8) + (number_of_small_boxes * 3) - 28

# Display Result

# Sample Output: 3

With This we have come to the end of this session, of coding... How about take some coffee and recharge for next round.. haha just kidding.

You learn coding by writing coding. There are many differents ways of solving a problem, if your solution does the right thing but not like mine, it doesnt mean either is wrong. Rather its an opportunity for you to compare your code and mine to learn alternative approach.

Join me in the next post to learn more...