Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
RafayAK
CodingPrep
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
CodingPrep
/
DailyCodingProblem
/
148_Apple_Gray_Code.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
44 lines (33 loc) · 1.44 KB
master
Breadcrumbs
CodingPrep
/
DailyCodingProblem
/
148_Apple_Gray_Code.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
44 lines (33 loc) · 1.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
This problem was asked by Apple.
Gray code is a binary code where each successive value differ in only one bit,
as well as when wrapping around. Gray code is common in hardware so that we don't
see temporary spurious values during transitions.
Given a number of bits n, generate a possible gray code for it.
For example, for n = 2, one gray code would be [00, 01, 11, 10].
"""
# Solution:(Wikipedia)
#
# The binary-reflected Gray code list for n bits can be generated recursively
# from the list for n − 1 bits by reflecting the list (i.e. listing the entries
# in reverse order), prefixing the entries in the original list with a binary 0,
# prefixing the entries in the reflected list with a binary 1, and then concatenating
# the original list with the reversed list.[11] For example, generating the n = 3 list from the n = 2 list:
#
# 2-bit list: 00, 01, 11, 10
# Reflected: 10, 11, 01, 00
# Prefix old entries with 0: 000, 001, 011, 010,
# Prefix new entries with 1: 110, 111, 101, 100
# Concatenated: 000, 001, 011, 010, 110, 111, 101, 100
#
memo_table = {1: ['0', '1']}
def create_gray_code(n:int):
if n in memo_table:
return memo_table[n]
gray_code_prev_n = create_gray_code(n-1)
prefix_old = ['0'+entry for entry in gray_code_prev_n]
prefix_new = ['1'+entry for entry in reversed(gray_code_prev_n)]
memo_table[n] = prefix_old + prefix_new
return memo_table[n]
if __name__ == '__main__':
print(create_gray_code(3))
You can’t perform that action at this time.