Share
Explore

w24 PYTHON CSD 4523 Intro To Data Structures

This is our OOAD planning document:
The Object-oriented analysis and design is what you do before you start writing code:

Data Structures lab workbook:

error

To create a Python program for the card game of 21, also known as Blackjack, you can follow the algorithmic steps below, which are easy to translate into a Python program.

Algorithm for the Card Game of 21 (Blackjack)
Initializing the Game:
Create a deck of cards.
Shuffle the deck.
Deal two cards to the player and two cards to the dealer.
Player's Turn:
Allow the player to "hit" (draw another card) or "stand" (not draw any more cards) until they choose to stand or their total exceeds 21.
Dealer's Turn:
The dealer reveals their facedown card.
The dealer must hit until their total is at least 17.
Determine the Winner:
If the player's total is over 21, the player busts and loses.
If the dealer busts, the player wins.
If neither busts, the higher total wins.
If the totals are the same, it's a tie (push).
Game End:
Ask the player if they want to play again.
The provided algorithmic steps can be translated into a Python program by using lists, dictionaries, loops, and conditional statements. Additionally, you can refer to the provided resources such as the GeeksforGeeks article and the YouTube tutorial for further guidance and implementation details.
By following this algorithm, you can create a simple and functional Python program for the card game of 21.

Assignment 1: DUE Feb 8
We will introduce some software engineering concepts such as:
Object Interaction Diagram as a tool to visualize how methods call each other (Method Choreography)
Traceability Matrix to visualize how requirements connect to methods


Lexical Analysis is the part of the process in which we study the NOUNs (THING WORDS) and VERBS (action words) of the Business Domain requirements and generate them into DATA Fields and METHODS
Loading…
Python programming language. Python is a high-level, interpreted language known for its simplicity and readability.

It is often used for web development, data analysis, artificial intelligence, data science, high performance computing, and more.

Today we will do Assignment 1: Data Structures

In Python, there are several fundamental data structures that are widely used for organizing and managing data. Some of the basic data structures in Python include:
1. Lists: Lists are ordered collections of items that can be of any data type. They are mutable, meaning their elements can be changed.
2. Tuples: Tuples are similar to lists but are immutable, meaning they cannot be changed after creation. They are often used to store related pieces of information.
3. Dictionaries: Dictionaries are unordered collections of key-value pairs. They are often used to store and retrieve data based on a unique key.
4. Sets: Sets are unordered collections of unique elements. They are useful for performing mathematical set operations like union, intersection, and difference.
5. Arrays: Arrays are used to store a collection of elements of the same type. Python arrays can be created using the array module.
6. Stack: Stacks are a type of data structure that follows the Last In, First Out (LIFO) principle.
image.png
7. Queue: Queues are another type of data structure that follows the First In, First Out (FIFO) principle.
Understanding these data structures and their characteristics is essential for effectively organizing and manipulating data in Python.
Each data structure has its own set of operations and use cases that make them suitable for various programming tasks.
megaphone

Code drills that focuses on Python data structures, with a solution code for each exercise.

Code Drill: Python Data Structures
Exercise 1: List Operations
Write a function that takes a list of integers as input and returns a new list with only the even numbers from the input list.
Solution: ```python def filter_even_numbers(input_list): return [num for num in input_list if num % 2 == 0]
# Test the function input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(filter_even_numbers(input_list)) ```

Let’s factor objects into data structures: Starting the long journey towards building the Pythonic Information System

Below is an example Python program using classes and arrays to represent customers orders in an Ice Cream Shoppe.
```python class IceShoppeOrder: def __init__(self, customer_name, flavor, size): self.customer_name = customer_name self.flavor = flavor self.size = size
# Create an array to store IceShoppeOrder objects order_list = []
# Add orders to the array order1 = IceShoppeOrder("Alice", "Chocolate", "Large") order_list.append(order1)
order2 = IceShoppeOrder("Bob", "Vanilla", "Medium") order_list.append(order2)
order3 = IceShoppeOrder("Eve", "Strawberry", "Small") order_list.append(order3)
# Print the orders for order in order_list: print(f"Customer: {order.customer_name}, Flavor: {order.flavor}, Size: {order.size}") ```
In this example, we have a `IceCreamShoppeOrder` class representing an individual customer's order at an ice cream shop. We then create instances of this class and store them in an array `order_list`. Finally, we iterate through the array to print out the details of each customer's order.
This program demonstrates the use of Python classes and arrays to model and store Ice Shoppe orders.

