Wednesday, August 24, 2016

Learning Python!

Here are my first few basic Python programs (before my first Python assignment consuming and processing perf samples). :)


1)
#!/usr/bin/python
i = raw_input()
print i

2)
#!/usr/bin/python
T = int(raw_input())
print "Range of t is", range(T)
for i in range(T):
    print i
    ans = 42
    print "Case #%d: %d" % (i+1, ans)
    
3)
#!/usr/bin/python
T = int(raw_input())
for i in range(T):
    line = raw_input()
    N, J = line.split()
    N, J = int(N), int(J)
    print "N = %d, J = %d" % (N, J)   

4)
#!/usr/bin/python
T = int(raw_input())
i = 1
while i<=T:
    tmp = raw_input()
    N = int (tmp.split()[0])
    J = int (tmp.split()[1])
    print N, J
    print 2**N
    print 2**N-1
    print bin(2**N-1)
    i +=1   

5)
#!/usr/bin/python
#program 5
T = int(raw_input())
i = 1
while i<=T:
    tmp = raw_input()
    N = int (tmp.split()[0])
    J = int (tmp.split()[1])
    print N, J
    print 2**N
    print 2**N-1
    print bin(2**N-1)
    s = bin(2**N-1)[2:]
    print int(s, 2)
    print int(s, 3)
    print int(s, 4)
    print int(s, 10)
    i+=1      

6)
#!/usr/bin/python
 
#program 6
#defining functions
def factorial(n):
    ret = 1
    for i in range(n):
        ret *= i+1
     
    return ret
 
print factorial(5)
print factorial(4), factorial(3)

1 comment:

Featured Post

interviewBit Medium: Palindrome Partitioning II

Problem Name:  Palindrome Partitioning II Problem Description : https://www.interviewbit.com/problems/palindrome-partitioning-ii/ Problem Ap...