Share t python examples

ducquangrolling

New member
## T Python Ví dụ

### 1. FizzBuzz trong Python

Fizzbuzz là một câu hỏi phỏng vấn lập trình cổ điển.Mục tiêu là in các số từ 1 đến 100, nhưng với một vài ngoại lệ.Đối với bội số của 3, in "fizz" thay vì số.Đối với bội số của 5, in "buzz".Và đối với các số là bội số của cả 3 và 5, in "fizzbuzz".

Đây là một giải pháp Python cho FizzBuzz:

`` `Python
def fizzbuzz (n):
"" "In các số từ 1 đến N, nhưng với một vài ngoại lệ.

Đối với bội số của 3, in "fizz" thay vì số.
Đối với bội số của 5, in "buzz".
Và đối với các số là bội số của cả 3 và 5, 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)

`` `

### 2. Trình tự Fibonacci trong Python

Trình tự Fibonacci là một loạt các số trong đó mỗi số là tổng của hai số trước.Trình tự bắt đầu bằng 0 và 1, và các số tiếp theo là 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, v.v.

Dưới đây là một giải pháp Python cho chuỗi Fibonacci:

`` `Python
def fibonacci (n):
"" "Trả về số Fibonacci thứ n.

Args:
N: Chỉ số của số Fibonacci để trả về.

Trả lại:
Số Fibonacci thứ n.
"" "

Nếu n <2:
trả lại n
khác:
Trả về Fibonacci (N - 1) + Fibonacci (N - 2)

`` `

### 3. Số nguyên tố trong Python

Một số nguyên tố là một số tự nhiên lớn hơn 1 không phải là sản phẩm của hai số tự nhiên nhỏ hơn.Một vài số nguyên tố đầu tiên là 2, 3, 5, 7, 11, 13, 17, 19 và 23.

Dưới đây là giải pháp Python để tìm số nguyên tố:

`` `Python
def is_prime (n):
"" "Trả về true nếu n là số nguyên tố, sai nếu không.

Args:
N: Số để kiểm tra xem nó có phải là nguyên tố không.

Trả lại:
Đúng nếu n là nguyên tố, sai nếu không.
"" "

Nếu n <2:
trả lại sai
Đối với i trong phạm vi (2, int (n ** 0,5) + 1):
Nếu n % i == 0:
trả lại sai
trả về đúng

`` `

### 4. Sắp xếp trong Python

Có nhiều thuật toán sắp xếp khác nhau trong Python.Một số phổ biến nhất là loại bong bóng, loại lựa chọn, loại chèn và sắp xếp hợp nhất.

Dưới đây là việc triển khai Python của thuật toán Sort Bubble:

`` `Python
def bubble_sort (nums):
"" "Sắp xếp một danh sách các số theo thứ tự tăng dần bằng thuật toán sắp xếp bong bóng.

Args:
NUMS: Danh sách các số để sắp xếp.

Trả lại:
Danh sách các số được sắp xếp.
"" "

Đối với I trong phạm vi (Len (Nums) - 1):
Đối với J trong phạm vi (LEN (NUMS) - 1 - I):
Nếu nums [j]> nums [j + 1]:
nums [j], nums [j + 1
=======================================
## T Python Examples

### 1. FizzBuzz in Python

FizzBuzz is a classic programming interview question. The goal is to print the numbers from 1 to 100, but with a few exceptions. For multiples of 3, print "Fizz" instead of the number. For multiples of 5, print "Buzz". And for numbers that are multiples of both 3 and 5, print "FizzBuzz".

Here is a Python solution to FizzBuzz:

```python
def fizzbuzz(n):
"""Prints the numbers from 1 to n, but with a few exceptions.

For multiples of 3, print "Fizz" instead of the number.
For multiples of 5, print "Buzz".
And for numbers that are multiples of both 3 and 5, 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)

```

### 2. Fibonacci Sequence in Python

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, and the next numbers are 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, and so on.

Here is a Python solution to the Fibonacci sequence:

```python
def fibonacci(n):
"""Returns the nth Fibonacci number.

Args:
n: The index of the Fibonacci number to return.

Returns:
The nth Fibonacci number.
"""

if n < 2:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)

```

### 3. Prime Numbers in Python

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The first few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, and 23.

Here is a Python solution to find prime numbers:

```python
def is_prime(n):
"""Returns True if n is a prime number, False otherwise.

Args:
n: The number to check if it is prime.

Returns:
True if n is prime, False otherwise.
"""

if n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True

```

### 4. Sorting in Python

There are many different sorting algorithms in Python. Some of the most common are the bubble sort, the selection sort, the insertion sort, and the merge sort.

Here is a Python implementation of the bubble sort algorithm:

```python
def bubble_sort(nums):
"""Sorts a list of numbers in ascending order using the bubble sort algorithm.

Args:
nums: The list of numbers to sort.

Returns:
The sorted list of numbers.
"""

for i in range(len(nums) - 1):
for j in range(len(nums) - 1 - i):
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top