Home > Software design >  How can I loop through a concatenation
How can I loop through a concatenation

Time:12-02

I'm trying to split a word with a '.' after every letter which I was successful in doing, however, my next step is to split the current splitted words again but I dont want to repeat variations.

my expected output is this:

input word: amaxa first loop will give - a.maxa, am.axa, ama.xa, amax.a

then the next split should give - a.m.axa, a.ma.xa,a.max.a,

Essentially I wanted different variations of the word with '.' being added when a full loop had been exhausted however, my main issue was I had '.'s appearing next to each other and I tried to use a continue statement but it didn't work. Below is my source code

print("enter email without @gmail.com")

word = input("word: ")


lenofword = len(word) - 1

for i in range(0,lenofword):
    sliceword = word[:1 i]   "."   word[1 i:]
    lis.append(sliceword)

print(sliceword)

for j in range(0,lenofword):
    slices = sliceword[:1 j]   "."   sliceword[j 1:]

    if slices[i:] == slices[:]:
        continue
    print(slices)
    

ouput given:

enter email without @gmail.com
word: amax
a.max
am.ax
a.m.ax
am..ax
am..ax
ama.x
a.ma.x
am.a.x
ama..x

basically i want to get rid of the 'am..ax' and 'ama..x'

CodePudding user response:

It is easier to choose the two locations where the dots should be placed (may be the same location for the single dot case). For example:

for i in range(1, lenofword   1):
    for j in range(i, lenofword   1):
        sliced = ".".join(filter(None, [word[:i], word[i:j], word[j:]]))
        print(sliced)

This will print the following for the input word "amax":

a.max
a.m.ax
a.ma.x
am.ax
am.a.x
ama.x
  • Related