Monthly Archives: June 2011

A sequence of n > 0 integers is called a jolly jumper if the absolute values of the difference between successive elements take on all the values 1 through n-1. For instance,
1 4 2 3
is a jolly jumper, because the absolutes differences are 3, 2, and 1 respectively. The definition implies that any sequence of a single integer is a jolly jumper. You are to write a program to determine whether or not each of a number of sequences is a jolly jumper.

A tug of war is to be arranged at the local office picnic. For the tug of war, the picnickers must be divided into two teams. Each person must be on one team or the other; the number of people on the two teams must not differ by more than 1; the total weight of the people on each team should be as nearly equal as possible.

Computing the Levenshtein distance is based on the observation that if we reserve a matrix to hold the Levenshtein distances between all prefixes of the first string and all prefixes of the second, then we can compute the values in the matrix by flood filling the matrix, and thus find the distance between the two full strings as the last value computed.

In 1969, Cole and Davie developed a two-player game based on the Euclidean algorithm, called The Game of Euclid,which has an optimal strategy.The players begin with two piles of a and b stones. The players take turns removing m multiples of the smaller pile from the larger. Thus, if the two piles consist of x and y stones, where x is larger than y, the next player can reduce the larger pile from x stones to x − my stones, as long as the latter is a non negative integer. The winner is the first player to reduce one pile to zero stones.Wikipedia

Two players,A and B play, starting with two natural numbers(stones), the first player A, subtracts any positive multiple of the lesser of the two numbers from the greater of the two numbers, provided that the resulting number must be non negative. Then the second player B, does the same with the two resulting numbers, then A, etc., alternately, until one player is able to subtract a multiple of the lesser number from the greater to reach 0, and thereby wins. For example, the players may start with (25,7):

25 7

11 7

4 7

4 3

1 3

1 0
One of the readers has rectified an issue with my previous code. I am displaying the code given by him

Firstly I am back to writing. I hope to put many articles and code here in the coming weeks.

Problem statement is simple: We need to find the next permutation of a number or string.

For example 112 has next permutation of 121
and a string for example chin has next permutation of chni.

The code has explanation.

def NextPerm(s):
Len = len(s)
# char at the end of string
EndChar = s[Len-1]
for i in range (Len-2,-1,-1):
# compare with char to left of the last char
c = s[i]
# if smaller, then it has to be substituted
if(c < EndChar):
# left to the char under check is remaining the same and put into buffer
buff = s[:i]
#print buff
# find next biggest digit on the right starting from end and notice the last char is the smallest)
for j in range(Len-1,i,-1):
# check if the digit is bigger
if(s[j] > c):
break
# Add the char to buffer
buff = buff + s[j]
# book keep the index
index = j
# add remaining chars in ascending order(reverse order) and put also insert C in its index
for j in range(Len-1,i,-1):
# check if the the char was used to replace 'c'
if(j == index):
# if yes, keep 'c' in its place
buff = buff + c
else:
# else copy next char
buff = buff + s[j]
return(buff)
# last char is stored for the loop process
EndChar = c
# if char are in descending order its the biggest permutation
return("its already the biggest permutation")
input = raw_input("Enter any number or string : ")
print "The next perm is :",NextPerm(input)

OUTPUT:

labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : abc
The next perm is : acb
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : acb
The next perm is : bac
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : bac
The next perm is : bca
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : bca
The next perm is : cab
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : cab
The next perm is : cba
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : cba
The next perm is : its already the biggest permutation
labuser@ubuntu:~$
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : 112
The next perm is : 121
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : 121
The next perm is : 211
labuser@ubuntu:~$ python NextPerm.py
Enter any number or string : 211
The next perm is : its already the biggest permutation
labuser@ubuntu:~$