# Task 13: Odd reverse Write a Python program to reverse all words of odd lengths. Sample data: ("The quick brown fox jumps over the lazy dog") ("Python Exercises") Sample output: The quick brown fox jumps revo the yzal dog nohtyP Exercises
时间: 2024-01-11 17:05:52 浏览: 128
Sure, here's a Python program that reverses all words of odd lengths in a given string:
```python
def reverse_odd_words(string):
words = string.split()
for i in range(len(words)):
if len(words[i]) % 2 != 0:
words[i] = words[i][::-1]
return " ".join(words)
```
Here's how you can use it:
```python
string1 = "The quick brown fox jumps over the lazy dog"
string2 = "Python Exercises"
print(reverse_odd_words(string1))
print(reverse_odd_words(string2))
```
Output:
```
The quick brown fox jumps revo the yzal dog
nohtyP Exercises
```
相关问题
(Algebra: perfect square) Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here are some sample runs: Enter an integer m: 1500 The smallest number n for m * n to be a perfect square is 15 m * n is 22500
Here's a possible solution in Python:
```python
import math
# Function that returns all the factors of a given number
def factors(n):
result = []
i = 2
while i <= n:
if n % i == 0:
result.append(i)
n = n // i
else:
i += 1
return result
# Prompt the user to enter an integer m
m = int(input("Enter an integer m: "))
# Compute the factors of m
m_factors = factors(m)
# Compute the product of the factors that appear an odd number of times
n = 1
for factor in set(m_factors):
if m_factors.count(factor) % 2 == 1:
n *= factor
# Compute the smallest integer n such that m * n is a perfect square
mn = m * n
sqrt_mn = int(math.sqrt(mn))
while sqrt_mn * sqrt_mn != mn:
mn += m
sqrt_mn = int(math.sqrt(mn))
# Print the result
print("The smallest number n for m * n to be a perfect square is", n)
print("m * n is", mn)
```
Sample output:
```
Enter an integer m: 1500
The smallest number n for m * n to be a perfect square is 15
m * n is 22500
```
Java(Algebra: perfect square) Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here are some sample runs: Enter an integer m: 1500 The smallest number n for m * n to be a perfect square is 15 m * n is 22500
Here's the Java code to solve this problem:
```
import java.util.*;
public class PerfectSquare {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer m: ");
int m = input.nextInt();
List<Integer> factors = getSmallestFactors(m);
int n = 1;
for (int factor : factors) {
if (getFrequency(factors, factor) % 2 != 0) {
n *= factor;
}
}
System.out.println("The smallest number n for m * n to be a perfect square is " + n);
System.out.println("m * n is " + (m * n * n));
}
public static List<Integer> getSmallestFactors(int n) {
List<Integer> factors = new ArrayList<>();
int factor = 2;
while (n > 1) {
if (n % factor == 0) {
factors.add(factor);
n /= factor;
} else {
factor++;
}
}
return factors;
}
public static int getFrequency(List<Integer> list, int value) {
int count = 0;
for (int element : list) {
if (element == value) {
count++;
}
}
return count;
}
}
```
Explanation:
The `getSmallestFactors` method takes an integer `n` as input and returns a list of its smallest factors. It does this by repeatedly dividing `n` by its smallest factor until `n` becomes 1.
The `getFrequency` method takes a list of integers and a value as input and returns the number of times the value appears in the list.
In the `main` method, we first prompt the user to enter an integer `m`. Then we call `getSmallestFactors` to get the list of smallest factors of `m`. We then iterate through the list of factors and multiply the ones that appear an odd number of times. This gives us the smallest integer `n` such that `m * n` is a perfect square. Finally, we print out the value of `n` and `m * n * n`.
阅读全文