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 convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
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...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
Hi there, please write code in Python 3 and show what input you used for the...
Hi there, please write code in Python 3 and show what input you used for the program. I've been stuck on this for hours! (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here;...
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....
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...
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:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT