简体   繁体   中英

How do I count the number of characters in a code using accumulation pattern without using len()?

Write code to count the number of characters in original_str using the accumulation pattern and assign the answer to a variable num_chars. Do NOT use the len function to solve the problem (if you use it while you are working on this problem, comment it out afterward!)

original_str = "The quick brown rhino jumped over the extremely lazy fox."
num_chars = len(original_str)
print(len(original_str))
for i in original_str:
    print(len(i))

The computer tells me this is correct, but it's doesn't answer the question. I must replace len with another function.

With the accumulator pattern, you have a variable, and you add to it when something happens. You can make that "something" mean "counting a particular character".

So, write a loop that steps through each character in the string, and each time you go through that loop, add one to a variable, starting from zero.

If you cannot use the len() function you could write a function like num_characters below that uses a for loop to iterate over the characters in the passed in string and increments and subsequently returns a variable total based on the total amount of characters. I think that is what you mean by an accumulator right?

def num_characters(string):
  total = 0
  for character in string:
    total += 1
  return total

original_string = "The quick brown rhino jumped over the extremely lazy fox."
print(f"The numbers of characters in the original string using `len` is {len(original_string)}.")
print(f"The numbers of characters in the original string using `num_characters` is {num_characters(original_string)}.")

Output:

The numbers of characters in the original string using `len` is 57.
The numbers of characters in the original string using `num_characters` is 57.
original_str = "The quick brown rhino jumped over the extremely lazy fox."

num_chars = original_str.count('') - 1

print (num_chars)
original_str = "The quick brown rhino jumped over the extremely lazy fox."
count = 0
for w in original_str:
    count = count + 1
num_chars = count
print(num_chars)

original_str = "The quick brown rhino jumped over the extremely lazy fox."

for num_chars in range(0,58): 
print(original_str.count)

You could write a loop that goes through each character in the string, and each time you go through that loop, add one to the accumulator variable. For this, you need to set the accumulator variable to zero before executing the loop.

original_str = "The quick brown rhino jumped over the extremely lazy fox."

num_chars = 0
for achar in original_str:
    num_chars = num_chars + 1

print (num_chars)
original_str = "The quick brown rhino jumped over the extremely lazy fox."

num_chars=0
for i in original_str:
    if i!=" ":
        num_chars+=1
print(num_chars)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM