DESCRIPTION:
You are given three integer inputs: length, minimum, and maximum.
Return a string that:
- Starts at minimum
- Ascends one at a time until reaching the maximum, then
- Decends one at a time until reaching the minimum
- repeat until the string is the appropriate length
Examples:
length: 5, minimum: 1, maximum: 3 ==> "12321"
length: 14, minimum: 0, maximum: 2 ==> "01210121012101"
length: 11, minimum: 5, maximum: 9 ==> "56789876567"
Notes:
- length will always be non-negative
- negative numbers can appear for minimum and maximum values
- hyphens/dashes ("-") for negative numbers do count towards the length
- the resulting string must be truncated to the exact length provided
- return an empty string if maximum < minimum or length == 0
- minimum and maximum can equal one another and result in a single number repeated for the length of the string
def ascend_descend(length, minimum, maximum):
tempStr=""
if minimum==maximum:
tempStr+=str(minimum)*length
if length==0:
return ''
if maximum<minimum:
return ''
while len(tempStr)<length:
for i in range(minimum,maximum):
if len(tempStr)>length:
break
else:
tempStr+=str(i)
for i in range(maximum,minimum,-1):
if len(tempStr)>length:
break
else:
tempStr+=str(i)
return (tempStr[:length])
'알고리즘 문제 > CodeWar' 카테고리의 다른 글
Count the divisors of a number (0) | 2022.09.30 |
---|---|
Range Bit Counting (0) | 2022.09.30 |
Find the missing letter (1) | 2022.09.29 |
Shortest Word (0) | 2022.09.29 |
Split Strings (0) | 2022.09.29 |