...

Harnessing the Power of Classes, Objects, and Data Structures in Business Domain Modeling


In the realm of software development for business applications, leveraging the concept of classes, objects, and data structures is paramount to effectively model and represent various entities and processes within the business domain. This lecture note aims to elucidate the immense power and significance of utilizing classes and objects, and storing these objects in data structures, for business domain modeling.
1. Understanding Classes and Objects: - Classes serve as blueprints for creating objects, encapsulating data and behavior related to a specific entity or concept within the business domain. - Objects are instances of classes, representing individual occurrences of the entity or concept, and possessing their own unique state.
2. Benefits of Class and Object Modeling: - Real-world mapping: Classes and objects enable a direct mapping of real-world entities such as customers, products, orders, etc., facilitating a clear representation of business concepts within the software system. - Encapsulation and reusability: Classes encapsulate data and behavior, promoting data integrity and modularity, and allowing for reuse of code and logic across the application.
3. The Power of Data Structures: - Data structures such as arrays, lists, stacks, and queues offer efficient means to store and manage collections of objects in memory. - Arrays and lists: Ideal for storing homogeneous collections of objects, suitable for scenarios such as storing customer details, product listings, etc. - Stacks and queues: Applicable for managing sequential processes, such as order processing or task scheduling, in a manner that reflects real-world business operations.
4. Business Domain Modeling with Classes, Objects, and Data Structures: - Example: Representing customers, products, and orders in an e-commerce system using classes for Customer, Product, and Order, and storing objects of these classes in arrays and queues to manage transactions and inventory. 5. Leveraging the Power for Analysis and Operations: - Using the structured model built with classes, objects, and data structures to analyze business data, make informed decisions, and streamline business operations. - Seamless integration with databases: Classes and objects can be mapped to database tables, allowing for smooth interaction between the software system and persistent data storage.
Conclusion: In conclusion, the potency of classes, objects, and data structures in business domain modeling cannot be overstated. By leveraging these foundational concepts, software developers can construct robust and effective representations of business entities and processes, leading to agile, scalable, and maintainable business applications.
This lecture note has elucidated the power of classes, objects, and data structures in the context of business domain modeling, highlighting their pivotal role in shaping software solutions that align closely with the intricacies of the business world.
...

Exercise 2: Dictionary Operations
Write a function that takes a dictionary as input and returns a new dictionary with only the key-value pairs where the value is a string.
Solution: ```python def filter_string_values(input_dict): return {key: value for key, value in input_dict.items() if isinstance(value, str)}
# Test the function input_dict = {'a': 1, 'b': 'hello', 'c': 3.14, 'd': 'world'} print(filter_string_values(input_dict)) ```

Exercise 3: Set Operations

Write a function that takes two sets as input and returns a new set containing the elements that are common to both input sets.
Solution: ```python def set_intersection(set1, set2): return set1.intersection(set2)
# Test the function set1 = {1, 2, 3, 4, 5} set2 = {3, 4, 5, 6, 7} print(set_intersection(set1, set2)) ```
Here are some Python code examples involving pets and colors for arrays, stack, and queue data structures:

5. Arrays

Arrays in Python can be implemented using the array module.
Here's an example involving pets and colors:
```python from array import array
# Create an array of colors colors = array('u', ['r', 'e', 'd', 'g', 'r', 'e', 'e', 'n', 'b', 'l', 'u', 'e'])
# Create an array of pets pets = array('u', ['c', 'a', 't', 'd', 'o', 'g', 'b', 'i', 'r', 'd'])
print(colors) print(pets) ```

6. Stack

In Python, you can implement a stack using a list and its built-in methods. Here's an example using pets:
```python # Implementing a stack using a list stack = []
# Push pets onto the stack stack.append('cat') stack.append('dog') stack.append('bird')
# Pop pets from the stack print(stack.pop()) # Output: 'bird' print(stack.pop()) # Output: 'dog' ```

7. Queue

The `queue` module in Python provides a Queue class for implementing FIFO queues. Here's an example involving colors:
```python from queue import Queue
# Create a FIFO queue for colors color_queue = Queue()
# Enqueue colors color_queue.put('red') color_queue.put('green') color_queue.put('blue')
# Dequeue colors print(color_queue.get()) # Output: 'red' print(color_queue.get()) # Output: 'green'
print(color_queue.get()) ```
These examples demonstrate the usage of arrays, stacks, and queues in Python with a fun twist involving pets and colors.
Each of these exercises focuses on manipulating a specific data structure in Python and provides an opportunity to practice working with lists, dictionaries, and sets. The solutions demonstrate how to perform the specified operations effectively using Python.
If you have any further queries or would like additional code drills, feel free to ask!

