Monday, June 22, 2015
Thursday, June 18, 2015
Wednesday, June 17, 2015
Remove punctation from the text - Python
import re
import string
def removePunctuation(text):
#converts to lowercase
text1 = text.lower()
#removes punctuation
#punctation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
text2 = re.sub('[%s]' % string.punctuation, ' ' ,text1)
#removes leading and trailing spaces
text3 = text2.strip()
return text3
print removePunctuation("'The best investments today,'")
print removePunctuation("Misery in Paradise;")
Output:
the best investments today misery in paradise
Monday, June 15, 2015
Program to find two largest numbers using Python
nums = [10, 90, 80, 60, 50]
maxone = 0
maxtwo = 0
for x in nums:
if(maxone < x):
maxtwo = maxone
maxone = x
elif (maxtwo < x):
maxtwo = x
print "First two largest numbers"
print "First largest number:", maxone
print "First largest number:", maxtwo
output:
maxone = 0
maxtwo = 0
for x in nums:
if(maxone < x):
maxtwo = maxone
maxone = x
elif (maxtwo < x):
maxtwo = x
print "First two largest numbers"
print "First largest number:", maxone
print "First largest number:", maxtwo
output:
First two largest numbers First largest number: 90 First largest number: 80
Friday, June 12, 2015
Conversion of decimal to hexadecimal, binary and octal in Python
#num= int(input("Enter a decimal number:))
num = 15
print "Decimal value of given number:", num
print "Octal value of", num, ":",oct(num)
print "Binary value of", num, ":",bin(num)
print "Hexadecimal value of", num, ":",hex(num)
num = 15
print "Decimal value of given number:", num
print "Octal value of", num, ":",oct(num)
print "Binary value of", num, ":",bin(num)
print "Hexadecimal value of", num, ":",hex(num)
Output:
Decimal value of given number: 15 Octal value of 15 : 017 Binary value of 15 : 0b1111 Hexadecimal value of 15 : 0xf
To interchange key value pairs of a dictionary object in Python
dt = {'a': '1', 'b' : '2', 'c': '3', 'd': '4'}
print "Original dictionary:", dt
dt1 = {}
for (x, y) in dt.iteritems():
(x,y) = (y,x)
dt1.update(dict([(x,y)]))
print "After interchanging key value pairs:", dt1
Output:
print "Original dictionary:", dt
dt1 = {}
for (x, y) in dt.iteritems():
(x,y) = (y,x)
dt1.update(dict([(x,y)]))
print "After interchanging key value pairs:", dt1
Output:
Original dictionary: {'a': '1', 'c': '3', 'b': '2', 'd': '4'} After interchanging key value pairs: {'1': 'a', '3': 'c', '2': 'b', '4': 'd'}
Wednesday, June 10, 2015
Sorting a list with out sort/sorted functions - Python
lst = [1,0,4,5,6,7,8,0,0,0]
print(lst)
cou = len(lst)
for i in range(cou):
for j in range(1, cou-i):
#descending
"""if lst[j] > lst[j-1]:
(lst[j-1], lst[j]) = (lst[j], lst[j-1])"""
#ascending
if lst[j] < lst[j-1]:
(lst[j-1], lst[j]) = (lst[j], lst[j-1])
print(lst)
print(lst)
cou = len(lst)
for i in range(cou):
for j in range(1, cou-i):
#descending
"""if lst[j] > lst[j-1]:
(lst[j-1], lst[j]) = (lst[j], lst[j-1])"""
#ascending
if lst[j] < lst[j-1]:
(lst[j-1], lst[j]) = (lst[j], lst[j-1])
print(lst)
Reverse each word in a sentence - Python
# your code goes here
st = "This is awesome!"
st1 = st.split(" ")
st2 = ''
st3 = []
print st1
for y in st1:
#print y
ls = len(y)
for x in range(len(y)):
st2 = st2 + y[ls - 1 -x]
st2 = st2 + ' '
st3.append(st2)
print ' '.join(st3)
st = "This is awesome!"
st1 = st.split(" ")
st2 = ''
st3 = []
print st1
for y in st1:
#print y
ls = len(y)
for x in range(len(y)):
st2 = st2 + y[ls - 1 -x]
st2 = st2 + ' '
st3.append(st2)
print ' '.join(st3)
Reverse a sentence in python
st = "This is awesome!"
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1
Reverse a string - Python
st = "awesome!"
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1
st1 = ''
ls = len(st)
for x in range(len(st)):
st1 = st1 + st[ls - 1- x]
print st1
Length of a string with out len() function - Python
st = " This is awesome!"
count = 0
for x in st:
count = count + 1
print count
if(len(st) == count):
print "it works!"
count = 0
for x in st:
count = count + 1
print count
if(len(st) == count):
print "it works!"
Java program to print two largest numbers
Java program to print maximum of two numbers:
package caveof;
public class Maxtwo {
public void twoMaxNumbers(int[] nums){
int maxone = 0;
int maxtwo = 0;
for(int n:nums){
if(maxone < n){
maxtwo = maxone;
maxone = n;
}
else if(maxtwo < n){
maxtwo = n;
}
}
System.out.println("First Max Number: "+maxone);
System.out.println("Second Max Number: "+maxtwo);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num[] = {5,34,78,2,45,1,99,23};
Maxtwo twomn = new Maxtwo();
twomn.twoMaxNumbers(num);
}
}
package caveof;
public class Maxtwo {
public void twoMaxNumbers(int[] nums){
int maxone = 0;
int maxtwo = 0;
for(int n:nums){
if(maxone < n){
maxtwo = maxone;
maxone = n;
}
else if(maxtwo < n){
maxtwo = n;
}
}
System.out.println("First Max Number: "+maxone);
System.out.println("Second Max Number: "+maxtwo);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int num[] = {5,34,78,2,45,1,99,23};
Maxtwo twomn = new Maxtwo();
twomn.twoMaxNumbers(num);
}
}
Subscribe to:
Posts (Atom)