0% found this document useful (0 votes)
7 views12 pages

Coding Challenges: Power of Two & More

The document contains several coding problems and their solutions, including checking if a number is a power of two, sorting colors, adding digits, moving zeros in an array, and reversing vowels in a string. Each problem is presented with a class and method structure typical in programming challenges. The solutions utilize basic control flow and data manipulation techniques.

Uploaded by

vidyagg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Coding Challenges: Power of Two & More

The document contains several coding problems and their solutions, including checking if a number is a power of two, sorting colors, adding digits, moving zeros in an array, and reversing vowels in a string. Each problem is presented with a class and method structure typical in programming challenges. The solutions utilize basic control flow and data manipulation techniques.

Uploaded by

vidyagg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Coding Questions

Power of 2-231
n = n/2
class Solution(object): if n==1:
def isPowerOfTwo(self, return True
n): if n==1:
elif n%2!=0:
return True
elif n==0:
return False
return False else: return False
elif n%2==0:
while n!=1:
Sort Colors
class Solution(object): else:
def sortColors(self, nums): cnt2 += 1
arr=nums
for i in range(cnt0):
cnt0 = 0
cnt1 = 0
arr[i] = 0
cnt2 = 0 for i in range(cnt0, cnt0 +
for num in arr: cnt1):
if num == 0: arr[i] = 1
cnt0 += 1 for i in range(cnt0 + cnt1,
elif num == 1: len(arr)):
cnt1 += 1 arr[i] = 2
Add Digits
class Solution(object): while num>0:
def addDigits(self, num): k = num%10
true = True sums += k
sums = 0 num//=10
while true: if sums<10:
sums = 0 return sums
num = sums
true = True
Move Zeros
for i in range(len(nums)):
if nums[i] == 0:
[Link](0)
[Link](0)
return nums
Reverse Vowels of a String-345
l=['A','E','I','O','U','a','e','i','o', for i in range(len(s)):
'u’] if s[i] in l:
n=” “ n+=m[j]
m=[]
j+=1
for i in s:
if i in l:
else:
[Link](i) n+=s[i]
m=m[::-1] return n
j=0
Thank You

You might also like