Categories: CodeChef's Solutions

Closing the Tweets Problem’s Solution with Approach – CodeChef

Problem Statement

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father’s laptop.

They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140 characters each. The only thing they liked to play with it is, closing and opening tweets.

There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game.

Related Post

Input

The first line contains two integers N K, the number of tweets (numbered 1 to N) and the number of clicks respectively (1 ≤ N, K ≤ 1000). Each of the following K lines has one of the following.

  • CLICK X , where X is the tweet number (1 ≤ X ≤ N)
  • CLOSEALL

Output

Output K lines, where the ith line should contain the number of open tweets just after the ith click.


Example

Input:
3 6
CLICK 1
CLICK 2
CLICK 3
CLICK 2
CLOSEALL
CLICK 1

Output:
1
2
3
2
0
1

Solution

Approach

  1. Firstly you have to create a list to store the status of the tweets
  2. Then initialize the list for the N number of zeroes
  3. take the input for K times
  4. If the input says ‘CLOSEALL‘, again initialize the list to N zeroes
  5. If not then check is the tweet is open or not
  6. If it is active change the value at its position to 0
  7. Else make it active
  8. Lastly print the number of open tweets

Code

# Taking the inputs
N,K = [int(x) for x in input().split()]

# intializing the main list to store the status of tweet
arr = [0]*N
for i in range(K):
    e = input()
    if e != 'CLOSEALL':
        com, pos = [x for x in e.split()]
        if arr[int(pos) - 1] == 1:
            arr[int(pos)-1] = 0
        else:
            arr[int(pos)-1] = 1
    else:
        arr = [0]*N
    print(arr.count(1))

Question Link – link

Aditya Kumar

Share
Tags: CodeChef Python

Recent Posts

  • Programming

Mastering Print Formatting in Python: A Comprehensive Guide

In Python, the print() function is a fundamental tool for displaying output. While printing simple…

8 months ago
  • Programming

Global Variables in Python: Understanding Usage and Best Practices

Python is a versatile programming language known for its simplicity and flexibility. When working on…

8 months ago
  • Programming

Secure Your Documents: Encrypting PDF Files Using Python

PDF (Portable Document Format) files are commonly used for sharing documents due to their consistent…

8 months ago
  • Programming

Creating and Modifying PDF Files in Python: A Comprehensive Guide with Code Examples

PDF (Portable Document Format) files are widely used for document exchange due to their consistent…

8 months ago
  • Programming

Boosting Python Performance with Cython: Optimizing Prime Number Detection

Python is a high-level programming language known for its simplicity and ease of use. However,…

8 months ago
  • Programming

Using OOP, Iterator, Generator, and Closure in Python to implement common design patterns

Object-Oriented Programming (OOP), iterators, generators, and closures are powerful concepts in Python that can be…

8 months ago

This website uses cookies.