Algorithms and problem solving Homework 2
Problem 1: Fibonacci Sequence Generator
Write a program that generates the Fibonacci sequence up to a specified number of
terms.
1. Create a function fibonacci(n) that takes an integer n and returns a list containing
the first n terms of the Fibonacci sequence.
2. Use a loop to calculate each term and add it to a list.
3. Ask the user to enter the number of terms they want, and print the Fibonacci
sequence.
4. Repeat until the user types “exit.”
Sample run:
Enter the number of terms: 5
Fibonacci sequence: [0, 1, 1, 2, 3]
Problem 2: Inventory Management System
Write a program to manage an inventory of items in a store.
1. Create a function add_item(inventory, item) that adds an item to a list of
inventory items.
2. Create another function remove_item(inventory, item) that removes an item from
the inventory if it exists.
3. Use a loop and a decision structure to display a menu:
o Option 1: Add an item
o Option 2: Remove an item
o Option 3: Display all items
o Option 4: Exit
4. The program should keep looping until the user selects “Exit.”
Menu:
1. Add an item
2. Remove an item
3. Display all items
4. Exit
Enter an option: 1
Enter item name: Apples
Inventory: ['Apples']
Problem 3: Unique Elements Finder
Write a program that finds unique elements from a list of numbers provided by the user.
1. Create a function find_unique_elements(numbers) that takes a list of numbers
and returns a new list containing only the unique elements.
2. Use a loop to repeatedly ask the user for numbers until they type “done.”
3. After the input phase, call the function and print the unique elements found.
Enter a number (or 'done' to finish): 1
Enter a number (or 'done' to finish): 2
Enter a number (or 'done' to finish): 2
Enter a number (or 'done' to finish): 3
Enter a number (or 'done' to finish): done
Unique elements: [1, 2, 3]
Problem4: Guess the Number Game
Write a program that allows the user to guess a randomly generated number within a
specified range.
1. Use the random module to generate a random integer between 1 and 100.
2. Create a function guess_number(target) that repeatedly asks the user to guess
the number until they guess correctly. Provide hints if the guess is too high or too
low.
3. Keep track of the number of attempts and display it once the user guesses the
number.
Guess the number: 50
Output: Too high!
Guess the number: 25
Output: Too low!
Guess the number: 37
Output: Correct! It took you 3 attempts.
Problem 5: Simple To-Do List Application
Write a program that manages a simple to-do list for tasks.
1. Create a function display_todo_list(todo_list) that takes a list of tasks and prints
each task with its index number.
2. Use a loop to display a menu of options:
o Option 1: Add a task
o Option 2: Remove a task
o Option 3: View all tasks
o Option 4: Exit
3. For adding a task, prompt the user to enter the task name and append it to the
list.
4. For removing a task, display the current list and prompt the user to enter the
index of the task they want to remove.
5. The program should keep looping until the user selects “Exit.”
Menu:
1. Add a task
2. Remove a task
3. View all tasks
4. Exit
Enter your choice: 1
Enter the task: Buy groceries
Enter your choice: 1
Enter the task: Clean the house
Enter your choice: 3
To-Do List:
0: Buy groceries
1: Clean the house
Enter your choice: 2
Enter the index of the task to remove: 0
Enter your choice: 3
To-Do List:
#0: Clean the house
Enter your choice: 4