#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Need answer for a Python interview question


    Hi Friends
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Note, please, that this code works in python2 and in python3.
    Code:
    def interview2(a,b):
        '''
            >>> interview2('brag','garb')
            True
            >>> interview2('hoax','Zamboni')
            False
        '''
        return list(a)==list(reversed(list(b)))
    Now, if you weren't allowed to use reversed,
    well, you solve that puzzle.

    OK, let's test:
    Code:
    $ python3 -m doctest -v /tmp/p.py
    Trying:
        interview2('brag','garb')
    Expecting:
        True
    ok
    Trying:
        interview2('hoax','Zamboni')
    Expecting:
        False
    ok
    1 items had no tests:
        p
    1 items passed all tests:
       2 tests in p.interview2
    2 tests in 2 items.
    2 passed and 0 failed.
    Test passed.
    If anyone understands why doctest in python 3 reports
    Code:
    1 items had no tests:
        p
    please explain. Thanks, Dave.

    The contents of my file /tmp/p.py is cut-n-paste as shown above. This command gives same report.
    Code:
    $ cd /tmp && PYTHONPATH='' python3 -m doctest -v p.py

    [edit]better expression may be
    a==''.join(reversed(b))
    I was unsure about applying reversed to an immutable.[/edit]
    Last edited by b49P23TIvg; April 10th, 2013 at 01:31 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Thanks b49P23TIvg


    Thanks b49P23TIvg
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    How to access those word from url


    Hi

    Can u please provide me exact code how to access those words from that url.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    wget http://www.puzzlers.org/pub/wordlists/pocket.txt
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Getting Error


    Hi

    I'm getting error while accessing words from URL, I used below

    Code:
    Python 3.3.1 (v3.3.1:d9893d13c628, Apr  6 2013, 20:30:21) [MSC v.1600 64 bit (AMD64)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> def IsReverse(word1, word2):
        if len(word1) != len(word2):
            return False
        i = 0
        j = len(word2)-1
    
    
        while j >= 0:
            if word1[i] != word2[j]:
                return False
            i += 1
            j -= 1
        return True
    
    >>> def SeparateLengths(list1):
        d = {}
        for k in list1:
            key = len(k)
            if key not in d:
                d[key] = [k]
            else:
                d[key].append(k)
        return d
    
    >>> def ReadFinToList(fin):
        words = []
        for line in fin:
            word = line.strip()
            words.append(word)
        return words
    
    >>> fin = open('http://www.puzzlers.org/pub/wordlists/pocket.txt')
    list1 = ReadFinToList(fin)
    overdict = SeparateLengths(list1)     
    for k in overdict:
        print k, len(overdict[k])
        
    SyntaxError: multiple statements found while compiling a single statement
    >>>
    Last edited by chetansaip; April 18th, 2013 at 02:50 AM. Reason: mistake
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    My error message is different. Why would you expect open to look on the internet?
    open('http://www.puzzlers.org/pub/wordlists/pocket.txt')
    I had a generic Open function that worked for several types of sources. I'd post it if I could find it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Need Code


    Hi

    With the above code we can find out whether the given 2 words are reverse pair or not, that's it.

    But how to write a program that finds all the reverse pairs in the given word list(http://wwwpuzzlers.org/pub/wordlists/pocket.txt). How can we access that list and check if there are any reverse pair words available and print only those words and their reverse that is available in the list.

    How to write a program for this in Python 3. Thanks

    Originally Posted by b49P23TIvg
    Note, please, that this code works in python2 and in python3.
    Code:
    def interview2(a,b):
        '''
            >>> interview2('brag','garb')
            True
            >>> interview2('hoax','Zamboni')
            False
        '''
        return list(a)==list(reversed(list(b)))
    Now, if you weren't allowed to use reversed,
    well, you solve that puzzle.

    OK, let's test:
    Code:
    $ python3 -m doctest -v /tmp/p.py
    Trying:
        interview2('brag','garb')
    Expecting:
        True
    ok
    Trying:
        interview2('hoax','Zamboni')
    Expecting:
        False
    ok
    1 items had no tests:
        p
    1 items passed all tests:
       2 tests in p.interview2
    2 tests in 2 items.
    2 passed and 0 failed.
    Test passed.
    If anyone understands why doctest in python 3 reports
    Code:
    1 items had no tests:
        p
    please explain. Thanks, Dave.

    The contents of my file /tmp/p.py is cut-n-paste as shown above. This command gives same report.
    Code:
    $ cd /tmp && PYTHONPATH='' python3 -m doctest -v p.py

    [edit]better expression may be
    a==''.join(reversed(b))
    I was unsure about applying reversed to an immutable.[/edit]
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    The problems of acquiring the information from the puzzling web site and of efficiently processing the list were solved in recent threads.

    Search the devshed for these
    site:forums.devshed.com wwwpuzzlers
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by b49P23TIvg
    If anyone understands why doctest in python 3 reports
    Code:
    1 items had no tests:
        p
    please explain. Thanks, Dave.
    I get the same message with python 2.7.3, and I believe it's because the root level of the module does not contain any tests.

    Given this code...

    Code:
    #!/usr/bin/python
    
    """
        >>> True == True
        True
    """
    
    def interview2(a,b):
        '''
            >>> interview2('brag','garb')
            True
            >>> interview2('hoax','Zamboni')
            False
        '''
        return list(a)==list(reversed(list(b)))
    ...you don't get that message.

    Code:
    Trying:
        True == True
    Expecting:
        True
    ok
    Trying:
        interview2('brag','garb')
    Expecting:
        True
    ok
    Trying:
        interview2('hoax','Zamboni')
    Expecting:
        False
    ok
    2 items passed all tests:
       1 tests in doct
       2 tests in doct.interview2
    3 tests in 2 items.
    3 passed and 0 failed.
    Test passed.

    Comments on this post

    • b49P23TIvg agrees : Thank you. Maybe there's a module scope disabling flag.
    http://www.catb.org/esr/faqs/smart-questions.html#code
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    K Thanks

    But how to write a program that finds all the reverse pairs in the given word list, eg: how to find the reverse pair words in a list of 20 words.

    BEcause with the above code we can only find out whether the given 2 words are reverse pair or not, that's it.

    I'm thinking of using the below code which is written for Python 2.7, but how can we write the same in Python 3.


    Code:
    def reverse_pair(word_list, word):
        """Checks whether a reversed word appears in word_list.
    
        word_list: list of strings
        word: string
        """
        rev_word = word[::-1]
        return in_bisect(word_list, rev_word)
            
    
    if __name__ == '__main__':
        word_list = make_word_list()
        
        for word in word_list:
            if reverse_pair(word_list, word):
                print word, word[::-1]
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    I don't use Python 3.2, but I do know that print is done differently in Python 3.2. That may be all you need to fix in your script.
  24. #13
  25. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    Read and experiment with PEP 3100.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo