Thursday 24 May 2018

Find the second largest number in a list using python

n = int(raw_input())
arr = map(int, raw_input().split())
print set(arr).sort()[-2]

Get a list of integers from user in python

>>> arr= map(int, raw_input().split())
3 4 6 7 8
>>> arr
[3, 4, 6, 7, 8]

Program to check leap year or not in python

In the Gregorian calendar three criteria must be taken into account to identify leap years:
  • The year can be evenly divided by 4, is a leap year, unless:
    • 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.

Read the contents of a file in python

//To read the entire contents  
with open("filename.txt") as f:
    contents = f.read()
 
//To read line by line 
#!/usr/bin/python

filename = "myfile.txt"
with open( filename ) as f:
    # file read can happen here
    # print "file exists"
    print f.readlines()

with open( filename, "w") as f:
    # print "file write happening here"
    f.write("write something here ") 

Wednesday 23 May 2018

Compile and Run a c program in Ubuntu/linux

  1. Open a file using the command vi filename.c
  2. Write the c program and save the file
  3. Compile the program using the command.
     gcc -o filename filename.c
    This command will invoke the GNU C compiler to compile the file and create an executable called filename
  4. To execute the program type the command ./filename

Example
  1. vi hello.c
  2. gcc -o hello hello.c
  3. ./hello

list operations in python

How to make each term of an array repeat, with the same terms grouped together? i/p: [A,B,C] o/p: [A,A,A,B,B,B,C,C,C] Solution: >...