megaphone

Python offers a variety of built-in data structures that are essential for organizing and manipulating data. Here are some key Python data structures that could be covered in the assignment for Class CSD 4523:

1. Lists: Lists are ordered collections of items that can be of any data type. They are mutable, meaning their elements can be changed.
2. Tuples: Tuples are similar to lists but are immutable, meaning they cannot be changed after creation. They are often used to store related pieces of information.
3. Dictionaries: Dictionaries are unordered collections of key-value pairs. They are often used to store and retrieve data based on a unique key.
4. Sets: Sets are unordered collections of unique elements. They are useful for performing mathematical set operations like union, intersection, and difference.
5. Arrays: Arrays are used to store a collection of elements of the same type. Python arrays can be created using the array module.
6. Stack: Stacks are a type of data structure that follows the Last In, First Out (LIFO) principle.
7. Queue: Queues are another type of data structure that follows the First In, First Out (FIFO) principle.

Assignment activities:
Demonstrating how to create, manipulate, and use them effectively in Python.

Our vertical: The Card Game

Popular card games:

Poker: A game of skill and strategy involving betting and individual play, where the goal is to have the best hand or to bluff opponents into folding.
Bridge: A trick-taking game played by four players in two partnerships, where communication and cooperation between partners are crucial.
Blackjack: Also known as 21, this game involves players trying to beat the dealer by getting as close to 21 points as possible without going over.
Solitaire: A single-player game involving the arrangement of playing cards according to specific rules, with the goal of reordering the deck by suit and rank.
These games often required strategic thinking, risk assessment, and mathematical calculation.

Of the mentioned card games, I believe that "Solitaire" would best lend itself to a vertical for Python data structures.
Solitaire involves the arrangement of cards according to specific rules, with the goal of reordering the deck by suit and rank.
This aligns well with demonstrating the use of various Python data structures, such as lists, tuples, and dictionaries, to represent and manipulate the deck of cards.
For example, the deck of cards in Solitaire could be represented as a list of tuples, where each tuple consists of the card's suit and rank. The game could involve demonstrating how to use Python's built-in data structures to shuffle the deck, draw cards, and organize them based on the game rules.
Additionally, given the nature of Solitaire as a single-player game, it could also provide an opportunity to explore concepts such as stack-like behavior for dealing and moving cards, as well as the usage of other data structures for managing game states and moves.
By implementing Solitaire as a Python program, students could gain hands-on experience in utilizing Python data structures effectively while also applying principles of clean, efficient, and well-documented code.
...

Assignment One Part 1:


megaphone

Below is a Python code lab that illustrates running a solitaire game using lists, tuples, and dictionaries to represent and manipulate the deck of cards. This code lab will introduce :

- basic game logic

- manipulation of cards

- deck management

Python card game version 1:

