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 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:...
Python programming: can someone please fix my code to get it to work correctly? The program...
Python programming: can someone please fix my code to get it to work correctly? The program should print "car already started" if you try to start the car twice. And, should print "Car is already stopped" if you try to stop the car twice. Please add comments to explain why my code isn't working. Thanks! # Program goals: # To simulate a car game. Focus is to build the engine for this game. # When we run the program, it...
Python 3 Fix the code so if the user does not select a value in the...
Python 3 Fix the code so if the user does not select a value in the sold by field, it shows a warning message indicating "Choose one value" import tkinter as tk from tkcalendar import DateEntry from openpyxl import load_workbook window = tk.Tk() window.title("daily logs") window.grid_columnconfigure(1,weight=1) window.grid_rowconfigure(1,weight=1) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="sold by").grid(row=3, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=4, sticky="W", pady=20, padx=20) #...
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