Question

In: Computer Science

Assume you already have a non-empty string S, which is guaranteed to contain only digits 0...

Assume you already have a non-empty string S, which is guaranteed to contain only digits 0 through 9. It may be of any length and any number of digits may occur multiple times. Starting from the front of the string, write a loop that jumps through the characters in the string according to the following rule: Examine the current character and jump that many characters forward in the string Stop if you jump past the end of the string, or if you ever land on a 0 Keep track of how many characters are examined during this jumping process (including the final 0). Associate the answer with a variable called charCount. Example 1: For S = "11011", you will examine 3 characters (the first two 1s and then the 0. The last two 1s will not be examined). Example 2: For S = "3120" you will examine 2 characters (seeing the first 3, you will jump 3 spaces to the right and land on the 0). Example 3: For S = "11111" you will examine 5 characters.(in python)

Solutions

Expert Solution

Python Program:

""" Python program that counts the number of characters examined """
# Reading string
S = input("Enter S: ")

# Iterating over String
len = len(S)

# Counter for characters examined
examinedCharacters = 1

# Set position to 0
pos = 0

# Loop till all characters are examined or 0 is encountered
while True:
   # Fetching starting number
   val = int(S[pos])
   # Calculating ending position
   end = pos + val
   # Checking ending character
   if end >= len:
       break
   elif S[end]=='0':
       examinedCharacters=examinedCharacters+1
       break
   else:
       examinedCharacters=examinedCharacters+1
       # Updating position
       pos = end
      
print(examinedCharacters)
      

__________________________________________________________________________________________

Sample Run:


Related Solutions

Let S be the set of natural numbers which can be written as a non-empty string...
Let S be the set of natural numbers which can be written as a non-empty string of ones followed by a non-empty string of zeroes. For example, 10, 111100 and 11100000 are all in S, but 11 and 1110011 are not in S. Prove that there exists a natural number n∈S, such that 2018 | n.
a. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an octal (Base 8) digits (0-7)
In Python: 1 line short expressionsa. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an octal (Base 8) digits (0-7)b. Assume that x is a variable that has been given a string value. Write an expression whose value is True if and only if x is an letter.c. Assume that x is a variable that has been given a string value. Write...
A binary string is a “word” in which each “letter” can only be 0 or 1...
A binary string is a “word” in which each “letter” can only be 0 or 1 Prove that there are 2^n different binary strings of length n. Note: Your goal is to produce a properly constructed proof by induction, but this does not mean you have to follow Mathematical induction, step-by-step.. Write the statement with n replaced by k Write the statement with n replaced by k+1. Identify the connection between the kth statement and the (k+1)th statement. Complete the...
A binary string is a “word” in which each “letter” can only be 0 or 1...
A binary string is a “word” in which each “letter” can only be 0 or 1 Prove that there are 2^n different binary strings of length n. Note: Your goal is to produce a properly constructed proof by induction, but this does not mean you have to follow Mathematical induction, step-by-step.. Write the statement with n replaced by k Write the statement with n replaced by k+1. Identify the connection between the kth statement and the (k+1)th statement. Complete the...
A binary string is a “word” in which each “letter” can only be 0 or 1...
A binary string is a “word” in which each “letter” can only be 0 or 1 Prove that there are 2^n different binary strings of length n. Note: Your goal is to produce a properly constructed proof by induction, but this does not mean you have to follow Mathematical induction, step-by-step.. Write the statement with n replaced by k Write the statement with n replaced by k+1. Identify the connection between the kth statement and the (k+1)th statement. Complete the...
A binary string is a “word” in which each “letter” can only be 0 or 1...
A binary string is a “word” in which each “letter” can only be 0 or 1 Prove that there are 2^n different binary strings of length n. Note: Your goal is to produce a properly constructed proof by induction, but this does not mean you have to follow Mathematical induction, step-by-step.. Write the statement with n replaced by k Write the statement with n replaced by k+1. Identify the connection between the kth statement and the (k+1)th statement. Complete the...
An 8-digit password is required to have exactly three 0’s. The other 5 digits can be...
An 8-digit password is required to have exactly three 0’s. The other 5 digits can be any number 1-7, but numbers 1-7 may not be repeated. Write a program that lists all the possible outcomes. Provide your code and first 100 outcomes (Python).
Assume we have already defined two classes, ClickableShape and Pentagon. Using only the extends functionality of...
Assume we have already defined two classes, ClickableShape and Pentagon. Using only the extends functionality of Java, can we create a new class ClickablePentagon?
Which of the following groups contain only accounts that normally have credit balances? Accounts Receivable and...
Which of the following groups contain only accounts that normally have credit balances? Accounts Receivable and Fees Income b.    Salaries Expense and Accounts Payable c.     Fees Income and Notes Payable d.   Accounts Payable and Equipment The Chef Manager received a vendor shipment on the last day of the accounting period, after submitting the period’s ending inventory. What should be done to make sure the financials are correctly stated? Nothing Adjust the ending inventory Adjust the ending inventory and process the...
You need to assume you have your only inputs are coffee beans which you import from...
You need to assume you have your only inputs are coffee beans which you import from Brazil and labor which you hire from the local area. But you sell coffee lattes here in the local area using labor and coffee beans as the two inputs. Follow these steps: A. Draw demand and supply for your business or market. Label the x axis with quantity of coffee you making and the price level of coffee lattes on the y axis showing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT