Codewars:Create Phone Number
Challenge Name:
Create Phone Number
Challenge Description
Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number.
Challenge link:
https://www.codewars.com/kata/525f50e3b73515a6db000b83/train/python
Example:
create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) #=> returns "(123) 456-7890"
Code:
def create_phone_number(n):
cc=""
mid=""
end=""
res=""
for i in range(3):
cc=cc+str(n[i])
for i in range(3,6):
mid=mid+str(n[i])
for i in range(6,10):
end=end+str(n[i])
res="("+cc+")"+" "+mid+"-"+end
return res
Explanation:
1.Declare 3 empty strings cc,mid,res,end
2.Iterate loop such that first 3 elements are added to "cc"
3.Iterate loop such that next 3 elements are added to "mid"
4.Iterate loop such that last 4 elements are added to "end"
5.Format the code using cc,mid,end such that the it matches the requierd output
6.return the string
0 Comments
Post a Comment