# Task 9: Replace the space character Write a Python program to replace the space character that occurs after a word ending with a or r with a newline character. Sample data : 'area not a a2\_b roar took 22' Sample output : area not a a2\_b roar took 22
时间: 2023-07-02 21:11:03 浏览: 142
Here's the Python code to achieve the desired output:
```python
data = 'area not a a2_b roar took 22'
output = ''
i = 0
while i < len(data):
if data[i] == ' ' and (i > 0 and (data[i-1] == 'a' or data[i-1] == 'r')):
output += '\n'
else:
output += data[i]
i += 1
print(output)
```
Output:
```
area not a a2_b roar took 22
```
相关问题
Write a program that asks a string to the user, and counts the number of time each character occurs in the string,output is a set
Sure, I can help you with that! Here's a Python program that does what you're asking for:
```
string = input("Please enter a string: ")
count = {}
for char in string:
if char in count:
count[char] += 1
else:
count[char] = 1
print("Character count:")
for char, num in count.items():
print(char, num)
```
This program prompts the user to enter a string, then creates a dictionary called `count` to keep track of how many times each character appears in the string. It loops through each character in the string, and if the character is already in the `count` dictionary, it increments the count by 1. If the character is not yet in the dictionary, it adds it with a count of 1. Finally, it prints out the character count for each character in the string.
## Problem 3: Store Digits Write a function `store_digits` that takes in an integer `n` and returns a linked list where each element of the list is a digit of `n`. ```python def store_digits(n): """Stores the digits of a positive number n in a linked list. >>> s = store_digits(1) >>> s Link(1) >>> store_digits(2345) Link(2, Link(3, Link(4, Link(5)))) >>> store_digits(876) Link(8, Link(7, Link(6))) """ "*** YOUR CODE HERE ***" ``` ##
Here's one way to implement the `store_digits` function in Python:
```python
class Link:
empty = ()
def __init__(self, first, rest=empty):
self.first = first
self.rest = rest
def __repr__(self):
if self.rest is not Link.empty:
rest_str = ', ' + repr(self.rest)
else:
rest_str = ''
return 'Link(' + str(self.first) + rest_str + ')'
def store_digits(n):
"""Stores the digits of a positive number n in a linked list.
>>> s = store_digits(1)
>>> s
Link(1)
>>> store_digits(2345)
Link(2, Link(3, Link(4, Link(5))))
>>> store_digits(876)
Link(8, Link(7, Link(6)))
"""
if n < 10:
return Link(n)
else:
all_but_last, last = n // 10, n % 10
return Link(last, store_digits(all_but_last))
```
The `store_digits` function recursively constructs a linked list by repeatedly taking the last digit of the input number `n` and attaching it to the front of the linked list. The base case occurs when `n` is less than 10, in which case we simply return a single-element linked list containing `n`.
Note that we use the `//` operator to perform integer division and obtain the quotient `all_but_last`, which is the input number with its last digit removed. We then use the `%` operator to obtain the remainder `last`, which is the last digit of the input number. Finally, we recursively call `store_digits` on `all_but_last` and attach the resulting linked list to the front of the linked list containing `last`.
阅读全文