Coding Shorts: User Input — A Competitive Programming Guide

Divyansh Chaudhary
8 min readOct 6, 2021

In my previous blog “User Input in Python with GUVI Codekata”, we saw how python handles input from the user. Using the input() statement, we were able to read various types of inputs. This blog focuses on applying that knowledge using some basic questions from GUVI Codekata and analyzing the output.

Source: Giphy

For this blog, we’ll look into some basics of the input() function and try to understand how we are required to read input for solving a question. These questions will help you brush up on some more concepts along the way. Practicing easier questions is never wasteful. With more practice, we’ll make progress overall and will be able to tackle much harder questions by breaking them down into simpler bits. Alright then, let's move on!

Question 1

The area of an equilateral triangle is ¼(√3a²) where “a” represents a side of the triangle. You are provided with the side “a”. Find the area of the equilateral triangle.

Input Description:

The side of an equilateral triangle is provided as the input.

Output Description:

Find the area of the equilateral triangle and print the answer up to 2 decimal places after rounding off.

Sample Input :

20

Sample Output :

173.21

First, we need to analyze the question and its requirements. Reading the problem statement, we can clearly figure out that we need to find the area of any given equilateral triangle with side “a”.

Note — The side name “a” as mentioned in the question can be named as any other variable name of your liking.

The problem statement provides a straightforward formula to obtain the result. The question needs just a single input in integer or float format as an input. Finally, we have to print the result up to 2 decimal places after rounding off. Before looking at the code, try the question yourself and verify if you arrived at the same solution.

Solving this question on Codekata would look like this:

Source: Author

Codekata provides its own code editor where you can choose any programming language to compile your code. The interface is pretty intuitive. After writing your code, just press the submit button, and voila! your code is compiled within seconds and checked against thoughtfully written test cases.

Looking into the “Private Testcase #1” we can see how we performed with our code:

Source: Author

Similarly, we would try to clear all the test cases for each question discussed in this blog.

Note — For this blog, we will use Google Colab as the code editor. This is to create a dynamic environment for the reader so they can run and edit the code in their own Colab notebooks. The code would work in exactly the same manner on Codekata platform.

Here, there are a few points to be explained:

  • We can use any variable name ( even when it is mentioned in the problem)
  • round(number[,ndigits]) is used to round the digits to ndigits=2 decimal places
  • Single integer input is read by using Type-Conversion on input using int(input(prompt)) function

The code itself is pretty self-explanatory, we used int(input()) to read in a number from the user, store it in the variable side and apply the formula of the area and store the result in a variable area. Now, using round(area, 2) we specify that we want to round the resulting area up to 2 decimal places.

Question 2

Print the First 3 multiples of the given number “N”. (N is a positive integer)

Note: print the characters with a single space between them.

Input Description:

A positive integer is provided to you as an input.

Output Description:

Print the First 3 multiples of the number with single spaces between them as an output.

Sample Input :

2

Sample Output :

2 4 6

So for this question, we can see that we need to somehow figure out a way to calculate the multiples of a number “N” and return the output in a single line. Single line output can be a bit tricky. You have to provide a space-separated output but also be careful so as to not have trailing whitespace in the output. There are a few ways to do it one of which is shown in this program:

Here, we initiate a Python list multiples which will store the first 3 multiples of the number “N”. Next, we input the value of “N” which is again a single integer input so we receive it in the same fashion as in the previous question. There are again multiple ways to accomplish this next task. We need to calculate the first three multiples of “N” —

  • One way would be to store a different variable for each multiple, this method will not require the list multiples as we can then directly print the values of the variables using print(var1, var2, var3) which will output the correct result.
var1 = N*1
var2 = N*2
var3 = N*3
  • The other preferable method would be to use range() and create a loop to find the multiples. For this question, we only required three multiples but, some other questions might require more and we cannot go on writing new variables for each value. Thus, to save time and space, we create a range starting from 1 going till 4 and having a step value of 1. This creates a list of numbers [1, 2, 3] on which we iterate, multiply with “N”, and then store the value in our multiples list.

Finally, to print the values from the list, you will notice that we have used an asterisk (*) before the variable multiples in our print statement. This asterisks symbol is used to interact directly with the arguments of the expression. This helps us print the values inside the list with spaces in between each value.

