-2

I have the following code working in Python 3.6, which returns the position of the given word in a list of all possible permutations of the characters of the word sorted alphabetically. See tests below for example. When submitting this code the test environment is using 2.7 which breaks my code, any idea how I can easily fix it? I'm not familiar with the difference between 2.7 and 3.6.

My code and tests:

def listPosition(word):
    val = [ord(char) - 96 for char in word.lower()]
    minn = 1
    maxx = npermutations(word)
    if all(a >= b for a, b in zip(val, val[1:])):
        return maxx
    if all(a <= b for a, b in zip(val, val[1:])):
        return minn
    for indx in range(len(word)):
            ordi = order(val[indx:],val[indx])
            between = (maxx+1-minn)
            if ordi is 0:
                maxx = maxx - int(between * (1-frequency(val[indx:],val[indx])))
            elif ordi is max([order(val[indx:],i) for i in val[indx:]]):
                minn = minn + int(between * (1-frequency(val[indx:],val[indx])))
            else:
                before = sumfreq(val[indx:],val[indx],'before')
                minn = minn + int(round((between * before),0))
                after = sumfreq(val[indx:],val[indx],'after')
                maxx = maxx - int(round((between * after),0))
    return maxx 
import operator from collections 
import Counter from math 
import factorial from functools 
import reduce 
def npermutations(word):
    num = factorial(len(word))
    mults = Counter(word).values()
    den = reduce(operator.mul, (factorial(v) for v in mults), 1)
    return int(num / den) 
def frequency(val,value):
    f = [val.count(i)/len(val) for i in val]
    indx = val.index(value)
    return f[indx] 
def order(val,value):
    return sorted([i for i in set(val)]).index(value) 
def sumfreq(val,value,BorA):
    if BorA is 'before':
        check = [i for i in set(val) if i < value]
    if BorA is 'after':
        check = [i for i in set(val) if i > value]
    freqs = [frequency(val,i) for i in check]
    return sum(freqs)

tests = ['A','ABAB','AAAB','BAAA','QUESTION','BOOKKEEPER','ABCABC'] 
print(listPosition(tests[0]),"should equal 1") 
print(listPosition(tests[1]),"should equal 2") 
print(listPosition(tests[2]),"should equal 1") 
print(listPosition(tests[3]),"should equal 4") 
print(listPosition(tests[4]),"should equal 24572") 
print(listPosition(tests[5]),"should equal 10743") 
print(listPosition(tests[6]),"should equal 13")

Output from Python 3.6:

1 should equal 1
2 should equal 2
1 should equal 1
4 should equal 4
24572 should equal 24572
10743 should equal 10743
13 should equal 13

When submitting this code for grading, it turns out that the testing environment is using Python 2.7, which is giving me this output:

Test Passed
Incorrect list position for: ABAB: 0 should equal 2
Incorrect list position for: QUESTION: 40320 should equal 24572
Incorrect list position for: BOOKKEEPER: 0 should equal 10743
Test Passed
Test Passed
Cyber17C
  • 25
  • 2

1 Answers1

2

In python 2 the / operator acts like below

3 / 2 == 1 # True

While in python3

3 / 2 == 1.5 # True
3 // 2 == 1 # True

EDIT

The easiest way to solve this problem is add

from __future__ import division

at the first line of your code

James Liu
  • 497
  • 3
  • 8