Interchange two numbers

Problem Statement

You are given two integers. Store them into two variables and exchange them. Can it get any easier! Make sure you use tuple to do the task, rather than using any fancy logic. Print the two numbers.

Input Format

Two integers

Output Format

Two integers

Sample Input

2
1

Sample Output

1
2

Concept

A tuple is similar to a list. The difference is that a tuple is immutable i.e. it cannot be changed. Once you assign some value to a tuple, it cannot be changed. Also, no additional members can be added once a tuple is assigned.

Eg:
>> a = 1, # Define a tuple with one member
>> a = (2, 6)
>> a[1] = 1 # Can’t alter the elements in tuple
TypeError: ‘tuple’ object does not support item assignment

For traversing a tuple, the method is similar to that of a list. Tuples are most commonly used to assign more than one variables in one statement i.e. for simultaneous assignment.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=int(raw_input())
b=int(raw_input())
a,b=b,a
print a
print b

What’s Your Name?

Problem Statement

Let’s learn the basics of Python! You are given the first name and the last name of a person. Your task is to read them and print the following:

Hello firstname lastname! You just delved into python.

It’s that simple!

In Python you can read a line as a string using

s = raw_input()
#here s reads the whole line.  

Input Format The first line contains the first name, and the second line contains the last name.

Constraints
The length of the first and last name ≤ 10.

Output Format Print the output as mentioned above.

Sample Input

Guido
Rossum

Sample Output

Hello Guido Rossum! You just delved into python.

Concept The input read by the program is stored as a string data type. A string is a collection of characters.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
f = raw_input()
l = raw_input()
print "Hello" , f ,l + "! You just delved into python."

Arithmetic Operators

Problem Statement

Let’s learn about Python’s arithmetic operators.

First, let’s read two integers:

a = int(raw_input())
b = int(raw_input())

Now, the three basic arithmetic operators are the following:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)

(We’ll learn about division in the next task)

Task
Read two integers from STDIN and print three lines where:
(1) the first line contains the sum of the two numbers,
(2) the second line contains the difference of the two numbers (first – second),
(3) the third line contains the product of the two numbers.

Input Format
The first line contains the first integer, a, and the second line contains the second integer, b.

Constraints
1a1010
1b1010

Output Format
Print three lines as explained above.

Sample Input

3
2

Sample Output

5
1
6

Explanation
3+25
321
326

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
a = int(raw_input())
b = int(raw_input())
print a+b
print a-b
print a*b

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"