Question

Implement function q1w so that it is just like q1 but uses a while loop instead...

Implement function q1w so that it is just like q1 but uses a while loop instead of a for loop.

def q1(inputString, inputDict, specialValue):
    s = 0
    s2 = 0
    a = 0.0
    for char in inputString:
        s = s + inputDict[char]
        if inputDict[char] == specialValue:
            s2 = s2 + 1
    a = s / len(inputString)
    return (a, s2)

Homework Answers

Answer #1

Implement function q1w so that it is just like q1 but uses a while loop instead of a for loop

inputScreen:

Code:


def q1w(inputString, inputDict, specialValue):
s = 0
s2 = 0
a = 0.0
char=1
while(char<=len(inputString)):
  
s = s + inputDict[char]
if inputDict[char] == specialValue:
s2 = s2 + 1
char=char+1
a = s / len(inputString)
return (a, s2)

# if any doubt please comment in the comment box...

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider function q1 below. Assume that inputString is not empty, that every character in inputString appears...
Consider function q1 below. Assume that inputString is not empty, that every character in inputString appears as a key in inputDict, that every value in inputDict is a non-negative integer, and that specialValue is a non-negative integer. q1 returns a tuple of two items. For example, q1("abcd", {'a':3, 'b': 4, 'c':0, 'd':0}, 4) returns (1.75, 1) In terms of inputString, inputDict, and specialValue a) describe the first returned item b) describe the second returned item def q1(inputString, inputDict, specialValue): s...
Implement a function that detects if there is a “loop” in a singly linked list. A...
Implement a function that detects if there is a “loop” in a singly linked list. A “loop” is defined as a link from one node to itself or to another node that is before the node in the list. The function may only make one pass through the list. It must return 1 if a “loop” exists; 0 otherwise. Be sure to test your function! You may start with code that you have written for other labs. This is a...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop...
Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider that your user might give you two of the same...
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
python problem: ( use a loop to read each character from the string and insert into...
python problem: ( use a loop to read each character from the string and insert into the stack) 1. The function main a5.py continually asks the user for a string, and calls isPalindrome to check whether each string is a palindrome. A palindrome is a word or sequence that reads the same backward as forward, e.g., noon, madam or nurses run. 2. Your function must ignore spaces: when the user enters 'nurses run', the function returns True, to indicate that...
Write a algorthim only by using while loop That uses a function, reverseDigit. The function takes...
Write a algorthim only by using while loop That uses a function, reverseDigit. The function takes an integer as a parameter and returns the number with its digits reversed. For example, the value ofreverseDigit(12345) is 54321; the value of reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-532) is -235.
Prove binary search (Seen below) is correct using a loop invariant. def search_binary(arr, val): low, up...
Prove binary search (Seen below) is correct using a loop invariant. def search_binary(arr, val): low, up = 0, len(arr) - 1 while low != up: # binary search mid = (low + up) / 2 if arr[mid] == val: return mid elif val < arr[mid]: up = mid - 1 else: low = mid + 1 return low
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
Pointer Rewrite: The following function uses reference variables as parameters. Rewrite the function so it uses...
Pointer Rewrite: The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, then demonstrate the function in a complete program. int doSomething(int &x, int &y) {      int temp = x;      x = y * 10;      y = temp * 10;      return x + y; }
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...