In: Computer Science
Python 3
can you explain what every single line of code does in simple terms please
so basically # every single line with an explanation thanks
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)
Basically ,Given function finds the longest string in ascending
order for given variable string.
And then calculate average value of that longest string.By
summation the ASCII value of all characters of that string divided
by string length.
//Function longest() find longest string and their average
value.
def longest(string):
start = 0;end = 1;i = 0; //variable declaration and
initialization,start stores the start index of longest string and
end stores the end index of longest string
while i<len(string): //while loop starts and run until
i<len(string)
j = i+1
while j<len(string) and
string[j]>string[j-1]://This loop run until value of j is lesser
than length of string and current character (character at index j )
is greater than previous character(character at index j-1)
j+=1 //increasing value of j by 1
//This while loop finds the current longest ascending string
if end-start<j-i: //if length of current longest
ascending string is greater then previous then update value of
start and end
end = j
start = i
i = j;
avg = 0
for i in string[start:end]: //calculating average value of the
longest string.By summing the ASCII value of all characters of that
string
avg+=int(i)
print('The longest string in ascending order is',string[start:end])
//prints longest ascending order string
print('The average is',avg/(end-start)) //prints average value of
longest ascending order string
s = input('Enter a string ') //prompt user to enter a string
longest(s) //calling longest function by passing string s