
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If Number Can Be Displayed Using Seven Segment LED in Python
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 −
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
Advertisements