1

I want a code that input the number of lines (empty ones are counted), then the lines itself, then the word I want to find and finally, where the first occurrence of one character happened. Please with no imports.

I have this rn:

lines = int(input())
phrases = []

for i in range(lines):
  phrase = str(input())
  phrase = phrase.replace(',','')
  phrase = phrase.replace('!','')
  phrase = phrase.replace('.','')
  phrase = phrase.replace(';','')
  phrase = phrase.replace(':','')
  phrase = phrase.replace('?','')
  phrase = phrase.replace(' ','')
  phrase = phrase.lower()
  phrases.append(phrase)

word = str(input())

So the input will be like:

6  
I have a computer  

Hello world    
Please help  
Dammit code  
I wish I finished this today  
happy   

I want the results to be like:

Word founded  
h: word 2, letter 1  
a: word 3, letter 1  
p: word 4, letter 4
p: word 7, letter 1  
y: word 16, letter 5

It can't be in the same word as the last one.
In the case where this doesn't happen, print:

Word not founded
macropod
  • 12,757
  • 2
  • 9
  • 21

2 Answers2

1

Something like this maybe:

lines = int(input())
words = []

for i in range(lines):
    phrase = input().lower()
    if phrase:
        words.extend(phrase.split(' '))

search_word = input()
output_rows = []
word_found = True

for i, word in enumerate(words):
    if search_word[0] in word:
        output_rows.append(f'{search_word[0]}: word {i+1}, letter {word.index(search_word[0])+1}')
        search_word = search_word[1:]
        if not search_word:
            break
else:
    print("Word not found")
    word_found = False

if word_found:
    print("Word found")
    print('\n'.join(output_rows))
0

You can try using the list.extend() method:

import re

lines = int(input("How many lines? >>> "))
words = []

for _ in range(lines):
    text = input(">>> ").lower()
    words.extend(re.findall(r'\b\S+\b', text))

word = input("Input word >>> ")
i = 0
output = []
for index, w in enumerate(words):
    for letter in word[i:]:
        if letter in w:
            output.append(f"{letter}: w {index + 1}, l {w.index(letter) + 1}")
            i += 1
            break
    else:
        print("Word not founded.")
        break
else:
    print("Word founded.")
    print("\n.join(output)")
Red
  • 26,798
  • 7
  • 36
  • 58
  • thank you so much!! the only that its missing its that it doesn't print "Word founded" when you find all the letters of the word, as I mention in the question. As well as "Word not founded" when not ALL of the characters are founded. For example: 2 'hello world' 'bye world' 'halo' it contais "h" but it doesn't contain "a", so it should be printed "Word not founded" – japanesemagic May 25 '21 at 19:31