Merge the Tools!

  • + 15 comments

    My solution:

    def merge_the_tools(string, k):
    # your code goes here
    while string:
        s = string[0:k]
        seen = ''
        for c in s:
            if c not in seen:
                seen += c
        print(seen)
        string = string[k:]
    
    • + 0 comments

      simplest possible

    • + 2 comments

      I don't understand

      while string

      what is the logic of the while loop here, when it will break ??

      • + 1 comment
        string = string[k:]
        

        when the splicing is over

        • + 3 comments

          what does c do in for c in s:

          • + 0 comments

            c is an iterator which iterates character by character over the substring s

          • + 0 comments

            That c iterates over each character in the string 's'.

          • + 0 comments

            Chandan, let me explain:

            1. string = string[k:] -- This code dynamically splices the string k chunks,alternately you can do something like

            [ s[i:i+chunk_size] for i in range(0, chunks, chunk_size) ]

            And then iterate over this list of chunks 2. seen stores the unique list. If the alphabet is not present n seen, it will be added.This is done for all the chunks, reseting seen for each iteration.

      • + 0 comments

        It means run the loop the string is not None.

    • + 0 comments

      That's a very good solution

    • + 0 comments

      really great and simple solution

    • + 0 comments

      this code is brillent and easy to know how the code is work.. absolutly genius

    • + 0 comments

      Simple and less complex

    • + 1 comment

      Elegant solution!

      • + 1 comment

        seen = ' ' is not something I am familair with, can you please explain the logic here?

        • + 0 comments
          def merge_the_tools(string, k):
          # while loop runs till the string becomes empty
          while string:
              # splice the given string
              s = string[0:k]
              # initiate an empty string
              # (seen is just the name of our variable)
              seen = ''
              # run a for loop through our spliced string (s),
              # appending only unique characters to our variable seen
              for c in s:
                  if c not in seen:
                      seen += c
              print(seen)
              # remove the spliced part from original string for the next iteration
              string = string[k:]
          
    • + 0 comments

      I like Your solution. I made it somehow similiar to what you did. I add that I learn python 3rd day, so if someone see the problem in the code, all the comments are much appreciated.

      def merge_the_tools(string, k):

      for i in range (int(len(string)/k)):
          s = string[i*k:(i+1)*k]
          result = ''
          for j in range (0 ,k):
              if s[j] not in result:
                  result += s[j]
          print (result)
      
    • + 0 comments

      beautiful approach

    • + 0 comments

      wont non continous letters also get appended to seen which breaks one of the criteria? all test cases passed tho :) so i dont think the question isproperly framed

    • + 0 comments

      string = string[k:] superb

    • + 0 comments

      you = 'super awesome'

    • + 0 comments

      simple and straightforward. thanks for sharing

    • + 0 comments

      Excellent code, in this we dont have to use things like OrderedDict etc !