Share python 99

hominh.nhan

New member
## Python 99: Một danh sách các vấn đề Python thú vị

[Liên kết đến bài viết tham khảo] (https://www.pythonprogramming.net/python-99-probols/)

** 1.Fizzbuzz **

Viết một chương trình in các số từ 1 đến 100. Nhưng đối với bội số của ba, in "fizz" thay vì số và cho bội số của năm, in "buzz".Đối với các số là bội số của cả ba và năm, in "fizzbuzz".

`` `Python
def fizzbuzz (n):
"" "In các số từ 1 đến n.

Đối với bội số của ba, in "fizz" thay vì số.
Đối với bội số của năm, in "buzz".
Đối với các số là bội số của cả ba và năm, in "fizzbuzz".

Args:
N: giới hạn trên của phạm vi để in.
"" "

Đối với i trong phạm vi (1, n + 1):
Nếu i % 3 == 0 và i % 5 == 0:
In ("Fizzbuzz")
Elif I % 3 == 0:
in ("fizz")
Elif I % 5 == 0:
In ("Buzz")
khác:
in (i)


Nếu __name__ == "__main__":
FizzBuzz (100)
`` `

** 2.Đảo ngược một chuỗi **

Viết một chương trình đảo ngược một chuỗi.

`` `Python
Def Reverse_String (S):
"" "Đảo ngược một chuỗi.

Args:
S: Chuỗi để đảo ngược.

Trả lại:
Chuỗi đảo ngược.
"" "

Trả lại S [::-1]


Nếu __name__ == "__main__":
in (Reverse_String ("Xin chào")))
`` `

** 3.Tìm ký tự không lặp lại đầu tiên trong chuỗi **

Viết một chương trình tìm thấy ký tự không lặp lại đầu tiên trong một chuỗi.

`` `Python
def find_first_non_repeating_character (s):
"" "Tìm ký tự không lặp lại đầu tiên trong một chuỗi.

Args:
S: Chuỗi để tìm kiếm.

Trả lại:
Ký tự không lặp lại đầu tiên trong chuỗi hoặc không có nếu không có
Nhân vật như vậy.
"" "

See = set ()
Đối với tôi trong phạm vi (Len (s)):
Nếu s không nhìn thấy:
nhìn thấy.add (s )
trả lại S
trả lại không


Nếu __name__ == "__main__":
in (find_first_non_repeating_character ("abcdefg")))
`` `

**4.Kiểm tra xem một chuỗi là palindrom **

Một palindrom là một chuỗi giống nhau về phía sau và về phía trước.Viết một chương trình kiểm tra xem một chuỗi là một palindrom.

`` `Python
def is_palindrom (s):
"" "Kiểm tra xem một chuỗi là palindrom.

Args:
S: Chuỗi để kiểm tra.

Trả lại:
Đúng nếu chuỗi là một palindrom, sai nếu không.
"" "

Nếu Len (S) <= 1:
trả về đúng
Trả lại S [::-1] == S


Nếu __name__ == "__main__":
in (is_palindrom ("đường đua")))
`` `

** 5.Tạo một số ngẫu nhiên **

Viết một chương trình tạo ra một số ngẫu nhiên.

`` `Python
Nhập ngẫu nhiên


def Generate_random_number (min_value, max_value):
"" "Tạo một số ngẫu nhiên giữa min_value và max_value.

Args:
MIN_VALUE: Giá trị tối thiểu của số ngẫu nhiên.
MAX_VALUE: Giá trị tối đa của số ngẫu nhiên.

Trả lại:
Số ngẫu nhiên.
"" "
=======================================
## Python 99: A List of Interesting Python Problems

[Link to reference article](https://www.pythonprogramming.net/python-99-problems/)

**1. FizzBuzz**

Write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers that are multiples of both three and five, print "FizzBuzz".

```python
def fizzbuzz(n):
"""Prints the numbers from 1 to n.

For multiples of three, print "Fizz" instead of the number.
For multiples of five, print "Buzz".
For numbers that are multiples of both three and five, print "FizzBuzz".

Args:
n: The upper bound of the range to print.
"""

for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)


if __name__ == "__main__":
fizzbuzz(100)
```

**2. Reverse a string**

Write a program that reverses a string.

```python
def reverse_string(s):
"""Reverses a string.

Args:
s: The string to reverse.

Returns:
The reversed string.
"""

return s[::-1]


if __name__ == "__main__":
print(reverse_string("hello"))
```

**3. Find the first non-repeating character in a string**

Write a program that finds the first non-repeating character in a string.

```python
def find_first_non_repeating_character(s):
"""Finds the first non-repeating character in a string.

Args:
s: The string to search.

Returns:
The first non-repeating character in the string, or None if there is no
such character.
"""

seen = set()
for i in range(len(s)):
if s not in seen:
seen.add(s)
return s
return None


if __name__ == "__main__":
print(find_first_non_repeating_character("abcdefg"))
```

**4. Check if a string is a palindrome**

A palindrome is a string that is the same backwards and forwards. Write a program that checks if a string is a palindrome.

```python
def is_palindrome(s):
"""Checks if a string is a palindrome.

Args:
s: The string to check.

Returns:
True if the string is a palindrome, False otherwise.
"""

if len(s) <= 1:
return True
return s[::-1] == s


if __name__ == "__main__":
print(is_palindrome("racecar"))
```

**5. Generate a random number**

Write a program that generates a random number.

```python
import random


def generate_random_number(min_value, max_value):
"""Generates a random number between min_value and max_value.

Args:
min_value: The minimum value of the random number.
max_value: The maximum value of the random number.

Returns:
The random number.
"""
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top