MDCS
MDCS
Register No :
AIM:
ALGORITHM :
PROGRAM :
import heapq
class Node:
def _init_(self, symbol, prob):
self.symbol = symbol
self.prob = prob
self.left = None
self.right = None
def huffman_coding(symbols_with_probs):
heap = [Node(symbol, prob) for symbol, prob in symbols_with_probs]
heapq.heapify(heap)
1
root = heap[0]
codes = {}
generate_codes(root)
return codes
# User input
n = int(input("Enter the number of symbols: "))
symbols_with_probs = []
for _ in range(n):
symbol = input("Enter symbol: ")
prob = float(input(f"Enter probability for '{symbol}': "))
symbols_with_probs.append((symbol, prob))
print("\nHuffman Codes:")
for symbol in codes:
print(f"Symbol: {symbol}, Code: {codes[symbol]}")
OUTPUT :
RESULT :
Thus the Huffman code for given symbol probabilities has been executed
successfully.
2
EX.NO:02
DATE: ENCODE RUN LENGTHS WITH FIXED-LENGTH CODE
AIM:
ALGORITHM :
PROGRAM :
i=0
while i < len(data):
count = 1
while (i + count < len(data) and data[i] == data[i + count] and count < max_run):
count += 1
return encoded
print("\nEncoded Output:")
for symbol, binary_count in encoded_output:
print(f"({symbol}, {binary_count})")
3
OUTPUT :
RESULT :
Thus the Run lengths with fixed-length code has been executed successfully.
4
EX.NO:03
DATE: LEMPEL -ZIV ALGORITHM FOR ADAPTIVE VARIABLE-LENGTH
ENCODING
AIM:
ALGORITHM :
PROGRAM:
def lz78_compress(data):
dictionary = {}
current = ""
result = []
dict_size = 1
if current:
result.append((dictionary[current], ""))
return result
5
# User input
input_data = input("Enter the string to compress using LZ78: ")
OUTPUT :
RESULT :
Thus the Lempel -ziv algorithm for adaptive variable-length encoding has been
executed successfully.
6
EX.NO:04 COMPRESS THE GIVEN WORD USING ARITHMETIC CODING BASED
DATE: ON THE FREQUENCY OF THE LETTERS
AIM:
To compress the given word using arithmetic coding based on the frequency of the
letters.
ALGORITHM :
PROGRAM :
def arithmetic_encode(data):
freq = {ch: data.count(ch) for ch in set(data)}
total = len(data)
probs = {ch: freq[ch]/total for ch in freq}
low = 0.0
high = 1.0
cum = 0.0
bounds = {}
for ch in sorted(probs):
bounds[ch] = (cum, cum + probs[ch])
cum += probs[ch]
for ch in data:
ch_low, ch_high = bounds[ch]
range_ = high - low
high = low + range_ * ch_high
low = low + range_ * ch_low
7
s = input("Enter a string: ")
encoded_value, intervals = arithmetic_encode(s)
OUTPUT :
RESULT :
Thus compressed the given word using arithmetic coding based on the
frequency of the letters has been executed successfully.
8
EX.NO:05 WRITE A SHELL SCRIPT,WHICH CONVERTS ALL
DATE: IMAGES IN THE CURRENT DIRECTORY IN JPEG
AIM:
To write a shell script,which converts all images in the current directory in JPEG.
ALGORITHM :
PROGRAM :
#!/bin/bash
# Convert to JPEG
convert "$img" "$filename-converted.jpg"
9
OUTPUT :
RESULT :
Thus the shell script,which converts all images in the current directory in JPEG
has been executed successfully .
10
EX.NO:06 WRITE A PROGRAM TO SPLIT IMAGES FROM A VIDEO WITHOUT
USING ANY PRIMITIVES
DATE:
AIM:
To write a program to split images from a video without using any primitives.
ALGORITHM :
PROGRAM :
import cv2
from PIL import Image
import os
if not cap.isOpened():
print(f"Error: Cannot open video at {video_path}")
return
frame_count = 0
while True:
ret, frame = cap.read()
if not ret:
break
frame_count += 1
image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
image.save(os.path.join(output_folder, f"frame_{frame_count:04d}.png"))
print(f"Saved frame {frame_count}")
cap.release()
print(" FRAMES SPLITTED SUCCESSFULLY!")
11
# Fixed path
video_path = r"C:\Users\INDIRA\OneDrive\Pictures\Screenshots\sample_video.mp4"
output_folder = r"C:\Users\INDIRA\OneDrive\Desktop\MDCS"
split_video_to_images(video_path, output_folder)
OUTPUT:
RESULT :
Thus the program to split images from a video without using any primitives
has been executed successfully .
12
EX.NO:07 CREATE A PHOTO ALBUM OF A TRIP BY APPLYING
APPROPRIATE IMAGE DIMENSIONS AND FORMAT
DATE:
AIM:
ALGORITHM :
PROGRAM :
album.save(out_path, fmt)
print(f"Saved album to {out_path}")
13
OUTPUT :
RESULT :
14
EX.NO:08 WRITE THE CODE FOR IDENTIFYING THE POPULARITY OF
CONTENT RETRIEVAL FROM MEDIA SERVER
DATE:
AIM:
To write the code for identifying the popularity of content retrieval from media server.
ALGORITHM :
PROGRAM :
while True:
item = input("Content accessed: ").strip()
if item.lower() == 'done':
break
if item:
logs.append(item)
if not logs:
print("No content was entered.")
exit()
OUTPUT :
RESULT :
Thus the code for identifying the popularity of content retrieval from media
server has been executed successfully .
16
EX.NO:09 WRITE THE CODE FOR ENSURING DATA AVAILABILITY IN DISKS
USING STRIP BASED METHOD
DATE:
AIM:
To write the code for ensuring data availability in disks using strip based method.
ALGORITHM :
PROGRAM :
import os
n = int(input("Enter number of disks: "))
path = input("Enter disk folder path: ")
data = input("Enter data: ")
print("Original:", data)
print("Reconstructed:", reconstructed)
print("Match!" if data == reconstructed else " Mismatch.")
17
OUTPUT :
RESULT :
Thus write the code for ensuring data availability in disks using strip based
method has been executed successfully .
18
EX.NO:10 PROGRAM FOR SCHEDULING REQUESTS FOR DATA
DATE: STREAMS
AIM:
ALGORITHM :
PROGRAM :
import time
from collections import deque
if stream_queue:
19
data = stream_queue.popleft()
print(f" Served {data} from {current_stream}")
time.sleep(0.5) # Simulate delay
# Put the stream back if it still has data
if stream_queue:
round_robin_order.append(current_stream)
OUTPUT :
RESULT :
Thus the Program for scheduling requests for data streams has been executed
successfully .
20