Check if number can be displayed using seven segment led in Python

PythonServer Side ProgrammingProgramming

Suppose we have a number n, and we have another input c. We have to check whether n can be displayed using 7-segment displays or not. Now here is a constraint. We are only allowed to glow at most c number of LEDs.

So, if the input is like n = 315 c = 17, then the output will be True as 315 needs 12 LEDs and we have 17.

To solve this, we will follow these steps −

  • seg := a list containing led counts for all digits : [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
  • s := n as string
  • led_count := 0
  • for i in range 0 to size of s - 1, do
    • led_count := led_count + seg[value for ith character]
  • if led_count <= c, then
    • return True
  • return False

Example

Let us see the following implementation to get better understanding −

 Live Demo

seg = [6, 2, 5, 5, 4, 5, 6, 3, 7, 6]
def solve(n, c) :
   s = str(n)
   led_count = 0
   for i in range(len(s)) :
      led_count += seg[ord(s[i]) - 48]
   if led_count <= c:
      return True
   return False
n = 315
c = 17
print(solve(n, c))

Input

315, 17

Output

True
raja
Published on 19-Jan-2021 09:49:45
Advertisements