Raw Input

Problem Statement

Let’s look at Python’s input reading method.

>>> name = raw_input("Hey what's your name?\n")
Hey what's your name?
R2D2
>>> print name
R2D2

That’s one way of doing it for yourself. But in competitive programming we don’t ask questions. We only answer them. So we will only use raw_input() to read a string without asking any question to the system.

Task
Read a string from the console and print it.

Input Format
The first and only line of input contains a string.

Constraints
String length ≤ 500

Output Format
Print the same string back.

Sample Input

How many chickens does it take to cross the road?

Sample Output

How many chickens does it take to cross the road?

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
print raw_input()


Hello World!

Problem Statement

Python is an interactive language that allows us to execute statements individually and see their results. In other words, you don’t have to write a full code and compile it before you execute to see the results as in other languages like C++ and Java.

Here is a sample line of code that can be executed in Python

print "Hello World!" 

You can even store the string in a variable and then print it:

my_string = "Hello World!"
print my_string

The above code will print Hello World! on your screen. You can try it in the console below.

P.S. Do try Solve Me First and Solve Me Second in Python before moving ahead.

Solution

  
#This is a single line comment
'''
This is 
a 
multiline comment
'''
#write your code in next line. 
my_string = "Hello World!"
print my_string

Comparing Numbers

Problem Statement

Given two integers, X and Y, identify whether X<Y or X>Y or X=Y.

Comparisons in a shell script may either be accomplished using regular operators (such as < or >) or using (-lt, -gt, -eq, i.e. less than, greater than, equal to) for POSIX shells. This discussion on stack overflow contains useful information on this topic.

Input Format
Two lines containing one integer each (X and Y, respectively).

Output Format
Exactly one of the following lines:
X is less than Y
X is greater than Y
X is equal to Y

Sample Input 1

5  
2  

Sample Output 1

X is greater than Y  

Sample Input 2

2
2  

Sample Output 2

X is equal to Y   

Sample Input 3

2
3  

Sample Output 3

X is less than Y  

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

These discussions on stack overflow and geeksforgeeks contains useful information on this topic.

Solution

  
read first
read second
if [ $first -eq $second ]
then
	echo "X is equal to Y"
elif [ $first -gt $second ]
then
	echo "X is greater than Y"
else
	echo "X is less than Y"
fi

The World of Numbers

Problem Statement

Given two integers, X and Y, find their sum, difference, product, and quotient.

Input Format
Two lines containing one integer each (X and Y, respectively).

Input Constraints
100X,Y100
Y0

Output Format
Four lines containing the sum (X+Y), difference (XY), product (X×Y), and quotient (XY), respectively.
(While computing the quotient, print only the integer part.)

Sample Input

5  
2

Sample Output

7
3
10
2

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

Solution

  
read a
read b
echo $(($a+$b))
echo $(($a-$b))
echo $(($a*$b))
echo $(($a/$b))


Looping with Numbers

Problem Statement

for loops in Bash can be used in several ways: – iterating between two integers, a and b – iterating between two integers, a and b, and incrementing by c each time – iterating through the elements of an array, etc.

Use _for: loops to display the natural numbers from 1 to 50.

Input
There is no input

Output

1
2
3
4
5
.
.
.
.
.
50  

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for x in {1..50}
do
    echo $x
done

A Personalized Echo

Problem Statement

Write a Bash script which accepts name as input and displays a greeting: “Welcome (name)”

Input Format
One line, containing a name.

Output Format
One line: “Welcome (name)” (quotation marks excluded).
The evaluation will be case-sensitive.

Sample Input 0

Dan  

Sample Output 0

Welcome Dan  

Sample Input 1

Prashant

Sample Output 1

Welcome Prashant

A quick introduction to ‘Echo’

This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings `$USER, your current working directory is $`PWD"

This picks up the values of the environment variables USER and PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Accepting Inputs

This can be accomplished using the read statement.

read number
echo "The number you have entered is $number"  

If the user provides an input 4, the output is:

The number you have entered is 4  

Consider this snippet:

read name
echo "The name you have entered is $name"  

If the user provides an input Max, the output is:
The name you have entered is Max

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.

Solution

  
read name
echo "Welcome $name"

Looping and Skipping

Problem Statement

for loops in Bash can be used in several ways:
– iterating between two integers, a and b
– iterating between two integers, a and b, and incrementing by c each time
– iterating through the elements of an array, etc.

Your task is to use for loops to display only odd natural numbers from 1 to 99.

Input
There is no input.

Output

1
3
5
.
.
.
.
.
99  

Recommended Resources

A quick but useful tutorial for bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for (( c=1; c<=100; c+=2 ))
do
   echo "$c"
done

Let’s Echo

Problem Statement

Write a bash script which does just one thing: saying “HELLO”.

Input Format
There is no input file required for this problem.

Output Format
HELLO

Sample Output
HELLO

A quick introduction to ‘Echo’
This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings $USER, your current working directory is $PWD"

This picks up the values of the environment variables $USER and $PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Recommended Resource
A quick but useful tutorial for bash newcomers is here.

Solution

  
echo "HELLO"

Solve me first FP

Problem Statement

This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning 2 integers from STDIN, calling a function, returning a value, and printing it to STDOUT.

Your task is to scan two numbers from STDIN, and print the sum A+B on STDOUT.

Note: The code has been saved in a template, which you can submit if you want.

Input Format
Given A and B on two different lines.

Output Format
An integer that denotes Sum (A + B)

Constraints
1 ≤ A, B ≤ 1000

Sample Input

2
3

Sample Output

5

Solution : In Haskell

  
solveMeFirst a b = a + b

main = do
    val1 <- readLn
    val2 <- readLn
    let sum = solveMeFirst val1 val2
    print sum


Basic Data Types

Problem Statement

C++ has the following data types along with their format specifier:

  • Int (“%d”): 32 Bit integer
  • Long (%ld): 32 bit integer (same as Int for modern systems)
  • Long Long (“%lld”): 64 bit integer
  • Char (“%c”): Character type
  • Float (“%f”): 32 bit real value
  • Double (“%lf”): 64 bit real value

Reading
In order to read a data type, you need the following syntax:

scanf("`format_specifier`", &val)

E.g., in order to read a character and then a double

char ch;
double d;
scanf("%c %lf", &ch, &d);

P.S.: For the moment, we can ignore the spacing between format specifiers.


Printing
In order to print a data type, you need the following syntax:

printf("`format_specifier`", val)

E.g., in order to print a character and then a double

char ch = 'd';
double d = 234.432;
printf("%c %lf", ch, d);

P.S.: For the moment, we can ignore the spacing between format specifiers.

Input Format

Input will consists of an int, long, long long, char, float and double, each separated by a space.

Output Format

Print the elements in the same order, but each in a new line.

Sample Input

3 444 12345678912345 a 334.23 14049.30493

Sample Output

3
444
12345678912345
a
334.23
14049.30493

Solution

  
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    long b;
    char ch;
    long long c;
    float d;
    double f;    
    scanf("%d %ld %lld %c %f %lf  ", &a,&b,&c,&ch,&d,&f);
    printf("%d\n%ld\n%lld\n%c\n%f\n%lf",a,b,c,ch,d,f);
    return 0;
}