Question 3

You are provided with the radius of a circle “A”. Find the length of its circumference.

Note: In case the output is coming in decimal, roundoff to 2nd decimal place. In case the input is a negative number, print “Error”.

Input Description:

The Radius of a circle is provided as the input of the program.

Output Description:

Calculate and print the Circumference of the circle corresponding to the input radius up to two decimal places.

Sample Input :

2

Sample Output :

12.57

This question is similar to Question 1 with just a simple twist. We need to print “Error” whenever the input is negative. This is a really basic question that can be solved by editing just a few lines of code from the above solution.

Note — from math import pi is not a necessary import for this question as the value of “pi” can be directly set by creating a variable pi = 3.14.

To solve this problem, we simply take an integer input from the user and check if it’s less than 0. Any number less than 0 is a negative number and we have to print “Error” in this case. Therefore, whenever the number is greater than or equal to 0, we turn to the else part and execute the formula of calculating the circumference of a circle with radius “A”.

Question 4

You are provided with two numbers. Find and print the smaller number.

Input Description:

You are provided with two numbers as input.

Output Description:

Print the small number out of the two numbers.

Sample Input :

23 1

Sample Output :

1

This is a new kind of question. The problem wants the user to provide a space-separated input of integers and then figure out which integer has the least value. These types of questions which require a space-separated input are tackled by using functions map() and split(). We want to “split” the input on spaces, “map” the function int() on each value achieved from the “split”, and finally, obtain a list of integers.

Here, we see how split() , map() , and list() are used to extract the 2 numbers in two different variables num1 and num2. The underlying work would look like this:

input().split()
> Creates a list of strings like - ['23', '1']
map(int, ['23', '1'])
> Apply the function int() to each value in the list.
> It creates a "map object" which cannot be read by the user directly.
list("map object")
> Converts the "map object" back to user readable list format -
[23, 1]
Therefore, from input of "23 1", we get a list of integers [23, 1]

Now, we apply Python’s in-built function min(iterable, *[,key, default]) which returns the smallest item in an iterable or smaller of two values. We acquire the result by applying this function as “1”.

Question 5

You are given with Principle amount($), Interest Rate(%) and Time (years) in that order. Find Simple Interest.

Print the output up to two decimal places (Round-off if necessary).

(S.I. = P*T*R/100)

Input Description:

Three values are given to you as the input. these values correspond to Principle amount, Interest Rate and Time in that particular order.

Output Description:

Find the Simple interest and print it up to two decimal places. Round off if required.

Sample Input :

1000 2 5

Sample Output :

100.00

Although this question might look similar to Question 4 this time there is a huge twist. The problem wants us to “print the output up to two decimal places” which means that whatever be our answer, we need to print it with 2 decimal places and round-off if and when we have more than 2 decimal places. The trick is to know how to perform formatting while printing the final value.

First, let's clear out some doubts. We might want to use “float” instead of “int” while taking our input using map to change the input directly to numbers with decimal places. This will definitely add decimal places behind the number entered, but after evaluating the Simple Interest, when we print the result using simply print(si) , any value which has 2 trailing zeros in the decimal places will only print a single decimal place. Therefore, using this approach will not work. Even if we Type-Convert “si” again to float(), the result would still be the same.

So to achieve the correct answer even when there is a 0 or 1 decimal place value, we format the string using {:.2f}.format() which will allow us to have 2 decimal places at the end. Inside format(), we pass our round-off result of “si” so we don’t accidentally cut off the useful decimal places required for rounding the number.

Source: Giphy

Finally, with all these notes, you can move ahead and tackle some more questions using all the different kinds of inputs easily! Remember — Practice makes PROGRESS!

There are a large number of questions available on Codekata. From easy difficulty to hard, practicing questions is the fastest way to increase the speed and accuracy of achieving results. Codekata provides a great platform to practice various types of questions and test your code against well-built test cases. This blog only scratches the surface of questions that require a certain type of inputs. The reader is advised to use this as a guide moving forward to gain expertise in User Input in Python.

Thanks for reading.
Don’t forget to click on 👏!

--

--

Divyansh Chaudhary

Machine Learning and Python Student. Coding Enthusiast. Pursuing Bachelors in Computer Science.