Question

In: Computer Science

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 ')
longest(s)

Solutions

Expert Solution

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


Related Solutions

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)
Can you please explain in detail what each line of code stands for in the Main...
Can you please explain in detail what each line of code stands for in the Main method import java.util.Scanner; public class CashRegister { private static Scanner scanner = new Scanner(System.in); private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100}; private static int cents[] = {25, 10, 5, 1}; public static void main(String[] args) { double totalAmount = 0; int count = 0; for (int i = 0; i < dollarBills.length; i++) { count = getBillCount("How many $"...
can you please explain each and every statement of the code without missing. it would be...
can you please explain each and every statement of the code without missing. it would be really helpful AVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would...
PYTHON PLEASE!! ALSO: if you can include an explanation of what the code means that would be appreciated 8.19 LAB*: Program: Soccer team roster (Dictionaries) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers and the ratings in a...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
Please provide Python code that does the following: 3) Write a program that takes a string...
Please provide Python code that does the following: 3) Write a program that takes a string as input, checks to see if it is comprised entirely of letters, and if all those letters are lower case. The output should be one of three possible messages: Your string is comprised entirely of lower case letters. Your string is comprised entirely of letters but some or all are upper case. Your string is not comprised entirely of letters. Your program may NOT:...
can you give me every single questions answers with reference. What do the terms “genotype” and...
can you give me every single questions answers with reference. What do the terms “genotype” and “phenotype” mean? Briefly explain the three factors that contribute to genetic variability. Independent assortment Crossover of homologues Random fertilization Briefly describe the following. Provide an example for each. Dominant-recessive inheritance Incomplete dominance inheritance Multiple-allele inheritance Sex-linked inheritance Polygene inheritance Extranuclear (mitochondrial) inheritance (not really a pattern What role, if any, do environmental factors play in gene expression? What is genetic screening? Why do they...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please...
1. Please program the following in Python 3 code. 2. Please share your code. 3. Please show all outputs. Instructions: Run Python code  List as Stack  and verify the following calculations; submit screen shots in a single file. Postfix Expression                Result 4 5 7 2 + - * = -16 3 4 + 2  * 7 / = 2 5 7 + 6 2 -  * = 48 4 2 3 5 1 - + * + = 18   List as Stack Code: """...
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
I have this mystery code. I dont know what the code does. Can somone please explain...
I have this mystery code. I dont know what the code does. Can somone please explain with examples. (python 3.xx) from typing import Dict, TextIO, Tuple, List def exam(d1: Dict[str, List[int]], d2: Dict[int, int]) -> None: """ *Mystery code* """ for key in d1: value = d1[key] for i in range(len(value)): value[i] = d2[value[i]]   
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT