Skip to content
DSC4828 practice
import matplotlib.pyplot as plt
import os
import zipfile
import tempfile
import shutil
# Function to generate floor plan PNG
def create_floor_plan_png(png_path='floor_plan.png'):
"""
Generates a simple 4m x 3m master bathroom floor plan and saves as PNG.
"""
fig, ax = plt.subplots(figsize=(8, 6))
ax.set_xlim(0, 4)
ax.set_ylim(0, 3)
ax.set_aspect('equal')
ax.axis('off')
# Draw outer walls
outer = plt.Rectangle((0, 0), 4, 3, edgecolor='black', facecolor='none', lw=2)
ax.add_patch(outer)
# Draw Shower/Steam/Bath enclosure
shower = plt.Rectangle((0, 0), 2, 3, edgecolor='blue', facecolor='lightblue', lw=2)
ax.add_patch(shower)
ax.text(1, 1.5, 'Shower/Steam/Bath', ha='center', va='center', fontsize=10)
# Draw Vanity area
vanity = plt.Rectangle((2, 0.9), 1, 1.2, edgecolor='green', facecolor='lightgreen', lw=2)
ax.add_patch(vanity)
ax.text(2.5, 1.5, 'Double Vanity', ha='center', va='center', fontsize=10)
# Draw Toilet area
toilet = plt.Rectangle((3, 1), 0.7, 1, edgecolor='purple', facecolor='plum', lw=2)
ax.add_patch(toilet)
ax.text(3.35, 1.5, 'Toilet', ha='center', va='center', fontsize=10)
# Add Title
plt.title('Master Bathroom Floor Plan (4m x 3m)', fontsize=16, pad=20)
plt.savefig(png_path, bbox_inches='tight')
plt.close()
print(f"Floor plan PNG saved as '{png_path}'")
return png_path
# Function to assemble a basic PPTX and embed the PNG
def create_pptx_from_png(png_path, pptx_path='Master_Bathroom_Floor_Plan.pptx'):
"""
Creates a minimal PowerPoint (.pptx) file embedding the given PNG as the first slide.
"""
if not os.path.exists(png_path):
raise FileNotFoundError(f"'{png_path}' not found.")
# Minimal XML parts for a single-slide PPTX
content_types = '''<?xml version="1.0" encoding="UTF-8"?>
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
<Default Extension="xml" ContentType="application/xml"/>
<Default Extension="png" ContentType="image/png"/>
<Override PartName="/ppt/presentation.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml"/>
<Override PartName="/ppt/slides/slide1.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml"/>
</Types>'''
rels_root = '''<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="ppt/presentation.xml"/>
</Relationships>'''
presentation_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<p:sldIdLst>
<p:sldId id="256" r:id="rId1"/>
</p:sldIdLst>
<p:sldSz cx="9144000" cy="6858000"/>
<p:notesSz cx="6858000" cy="9144000"/>
</p:presentation>'''
presentation_rels = '''<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/>
</Relationships>'''
slide_xml = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
<p:cNvPr id="1" name=""/>
<p:cNvGrpSpPr/>
<p:nvPr/>
</p:nvGrpSpPr>
<p:grpSpPr/>
<p:pic>
<p:nvPicPr>
<p:cNvPr id="2" name="Floor Plan"/>
<p:cNvPicPr/>
</p:nvPicPr>
<p:blipFill>
<a:blip r:embed="rId2"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</p:blipFill>
<p:spPr>
<a:xfrm>
<a:off x="1524000" y="1524000"/>
<a:ext cx="6096000" cy="4572000"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</p:spPr>
</p:pic>
</p:spTree>
<p:extLst/>
</p:cSld>
<p:clrMapOvr>
<a:masterClrMapping/>
</p:clrMapOvr>
</p:sld>'''
slide_rels = '''<?xml version="1.0" encoding="UTF-8"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/image1.png"/>
</Relationships>'''
# Build the pptx package
tempdir = tempfile.mkdtemp()
try:
# Write content types and relationships
with open(os.path.join(tempdir, '[Content_Types].xml'), 'w', encoding='utf-8') as f:
f.write(content_types)
os.makedirs(os.path.join(tempdir, '_rels'))
with open(os.path.join(tempdir, '_rels', '.rels'), 'w', encoding='utf-8') as f:
f.write(rels_root)
# Presentation parts
os.makedirs(os.path.join(tempdir, 'ppt', '_rels'))
os.makedirs(os.path.join(tempdir, 'ppt', 'slides', '_rels'))
os.makedirs(os.path.join(tempdir, 'ppt', 'media'))
with open(os.path.join(tempdir, 'ppt', 'presentation.xml'), 'w', encoding='utf-8') as f:
f.write(presentation_xml)
with open(os.path.join(tempdir, 'ppt', '_rels', 'presentation.xml.rels'), 'w', encoding='utf-8') as f:
f.write(presentation_rels)
with open(os.path.join(tempdir, 'ppt', 'slides', 'slide1.xml'), 'w', encoding='utf-8') as f:
f.write(slide_xml)
with open(os.path.join(tempdir, 'ppt', 'slides', '_rels', 'slide1.xml.rels'), 'w', encoding='utf-8') as f:
f.write(slide_rels)
# Copy image into media
shutil.copy(png_path, os.path.join(tempdir, 'ppt', 'media', 'image1.png'))
# Zip the structure into a .pptx
with zipfile.ZipFile(pptx_path, 'w', zipfile.ZIP_DEFLATED) as pptx:
for foldername, subfolders, filenames in os.walk(tempdir):
for filename in filenames:
filepath = os.path.join(foldername, filename)
arcname = os.path.relpath(filepath, tempdir)
pptx.write(filepath, arcname)
finally:
shutil.rmtree(tempdir)
print(f"PPTX saved as '{pptx_path}'")
return pptx_path
# Main execution
if __name__ == '__main__':
png_file = create_floor_plan_png('floor_plan.png')
pptx_file = create_pptx_from_png(png_file, 'Master_Bathroom_Floor_Plan.pptx')
# Simple test
if os.path.exists(pptx_file):
print("Test passed: PPTX file created successfully.")
else:
raise AssertionError("Test failed: PPTX file not found.")
from itertools import chain, combinations
# Function to generate all subsets of a set
def all_subsets(iterable):
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
# Function to check if any subset sums to N
def is_sum_possible(S, N):
for subset in all_subsets(S):
if sum(subset) == N:
return True
return False
# Input set S
S = {1, 3, 9, 15, 27, 39, 45, 63, 87, 99}
# Maximum value M
M = max(S)
print(M)
# Number N
N = 10 * M - 740 # Choose N < 10M
print(N)
# Check if N can be formed as the sum of any subset of S
if not is_sum_possible(S, N):
print(f"The set S = {S} satisfies the conditions.")
print(f"No subset of S sums to N = {N}, and N < 10M where M = {M}.")
else:
print(f"The set S = {S} does not satisfy the conditions.")
print(f"A subset of S sums to N = {N}.")import networkx as nx
import matplotlib.pyplot as plt
# Create the Petersen Graph
G = nx.petersen_graph()
# Relabel nodes with the custom labels (1 to 10)
mapping = {i: i + 1 for i in range(10)}
G = nx.relabel_nodes(G, mapping)
# Define the Hamiltonian Path
hamiltonian_path = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)]
# Draw the graph
pos = nx.spring_layout(G) # Layout for visualization
plt.figure(figsize=(8, 8))
# Draw all edges and nodes
nx.draw(G, pos, with_labels=True, node_color="lightblue", edge_color="gray", node_size=700, font_size=10)
# Highlight the Hamiltonian path
nx.draw_networkx_edges(G, pos, edgelist=hamiltonian_path, edge_color="red", width=2)
# Add title
plt.title("Petersen Graph with Hamiltonian Path Highlighted", fontsize=14)
plt.show()