-
Notifications
You must be signed in to change notification settings - Fork 0
/
Here .txt
142 lines (113 loc) · 5.47 KB
/
Here .txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Here is a Python program that sorts a list of integers in descending order using the quicksort algorithm:
```python
def quicksort_desc(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x > pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x < pivot]
return quicksort_desc(left) + middle + quicksort_desc(right)
# Test the program
arr = [5, 3, 8, 1, 2, 7, 6, 4]
sorted_arr = quicksort_desc(arr)
print(sorted_arr)
```
When you run this program, it will sort the list `[5, 3, 8, 1, 2, 7, 6, 4]` in descending order using the quicksort algorithm and output the sorted list `[8, 7, 6, 5, 4, 3, 2, 1]`.Here is a Python program that generates a Fibonacci sequence up to a user-specified number of terms:
```python
def fibonacci(n):
fib_sequence = [0, 1]
while len(fib_sequence) < n:
next_num = fib_sequence[-1] + fib_sequence[-2]
fib_sequence.append(next_num)
return fib_sequence
n = int(input("Enter the number of terms in the Fibonacci sequence: "))
fib_seq = fibonacci(n)
print("Fibonacci sequence up to", n, "terms:")
print(fib_seq)
```
You can run this program and enter the number of terms you want in the Fibonacci sequence. The program will then generate and display the Fibonacci sequence up to that number of terms.Here is a function that checks if a given string is a palindrome in Python:
```python
def is_palindrome(s):
s = s.lower() # Convert string to lowercase
s = ''.join(e for e in s if e.isalnum()) # Remove non-alphanumeric characters
return s == s[::-1] # Return True if the string is equal to its reverse, False otherwise
# Test the function
print(is_palindrome("A man, a plan, a canal, Panama")) # True
print(is_palindrome("racecar")) # True
print(is_palindrome("hello")) # False
```
This function converts the input string to lowercase and removes any non-alphanumeric characters. It then compares the modified string to its reverse and returns True if they are equal, indicating that the original string is a palindrome.Here is a simple implementation of a rock-paper-scissors game in Python:
```python
import random
def get_user_choice():
user_choice = input("Enter your choice (rock, paper, or scissors): ")
return user_choice.lower()
def get_computer_choice():
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
return computer_choice
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
return "You win!"
else:
return "Computer wins!"
def play_game():
user_choice = get_user_choice()
computer_choice = get_computer_choice()
print(f"Computer chose: {computer_choice}")
print(determine_winner(user_choice, computer_choice))
play_game()
```
To play the game, run the `play_game()` function in your Python interpreter. You will be prompted to enter your choice (rock, paper, or scissors), and the computer will randomly choose its own. The program will then determine the winner and print the result.Here is a Python program that takes a list of numbers as input and outputs a new list containing only the even numbers in the original list:
```python
# Function to filter even numbers from a list
def filter_even_numbers(input_list):
return [num for num in input_list if num % 2 == 0]
# Input list of numbers
input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Output list containing only even numbers
output_list = filter_even_numbers(input_list)
# Print the output list
print(output_list)
```
When you run this program with the provided input list `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, it will output `[2, 4, 6, 8, 10]` which are the even numbers in the input list.Here is the pseudo code for the program:
1. Set starting_balance = 1000
2. Display welcome message and options:
- Check balance
- Deposit money
- Withdraw money
- Exit program
3. Start a loop to allow the user to continue making transactions:
- Ask user to input their choice
- If user chooses to check balance:
- Display current balance
- If user chooses to deposit money:
- Ask user to input the amount to deposit
- Update balance = balance + deposit amount
- If user chooses to withdraw money:
- Ask user to input the amount to withdraw
- If withdrawal amount > balance:
- Display error message
- Else:
- Update balance = balance - withdrawal amount
- If user chooses to exit program:
- Display goodbye message and exit loop
4. End program
You can implement this pseudo code in your programming language of choice to create a basic ATM machine simulation.Here is a Python script generated based on the prompt provided:
```python
try:
num_list = input("Enter a list of numbers separated by spaces: ").split()
num_list = list(map(int, num_list))
sum_nums = sum(num_list)
print(f"The sum of the numbers is: {sum_nums}")
except ValueError:
print("Please enter valid numbers separated by spaces.")
except Exception as e:
print(f"An error occurred: {e}")
```
This script reads a list of numbers from the user, calculates the sum of the numbers, and then displays the result. It also handles exceptions such as invalid input or any other errors that may occur during the input process.