image.png
```
In this code lab: 1. We create a standard deck of cards using a list of dictionaries to represent each card.
2. We define a function to shuffle the deck using the `random.shuffle` function.
3. We define a function to deal cards to the tableau, a list of lists representing the columns in the solitaire game.
image.png
4. The `play_solitaire` function serves as a placeholder for the game logic.
This code lab provides a starting point for implementing a solitaire game using lists, tuples, and dictionaries to represent and manipulate the deck of cards.
You can further expand this code lab by adding functions to implement the game rules, move cards within the tableau, and manage the foundation and waste piles.
Feel free to integrate this code lab into your own solitaire game project and further customize it to meet your specific requirements!

megaphone

Python Card Game v2 (Get this running before the next class/problems? ping me on slack!!)


Let's break down the given code line by line and explain each piece of operation:
import random
# Function to create a standard deck of cards def create_deck(): ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A'] suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades'] deck = [{'rank': rank, 'suit': suit} for rank in ranks for suit in suits] return deck ```
1. `import random`: This line imports the `random` module, which provides various functions related to random number generation and shuffling.
2. `def create_deck():`: This line defines a function named `create_deck` that will be responsible for creating a standard deck of cards.
3. `ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']`: This line initializes a list of all the ranks in a standard deck of cards.
4. `suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']`: This line initializes a list of all the suits in a standard deck of cards.
5. `deck = [{'rank': rank, 'suit': suit} for rank in ranks for suit in suits]`: This line uses a list comprehension to create a deck of cards, where
each card is represented as a dictionary with keys 'rank' and 'suit' and their respective values from the ranks and suits lists.
The list comprehension iterates over all ranks and suits to create every combination of rank and suit, resulting in a complete deck of cards.
6. `return deck`: This line returns the created deck of cards when the `create_deck` function is called.
The next part of the code involves shuffling the deck:
```python # Function to shuffle the deck of cards def shuffle_deck(deck): random.shuffle(deck) return deck ```
1. `def shuffle_deck(deck):`: This line defines a function named `shuffle_deck` that takes a deck of cards as input and is responsible for shuffling the deck.
2. `random.shuffle(deck)`: This line uses the `shuffle` function from the `random` module to shuffle the cards in the deck.
3. `return deck`: This line returns the shuffled deck of cards.
The next part of the code involves dealing cards to the players:
```python # Function to deal cards to each player ​def deal_cards_to_players(deck): player_hands = [[], []] # Two players for _ in range(7): # Deal 7 cards to each player for hand in player_hands: card = deck.pop(0) card['facing'] = 'up' hand.append(card) return player_hands, deck ```
1. `def deal_cards_to_players(deck):`: This line defines a function named `deal_cards_to_players` that takes a deck of cards as input and is responsible for dealing cards to the players.
2. `player_hands = [[], []]`: This line initializes a list of two empty lists, representing the hands of two players.
3. `for _ in range(7):`: This line starts a loop to deal 7 cards to each player.
4. `for hand in player_hands:`: This line iterates over each player's hand to deal the cards.
5. `card = deck.pop(0)`: This line removes the first card from the deck and assigns it to the variable `card`.
6. `card['facing'] = 'up'`: This line sets the 'facing' key of the card dictionary to 'up', indicating that the card is facing up.
7. `hand.append(card)`: This line adds the dealt card to the player's hand.

8. `return player_hands, deck`: This line returns the hands (hand = the set of cards a player is holding) of the players along with the remaining deck of cards after dealing.
Lastly, the code involves simulating one round of the card game:
```python # Function to simulate one round of the card game def simulate_one_round(): deck = create_deck() shuffled_deck = shuffle_deck(deck) player_hands, shuffled_deck = deal_cards_to_players(shuffled_deck) # Print the hands of each player for i, hand in enumerate(player_hands): print(f"Player {i+1}'s Hand:") for card in hand: print(f"{card['rank']} of {card['suit']}") print() # Empty line to separate the hands
# Add game logic for one round ```
1. `def simulate_one_round():`: This line defines a function named `simulate_one_round` that simulates one round of the card game.
2. `deck = create_deck()`: This line creates a new deck of cards using the `create_deck` function.
3. `shuffled_deck = shuffle_deck(deck)`: This line shuffles the deck of cards using the `shuffle_deck` function.
4. `player_hands, shuffled_deck = deal_cards_to_players(shuffled_deck)`: This line deals cards to the players using the `deal_cards_to_players` function.
5. The subsequent code within `simulate_one_round` prints the hands of each player and then may include game logic for one round.
The lecture on the code breaks down each operation, from creating the deck of cards, shuffling the deck, dealing cards to the players, and simulating one round of the card game. This explanation provides a clear understanding of the functionality and flow of the provided Python code.

Title: Python Data Structures: A Comprehensive Survey
Lecture:
Introduction to Python Data Structures - Overview of the importance of data structures in programming - Advantages of using built-in data structures in Python
1. Lists - Explanation of lists as ordered, mutable collections - Common operations and methods (e.g., append, insert, remove, and slicing) - Use cases and examples of lists in real-world scenarios
megaphone

Here are four programming exercises focused on lists in Python, tailored towards building the Solitaire card game:

Exercise 1: Deck Initialization Task: Create a Python program to initialize a standard 52-card deck for the Solitaire game. Use a list to represent the deck and populate it with card values and suits.
Exercise 2: Shuffling the Deck Task: Implement a function that shuffles the deck of cards. Utilize the random module to shuffle the list representing the deck in place.
Exercise 3: Dealing Cards Task: Write a function to deal a specified number of cards from the deck to the tableau (playing area). This function should remove the dealt cards from the deck list and return them as a new list.
Exercise 4: Moving Cards within the Tableau Task: Create functions to facilitate moving cards within the tableau according to Solitaire's rules. For example, implementing functions to move cards between columns and to the foundation piles.
These exercises are designed to provide hands-on experience with manipulating lists, a fundamental data structure in Python, while also directly contributing to the development of the Solitaire card game. Students can test their implementations and build a functional Solitaire game by combining these exercises with additional elements such as rules enforcement, win conditions, and user interface.

