fruktoed
BAN USER
My naive implementation. (Kotlin)
fun addBinary(addendumLeft: String, addendumRight: String): String {
var i = addendumLeft.length-1
var j = addendumRight.length-1
/* Handling edge cases */
if(addendumLeft.isEmpty() && addendumRight.isNotEmpty())
return addendumRight
else if(addendumRight.isEmpty() && addendumLeft.isNotEmpty())
return addendumLeft
else if(addendumLeft.isEmpty() && addendumRight.isEmpty())
return ""
/* Implementation */
val result = StringBuilder()
var carryOn = false
while (i >= 0 || j >= 0) {
if(i < 0 || j < 0)
{
val bin = if(j < 0) addendumLeft[i] else addendumRight[j]
if(carryOn) {
if(bin == '1')
result.insert(0,'0')
else {
result.insert(0,'1')
carryOn = false
}
} else
result.insert(0, bin)
} else {
if(addendumLeft[i] == '0' && addendumRight[j] == '1' ||
addendumLeft[i] == '1' && addendumRight[j] == '0') {
if(carryOn)
result.insert(0, '0')
else
result.insert(0, '1')
}
else if(addendumLeft[i] == '1' && addendumRight[j] == '1') {
if(carryOn)
result.insert(0, '1')
else {
carryOn = true
result.insert(0, '0')
}
}
else {
if(carryOn) {
result.insert(0, '1')
carryOn = false
} else
result.insert(0,'0')
}
}
if(i >= 0)
i--
if(j >= 0)
j--
}
if(carryOn)
result.insert(0, '1')
return result.toString()
}
Any more elegant solution?
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 ...
Repcandacebdemars876, Android Engineer at A9
Hello, I am the Skills Training Coordinator. A skills training coordinator works with human resources and management to identify training ...
RepHayleyGuez, Malwarebytes customer service at CDK Global
I am Hayley , a freelance artist with 7 years of experience in creating impressionist works. My most recent work was ...
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 ...
RepGlennaJames, Android test engineer at Accenture
Glenna , an illustrator for more than 8 years in California . I also feel I could have an actual understanding of ...
RepMaryGriffith, Cloud Support Associate at Auto NInja
I am Mary , have graduated with marketing specialization and I am passionate about Internet Marketing and Social Media. Creates business ...
Repdawnmhodges111, abc at 247quickbookshelp
Hello I am a Web writer. All my studies complete from california.my hobby is write different type article.Right ...
RepElijahMiller, abc at A9
I am highly organized with exceptional commitment to task completion and quality assurance when working with computer software programs. I ...
RepKelseyOliver, Security Analyst at Accenture
Kelsey , a Storyboard Artist with demonstrated experience in developing visual templates, storyboards, and sketches. The Award winner (2020) for storyboard ...
Reppepsyfarely, Front-end Software Engineer at 247quickbookshelp
I am Pepsy , a security supervisor at Edge Services for the last 4 years managing security teams and corporate environments ...
RepI am a management dietitian who plans food programs . I use personal counselling , cooking demonstrations, lectures and workshops as a ...
Reppamulapaya2, Area Sales Manager at Alcatel Lucent
Jorie , a Customer services manager with more than 6 years' experience working is responsible for managing the relationships between an ...
RepEllaFlores, Associate at Alcatel Lucent
Ella , a Tour bus driver in Sounds Great company Inc. A charter bus driver is responsible for providing comfortable and ...
RepMacHeist, Data Scientist at British Telecom
Mac, a Desktop Publishing Specialist at VitaGrey, where I provide document formatting and publishing support to a wide variety of ...
RepBruceKeter, HTML Experienced at 247quickbookshelp
Bruce , a pediatrician studies psychoanalysis where I try to understand children's needs and family dynamics. To do more research ...
RepSharonSwann, AT&T Customer service email at A9
Bilingual, self-motivated Air Hostess with a proven record of providing excellent customer service and exceeding all corporate and personal expectations ...
RepDonnaTyler, HR Executive freshers at Bloomberg LP
I am Donna , a travel counselor who advises clients on travel options and tour packages, makes bookings , prepares tickets and ...
RepMillaSoth, Intern at ADP
I am Milla , a dedicated Emergency Response Technician regarded for performing advanced medical procedures with a high degree of accuracy ...
RepNirvedPerez, abc at 8x8
To make sure a thorough investigation is done if discrepancies occur. Execute the Brand Customer Service standards to meet or ...
RepRoseReynolds, Analyst at 8x8
Hi everyone I am Lilla from the UK.I am passionate about two things, gym and book lover.Last topic ...
RepMoniKim, Animator at Clean Power Research
I am Moni , a reliable Special Projects/Order Filler people oriented personality with great organizational and interpersonal communication skills. I ...
Repdanikademers, HTML Experienced at Barclays Capital
I am Danika , working as a junior content writer with Jafco where I use my blog writing and social media ...
RepNaomiAllen, abc at HUIM
Hard-working, passionate coach who excels at teaching children between the ages of 8 and 16 about the fundamentals of football ...
Repharveyoberion, Analyst at AMD
Hi, I am Harvey, from the USA. I am working as a soil scientist. I study soil as a natural ...
Repkany7635, optician at AMMB
I am a detail oriented and knowledgeable optical assistant with 2 years of experience in an optometry office setting. Collect ...
RepPhyllisGreene, Developer Advocate at Autonomy Zantaz
Phyllis , a Communication Specialist adept at executing promotion strategies, serving as a spokesperson, developing new communication tools, and analyzing marketing ...
RepChloePerez, cashier at POS
Versatile cashier with exemplary cash register system skills and proven commitment to store cleanliness and safely. Learning new and knowledgeable ...
RepValerieRodri, Associate at Apache Design
I am a Medical equipment repairer in the Morehouse lab at the University of Wichita. I enjoy working with high-technology ...
Repnormadyen99, Android Engineer at ABC TECH SUPPORT
I am the Directory Assistance Operator responsible for assisting the callers by making calls, supplying phone numbers, advising the on-call ...
Repmaryctedesco7485, Accountant at ABC TECH SUPPORT
Efficient Production Manager with 15 years of experience leading diverse manufacturing teams to create high-quality products at top speeds. Dedicated ...
RepAadavKiva, abc at 8x8
By profession I am a blogger and passionate about writing new and informative blog content who easily understand visitors. Nowaday ...
Repamritatorr, Paramedic at Capital Medical Billing
By profession, I am a paramedic in Chicago. I want to know about Indian ayurvedic treatment. I like to explore ...
RepRilynFreeman, Area Sales Manager at Knewton
Hi my name is Rilyn. Library assistant in Jeans Unlimited company. I help to keep libraries organised and efficient while ...
Repddmohsi890, Associate at Abs india pvt. ltd.
Working as a Photographic processing machine operator at Cardinal Stores for almost 10 years . I have lots of experience in ...
RepMarshaSimon, Game Programmer at ASU
I am Hayley , a freelance artist with 7 years of experience in creating impressionist works. My most recent work was ...
RepLilyBell, Accountant at 8x8
I am a creative and dedicated photo editor with experience in photojournalism and marketing material development. I have a proven ...
RepEsotaTaylor, abc at 8x8
I am working as a lecturer with exceptional teaching abilities seeking employment in your organization. I have excellent experience in ...
RepValerieHill, abc at 8x8
I am a highly motivated development and community advocate with over 2 years experience fostering strong community relations product advancements ...
Repswammesha, Blockchain Developer at ADP
As a machine operator at Star Bright Investment Group it's been almost 10 years . here i learn different things ...
Replesamean618, Android Engineer at Adap.tv
Working as a Choreographer it's almost 10 years at Happy Bear Investment . Here I am dealing with different people ...
Repjoshuacmurphy36, Animator at ABC TECH SUPPORT
Hey,I am an agricultural engineer. And I love this job. We Agricultural Engineers do engineering design and planning in ...
RepEshikaLopez, general assistant at MMSS
Dedicated and reliable general assistant with background in and strong knowledge of secretarial and administrative principles. Capable of providing direct ...
RepLicholsLowry, Associate at AMD
I am Lichols, I am an outreach worker in Desmonds Formal Wear company .I specialise in a variety of different ...
Repcolleenpbeverly02, Android test engineer at ABC TECH SUPPORT
Hello I am Colleen Dedicated and hardworking Geographic information specialist analyst with 15 years of experience working with ArcGIS software ...
Repsushiswla65, Area Sales Manager at ABC TECH SUPPORT
My name is SarahProctor . I am working as a Video editor at Konsili . I am exploring some magical tricks . vashikaran ...
RepOllieObrien, Financial Application Engineer at Accenture
Ollie , professional writer and translator with more than 4+ years of experience. I had a passion for lifelong learning about ...
Repopalphelan234, Associate at 247quickbookshelp
I am a specialized Cardiac and vascular nurse at the Circus World . Here I meet different people and observe their ...
RepPeterRox, Java Developer at Cleartrip.com
I am Peter, a punctual, problem-solving, and communicative individual who possesses administration, finance, sales, computer skills and experience. I enjoy ...
RepSusanBrown, Accountant at AMD
I am a resourceful and seasoned Screen Printing Machine Operator with a strong customer service record across a range of ...
RepZoyaCox, abc at A9
I am skilled in partnering with authorities and managers to identify items of concern and find innovative solutions to such ...
Repwoodscarla116, Associate at ASAPInfosystemsPvtLtd
I am a nurse . My name is CarlaWoods . I am working as a Clinical social worker. I met many people ...
RepDukeRollin, Java Experienced at AMD
Duke, a Qualified psychiatrist with seven years of experience effectively treating patients with a wide range of conditions with a ...
RepBravoDwayne, Blockchain Developer at ADP
Bravo , a Broker Assistant skilled at assisting financial advisors and stockbrokers in any tasks as required. As I love to ...
RepSimonPister, Blockchain Developer at Absolute Softech Ltd
Simon , a food scientist , records the Tracking status of all existing ingredients during the review and updating process and communicates ...
Improved solution
- fruktoed August 14, 2020fun addBinary(addendumLeft: String, addendumRight: String): String {
var i = addendumLeft.length-1
var j = addendumRight.length-1
/* Handling edge cases */
if(addendumLeft.isEmpty() && addendumRight.isNotEmpty())
return addendumRight
else if(addendumRight.isEmpty() && addendumLeft.isNotEmpty())
return addendumLeft
else if(addendumLeft.isEmpty() && addendumRight.isEmpty())
return ""
/* Implementation */
val result = StringBuilder()
var sum = 0
while (i >= 0 || j >= 0 || sum == 1) {
sum += if(i >= 0) addendumLeft[i] - '0' else 0
sum += if(j >= 0) addendumRight[j] - '0' else 0
result.insert(0, sum % 2)
sum /= 2
if(i >= 0)
i--
if(j >= 0)
j--
}
return result.toString()
}
Thanks to GeeksforGeeks