1

here is the question. No this is not homework this is self taught.

I am attempting the following question:

The mirror image of string vow is string wov, and the mirror image wood is string boow. The mirror image of string bed cannot be represented as a string, however, because the mirror image of e is not a valid character. The characters in the alphabet whose mirror image is a valid character are: b, d, i, o, v, w, and x. Develop function mirror() that takes a string and returns its mirror image but only if the mirror image can be represented using letters in the alphabet.

**My Code **

def mirror(word):
    '''returns mirror image of word but if it can be represented
       using letters in the alphabet'''
    for i in range (0,len(word)):
        if i == 'bdiovwx':
            print(str(word.reverse))
        else:
            print ('NOPE')

the result i am getting is nothing. As in i execute the program and nothing prints.

Any thoughts?

Thank you

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Christopher Jakob
  • 91
  • 1
  • 2
  • 11

3 Answers3

3

You don't need a for loop for this. Essentially, you are testing if all the characters of the word belong to one of the characters in 'bdiovwx', and so, you can just check fr exactly that - the subset relationship amongst the set of characters in word, and 'bdiovwx'.

Also, strings do not have a reverse method in python, so you can use the trick "string"[::-1] to print its reverse.

def mirror(word):
    '''
    returns mirror image of word but if it can be represented
    using letters in the alphabet
    '''
    if set(word).issubset(set('bdiovwx')):
        print(word[::-1])
    else:
        print ('NOPE')
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
1

Note that 'd' reflects a 'b' and 'b' reflects a 'd' in the mirror :)

def mirror(word):
    letters = list("bdiovwx")
    reflect_letters = list("dbiovwx")
    if set(word).issubset(letters):
        print ''.join([reflect_letters[letters.index(x)] for x in word[::-1]])
    else:
        print "NOPE!"
Phani
  • 3,267
  • 4
  • 25
  • 50
1

By fixing your algorithm, you can get the results. You just need to loop through the items, add all the elements to set and compare it later if it's included in the set of alphabets u defined 'bdiovwx' , notFound is used to stop the iteration when you found variable that doesn't belong to the accepted set u defined 'bdiovwx'

word[::-1] produces the reversed word

def mirror(word):
    '''returns mirror image of word but if it can be represented
       using letters in the alphabet'''
    i,notFound=0,True
    D=set()
    while notFound and i < len(list(word)):
        if word[i] not in list('bdiovwx'):
            notFound= False
        D.add(word[i])
        i+=1
    if notFound and D.issubset(set(list('bdiovwx'))):
        print word[::-1]
    else:
        print ('NOPE')

You can also use ALL operator, to verify that all the characthers in word are in 'bdiovwx', if it's True, then print the inverse, else; you print None

def mirror(word):
    if all(x for x in list(word) if x in list('bdiovwx')):
        print word[::-1]
    else:
        print ('NOPE')
Community
  • 1
  • 1
user3378649
  • 5,154
  • 14
  • 52
  • 76