DevonMeyer212
BAN USER
It's also important to note that you can get it by taking the modulo of subsequently decreasing parts, as another answer here indicates (- Y):
return ( ( ( n % 20 ) % 9 ) % 6 == 0 )
But keep in mind that the code above is exactly what the hardware does in this case. It subtracts 20 until the number is below 20, then subtracts 9 from that number until that number is below 9, and then subtracts six from that number until that number is below 6. If the remaining number is 0, return True.
Not really important overall, but being able to mention both in an interview would definitely give you some nerd points xD
This problem is the same as the ol' "minimum many quarters, dimes, nickels, and pennies do you need to make up x cents?" only quite a bit easier, because you don't need to keep track of a, b, c: you just need to determine if a, b, and c can exist such that their combination satisfies the conditional statement (6a + 9b + 20c == n)
And here it is in Python
def mcnuggets(n):
d = n
while d >= 20:
d = d - 20
while d >= 9:
d = d-9
while d >= 6:
d = d - 6
return d == 0
print mcnuggets(16)
Python has some ways of doing this very elegantly, but typically interviewers want you to implement it at its base level, as can be seen here:
def get_max_repeated(string):
chars = {}
max = 1
for c in string:
if c not in chars:
chars[c] = 1
else:
current = chars[c]+1
chars[c] = current
if current > max:
max = current
return max
print get_max_repeated("coffee tuffee")
Basically, think of the problem as a graph. For a string of length n, you have n sub-graphs. Each sub-graph then has n-1 sub-graphs. The total number of nodes in all of the sub-graphs is n!
So you start with a root and a remainder. You iteratively add each character from the remainder to the root, and then recurse down to the next level. When you have a remainder with only one character in it, you know you have hit a leaf, so you print the root plus the remainder.
In the case of no character repetitions, it's as simple as:
def get_perms(root, s):
if len(s) == 1:
print root+s
else:
for i in range(len(s)):
get_perms(root+s[i], s[:i]+s[i+1:])
get_perms("", "abcd")
I wrote a solution which also handles the case where the string includes duplicate characters:
def get_perms(root, s, items):
if len(s) == 1:
if root+s not in items:
items.append(root+s)
print root+s
else:
for i in range(len(s)):
get_perms(root+s[i], s[:i]+s[i+1:], items)
get_perms("", "abcdd", [])
It's pretty clear that this is O(n!) in time complexity, and if you have to handle duplicate characters, it becomes O(n!) space complexity as well.
Dynamic programming certainly could offer a solution here as well, but that requires storing all of the data. Therefore, in the case of no character repetitions, recursion will certainly be optimal, as you don't have to store anything other than the string. But in the case of repeated characters, perhaps there's a better way using dynamic programming.
This is my solution as well, but you encounter an issue whenever you have duplicate characters in the string. (i.e. abdcd).
Check my solution for an example of how to avoid this (but it requires maintaining a list of all options, therefore requiring O(n!) space complexity)
This is super easy to implement in python:
s1 = "110"
s2 = "01101"
def max_len(s1, s2)
[a, b] = [s1, s2] if len(s1) >= len(s2) else [s2, s1]
b = "".join(['0']*(len(a)-len(b)))+b
i = len(a) - 1
j = len(b) - 1
c = '0'
result = []
while i >= 0:
s = 0
for x in [a[i], b[j], c]:
if x == '1':
s = s + 1
c = '1' if s >=2 else '0'
result.insert(0, ('0' if s % 2 == 0 else '1'))
i = i - 1
j = j - 1
return "".join(result)
RepJesseDavis, Animator at 8x8
Hey I am Jack from us. I am working as a professor in MIT university.i Love my profession and ...
Repcheyennejmartin8547, Android Engineer at 247quickbookshelp
Hey CheyenneMartin and I am working as a machine operator. Today I am doing a new research like vasiyam specialist ...
Repkristinelavo, HR Executive freshers at Automated Traders Desk
Kristine , a Content Strategist who excels at developing and implementing social media plans, creating original online content, managing websites and ...
RepColaraJoshi, Floor manager at Thomes
By profession, I am Floor manager in the Thomes store. I am passionate about astrology and read tronto cards, horoscopes ...
RepNoraLillian, Applications Developer at Coupondesh
Nora, working as a ghostwriter with a history in manuscript development, proofreading, and editing, I apply with enthusiasm for this ...
Repbelindabest675, Animator at ABC TECH SUPPORT
Hey, my name is Belinda and I completed all my study from New York. And Nowadays I am working as ...
RepAriaScott, abc at 247quickbookshelp
I am a qualified medical representative who has been in the industry for many years and has proven sales experience ...
RepI am Neeva with 6 years of experience,seeking a career of packers and movers in gurgaon that allows me ...
Repnovathomes, abc at 8x8
I am a friendly and outgoing person who enjoys greeting people with a smile. I have more than eight years ...
RepDavidRNichols, Analyst at AMD
Skilled communicator and listener with a knack for remedying conflict, and keen organizational skills which allow for effective delivery of ...
RepGigiTaylor, abc at 8x8
Dedicated English Mandarin Chinese translator with years of experience working in professional and scientific communities.Voracious reader and participant in ...
Repewasam940, Backend Developer at Absolute Softech Ltd
I am a 29-year-old Investment Advisor from New York. I help people make the right investment decision. I met many ...
RepLopezKape, Applications Developer at A9
Lopez , an Outgoing Headhunter with a comprehensive background of client satisfaction in finding and recruiting top talent for various organizations ...
RepSusanKater, Animator at CDAC-ACTS
Susan , a highly experienced Sportscaster , Utilizes excellent communication skills and revolves local stories into feature stories, also delivered sportscasts in ...
Repherminanmuller87, Animator at 247quickbookshelp
Hello,I am a literacy teacher. I completed my degree from Chicago and now am a literacy teacher in a ...
RepRomilKazi, Member Technical Staff at AppNexus
Training Assistant with 6+ years of experience preparing flawless presentations, assembling progress reports, and providing support and training to secretarial ...
Repsalinagray, Development Support Engineer at Atmel
Hi I am Salina Gray,I live in Texas and work as a Design Team Member. I am new to ...
RepJamesFain, Area Sales Manager at ABC TECH SUPPORT
Phil has nearly 30 years of experience as a fisheries research scientist and directs the CFS Northwest science team. He ...
RepPaulMahone, Android test engineer at Booking.com
Hey, my name is Jazzy and I am a Journalist as a profession.I started my journey from last 4 ...
Repaliciaable183, Analyst at 247quickbookshelp
I am an agent contract clerk who is responsible for handling the recruitment process. I advertise the vacancies for agents ...
RepHey, I am Ruth and I am working as an interviewer.I take too many Interviews For my Company and ...
RepGizzyKale, Graphics Programmer at Arista Networks
Je suis Gizzy, un commis aux archives avec d'excellentes compétences interpersonnelles et de communication. Expert en nourriture , boisson et ...
RepAadavThomas, abc at A9
I am a well skilled and innovative Industrial Designer who focuses on taking products, noting product ideas and making them ...
Repbrendaalfaro847, Computer Scientist at ABC TECH SUPPORT
Brenda Alfaro, and I am a Skills training coordinator and I love my work. Apart from this, today I am ...
Reprafaeltanner210, Associate at 247quickbookshelp
I am Labor relations directors, oversee employment policies in union and nonunion settings. I draw up, negotiate, and administer labor ...
RepJamesGustin, Analyst at A9
Veteran technical assistant with over 5 years’ experience in the industry. Extensive knowledge and education in a variety of hardware ...
Repaishadavind7, Analyst at ASU
Being a Route driver I need to go here and there for the reason for delivering things here. I met ...
Reptabithalaws245, Computer Scientist at Absolute Softech Ltd
I am a Quality auditor Friendly technicians with over six years experience solving customer technical issues quickly and effectively. and ...
Repmarilyannrandall, Backend Developer at ABC TECH SUPPORT
As a Data input clerk, I deal with customers on a daily basis. Entering customer and account data from source ...
RepIsabellaJones, abc at 8x8
Extensive experience of 3 years in network architecture and application requirements for corporate data and voice networks, planning, designing and ...
Rephizahula, abc at 8x8
I am Erica and have lived in the US for the last 4 years.I did my degree in the ...
Repbenaryhell3454, Blockchain Developer at A9
Working as a carpet installer at Simple Solutions is almost 10 years . Here I daily meet new people and learn ...
Repjacquelinejlopes48, Analyst at 247quickbookshelp
Hey, I am Jacqueline and I am a court reporter.I had heard a lot about Vashikaran Specialist,now I ...
Repjudithasmith175, Accountant at Akamai
Hey, my name is Terry and I completed all my study from California. And Nowadays I am working as a ...
Repdelenestanf0, HR Executive at Agilent Technologies
Hi, I am a Clinical social worker. methods of prevention and treatment in providing mental-health/healthcare services, I also have ...
RepJohnCrter, Android test engineer at Alfa Chemaical Laboratory
Training Director responsibilities include identifying training needs based on skills gap analysis, developing group and individual learning courses and managing ...
RepBhrisBrown, Animator at Achieve Internet
I teach the art of cooking, including food preparation, various cuisines, and techniques of cooking. I work at colleges and ...
Rephcr689121, Data Engineer at Autonomy Zantaz
Harrison is an electronic data processor experience in electronic data processor and Promotion. I enjoy providing health resources and how ...
Repzippy28943, Data Scientist at AppNexus
Maxine , a grant writer specializing in researching and analysing grant opportunities, creating funding plans, and conducting proposals and budgets. Last ...
RepAngelaFox, Android Engineer at AMD
My name is AngelaFox . I'm working at Integra Design as a Municipal court judge . The adjudicator acts in the ...
I see a lot of
but as referenced here:
stackoverflow-dot-com/questions/3501382/checking-whether-a-variable-is-an-integer-or-not
it is better to assume your item is a list, and if it is not, run certain code on what you know, now, to be an integer. As can be seen here:
- DevonMeyer212 September 05, 2013