Similar Problems

Similar Problems not available

Print Zero Even Odd - Leetcode Solution

Companies:

LeetCode:  Print Zero Even Odd Leetcode Solution

Difficulty: Unknown

Topics: unknown  

Problem Statement:

Suppose you are given an integer n. Print zero(0), even and odd numbers from 0 to n.

  • For example, if n = 5, the output should be "0102030405"

You have to use three different threads in order to print these numbers. The first thread should only print 0's, the second thread should only print even numbers, and the third thread should only print odd numbers.

Solution:

This problem can be solved by using three different threads and synchronizing them using semaphores. We can create three semaphores, one for each type of number and use them to control the threads. When a thread finishes printing a number, it releases the semaphore for the next thread to take over.

Let's break down the solution into the following steps:

  1. Create three semaphores, one for each type of number - zeroSemaphore, evenSemaphore, and oddSemaphore, with an initial count of 1 for zeroSemaphore and 0 for evenSemaphore and oddSemaphore.
  2. Create three threads- ZeroThread to print zeros, EvenThread to print even numbers, and OddThread for odd numbers.
  3. Each thread will execute a function that prints the corresponding number and signals the next thread to run using semaphores. The ZeroThread will print 0 by taking zeroSemaphore and releasing evenSemaphore. EvenThread will print even numbers by taking evenSemaphore and releasing oddSemaphore and OddThread will print odd numbers by taking oddSemaphore and releasing evenSemaphore.
  4. Use a mutex lock for printing the numbers to avoid race conditions.

Here is the Python code for the solution:

from threading import Semaphore, Thread, Lock

class ZeroEvenOdd:
    def __init__(self, n):
        self.n = n
        self.zero_semaphore = Semaphore(1)
        self.even_semaphore = Semaphore(0)
        self.odd_semaphore = Semaphore(0)
        self.lock = Lock()

    def printNumber(self, num):
        with self.lock:
            print(num, end='')
    
    def zero(self):
        for i in range(self.n):
            self.zero_semaphore.acquire()
            self.printNumber(0)
            if i % 2 == 0:
                self.even_semaphore.release()
            else:
                self.odd_semaphore.release()

    def even(self):
        for i in range(2, self.n+1, 2):
            self.even_semaphore.acquire()
            self.printNumber(i)
            self.zero_semaphore.release()

    def odd(self):
        for i in range(1, self.n+1, 2):
            self.odd_semaphore.acquire()
            self.printNumber(i)
            self.zero_semaphore.release()

if __name__ == "__main__":
    n = 5
    zero_even_odd = ZeroEvenOdd(n)
    t1 = Thread(target=zero_even_odd.zero)
    t2 = Thread(target=zero_even_odd.even)
    t3 = Thread(target=zero_even_odd.odd)
    t1.start()
    t2.start()
    t3.start()
    t1.join()
    t2.join()
    t3.join()

In this code, we have defined a class ZeroEvenOdd that holds the three semaphores and has three functions - zero, even and odd that represent the three threads. The printNumber function prints the number and the acquire() and release() methods are used to control the threads using semaphores.

In the main function, we create three threads and start each one of them. Then, we join the threads to ensure that the output is printed in the correct order as per our requirements.

The output of this code is "0102030405" as expected.

Print Zero Even Odd Solution Code

1