Friday, November 11, 2016

Lists

 Lists
 Consider a list (list = []). You can perform the following commands:
  1. insert i e: Insert integer at position .
  2. print: Print the list.
  3. remove e: Delete the first occurrence of integer .
  4. append e: Insert integer at the end of the list.
  5. sort: Sort the list.
  6. pop: Pop the last element from the list.
  7. reverse: Reverse the list.
Initialize your list and read in the value of followed by lines of commands where each command will be of the types listed above. Iterate through each command in order and perform the corresponding operation on your list.
Input Format
The first line contains an integer, , denoting the number of commands.
Each line of the subsequent lines contains one of the commands described above.
Constraints
  • The elements added to the list must be integers.
Output Format
For each command of type print, print the list on a new line.
Sample Input
12
insert 0 5
insert 1 10
insert 0 6
print 
remove 6
append 9
append 1
sort 
print
pop
reverse
print
Sample Output
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
 
Solution: 
inputno=int(input())
newlist=[]
x=1
while (x<=inputno):
    temp=[]
    temp=input().split()
    if(temp[0]=="insert"):newlist.insert(int(temp[1]),int(temp[2]))
    if(temp[0]=="print"): print(newlist)
    if(temp[0]=="remove"):newlist.remove(int(temp[1]))
    if(temp[0]=="append"):newlist.append(int(temp[1]))
    if(temp[0]=="sort"):newlist.sort()
    if(temp[0]=="pop"):newlist.pop()
    if(temp[0]=="reverse"):newlist.reverse()
    x+=1; 

Thursday, November 10, 2016

continue vs pass in python

    Python has two different statements that can be used in loops one is 'continue' and other is 'pass' statement.Most of the beginners get confused with this two thinking that these two are having the same functionality but actually there is a slight difference in between these two statements.Below is the example for each of the statement so that you can compare and learn.

Pass Statement:
     Below is the example of pass statement
  
for letter in 'Python':
   if letter == 'h':
      pass
      print ('This is pass block')
   print ('Current Letter :', letter)

print ("Good bye pass")
Output for the above pass statement code:
   
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye pass
 
 
Continue Statement:
     Below is the example of continue statement
  
for letter in 'Python':
   if letter == 'h':
      continue
      print ('This is pass block')
   print ('Current Letter :', letter)

print ("Good bye continue")
Output for the above Continue statement code:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Good bye continue

    -If you compare both of the example statements you can see that 'h' is not printed in continue statements whereas it is available in pass statement.

 

Friday, November 4, 2016

Write a function

We add a Leap Day on February 29, almost every four years. The leap day is an extra, or intercalary, day and we add it to the shortest month of the year, February.
In the Gregorian calendar three criteria must be taken into account to identify leap years:
  • The year can be evenly divided by 4;
  • If the year can be evenly divided by 100, it is NOT a leap year, unless;
  • The year is also evenly divisible by 400. Then it is a leap year.
This means that in the Gregorian calendar, the years 2000 and 2400 are leap years, while 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years.Source
Task
You are given the year, and you have to write a function to check if the year is leap or not.
Note that you have to complete the function and remaining code is given as template.
Input Format
Read y, the year that needs to be checked.
Constraints
Output Format
Output is taken care of by the template. Your function must return a boolean value (True/False)
Sample Input
1990 
Sample Output
False  
Explanation
1990 is not a multiple of 4 hence it's not a leap year.


Python 3 Code:
def is_leap(year):
    leap = False
    if year%4 == 0 and (year%100 != 0 or year%400 == 0): leap = True
    return leap
year = int(input())
print(is_leap(year))
 
 

Output for Above code:

Input (stdin)
1990
Your Output (stdout)
False
Expected Output
False
 

Thursday, October 20, 2016

Java Datatypes

Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):
  • A byte is an 8-bit signed integer.
  • A short is a 16-bit signed integer.
  • An int is a 32-bit signed integer.
  • A long is a 64-bit signed integer.
Given an input integer, you must determine which primitive data types are capable of properly storing that input.
To get you started, a portion of the solution is provided for you in the editor.
Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Input Format
The first line contains an integer, , denoting the number of test cases.
Each test case, , is comprised of a single line with an integer, , which can be arbitrarily large or small.
Output Format
For each input variable and appropriate primitive , you must determine if the given primitives are capable of storing it. If yes, then print:
n can be fitted in:
* dataType
If there is more than one appropriate data type, print each one on its own line and order them by size (i.e.: ).
If the number cannot be stored in one of the four aforementioned primitives, print the line:
n can't be fitted anywhere.
Sample Input
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Sample Output
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long 
 

My Java Code 

import java.util.*;
import java.io.*;


class Solution{
    public static void main(String []argh)
    {

        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();

        for(int i=0;i<t;i++)
        {
            try
            {
             long x=sc.nextLong();
             System.out.println(x+" can be fitted in:");
             if(x>=-128 && x<=127)
             System.out.println("* byte");
             if(x>=-32768 && x<=32767)
             System.out.println("* short");
             if(x>=-(int)(Math.pow(2,31)) && x<=((int)(Math.pow(2,31))-1))
             System.out.println("* int");
             if(x>=-(long)(Math.pow(2,63)) && x<=((long)(Math.pow(2,63))-1))
             System.out.println("* long");
            }
            catch(Exception e)
            {
             System.out.println(sc.next()+" can't be fitted anywhere.");
            }

        }
    }
}

Output of MY JAVA Code
Input (stdin)
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Your Output (stdout)
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long
Expected Output
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long


Explanation:
can be stored in a short, an int, or a long.
is very large and is outside of the allowable range of values for the primitive data types discussed in this problem.