Close Menu
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
What's Hot

NAACP calls on Memphis officials to halt operations at xAI’s ‘dirty data center’

May 31, 2025

Meta plans to automate many of its product risk assessments

May 31, 2025

Legends Struggles in Box Office Bow, Lilo & Stitch No. 1

May 31, 2025
Facebook X (Twitter) Instagram
Facebook X (Twitter) Instagram
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
Home » Simple data compression algorithm
Misc

Simple data compression algorithm

HarishBy HarishJuly 14, 2024No Comments1 Min Read
Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email
Share
Facebook Twitter Pinterest Reddit WhatsApp Email
Question:

write a program for simple data compression algorithm

Example:

data = [1,2,3,4,0,0,0,3,4,6,0,0,0,21,2,3]

out = [1,2,3,4,”3×0″,4,6,”2×0″,21,2,3]

Logic:

  1. Initialize comp_count to 1 and an empty list called result.

  2. Iterate over the input list data (except for the last element).

  3. If the current element is the same as the next element, increment comp_count.

  4. If the current element is different from the next element:

    • If there were consecutive identical elements (i.e., comp_count > 1), append the count and the element to result.

    • Append the current element to result.

    • Reset comp_count to 1 for the next element.

  5. Return the compressed list in the format “count x element”.

Solution in python:

# Example 1

data = [1,2,3,4,0,0,0,3,4,6,0,0,0,21,2,3]

out = [1,2,3,4,”3×0″,4,6,”2×0″,21,2,3]

# Example 2

data = [1, 1, 2, 2, 2, 3, 4, 4, 4, 4],

output would be [‘2×1’, ‘3×2’, 3, ‘4×4’]

Preparing for interview?

  • Checkout our Interview prep page, It might help you.

Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
Previous ArticleInterview questions- SAN, FLOGI, PLOGI, Process login and zoning
Next Article Learn Docker for free : Building images
Harish
  • Website
  • X (Twitter)

Related Posts

We’ve entered an era of fintech maximalism according to Mark Goldberg

April 16, 2025

Apple says zero-day bugs exploited against ‘specific targeted individuals’ using iOS

April 16, 2025

India’s Uber-rival BluSmart appears to suspend service in wake of EV loan probe

April 16, 2025

Bluesky feed builder Graze raises $1M, rolls out ads

April 16, 2025

Kia is pitching Americans an affordable EV sedan with EV4 debut

April 16, 2025

NSO lawyer names Mexico, Saudi Arabia, and Uzbekistan as spyware customers accused of 2019 WhatsApp hacks

April 16, 2025
Add A Comment
Leave A Reply Cancel Reply

Our Picks

NAACP calls on Memphis officials to halt operations at xAI’s ‘dirty data center’

May 31, 2025

Meta plans to automate many of its product risk assessments

May 31, 2025

Legends Struggles in Box Office Bow, Lilo & Stitch No. 1

May 31, 2025

BitMEX discovers cybersecurity lapses in North Korea hacker group

May 31, 2025
Don't Miss
Blockchain

BitMEX discovers cybersecurity lapses in North Korea hacker group

May 31, 20253 Mins Read

The BitMEX crypto exchange’s security team discovered gaps in the operational security of the Lazarus…

Insurers Race to Cover Crypto Kidnap and Ransom Risks

May 31, 2025

FTX Bankruptcy Estate distributes $5 billion

May 30, 2025

MEXC detects 200% surge in fraud during Q1

May 30, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

About Us
About Us

Welcome to Luminari, your go-to hub for mastering modern tech and staying ahead in the digital world.

At Luminari, we’re passionate about breaking down complex technologies and delivering insights that matter. Whether you’re a developer, tech enthusiast, job seeker, or lifelong learner, our mission is to equip you with the tools and knowledge you need to thrive in today’s fast-moving tech landscape.

Our Picks

NAACP calls on Memphis officials to halt operations at xAI’s ‘dirty data center’

May 31, 2025

Meta plans to automate many of its product risk assessments

May 31, 2025

TC Sessions: AI Trivia Countdown — Your next shot at winning big

May 31, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

Facebook X (Twitter) Instagram Pinterest
  • Home
  • About Us
  • Advertise With Us
  • Contact Us
  • DMCA Policy
  • Privacy Policy
  • Terms & Conditions
© 2025 luminari. Designed by luminari.

Type above and press Enter to search. Press Esc to cancel.