Click here to Skip to main content
15,789,952 members

Comments by Guilherme Romero (Top 10 by date)

Guilherme Romero 7-Feb-23 23:12pm View    
Thanks a lot, it really worked. But the compiler of the course I'm doing doesn't allow me to use a function. I'm supposed to print the output directly whereas the compiler gives the input. So I changed the code a little and it became like this:
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
s = 'hzixbdqmdljvb'
max_length = 0
current_length = 0
start = 0
max_start = 0
for i in range(len(s) - 1):
if ord(s[i + 1]) >= ord(s[i]):
current_length += 1
else:
if current_length > max_length:
max_length = current_length
max_start = start
start = i + 1
current_length = 0
if current_length > max_length:
max_length = current_length
max_start = start


print('max_start:', max_start)
print('max_length:', max_length)
print('Longest substring in alphabetical order is: ',s[max_start:max_start + max_length + 1])
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

It does work in some cases, but it doesn't in most of them. In this case, when the input is s = 'hzixbdqmdljvb', the answer should be 'bdq' but I got 'ixbd'.
As you can see, I added print(max_start) and print(max_length) to see what happens. If you run the code, you'll see that max_start and max_length are different than they should.
Could you please help me in this case?
Thanks.
Guilherme Romero 7-Feb-23 20:59pm View    
Deleted
Thanks a lot.
It's working with some inputs, but not all of them.
For example, if s = 'jsvdyshlfcoypgtdud', the expected output is 'jsv', but the given answer is 'dyshl'
The same happens with s = 'vosssitenez', the real output is 'oss', but the output given is 'voss'
Or if s = 'cotojwhbzqrslew', the correct answer is 'cot', but it's giving 'ojwhb'.
Guilherme Romero 28-Jan-23 12:26pm View    
Ok, thank you.
Guilherme Romero 28-Jan-23 12:12pm View    
Thanks very much! Now "counts" is correct:
[('04', 3), ('06', 1), ('07', 1), ('09', 2), ('10', 3), ('11', 6), ('14', 1), ('15', 2), ('16', 4), ('17', 2), ('18', 1), ('19', 1)]

But still I'm getting a horizontal list instead of a vertical column. Do you know how to do it?

Thanks.
Guilherme Romero 28-Jan-23 11:44am View    
I've inserted the link to the data on the statement above.

The code I'm now using is this:

name = input("Enter file:")
if len(name) < 1:
name = "mbox-short.txt"
handle = open(name)
counts = dict();

for line in handle:
if line.startswith('From:'):
pass


elif line.startswith('From'):
x = line.split();
time = x[5];
t = time.split(':');
hour = t[0];
for line in hour:
counts[hour] = counts.get(hour,0) + 1;

print(sorted(counts.items()));