To find a location that meets the conditions with Python sequential search???

Asked 2 years ago, Updated 2 years ago, 21 views

f=open('data.txt', mode='r', encoding='utf-8')
lines = f.readlines()

print(type(lines))
print(lines)
print(lines[1])

start = int (input ("enter start: ")))
last = int (input ("last time input: ")))

lst=[]
for i in range(start,last+1):
    lst.append(str(i)+'\n')
print("the sum of the number of times you look for", len(lst)
lst.reverse()
print(lst)
print(lines)
print(lst[0])
print(lines[0])

list_group=list(set(lst).intersection(lines))
list_group.sort()
print("Inquired interval", list_group)
print("Inquired interval length", len(list_group)))

index_num=[]
for i in list_group:
    for d in lines:
        if d == i:
            index_num.append(lines.index(d))
            continue
index_num.sort()
print(index_num)

index_odd_oven=[]
for i in index_num:
    index_odd_oven.append(i+1)

print(index_odd_oven)

odd_count = 0
even_count = 0
for i in index_odd_oven:
    if lines[i] == "even\n":
        even_count+=1
    else:
        odd_count+=1

print("Number of holes viewed: ", od_count")
print("even count: ",even_count")
print("Total: ", od_count+even_count")

Thanks to your help, it was resolved...

python

2022-09-21 11:49

1 Answers

import sys

lines=["Hall", "Jjak", "Hall", "Jjak", "Jjak", "Jjak", "Hall"]

start = int (input ("initial number of entries: ")))
last = int (input ("the last number of entries: "))

if start < 0 :
    sys.exit(0)
if last > len(lines):
    sys.exit(0)

odd = 0
even = 0
oddloc = []
evenloc = []
for idx, i in enumerate(lines[start:last]):
    ifi == 'Hall':
        odd += 1
        oddloc.append(idx)
    elifi == 'Jjak':
        even += 1
        evenloc.append(idx)

print('')
print('Number of holes: %d'%odd)
print('locations of hall',oddloc)
print('Number of pairs: %d'%even)
print('pair locations',evenloc)

#Enter the first number: 0
#Enter the last number: 8

#Number of holes: 5
#Locations of the hall [0, 2, 4, 6, 7]
#Number of pairs: 3
#Places of pairs [1, 3, 5]

I don't understand the question, but I think you're thinking about the index... I don't know if it's right.

I think it would be helpful to use enumerate.


2022-09-21 11:49

If you have any answers or tips


© 2025 OneMinuteCode. All rights reserved.