Question

In: Computer Science

Python 3 can someone explain in very simple terms what EVERY line of code does, including...

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)

Solutions

Expert Solution

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)



Related Solutions

Python 3 can you explain what every single line of code does in simple terms please...
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 ')...
Can someone please explain in simple terms 1. the impact of compounding frequency and 2. the...
Can someone please explain in simple terms 1. the impact of compounding frequency and 2. the difference between annual percentage rate and the effective annual rate. PLEASE ANSWER BOTH 1 AND 2 FOR ME
Can someone please explain to me why "return 1" in this Python code returns the factorial...
Can someone please explain to me why "return 1" in this Python code returns the factorial of a given number? I understand recursion, but I do not understand why factorial(5) returns 120 when it clearly says to return 1. def factorial(n): if n == 0: return 1 else: return n * factorial(n-1)
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int...
Can someone explain the code for each line?(how do they run,what do they mean) void printArray2D(int arr[M][N]){ int i, j; for(int i =0; i < M; i++){ for(int j=0; j<N; j++){ printf(" %d ", arr[i][j]);//print all the array elements } printf("\n"); } } void populateRandom2D(int arr[M][N]){ int i, j; int min, max; printf("Enter min number in the array:\n"); scanf("%d", &min); printf("Enter max number in the array:\n"); scanf("%d", &max); for(int i =0; i < M; i++){ for(int j=0; j< N; j++){...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When...
Can someone tell me what is wrong with this code? Python pong game using graphics.py When the ball moves the paddles don't move and when the paddles move the ball doesn't move. from graphics import * import time, random def racket1_up(racket1):    racket1.move(0,20)        def racket1_down(racket1):    racket1.move(0,-20)   def racket2_up(racket2):    racket2.move(0,20)   def racket2_down(racket2):    racket2.move(0,-20)   def bounceInBox(shape, dx, dy, xLow, xHigh, yLow, yHigh):     delay = .005     for i in range(600):         shape.move(dx, dy)         center = shape.getCenter()         x = center.getX()         y = center.getY()         if x...
Can someone explain Bessel's Equations in Layman's terms
Can someone explain Bessel's Equations in Layman's terms
Can someone please write clear and concise comments explaining what each line of code is doing...
Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks #include <stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/wait.h> int join(char *com1[], char *com2[]) {    int p[2], status;    switch (fork()) {        case -1:            perror("1st fork call in join");            exit(3);        case 0:            break;        default:...
Can someone write very similar code to this but have it read the Absolute value, Cos,...
Can someone write very similar code to this but have it read the Absolute value, Cos, Sin, and Tan. Also, have it pass from the main method. import java.util.Scanner; public class Manymethods{ public void Max ()    { Scanner scan = new Scanner( System.in); int [] arr = new int [4]; arr = new int [4]; int i = 0, s = 0; for (i = 0; i < 4; i++) { arr[i] = scan.nextInt (); if(arr[i] > s) s=arr[i];...
Can someone please explain the Carnot Cycle graph in terms of what equations to use and...
Can someone please explain the Carnot Cycle graph in terms of what equations to use and the concepts. This is what I think but I keep getting mixed up: Path A to B which is Reversible Isothermal Expansion, Delta U= 0, and according to first law Q= -W and we use RTln V2/V1 for W?  In path B to C, the gas expand Adiabatically, so Q= 0 and Delta U= - W. Since it is expansion so the work will be...
explain the code for a beginner in c what each line do Question 3. In the...
explain the code for a beginner in c what each line do Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT