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

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    6

    Combinations of a String


    The following was a job interview question which I struggled with.
    (Unnecessary switching between list and set, tested it and realised it was missing an expected output, too many steps).
    If possible, looking for the proper answer or maybe a guide on how I should have tackled the problem. Thank you.

    Question: Give a String, find all possible combinations from it (Front and Reverse).
    Print all combinations and total count of combinations. Order doesn't matter.
    Example s = 'polo'
    Front Answer = 'p', 'po', 'pol', 'polo', 'ol', 'olo', 'lo', 'o', 'l'.
    Reverse Answer: 'o', 'ol', 'olo', 'olop', 'lop', 'op', 'p', 'l'.

    My answer:

    Code:
    count = 0
    count2 = -1
    length = len(s)
    my_list = []
    for i in s:
        temp = s[count:]
        temp2 = s[:count2]
        my_list.append(i)
        my_list.append(temp)
        my_list.append(temp2)
        count += 1
        count2 -= 1
    
    my_set = set(my_list) 
    for f in my_set:
        print(f)
    print(len(my_set)) # Answer for front
    
    new_list = []
    for f in my_set:
        new_list.append(f[::-1])
    
    print('Reverse Result:')
    for f in new_list:
        print(f)
    print(len(new_list)) # Answer for reverse
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    653
    Rep Power
    39
    You are missing the "l" in both answers. And there are many ways to reverse a string, but I am assuming that you are supposed to code your own. Hopefully this is constructive criticism. I would not hire you because 1. you use single letter, non-descriptive variable names, and nobody wants to try to understand and modify code written by a lazy programmer. 2. You store s[count:], etc. in a separate variable instead of using slicing.
    Code:
    def combos(test):
        output=[]
        for num_letters in range(1, len(test)+1):  ## capture for 1-->4 letters
            ## for each letter, store it + num_letters more
            for ctr in range(len(test)):
                ## if ctr+num_letters > len(string) the end of the string is substituted
                this_str=test[ctr:ctr+num_letters]
                if this_str not in output:
                    output.append(this_str)
        return output
    
    print(combos("polo"))
    test=""
    for ctr in range(len("polo")-1, -1, -1):
        test += "polo"[ctr]
    print(combos(test))
    print("'standard' way to reverse a string =", "polo"[::-1])
    Last edited by dwblas; October 8th, 2017 at 06:24 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    6
    Thanks for answering.
    Kind of hurtful being called lazy to be honest. (Been doing coding challenges for sometime now on a daily basis but something is simply not clicking and don't seem to have a flair for it).
    I do make it a point to declare descriptive variables.
    In this instance I was making up logic on the fly and lost the focus on proper naming and went with generic ones trying to write it fast.
    Did get the feeling I was getting a bit too generous with the amount of variables I was using though slicing didn't come to mind.
    Appreciate the feedback.

IMN logo majestic logo threadwatch logo seochat tools logo