In: Computer Science
Python 3
can someone explain in very simple terms what EVERY line of code does, including what j and i do
def longest(string):
start=0;end=1;i=0;
while i<len(string):
j=i+1
while j<len(string) and string[j]>string[j-1]:
j+=1
if end-start<j-i:
end=j
start=i
i=j;
avg=0
for i in string[start:end]:
avg+=int(i)
print('The longest string in ascending order
is',string[start:end])
print('The average is',avg/(end-start))
s=input('Enter a string ')
longest(s)
def longest(string):
start=0;end=1;i=0;
while i<len(string): #it will parse from first character to last
character
j=i+1 #j is taken to check for next charcter in the string suppose
in "Pavan" i points to p and j points to a
while j<len(string) and string[j]>string[j-1]: # it loops
through second character to until ascending order string
j+=1
if end-start<j-i: #suppose we have longest string with one size
and it checks for more long string and update start and end
end=j
start=i
i=j;
avg=0
for i in string[start:end]:
avg+=ord(i) #use ord instead of int;it sums the ascii values of
charcters
print('The longest string in ascending order
is',string[start:end])
print('The average is',avg/(end-start)) #the average is caluculated
by sum of ascii / no.of characters
s=input('Enter a string ')
longest(s)