Below are the Python implementations for the three exercises focusing on building the Solitaire card game.
image.png
```
image.png
image.png
These implementations provide the foundational functionalities for initializing the deck of cards, shuffling the deck, and moving cards within the tableau, essential components for the development of the Solitaire card game. These functions can be integrated into a larger program to further build and simulate the game.
If you need additional assistance or further elements added to the code, please feel free to ask!




2. Tuples - Understanding tuples as ordered, immutable collections - Differences between lists and tuples - Applications and benefits of utilizing tuples
3. Dictionaries - Explanation of dictionaries as collections of key-value pairs - Retrieving and modifying dictionary elements - Best practices and efficient usage of dictionaries
4. Sets - Understanding sets as collections of unique elements - Set operations (union, intersection, difference) - Implementing sets to solve problems effectively
5. Arrays - Overview of arrays for storing elements of the same type - Applications and limitations of arrays in Python - Exploring the array module and its functionalities
6. Stack and Queue - Understanding stack and queue data structures - LIFO and FIFO principles - Real-world scenarios that demonstrate the use of stacks and queues
Lab Workbook:
Lab 1: Exploring Lists and Tuples - Exercise 1: Creating and manipulating lists - Exercise 2: Exploring the properties of tuples and performing operations
Lab 2: Utilizing Dictionaries and Sets - Exercise 1: Implementing dictionaries for data retrieval and modification - Exercise 2: Performing set operations to solve specific problems
Lab 3: Understanding Stack and Queue - Exercise 1: Implementing a stack using lists and testing stack operations - Exercise 2: Utilizing queues to solve practical challenges
Lab 4: Applying Arrays in Python - Exercise 1: Working with arrays to store and manipulate homogeneous data - Exercise 2: Comparing arrays with other data structures for specific use cases
By employing this comprehensive lecture and lab workbook, students will gain a deep understanding of Python's diverse data structures, enabling them to make informed decisions when selecting and implementing data structures in their programs. Each lab exercise will provide hands-on experience, solidifying their comprehension and practical skills in utilizing Python data structures effectively.
If you need further elaboration on any specific aspect or examples to include in the lab exercises, please let me know!


megaphone

Title: Python Data Structures: A Comprehensive Survey

Lecture:
Introduction to Python Data Structures - Overview of the importance of data structures in programming - Advantages of using built-in data structures in Python
1. Lists - Explanation of lists as ordered, mutable collections - Common operations and methods (e.g., append, insert, remove, and slicing) - Use cases and examples of lists in real-world scenarios
2. Tuples - Understanding tuples as ordered, immutable collections - Differences between lists and tuples - Applications and benefits of utilizing tuples
3. Dictionaries - Explanation of dictionaries as collections of key-value pairs - Retrieving and modifying dictionary elements - Best practices and efficient usage of dictionaries
4. Sets - Understanding sets as collections of unique elements - Set operations (union, intersection, difference) - Implementing sets to solve problems effectively
5. Arrays - Overview of arrays for storing elements of the same type - Applications and limitations of arrays in Python - Exploring the array module and its functionalities
6. Stack and Queue - Understanding stack and queue data structures - LIFO and FIFO principles - Real-world scenarios that demonstrate the use of stacks and queues
Lab Workbook:
Lab 1: Exploring Lists and Tuples - Exercise 1: Creating and manipulating lists - Exercise 2: Exploring the properties of tuples and performing operations
Lab 2: Utilizing Dictionaries and Sets - Exercise 1: Implementing dictionaries for data retrieval and modification - Exercise 2: Performing set operations to solve specific problems
Lab 3: Understanding Stack and Queue - Exercise 1: Implementing a stack using lists and testing stack operations - Exercise 2: Utilizing queues to solve practical challenges
Lab 4: Applying Arrays in Python - Exercise 1: Working with arrays to store and manipulate homogeneous data - Exercise 2: Comparing arrays with other data structures for specific use cases
By employing this comprehensive lecture and lab workbook, students will gain a deep understanding of Python's diverse data structures, enabling them to make informed decisions when selecting and implementing data structures in their programs. Each lab exercise will provide hands-on experience, solidifying their comprehension and practical skills in utilizing Python data structures effectively.
If you need further elaboration on any specific aspect or examples to include in the lab exercises, please let me know!


Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.