Generating Random strings using Python


Just wanted to share a very small implementation of generating random string values for a range of 6 chars, I am actually working on creating a hashing of the keys for some authentication module. I will share the same implementation soon for that Keep watching :).

Just for now have a look on this. Hope this helps some one out there.

import random
import string

def genearateRandomStrings():

//creates a combination of chars
chars=string.ascii_lowercase + string.ascii_uppercase

//now using the random module functions and list comprehension generate a set of random strings.
return ”.join(random.choice(chars) for x in range(6))

print genearateRandomStrings()


Custom Sorting of List using Tuple python

from collections import namedtuple

# make a basic Link class
Link = namedtuple(‘Link’, [‘id’, ‘submitter_id’, ‘submitted_time’, ‘votes’,
‘title’, ‘url’])

# list of Links to work with
links = [
Link(0, 60398, 1334014208.0, 109,
“C overtakes Java as the No. 1 programming language in the TIOBE index.”,”),
Link(1, 60254, 1333962645.0, 891,
“This explains why technical books are all ridiculously thick and overpriced”,”),
Link(23, 62945, 1333894106.0, 351,
“Learn Haskell Fast and Hard”,”),
Link(2, 6084, 1333996166.0, 81,
“Announcing Yesod 1.0- a robust, developer friendly, high performance web framework for Haskell”,”),
Link(3, 30305, 1333968061.0, 270,
“TIL about the Lisp Curse”,”),
Link(4, 59008, 1334016506.0, 19,
“The Downfall of Imperative Programming. Functional Programming and the Multicore Revolution”,”),
Link(5, 8712, 1333993676.0, 26,
“Open Source – Twitter Stock Market Game – “,”),
Link(6, 48626, 1333975127.0, 63,
“First look: Qt 5 makes JavaScript a first-class citizen for app development”,”),
Link(7, 30172, 1334017294.0, 5,
“Benchmark of Dictionary Structures”, “”),
Link(8, 678, 1334014446.0, 7,
“If It’s Not on Prod, It Doesn’t Count: The Value of Frequent Releases”,”),
Link(9, 29168, 1334006443.0, 18,
“Language proposal: dave”,”),
Link(17, 48626, 1334020271.0, 1,
“LispNYC and EmacsNYC meetup Tuesday Night: Large Scale Development with Elisp “,”),
Link(101, 62443, 1334018620.0, 4,
“research!rsc: Zip Files All The Way Down”,”),
Link(12, 10262, 1334018169.0, 5,
“The Tyranny of the Diff”,”),
Link(13, 20831, 1333996529.0, 14,
“Understanding NIO.2 File Channels in Java 7”,”),
Link(15, 62443, 1333900877.0, 1244,
“Why vector icons don’t work”,”),
Link(14, 30650, 1334013659.0, 3,
“Python – Getting Data Into Graphite – Code Examples”,”),
Link(16, 15330, 1333985877.0, 9,
“Mozilla: The Web as the Platform and The Kilimanjaro Event”,!topic/”),
Link(18, 62443, 1333939389.0, 104,
“github is making me feel stupid(er)”,”),
Link(19, 6937, 1333949857.0, 39,
“BitC Retrospective: The Issues with Type Classes”,”),
Link(20, 51067, 1333974585.0, 14,
“Object Oriented C: Class-like Structures”,”),
Link(10, 23944, 1333943632.0, 188,
“The LOVE game framework version 0.8.0 has been released – with GLSL shader support!”,”),
Link(22, 39191, 1334005674.0, 11,
“An open letter to language designers: Please kill your sacred cows. (megarant)”,”),
Link(21, 3777, 1333996565.0, 2,
“Developers guide to Garage48 hackatron”,”),
Link(24, 48626, 1333934004.0, 17,
“An R programmer looks at Julia”,”)]



def query():
c = 62443
lst =[]
for l in links:
if l.submitter_id == 62443:
return sorted(lst, key=lambda Link: Link.submitted_time)
print query()

ROT13 Algorithm Using Python

ROT13 (“rotate by 13 places”, sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome.(Source Wiki). Here Is the new version I have written to apply ROT 13 for alphabets only, special characters are not touched they will remain as it is.

I have tried to implement this Algorithm Using Python. Here it goes..

# ROT13 Algorithm

def rot(text):
arr = list(text)
for i in range(len(arr)):
if (ord(arr[i]) > 64 and ord(arr[i]) <91) or (ord(arr[i])>96 and ord(arr[i])<123): #check for alphabets only
nextCharValue = (ord(arr[i]) + 13) #sum
asciValue = ord(arr[i]) #ascii value
# print “ascii “,asci, ” sum”, sum
if asciValue > 64 and asciValue < 91:
if nextCharValue > 90:
newValue = ord(arr[i]) – 13
newValue = ord(arr[i]) + 13
elif asciValue> 96 and asciValue < 123:
if nextCharvalue > 122:
newValue = ord(arr[i]) – 13
newValue = ord(arr[i]) + 13

arr[i] = chr(newValue)

return ”.join(arr)


str = rot(‘How can you tell an extrovert from an introvert at NSA? ‘)
print “ROT13:-“, str
print “Back to original:- “, rot(str)

Please share your feedback, I am open for any suggestions.(you can also have a look on the code where I have tested this


R Vashi

Pattern search in String using Python

For fun I just thought about writing an algorithm on using pattern search in any given string using python, Lets assume you have a string with a value of “SSDDFFEERRTTYYSS” and a character array [‘D’,’D’], Now use the values of character array as a pattern and search for the maximum character occurrence of match in the string value. in plain words, we have a ..


pattern : [‘D’,’D’] i.e DD

Expected output is :3 (as DD pattern appears thrice [XXDDXXXXXDDDXXX).

Also assume the Char array can be dynamic, I mean could be of any limit of characters.

#@Author :rvashisht

char =[‘D’,’D’]
count = 0

i =0
j = 0
pattern = ”
charLength = len(char) # get the length of char array
for i in range(len(char)):
pattern += char[i]

for j in range(len(string)-1):
#extract the chars from string, it should match char pattern length
chars = string[ j : j+charLength] #search in string with pattern
if pattern == chars : #check if pattern match
print chars, “=”, pattern
count = count +1 # increase the counter

print “Total occurrence of pattern found”, count

Well this is just a try.. Please review and comment..




Bubble Sort using Python

I have recently started using Python basically Jython to use with Java applications. The love for python is growing, I kind of started liking code in Python. Though I am still looking for some more practical reasons where I can see the effective mixing of Java and Python. So I started with  very basic program of doing bubble sort on a collection. Here it goes.

import time;

class BubbleSort() :

def __init__(self):

def doSearch(self):
list = [33,23, 13, 43, 63,53,73,83,93, 3]
found = 1 # assume there is at least one item to sort
while (found == 1): #set infinite loop
pos = 0; # start with initial pos
for i in range(len(list)-1):
next = list[i+1] #get the next element
if list[i] > next :
list[i+1] = list[i] #do a swap
list[i] = next
#print list
pos = i;

          if pos == 0:
found = 0 # all sorted, exit loop

print list

def getTime(self):
localtime = time.asctime( time.localtime(time.time()))
print localtime;

if __name__ == “__main__”:
obj = BubbleSort()

Keep watching for few more useful posts on Python/Jython.

R Vashi

Code Bubble IDE


I have worked on many different IDE’s (Integrated Development Environment) like Eclipse, Intellij, Jboss Studio, JCreator and few others, but working on code bubbles programming environment is unbelievable. Its pretty awesome…

Here is the video link on You Tube. Check this out.. if you really love to code on Java. Really hats off to the Code Bubble team.